STM32CUBEMX开发GD32F303(16)----移植兆易创新SPI Nor Flash之GD25Q64Flash
spi概述
SPI是串行外设接口(Serial Peripheral Interface)的缩写,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空间,提供方便,正是出于这种简单易用的特性,越来越多的芯片集成了这种通信协议,比如 EEPROM,FLASH,实时时钟,AD转换器。
W25Q64 是一款SPI接口的Flash芯片,其存储空间为 64Mbit,相当于8M字节。W25Q64可以支持 SPI 的模式 0 和模式 3,也就是 CPOL=0/CPHA=0 和CPOL=1/CPHA=1 这两种模式。
最近在弄ST和GD的课程,需要GD样片的可以加群申请:6_15061293 。
视频教学
https://www.bilibili.com/video/BV13g411z7gi/
STM32CUBEMX开发GD32F303(16)----移植兆易创新SPI Nor Flash之GD25Q64Flash
csdn课程
课程更加详细。
https://download.csdn.net/course/detail/37152
生成例程
这里准备了自己绘制的开发板进行验证。
配置时钟树,配置时钟为64M。
查看原理图,PA9和PA10设置为开发板的串口。
配置串口。
由于需要输入数据,开启DMA进行接收。
中断。
SPI配置
在开发板中有arduino接口,配置这几个接口为spi。
本次实验使用的SPI与Flash通信,配置如下。
SPI的通信原理很简单,它以主从方式工作,这种模式通常有一个主设备和一个或多个从设备,需要至少4根线,事实上3根也可以(单向传输时)。也是所有基于SPI的设备共有的,它们是MISO(主设备数据输入)、MOSI(主设备数据输出)、SCLK(时钟)、CS(片选)。
(1)MISO– Master Input Slave Output,主设备数据输入,从设备数据输出;
(2)MOSI– Master Output Slave Input,主设备数据输出,从设备数据输入;
(3)SCLK – Serial Clock,时钟信号,由主设备产生;
(4)CS – Chip Select,从设备使能信号,由主设备控制。
负责通讯的3根线了。通讯是通过数据交换完成的,这里先要知道SPI是串行通讯协议,也就是说数据是一位一位的传输的。这就是SCLK时钟线存在的原因,由SCLK提供时钟脉冲,SDI,SDO则基于此脉冲完成数据传输。数据输出通过 SDO线,数据在时钟上升沿或下降沿时改变,在紧接着的下降沿或上升沿被读取。完成一位数据传输,输入也使用同样原理。因此,至少需要8次时钟信号的改变(上沿和下沿为一次),才能完成8位数据的传输。
时钟信号线SCLK只能由主设备控制,从设备不能控制。同样,在一个基于SPI的设备中,至少有一个主设备。这样的传输方式有一个优点,在数据位的传输过程中可以暂停,也就是时钟的周期可以为不等宽,因为时钟线由主设备控制,当没有时钟跳变时,从设备不采集或传送数据。SPI还是一个数据交换协议:因为SPI的数据输入和输出线独立,所以允许同时完成数据的输入和输出。芯片集成的SPI串行同步时钟极性和相位可以通过寄存器配置,IO模拟的SPI串行同步时钟需要根据从设备支持的时钟极性和相位来通讯。
最后,SPI接口的一个缺点:没有指定的流控制,没有应答机制确认是否接收到数据。
其中,CS是从芯片是否被主芯片选中的控制信号,也就是说只有片选信号为预先规定的使能信号时(高电位或低电位),主芯片对此从芯片的操作才有效。这就使在同一条总线上连接多个SPI设备成为可能。
随便配置一个端口为CS片选,并且命名为CS。
NOR Flash
NOR Flash是一种非易失闪存技术,是Intel在1988年创建。是市场上两种主要的非易失闪存技术之一。
以GD25Q64E为例,该 Flash为64M-bit大小,即8192K-Byte。
W25Q64将8M的容量分为127个块(Block),每个块大小为64K字节,每个块又分为16个扇区(Sector),每个扇区4K个字节。W25Q64的最小擦除单位为一个扇区,也就是每次必须擦除4K个字节。
即4K16128=8192K=8M
W25Q64的原理及应用
复位初始化
对于复位,需要发送0x66和0x99
代码中的初始化。
/* Reset Operations */#define RESET_ENABLE_CMD0x66#define RESET_MEMORY_CMD0x99/** * @brief Initializes the W25Q128FV interface. * @retval None */uint8_t BSP_W25Qx_Init(void){ /* Reset W25Qxxx */BSP_W25Qx_Reset();return BSP_W25Qx_GetStatus();}/** * @brief This function reset the W25Qx. * @retval None */static voidBSP_W25Qx_Reset(void){uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};W25Qx_Enable();/* Send the reset command */HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);W25Qx_Disable();}
ID
对于兆易创新W25Q64,主要有三种查询ID方式。
可以使用90H查询设备ID,以判断是否是W25Q64设备。
/* Identification Operations */#define READ_ID_CMD 0x9F/** * @brief Read Manufacture/Device ID.* @param return value address * @retval None */void BSP_W25Qx_Read_ID(uint8_t *ID){uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);/* Reception of the data */HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);W25Qx_Disable();}
读取数据
对于兆易创新W25Q64,读取数据使用0x03指令,后面添加需要读取的数据地址。
数据可以一直进行读取,当不需要读取数据时候将片选CS拉高,关闭时钟SCLK即可。
#define READ_CMD 0x03/** * @brief Reads an amount of data from the QSPI memory. * @param pData: Pointer to data to be read * @param ReadAddr: Read start address * @param Size: Size of data to read * @retval QSPI memory status */uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size){uint8_t cmd[4];/* Configure the command */cmd[0] = READ_CMD;cmd[1] = (uint8_t)(ReadAddr >> 16);cmd[2] = (uint8_t)(ReadAddr >> 8);cmd[3] = (uint8_t)(ReadAddr);W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);/* Reception of the data */if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK) { return W25Qx_ERROR; }W25Qx_Disable();return W25Qx_OK;}
以读取10个数据为例子,波形如下所示。
BSP_W25Qx_Read(rData2,0x1000,0x00a);
擦除扇区
最小的擦除单位是扇区,擦除指令为0x20和3字节的地址。
#define SECTOR_ERASE_CMD0x20uint8_t BSP_W25Qx_Erase_Block(uint32_t Address){uint8_t cmd[4];uint32_t tickstart = HAL_GetTick();cmd[0] = SECTOR_ERASE_CMD;cmd[1] = (uint8_t)(Address >> 16);cmd[2] = (uint8_t)(Address >> 8);cmd[3] = (uint8_t)(Address);/* Enable write operations */BSP_W25Qx_WriteEnable();/*Select the FLASH: Chip Select low */W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);/*Deselect the FLASH: Chip Select high */W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);{/* Check for the Timeout */ if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME) { return W25Qx_TIMEOUT; }}return W25Qx_OK;}
写数据
对于写数据到flash中,使用0x02指令进行写数据,后面还需要指定24位地址,才能进行写数据。
#define PAGE_PROG_CMD 0x02/** * @brief Writes an amount of data to the QSPI memory. * @param pData: Pointer to data to be written * @param WriteAddr: Write start address * @param Size: Size of data to write,No more than 256byte. * @retval QSPI memory status */uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size){uint8_t cmd[4];uint32_t end_addr, current_size, current_addr;uint32_t tickstart = HAL_GetTick();/* Calculation of the size between the write address and the end of the page */ current_addr = 0; while (current_addr <= WriteAddr)//判断地址属于哪一扇区开始 { current_addr += W25Q128FV_PAGE_SIZE;//0x100-> 256 bytes } current_size = current_addr - WriteAddr; /* Check if the size of the data is less than the remaining place in the page */ if (current_size > Size) { current_size = Size; } /* Initialize the adress variables *///写入地址大小范围 current_addr = WriteAddr; end_addr = WriteAddr + Size; /* Perform the write page by page */ do {/* Configure the command */cmd[0] = PAGE_PROG_CMD;cmd[1] = (uint8_t)(current_addr >> 16);cmd[2] = (uint8_t)(current_addr >> 8);cmd[3] = (uint8_t)(current_addr);/* Enable write operations */BSP_W25Qx_WriteEnable();W25Qx_Enable(); /* Send the command */ if (HAL_SPI_Transmit(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK) { return W25Qx_ERROR; } /* Transmission of the data */ if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK) { return W25Qx_ERROR; }W25Qx_Disable(); /* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);{/* Check for the Timeout */if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE){ return W25Qx_TIMEOUT;}} /* Update the address and size variables for next page programming */ current_addr += current_size; pData += current_size; current_size = ((current_addr + W25Q128FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q128FV_PAGE_SIZE; } while (current_addr < end_addr);return W25Qx_OK;}
对flash的0x1000地址进行写数据,指令如下。
BSP_W25Qx_Write(wData2,0x1000,0x000a);
波形如下所示。
W25Qx.c
/*********************************************************************************************************** File : ws_W25Qx.c* Hardware Environment: * Build Environment : RealView MDK-ARM Version: 4.20* Version : V1.0* By : ** (c) Copyright 2005-2011, WaveShare* http://www.waveshare.net*All Rights Reserved**********************************************************************************************************/#include "W25Qx.h"/** * @brief Initializes the W25Q128FV interface. * @retval None */uint8_t BSP_W25Qx_Init(void){ /* Reset W25Qxxx */BSP_W25Qx_Reset();return BSP_W25Qx_GetStatus();}/** * @brief This function reset the W25Qx. * @retval None */static voidBSP_W25Qx_Reset(void){uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};W25Qx_Enable();/* Send the reset command */HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);W25Qx_Disable();}/** * @brief Reads current status of the W25Q128FV. * @retval W25Q128FV memory status */static uint8_t BSP_W25Qx_GetStatus(void){uint8_t cmd[] = {READ_STATUS_REG1_CMD};uint8_t status;W25Qx_Enable();/* Send the read status command */HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);/* Reception of the data */HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);W25Qx_Disable();/* Check the value of the register */ if((status & W25Q128FV_FSR_BUSY) != 0) { return W25Qx_BUSY; }else{return W25Qx_OK;}}/** * @brief This function send a Write Enable and wait it is effective. * @retval None */uint8_t BSP_W25Qx_WriteEnable(void){uint8_t cmd[] = {WRITE_ENABLE_CMD};uint32_t tickstart = HAL_GetTick();/*Select the FLASH: Chip Select low */W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);/*Deselect the FLASH: Chip Select high */W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);{/* Check for the Timeout */ if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE) { return W25Qx_TIMEOUT; }}return W25Qx_OK;}/** * @brief Read Manufacture/Device ID.* @param return value address * @retval None */void BSP_W25Qx_Read_ID(uint8_t *ID){uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);/* Reception of the data */HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);W25Qx_Disable();}/** * @brief Reads an amount of data from the QSPI memory. * @param pData: Pointer to data to be read * @param ReadAddr: Read start address * @param Size: Size of data to read * @retval QSPI memory status */uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size){uint8_t cmd[4];/* Configure the command */cmd[0] = READ_CMD;cmd[1] = (uint8_t)(ReadAddr >> 16);cmd[2] = (uint8_t)(ReadAddr >> 8);cmd[3] = (uint8_t)(ReadAddr);W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);/* Reception of the data */if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK) { return W25Qx_ERROR; }W25Qx_Disable();return W25Qx_OK;}/** * @brief Writes an amount of data to the QSPI memory. * @param pData: Pointer to data to be written * @param WriteAddr: Write start address * @param Size: Size of data to write,No more than 256byte. * @retval QSPI memory status */uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size){uint8_t cmd[4];uint32_t end_addr, current_size, current_addr;uint32_t tickstart = HAL_GetTick();/* Calculation of the size between the write address and the end of the page */ current_addr = 0; while (current_addr <= WriteAddr)//判断地址属于哪一扇区开始 { current_addr += W25Q128FV_PAGE_SIZE;//0x100-> 256 bytes } current_size = current_addr - WriteAddr; /* Check if the size of the data is less than the remaining place in the page */ if (current_size > Size) { current_size = Size; } /* Initialize the adress variables *///写入地址大小范围 current_addr = WriteAddr; end_addr = WriteAddr + Size; /* Perform the write page by page */ do {/* Configure the command */cmd[0] = PAGE_PROG_CMD;cmd[1] = (uint8_t)(current_addr >> 16);cmd[2] = (uint8_t)(current_addr >> 8);cmd[3] = (uint8_t)(current_addr);/* Enable write operations */BSP_W25Qx_WriteEnable();W25Qx_Enable(); /* Send the command */ if (HAL_SPI_Transmit(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK) { return W25Qx_ERROR; } /* Transmission of the data */ if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK) { return W25Qx_ERROR; }W25Qx_Disable(); /* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);{/* Check for the Timeout */if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE){ return W25Qx_TIMEOUT;}} /* Update the address and size variables for next page programming */ current_addr += current_size; pData += current_size; current_size = ((current_addr + W25Q128FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q128FV_PAGE_SIZE; } while (current_addr < end_addr);return W25Qx_OK;}/** * @brief Erases the specified block of the QSPI memory. * @param BlockAddress: Block address to erase * @retval QSPI memory status */uint8_t BSP_W25Qx_Erase_Block(uint32_t Address){uint8_t cmd[4];uint32_t tickstart = HAL_GetTick();cmd[0] = SECTOR_ERASE_CMD;cmd[1] = (uint8_t)(Address >> 16);cmd[2] = (uint8_t)(Address >> 8);cmd[3] = (uint8_t)(Address);/* Enable write operations */BSP_W25Qx_WriteEnable();/*Select the FLASH: Chip Select low */W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);/*Deselect the FLASH: Chip Select high */W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);{/* Check for the Timeout */ if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME) { return W25Qx_TIMEOUT; }}return W25Qx_OK;}/** * @brief Erases the entire QSPI memory.This function will take a very long time. * @retval QSPI memory status */uint8_t BSP_W25Qx_Erase_Chip(void){uint8_t cmd[4];uint32_t tickstart = HAL_GetTick();cmd[0] = SECTOR_ERASE_CMD;/* Enable write operations */BSP_W25Qx_WriteEnable();/*Select the FLASH: Chip Select low */W25Qx_Enable();/* Send the read ID command */HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);/*Deselect the FLASH: Chip Select high */W25Qx_Disable();/* Wait the end of Flash writing */while(BSP_W25Qx_GetStatus() != W25Qx_BUSY);{/* Check for the Timeout */ if((HAL_GetTick() - tickstart) > W25Q128FV_BULK_ERASE_MAX_TIME) { return W25Qx_TIMEOUT; }}return W25Qx_OK;}
W25Qx.h
/*********************************************************************************************************** File : W25Qx.h* Hardware Environment: * Build Environment : RealView MDK-ARM Version: 5.15* Version : V1.0* By : ** (c) Copyright 2005-2015, WaveShare* http://www.waveshare.net*All Rights Reserved**********************************************************************************************************//* Define to prevent recursive inclusion -------------------------------------*/#ifndef __W25Qx_H#define __W25Qx_H#ifdef __cplusplus extern "C" {#endif /* Includes ------------------------------------------------------------------*/#include "stm32f1xx.h"#include "spi.h" /** @addtogroup BSP * @{ */ /** @addtogroup Components * @{ */ /** @addtogroup W25Q128FV * @{ *//** @defgroup W25Q128FV_Exported_Types * @{ */ /** * @} */ /** @defgroup W25Q128FV_Exported_Constants * @{ */ /** * @brief W25Q128FV Configuration */ #define W25Q128FV_FLASH_SIZE 0x1000000 /* 128 MBits => 16MBytes */#define W25Q128FV_SECTOR_SIZE 0x10000 /* 256 sectors of 64KBytes */#define W25Q128FV_SUBSECTOR_SIZE0x1000 /* 4096 subsectors of 4kBytes */#define W25Q128FV_PAGE_SIZE 0x100 /* 65536 pages of 256 bytes */#define W25Q128FV_DUMMY_CYCLES_READ 4#define W25Q128FV_DUMMY_CYCLES_READ_QUAD 10#define W25Q128FV_BULK_ERASE_MAX_TIME 250000#define W25Q128FV_SECTOR_ERASE_MAX_TIME3000#define W25Q128FV_SUBSECTOR_ERASE_MAX_TIME 800#define W25Qx_TIMEOUT_VALUE 1000/** * @brief W25Q128FV Commands */ /* Reset Operations */#define RESET_ENABLE_CMD0x66#define RESET_MEMORY_CMD0x99#define ENTER_QPI_MODE_CMD 0x38#define EXIT_QPI_MODE_CMD 0xFF/* Identification Operations */#define READ_ID_CMD 0x90#define DUAL_READ_ID_CMD0x92#define QUAD_READ_ID_CMD0x94#define READ_JEDEC_ID_CMD 0x9F/* Read Operations */#define READ_CMD 0x03#define FAST_READ_CMD 0x0B#define DUAL_OUT_FAST_READ_CMD 0x3B#define DUAL_INOUT_FAST_READ_CMD 0xBB#define QUAD_OUT_FAST_READ_CMD 0x6B#define QUAD_INOUT_FAST_READ_CMD 0xEB/* Write Operations */#define WRITE_ENABLE_CMD0x06#define WRITE_DISABLE_CMD 0x04/* Register Operations */#define READ_STATUS_REG1_CMD 0x05#define READ_STATUS_REG2_CMD 0x35#define READ_STATUS_REG3_CMD 0x15#define WRITE_STATUS_REG1_CMD 0x01#define WRITE_STATUS_REG2_CMD 0x31#define WRITE_STATUS_REG3_CMD 0x11/* Program Operations */#define PAGE_PROG_CMD 0x02#define QUAD_INPUT_PAGE_PROG_CMD 0x32/* Erase Operations */#define SECTOR_ERASE_CMD0x20#define CHIP_ERASE_CMD 0xC7#define PROG_ERASE_RESUME_CMD 0x7A#define PROG_ERASE_SUSPEND_CMD 0x75/* Flag Status Register */#define W25Q128FV_FSR_BUSY ((uint8_t)0x01) /*!< busy */#define W25Q128FV_FSR_WREN ((uint8_t)0x02) /*!< write enable */#define W25Q128FV_FSR_QE ((uint8_t)0x02) /*!< quad enable */#define W25Qx_Enable() HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET)#define W25Qx_Disable() HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET)#define W25Qx_OK ((uint8_t)0x00)#define W25Qx_ERROR ((uint8_t)0x01)#define W25Qx_BUSY ((uint8_t)0x02)#define W25Qx_TIMEOUT((uint8_t)0x03)uint8_t BSP_W25Qx_Init(void);static voidBSP_W25Qx_Reset(void);static uint8_t BSP_W25Qx_GetStatus(void);uint8_t BSP_W25Qx_WriteEnable(void);void BSP_W25Qx_Read_ID(uint8_t *ID);uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size);uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size);uint8_t BSP_W25Qx_Erase_Block(uint32_t Address);uint8_t BSP_W25Qx_Erase_Chip(void);/** * @} */ /** @defgroup W25Q128FV_Exported_Functions * @{ */ /** * @} *//** * @} */ /** * @} */ /** * @} */ #ifdef __cplusplus}#endif#endif /* __W25Qx_H */
案例
向0扇区(0块0扇区),17扇区(1块1扇区),34扇区(2块2扇区)分别写入0x200的数据。
头文件定义
/* USER CODE BEGIN Includes */#include "stdio.h"#include "W25Qx.h"/* USER CODE END Includes */
串口接收和flash数组定义
/* USER CODE BEGIN PV */#define BUFFERSIZE 255 //可以接收的最大字符个数uint8_t ReceiveBuff[BUFFERSIZE]; //接收缓冲区uint8_t recv_end_flag = 0,Rx_len;//接收完成中断标志,接收到字符长度uint8_t wData1[0x200];uint8_t wData2[0x200];uint8_t wData3[0x200];uint8_t rData1[0x200];uint8_t rData2[0x200];uint8_t rData3[0x200];uint8_t ID[4];uint32_t i;uint8_t flag[1] ;int i_flag = 0;/* USER CODE END PV */
串口重定向
/* USER CODE BEGIN PFP */void uart1_data(void);#ifdef __GNUC__//串口重定向#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)#else#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)#endif PUTCHAR_PROTOTYPE{ HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF); return ch;}/* USER CODE END PFP */
串口中断设置
#include "stm32f1xx_it.c"文件中断外部变量引用:
/* USER CODE BEGIN 0 */#define BUFFERSIZE 255//可接收的最大数据量extern uint8_t recv_end_flag,Rx_len,bootfirst;/* USER CODE END 0 */
串口2中断函数:
/** * @brief This function handles USART2 global interrupt. */void USART1_IRQHandler(void){ /* USER CODE BEGIN USART2_IRQn 0 */ /* USER CODE END USART2_IRQn 0 */ HAL_UART_IRQHandler(&huart1); /* USER CODE BEGIN USART2_IRQn 1 */uint32_t temp;if(USART1 == huart1.Instance)//判断是否为串口2中断{ if(RESET != __HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE))//如果为串口2{__HAL_UART_CLEAR_IDLEFLAG(&huart1);//清除中断标志 HAL_UART_DMAStop(&huart1);//停止DMA接收 temp = __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);//获取DMA当前还有多少未填充 Rx_len = BUFFERSIZE - temp; //计算串口接收到的数据个数 recv_end_flag = 1; } } /* USER CODE END USART2_IRQn 1 */}
主程序
读取ID和flash数据及擦除。
/* USER CODE BEGIN 2 */printf("GD Nor Flash案例\n"); __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//使能串口1 IDLE中断 /*##-1- Read the device ID ########################*/ BSP_W25Qx_Init();//初始化W25Q128BSP_W25Qx_Read_ID(ID);//读取IDif((ID[0] != 0xC8) | (ID[1] != 0x16)){Error_Handler();//如果 ID不对打印错误}else//ID正确,打印ID{printf("W25Q64 ID : ");for(i=0;i<2;i++){printf("0x%02X ",ID[i]);}printf("\r\n\r\n");}/**************************读取第0扇区数据**************************************************************//*##-3- Read the flash ########################*/ /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/if(BSP_W25Qx_Read(rData1,0x0,0x200)== W25Qx_OK)printf("读取原始的0个扇区数据成功!\n");elseError_Handler();/*打印数据*/printf("读取原始的0个扇区数据为: \r\n");for(i =0;i<0x200;i++){if(i%20==0)printf("\n0扇区第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData1[i]);}printf("\n");/**************************读取第17扇区数据**************************************************************//*##-3- Read the flash ########################*/ /*读取数据,rData读取数据的指针,起始地址0x1000,读取数据长度0x200*/if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)printf("读取原始的17个扇区数据成功!\n");elseError_Handler();/*打印数据*/printf("读取原始的2个扇区数据为:");for(i =0;i<0x200;i++){if(i%20==0)printf("\n17扇区第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData2[i]);}printf("\n");/**************************读取第34扇区数据**************************************************************//*##-3- Read the flash ########################*/ /*读取数据,rData读取数据的指针,起始地址0x2000,读取数据长度0x200*/if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)printf("读取原始的34个扇区数据成功!\n");elseError_Handler();/*打印数据*/printf("读取原始的34个扇区数据为: ");for(i =0;i<0x200;i++){if(i%20==0)printf("\n34扇区第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData3[i]);}printf("\n");/**************************清除第0扇区数据为0**************************************************************//*##-2- Erase Block ##################################*/ if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)printf(" QSPI Erase Block ok\r\n");elseError_Handler();/*##-2- Written to the flash ########################*/ /* fill buffer */printf(" 初始化数据,清零第0扇区前0x200的数据!\r\n");for(i =0;i<0x200;i ++){wData1[i] = 0; rData1[i] = 0;}/*写入数据,wData写入数据的指针,起始地址0x00,写入数据长度0x200*/if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)printf("清零第0扇区前0x200的数据成功!\r\n");elseError_Handler();/*##-3- Read the flash ########################*/ /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)printf("读取第0扇区前0x200数据成功!\r\n\r\n");elseError_Handler();/*打印数据*/printf("读取第0扇区前0x200数据为: \r\n");for(i =0;i<0x200;i++){if(i%20==0)printf("\n第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData1[i]);}printf("\n");/**************************清除第17扇区数据为0**************************************************************//*##-2- Erase Block ##################################*/ if(BSP_W25Qx_Erase_Block(0x11000) == W25Qx_OK)printf(" QSPI Erase Block ok\r\n");elseError_Handler();/*##-2- Written to the flash ########################*/ /* fill buffer */printf(" 初始化数据,清零第17扇区前0x200的数据!\r\n");for(i =0;i<0x200;i ++){wData2[i] = 0; rData2[i] = 0;}/*写入数据,wData写入数据的指针,起始地址0x1000,写入数据长度0x200*/if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)printf("清零第2扇区前0x200的数据成功!\r\n");elseError_Handler();/*##-3- Read the flash ########################*/ /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)printf("读取第17扇区前0x200数据成功!\r\n\r\n");elseError_Handler();/*打印数据*/printf("读取第17扇区前0x200数据为: \r\n");for(i =0;i<0x200;i++){if(i%20==0)printf("\n第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData2[i]);}printf("\n");/**************************清除第34扇区数据为0**************************************************************//*##-2- Erase Block ##################################*/ if(BSP_W25Qx_Erase_Block(0x22000) == W25Qx_OK)printf(" QSPI Erase Block ok\r\n");elseError_Handler();/*##-2- Written to the flash ########################*/ /* fill buffer */printf(" 初始化数据,清零第34扇区前0x200的数据!\r\n");for(i =0;i<0x200;i ++){wData3[i] = 0; rData3[i] = 0;}/*写入数据,wData写入数据的指针,起始地址0x22000,写入数据长度0x200*/if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)printf("清零第34扇区前0x200的数据成功!\r\n");elseError_Handler();/*##-3- Read the flash ########################*/ /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)printf("读取第34扇区前0x200数据成功!\r\n\r\n");elseError_Handler();/*打印数据*/printf("读取第34扇区前0x200数据为: \r\n");for(i =0;i<0x200;i++){if(i%20==0)printf("\n第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData3[i]);}printf("\n"); /* USER CODE END 2 */
主程序。
/* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */uart2_data();HAL_Delay(100); } /* USER CODE END 3 */
数据处理
/* USER CODE BEGIN 4 */void uart2_data(void){if(recv_end_flag ==1)//接收完成标志{if(ReceiveBuff[0]==0x00){printf("写入数据长度:%d\n",Rx_len-2);for(int i =0;i<Rx_len-2;i++){wData1[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];}/*##-2- Erase Block ##################################*/ if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)printf(" QSPI Erase Block ok\r\n");elseError_Handler();/*写入数据,wData写入数据的指针,起始地址0x00,写入数据长度0x200*/if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)printf("扇区0数据成功~~~~~~~~~~~~~~~~~~~~~~~~~~!\r\n");elseError_Handler();if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)printf("读取扇区0前0x200数据成功!\r\n\r\n");elseError_Handler();/*打印数据*/printf("读取扇区0前0x200数据为: \r\n");for(i =0;i<0x200;i++){if(i%20==0)printf("\n第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",wData1[i]);}printf("\n");}else if(ReceiveBuff[0]==0x17){printf("写入数据长度:%d\n",Rx_len-2);for(int i =0;i<Rx_len-2;i++){//Data[i]=ReceiveBuff[i+2];wData2[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];}/*##-17- Erase Block ##################################*/ if(BSP_W25Qx_Erase_Block(0x11000) == W25Qx_OK)printf(" QSPI Erase Block ok\r\n");elseError_Handler();/*写入数据,wData写入数据的指针,起始地址0x11000,写入数据长度0x200*/if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)printf("扇区17数据成功~~~~~~~~~~~~~~~~~~~~~~~~~~!\r\n");elseError_Handler();if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)printf("读取扇区17前0x200数据成功!\r\n\r\n");elseError_Handler();/*打印数据*/printf("读取扇区17前0x200数据为: \r\n");for(i =0;i<0x200;i++){if(i%20==0)printf("\n第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData2[i]);}printf("\n");}else if(ReceiveBuff[0]==0x34){printf("写入数据长度:%d\n",Rx_len-2);for(int i =0;i<Rx_len-2;i++){//Data[i]=ReceiveBuff[i+2];wData3[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];}/*##-22- Erase Block ##################################*/ if(BSP_W25Qx_Erase_Block(0x22000) == W25Qx_OK)printf(" QSPI Erase Block ok\r\n");elseError_Handler();/*写入数据,wData写入数据的指针,起始地址0x22000,写入数据长度0x200*/if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)printf("扇区34数据成功~~~~~~~~~~~~~~~~~~~~~~~~~~!\r\n");elseError_Handler();if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)printf("读取扇区34前0x200数据成功!\r\n\r\n");elseError_Handler();/*打印数据*/printf("读取扇区34前0x200数据为: \r\n");for(i =0;i<0x200;i++){if(i%20==0)printf("\n第%d到%d的数据为:\r\n",i,i+19);printf("0x%02X ",rData3[i]);}printf("\n");}elseprintf("输入错误!"); for(int i = 0; i < Rx_len ; i++) //清空接收缓存区 ReceiveBuff[i]=0;//置0 Rx_len=0;//接收数据长度清零 recv_end_flag=0;//接收标志位清零//开启下一次接收 HAL_UART_Receive_DMA(&huart1,(uint8_t*)ReceiveBuff,BUFFERSIZE); }}/* USER CODE END 4 */
演示
W25Q64芯片型号的ID为0XEF17,下方读取为0XC816,所以读取成功。
开机会打印出0,17,34扇区的前0x200个数据。
打印完原始数据之后将数据全部清零,清零完成如下图所示。
串口定义了ReceiveBuff[0]的数据为写入什么扇区,ReceiveBuff[0]为1写入扇区1,ReceiveBuff[0]为2写入扇区2,ReceiveBuff[0]为3写入扇区3,若为其他数据,则打印输入错误;ReceiveBuff[1]则为写入的位置。
输入:00 05 01 02 03 04
向扇区0的的05号位置开始写入数据01 02 03 04。
输入:00 28 11 12 13 14 15
向扇区0的的40(28是十六进制)号位置开始写入数据11 12 13 14 15。
输入:17 10 aa bb
向扇区17的的16(10是十六进制)号位置开始写入数据aa bb。