|
各位前辈,我用Z300这个板子,用HAL库读SPI flash时,读出的数据和芯片ID都为0,请教下,这是什么原因啊?
主要代码如下:
uint8_t w25x_read_id = 0x90; // read chip ID
uint8_t m_addr[3] = {0,0,0}; //
uint8_t check_busy = 0x05; // ????????
uint8_t enable_write = 0x06; // ???????????
uint8_t erase_sector = 0x20; // ????
uint8_t write_page = 0x02; // ?????
uint8_t read_data = 0x03; // ????
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int fputc(int ch,FILE *f)
{
uint8_t temp[1]={ch};
HAL_UART_Transmit(&huart1,temp,1,0xffff);
return ch;
}
void CheckBusyF(void)
{
uint8_t status=1;
uint32_t timeCount=0;
do
{
timeCount++;
if(timeCount > 0xEFFFFFFF) //????
{
return ;
}
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // ??CS
HAL_SPI_Transmit(&hspi2, &check_busy, 1, 100); // ????
HAL_SPI_Receive(&hspi2, &status, 1, 100); // ??
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // ??CS
}while((status&0x01)==0x01);
}
void WriteData(void)
{
uint8_t temp_wdata[5] = {0x99,0x88,0x77,0x66,0x55}; // ???????
CheckBusyF();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // ??CS
HAL_SPI_Transmit(&hspi2, &enable_write, 1, 100); // ????
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // ??CS
CheckBusyF();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // ??CS
HAL_SPI_Transmit(&hspi2, &erase_sector, 1, 100); // ????
HAL_SPI_Transmit(&hspi2, m_addr, 3, 100); // ????
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // ??CS
CheckBusyF();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // ??CS
HAL_SPI_Transmit(&hspi2, &enable_write, 1, 100); // ????
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // ??CS
CheckBusyF();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // ??CS
HAL_SPI_Transmit(&hspi2, &write_page, 1, 100); // ????
HAL_SPI_Transmit(&hspi2, m_addr, 3, 100); // ??
HAL_SPI_Transmit(&hspi2, temp_wdata, 5, 100); // ????
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // ??CS
}
void ReadData(void)
{
uint8_t temp_rdata[5] = {0,0,0,0,0}; // ???????buff
CheckBusyF();
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // ??CS
HAL_SPI_Transmit(&hspi2, &read_data, 1, 100); // ?????
HAL_SPI_Transmit(&hspi2, m_addr, 3, 100); // ??
HAL_SPI_Receive(&hspi2, temp_rdata, 5, 100); // ????
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // ??CS
/* ???? */
printf("Read flash data is:%x %x %x %x %x\n",temp_rdata[0],temp_rdata[1],temp_rdata[2],temp_rdata[3],temp_rdata[4]);
}
void ReadID(void)
{
uint8_t temp_ID[5] = {0,0,0,0,0}; // ????
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // ??CS
HAL_SPI_Transmit(&hspi2, &w25x_read_id, 1, 100); // ?ID????
HAL_SPI_Receive(&hspi2, temp_ID, 5, 100); // ??ID
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET); // ??CS
/* ???? */
printf("readID is %x%x\n",temp_ID[3],temp_ID[4]);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI2_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
ReadID();
WriteData();
HAL_Delay(20);
ReadData(); |
|