/* * 函数名:main * 描述 :主函数 * 输入 :无 * 输出 :无 */ int main(void) { uint8_t k; /* 配置串口1为:115200 8-N-1 */ USART1_Config(); printf("\r\n 这是一个2M串行flash(W25Q16)实验 \r\n"); /* 2M串行flash W25X16初始化 */ SPI_FLASH_Init(); /* Get SPI Flash Device ID */ DeviceID = SPI_FLASH_ReadDeviceID(); Delay( 200 ); /* Get SPI Flash ID */ FlashID = SPI_FLASH_ReadID(); printf("\r\n FlashID is 0x%X, Manufacturer Device ID is 0x%X\r\n", FlashID, DeviceID); /* Check the SPI Flash ID */ if (FlashID == sFLASH_ID) /* #define sFLASH_ID 0xEF3015 */ { printf("\r\n 检测到华邦串行flash W25Q16 !\r\n"); /* Erase SPI FLASH Sector to write on */ SPI_FLASH_SectorErase(FLASH_SectorToErase); /* 将发送缓冲区的数据写到flash中 */ SPI_FLASH_BufferWrite(Tx_Buffer, FLASH_WriteAddress, BufferSize); for( k=0; k<6; k++ ) printf("\r\n 写入的数据为:%d \r\n", Tx_Buffer[k]); /* 将刚刚写入的数据读出来放到接收缓冲区中 */ SPI_FLASH_BufferRead(Rx_Buffer, FLASH_ReadAddress, BufferSize); for( k=0; k<6; k++ ) printf("\r\n 读出的数据为:%d \r\n", Tx_Buffer[k]); /* 检查写入的数据与读出的数据是否相等 */ TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize); if( PASSED == TransferStatus1 ) { printf("\r\n 2M串行flash(W25Q16)测试成功!\n\r"); } else { printf("\r\n 2M串行flash(W25Q16)测试失败!\n\r"); } }// if (FlashID == sFLASH_ID) else { printf("\r\n 获取不到 W25Q16 ID!\n\r"); } SPI_Flash_PowerDown(); while(1); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { RCC_Configuration(); SPI1_Flash_GPIO_Configuration(); SPI1_Flash_Configuration(); /* Get Flash ID */ FlashID =SPI_FLASH_ReadID(); if(sFLASH_ID== FlashID ) { /* Erase SPI FLASH Sector */ SPI_FLASH_SectorErase(FLASH_WriteAddress); /* Write Buffer_Tx to SPI FLASH */ SPI_FLASH_BufferWrite(Buffer_Tx,FLASH_WriteAddress,BufferSize); /* Read data from SPI FLASH */ SPI_FLASH_BufferRead (Buffer_Rx,FLASH_ReadAddress,BufferSize); /* if TransferStatus1 =PASSED, the Flash write and read operation are correct£¬ if TransferStatus1 =FAILED, the Flash write and read operation are failed */ TransferStatus1 = Buffercmp(Buffer_Tx, Buffer_Rx, BufferSize); /* Erase SPI FLASH Sector */ SPI_FLASH_SectorErase(FLASH_WriteAddress); /* Read data from SPI FLASH */ SPI_FLASH_BufferRead(Buffer_Rx, FLASH_ReadAddress, BufferSize); for (Index = 0; Index < BufferSize; Index++) { if (Buffer_Rx[Index] != 0xFF) { /* if TransferStatus2 =PASSED, the Flash erase and read operation are correct£¬ if TransferStatus2 =FAILED, the Flash erase and read operation are failed */ TransferStatus2 = FAILED; } } } while (1) { } }
/** * @brief Initializes the SPI FLASH * @param None * @retval None */ ErrorStatus SPI_FLASH_Init() { /* Make sure we only initialize it once */ if (!prvInitialized) { /* Mutex semaphore for mutual exclusion to the SPI Flash device */ xSemaphore = xSemaphoreCreateMutex(); /* Init GPIO */ FLASH_GPIO_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Pin = FLASH_SCK_PIN | FLASH_MISO_PIN | FLASH_MOSI_PIN; GPIO_InitStructure.Mode = GPIO_MODE_AF_PP; GPIO_InitStructure.Alternate = GPIO_AF5_SPI2; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(FLASH_PORT, &GPIO_InitStructure); GPIO_InitStructure.Pin = FLASH_CS_PIN; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; HAL_GPIO_Init(FLASH_PORT, &GPIO_InitStructure); /* Deselect the FLASH */ prvSPI_FLASH_CS_HIGH(); /* DMA Init */ __DMA1_CLK_ENABLE(); HAL_DMA_Init(&DMA_HandleTx); /* Associate the initialized DMA handle to the the SPI handle */ __HAL_LINKDMA(&SPI_Handle, hdmatx, DMA_HandleTx); HAL_DMA_Init(&DMA_HandleRx); /* Associate the initialized DMA handle to the the SPI handle */ __HAL_LINKDMA(&SPI_Handle, hdmarx, DMA_HandleRx); /* NVIC configuration for DMA transfer complete interrupt (SPI2_TX) */ HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY, 0); HAL_NVIC_EnableIRQ(DMA1_Stream4_IRQn); /* NVIC configuration for DMA transfer complete interrupt (SPI2_RX) */ HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, configLIBRARY_LOWEST_INTERRUPT_PRIORITY-1, 0); HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn); /* Init SPI */ FLASH_SPI_CLK_ENABLE(); HAL_SPI_Init(&SPI_Handle); /* Read FLASH identification */ prvDeviceId = SPI_FLASH_ReadID(); if (prvDeviceId == SPI_FLASH_S25FL127SABMFI101_ID) { /* Select the FLASH */ prvSPI_FLASH_CS_LOW(); /* Send "Write Enable Status" instruction */ prvSPI_FLASH_SendReceiveByte(SPI_FLASH_CMD_EWSR); /* Deselect the FLASH */ prvSPI_FLASH_CS_HIGH(); /* Select the FLASH */ prvSPI_FLASH_CS_LOW(); /* Send "Write Status Register" instruction and set all bits to 0 */ prvSPI_FLASH_SendReceiveByte(SPI_FLASH_CMD_WRSR); prvSPI_FLASH_SendReceiveByte(0); /* Deselect the FLASH */ prvSPI_FLASH_CS_HIGH(); prvInitialized = true; return SUCCESS; } } return ERROR; }