你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32CUBEIDE(15)----移植兆易创新SPI Nor Flash之GD25Q64Flash

[复制链接]
STMCU小助手 发布时间:2022-10-20 19:14
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 这两种模式。

生成例程

使用STM32CUBEMX生成例程,这里使用NUCLEO-F103RB开发板

839757b637ee4ab69623fce326742df6.png

配置时钟树,配置时钟为64M。

360005becba24788aee6f547baae5b52.png

查看原理图,PA2和PA3设置为开发板的串口。


4c2181dbda8946e7b90c9e404d7fe75e.png

配置串口。

6fa28172f063490c9f66b66f1808ef96.png

由于需要输入数据,开启DMA进行接收。

f412f32d11f4428ab1f21fac63759508.png

中断。

31ebe8f4bd604fec9bfa0b087220b3e0.png

SPI配置
在开发板中有arduino接口,配置这几个接口为spi。



72bdb04b993d4c71900160ab38f4822d.png

本次实验使用的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,从设备使能信号,由主设备控制。

e066bc13ff35431789a0f629dc56c583.png

负责通讯的3根线了。通讯是通过数据交换完成的,这里先要知道SPI是串行通讯协议,也就是说数据是一位一位的传输的。这就是SCLK时钟线存在的原因,由SCLK提供时钟脉冲,SDI,SDO则基于此脉冲完成数据传输。数据输出通过 SDO线,数据在时钟上升沿或下降沿时改变,在紧接着的下降沿或上升沿被读取。完成一位数据传输,输入也使用同样原理。因此,至少需要8次时钟信号的改变(上沿和下沿为一次),才能完成8位数据的传输。
时钟信号线SCLK只能由主设备控制,从设备不能控制。同样,在一个基于SPI的设备中,至少有一个主设备。这样的传输方式有一个优点,在数据位的传输过程中可以暂停,也就是时钟的周期可以为不等宽,因为时钟线由主设备控制,当没有时钟跳变时,从设备不采集或传送数据。SPI还是一个数据交换协议:因为SPI的数据输入和输出线独立,所以允许同时完成数据的输入和输出。芯片集成的SPI串行同步时钟极性和相位可以通过寄存器配置,IO模拟的SPI串行同步时钟需要根据从设备支持的时钟极性和相位来通讯。
最后,SPI接口的一个缺点:没有指定的流控制,没有应答机制确认是否接收到数据。

0518bf0da3df42a79291d95c298f9fed.png

其中,CS是从芯片是否被主芯片选中的控制信号,也就是说只有片选信号为预先规定的使能信号时(高电位或低电位),主芯片对此从芯片的操作才有效。这就使在同一条总线上连接多个SPI设备成为可能。
随便配置一个端口为CS片选,并且命名为CS。

baaf5e9bdae84d68a8599ed26c05f5f4.png

NOR Flash
NOR Flash是一种非易失闪存技术,是Intel在1988年创建。是市场上两种主要的非易失闪存技术之一。
以GD25Q64E为例,该 Flash为64M-bit大小,即8192K-Byte。



2558b1096ae9468b92039c513af4bf4a.png

W25Q64将8M的容量分为127个块(Block),每个块大小为64K字节,每个块又分为16个扇区(Sector),每个扇区4K个字节。W25Q64的最小擦除单位为一个扇区,也就是每次必须擦除4K个字节。
即4K16128=8192K=8M

f95a474eaf814b508082a9605b54504a.png

W25Q64的原理及应用
复位初始化
对于复位,需要发送0x66和0x99

8b2131d6a7a74015becf6dc6ffe5e2c3.png

代码中的初始化。

  1. /* Reset Operations */
  2. #define RESET_ENABLE_CMD                     0x66
  3. #define RESET_MEMORY_CMD                     0x99
  4. /**
  5.   * @brief  Initializes the W25Q128FV interface.
  6.   * @retval None
  7.   */
  8. uint8_t BSP_W25Qx_Init(void)
  9. {
  10.         /* Reset W25Qxxx */
  11.         BSP_W25Qx_Reset();
  12.         
  13.         return BSP_W25Qx_GetStatus();
  14. }

  15. /**
  16.   * @brief  This function reset the W25Qx.
  17.   * @retval None
  18.   */
  19. static void        BSP_W25Qx_Reset(void)
  20. {
  21.         uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};
  22.         
  23.         W25Qx_Enable();
  24.         /* Send the reset command */
  25.         HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);        
  26.         W25Qx_Disable();

  27. }
复制代码

b6587dfc4fcd4f4a8ca0d58f32bd0f3a.png

ID
对于兆易创新W25Q64,主要有三种查询ID方式。

a48ae7cccf1045c1ab70aa6ad37a32a1.png

可以使用90H查询设备ID,以判断是否是W25Q64设备。

1d515b4535034f138381e95e7dfaf268.png

  1. /* Identification Operations */
  2. #define READ_ID_CMD                          0x9F
  3. /**
  4.   * @brief  Read Manufacture/Device ID.
  5.         * @param  return value address
  6.   * @retval None
  7.   */
  8. void BSP_W25Qx_Read_ID(uint8_t *ID)
  9. {
  10.         uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};
  11.         
  12.         W25Qx_Enable();
  13.         /* Send the read ID command */
  14.         HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);        
  15.         /* Reception of the data */
  16.         HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);
  17.         W25Qx_Disable();
  18.                
  19. }
复制代码


e5f3e86b8a87496e942a4ea634b05f7c.png

读取数据
对于兆易创新W25Q64,读取数据使用0x03指令,后面添加需要读取的数据地址。
数据可以一直进行读取,当不需要读取数据时候将片选CS拉高,关闭时钟SCLK即可。

b272600672cf45799b3e64f55e6aae0d.png

  1. #define READ_CMD                             0x03

  2. /**
  3.   * @brief  Reads an amount of data from the QSPI memory.
  4.   * @param  pData: Pointer to data to be read
  5.   * @param  ReadAddr: Read start address
  6.   * @param  Size: Size of data to read   
  7.   * @retval QSPI memory status
  8.   */
  9. uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
  10. {
  11.         uint8_t cmd[4];

  12.         /* Configure the command */
  13.         cmd[0] = READ_CMD;
  14.         cmd[1] = (uint8_t)(ReadAddr >> 16);
  15.         cmd[2] = (uint8_t)(ReadAddr >> 8);
  16.         cmd[3] = (uint8_t)(ReadAddr);
  17.         
  18.         W25Qx_Enable();
  19.         /* Send the read ID command */
  20.         HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);        
  21.         /* Reception of the data */
  22.         if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
  23.   {
  24.     return W25Qx_ERROR;
  25.   }
  26.         W25Qx_Disable();
  27.         return W25Qx_OK;
  28. }
复制代码

以读取10个数据为例子,波形如下所示。

  1. BSP_W25Qx_Read(rData2,0x1000,0x00a);   
复制代码
   
c3d6f7aad1d44480af9c864c7ac99306.png

擦除扇区
最小的擦除单位是扇区,擦除指令为0x20和3字节的地址。

3d02dfd6d21f45bb83426a95008cdbed.png

  1. #define SECTOR_ERASE_CMD                     0x20
  2. uint8_t BSP_W25Qx_Erase_Block(uint32_t Address)
  3. {
  4.         uint8_t cmd[4];
  5.         uint32_t tickstart = HAL_GetTick();
  6.         cmd[0] = SECTOR_ERASE_CMD;
  7.         cmd[1] = (uint8_t)(Address >> 16);
  8.         cmd[2] = (uint8_t)(Address >> 8);
  9.         cmd[3] = (uint8_t)(Address);
  10.         
  11.         /* Enable write operations */
  12.         BSP_W25Qx_WriteEnable();
  13.         
  14.         /*Select the FLASH: Chip Select low */
  15.         W25Qx_Enable();
  16.         /* Send the read ID command */
  17.         HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);        
  18.         /*Deselect the FLASH: Chip Select high */
  19.         W25Qx_Disable();
  20.         
  21.         /* Wait the end of Flash writing */
  22.         while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  23.         {
  24.                 /* Check for the Timeout */
  25.     if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME)
  26.     {        
  27.                         return W25Qx_TIMEOUT;
  28.     }
  29.         }
  30.         return W25Qx_OK;
  31. }
复制代码


834c52348abb435c80e75aa7c084fe7d.png

写数据
对于写数据到flash中,使用0x02指令进行写数据,后面还需要指定24位地址,才能进行写数据。

08573995dfeb42a9afffabdeefe38f2b.png

  1. #define PAGE_PROG_CMD                        0x02
  2. /**
  3.   * @brief  Writes an amount of data to the QSPI memory.
  4.   * @param  pData: Pointer to data to be written
  5.   * @param  WriteAddr: Write start address
  6.   * @param  Size: Size of data to write,No more than 256byte.   
  7.   * @retval QSPI memory status
  8.   */
  9. uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
  10. {
  11.         uint8_t cmd[4];
  12.         uint32_t end_addr, current_size, current_addr;
  13.         uint32_t tickstart = HAL_GetTick();
  14.         
  15.         /* Calculation of the size between the write address and the end of the page */
  16.   current_addr = 0;

  17.   while (current_addr <= WriteAddr)//判断地址属于哪一扇区开始
  18.   {
  19.     current_addr += W25Q128FV_PAGE_SIZE;//0x100-> 256 bytes
  20.   }
  21.   current_size = current_addr - WriteAddr;

  22.   /* Check if the size of the data is less than the remaining place in the page */
  23.   if (current_size > Size)
  24.   {
  25.     current_size = Size;
  26.   }

  27.   /* Initialize the adress variables *///写入地址大小范围
  28.   current_addr = WriteAddr;
  29.   end_addr = WriteAddr + Size;
  30.         
  31.   /* Perform the write page by page */
  32.   do
  33.   {
  34.                 /* Configure the command */
  35.                 cmd[0] = PAGE_PROG_CMD;
  36.                 cmd[1] = (uint8_t)(current_addr >> 16);
  37.                 cmd[2] = (uint8_t)(current_addr >> 8);
  38.                 cmd[3] = (uint8_t)(current_addr);

  39.                 /* Enable write operations */
  40.                 BSP_W25Qx_WriteEnable();
  41.         
  42.                 W25Qx_Enable();
  43.     /* Send the command */
  44.     if (HAL_SPI_Transmit(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK)
  45.     {
  46.       return W25Qx_ERROR;
  47.     }

  48.     /* Transmission of the data */
  49.     if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK)
  50.     {
  51.       return W25Qx_ERROR;
  52.     }
  53.                         W25Qx_Disable();
  54.             /* Wait the end of Flash writing */
  55.                 while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  56.                 {
  57.                         /* Check for the Timeout */
  58.                         if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
  59.                         {        
  60.                                 return W25Qx_TIMEOUT;
  61.                         }
  62.                 }

  63.     /* Update the address and size variables for next page programming */
  64.     current_addr += current_size;
  65.     pData += current_size;
  66.     current_size = ((current_addr + W25Q128FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q128FV_PAGE_SIZE;
  67.   } while (current_addr < end_addr);

  68.         
  69.         return W25Qx_OK;
  70. }
复制代码

对flash的0x1000地址进行写数据,指令如下。

  1. BSP_W25Qx_Write(wData2,0x1000,0x000a);
复制代码

波形如下所示。

394ce62c94db47a2875ff0cec94ffbda.png

W25Qx.c
  1. /*********************************************************************************************************
  2. *
  3. * File                : ws_W25Qx.c
  4. * Hardware Environment:
  5. * Build Environment   : RealView MDK-ARM  Version: 4.20
  6. * Version             : V1.0
  7. * By                  :
  8. *
  9. *                                  (c) Copyright 2005-2011, WaveShare
  10. *                                       <a href="http://www.waveshare.net" target="_blank">http://www.waveshare.net</a>
  11. *                                          All Rights Reserved
  12. *
  13. *********************************************************************************************************/

  14. #include "W25Qx.h"

  15. /**
  16.   * @brief  Initializes the W25Q128FV interface.
  17.   * @retval None
  18.   */
  19. uint8_t BSP_W25Qx_Init(void)
  20. {
  21.         /* Reset W25Qxxx */
  22.         BSP_W25Qx_Reset();
  23.         
  24.         return BSP_W25Qx_GetStatus();
  25. }

  26. /**
  27.   * @brief  This function reset the W25Qx.
  28.   * @retval None
  29.   */
  30. static void        BSP_W25Qx_Reset(void)
  31. {
  32.         uint8_t cmd[2] = {RESET_ENABLE_CMD,RESET_MEMORY_CMD};
  33.         
  34.         W25Qx_Enable();
  35.         /* Send the reset command */
  36.         HAL_SPI_Transmit(&hspi1, cmd, 2, W25Qx_TIMEOUT_VALUE);        
  37.         W25Qx_Disable();

  38. }

  39. /**
  40.   * @brief  Reads current status of the W25Q128FV.
  41.   * @retval W25Q128FV memory status
  42.   */
  43. static uint8_t BSP_W25Qx_GetStatus(void)
  44. {
  45.         uint8_t cmd[] = {READ_STATUS_REG1_CMD};
  46.         uint8_t status;
  47.         
  48.         W25Qx_Enable();
  49.         /* Send the read status command */
  50.         HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);        
  51.         /* Reception of the data */
  52.         HAL_SPI_Receive(&hspi1,&status, 1, W25Qx_TIMEOUT_VALUE);
  53.         W25Qx_Disable();
  54.         
  55.         /* Check the value of the register */
  56.   if((status & W25Q128FV_FSR_BUSY) != 0)
  57.   {
  58.     return W25Qx_BUSY;
  59.   }
  60.         else
  61.         {
  62.                 return W25Qx_OK;
  63.         }               
  64. }

  65. /**
  66.   * @brief  This function send a Write Enable and wait it is effective.
  67.   * @retval None
  68.   */
  69. uint8_t BSP_W25Qx_WriteEnable(void)
  70. {
  71.         uint8_t cmd[] = {WRITE_ENABLE_CMD};
  72.         uint32_t tickstart = HAL_GetTick();

  73.         /*Select the FLASH: Chip Select low */
  74.         W25Qx_Enable();
  75.         /* Send the read ID command */
  76.         HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);        
  77.         /*Deselect the FLASH: Chip Select high */
  78.         W25Qx_Disable();
  79.         
  80.         /* Wait the end of Flash writing */
  81.         while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  82.         {
  83.                 /* Check for the Timeout */
  84.     if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
  85.     {        
  86.                         return W25Qx_TIMEOUT;
  87.     }
  88.         }
  89.         
  90.         return W25Qx_OK;
  91. }

  92. /**
  93.   * @brief  Read Manufacture/Device ID.
  94.         * @param  return value address
  95.   * @retval None
  96.   */
  97. void BSP_W25Qx_Read_ID(uint8_t *ID)
  98. {
  99.         uint8_t cmd[4] = {READ_ID_CMD,0x00,0x00,0x00};
  100.         
  101.         W25Qx_Enable();
  102.         /* Send the read ID command */
  103.         HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);        
  104.         /* Reception of the data */
  105.         HAL_SPI_Receive(&hspi1,ID, 2, W25Qx_TIMEOUT_VALUE);
  106.         W25Qx_Disable();
  107.                
  108. }

  109. /**
  110.   * @brief  Reads an amount of data from the QSPI memory.
  111.   * @param  pData: Pointer to data to be read
  112.   * @param  ReadAddr: Read start address
  113.   * @param  Size: Size of data to read   
  114.   * @retval QSPI memory status
  115.   */
  116. uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size)
  117. {
  118.         uint8_t cmd[4];

  119.         /* Configure the command */
  120.         cmd[0] = READ_CMD;
  121.         cmd[1] = (uint8_t)(ReadAddr >> 16);
  122.         cmd[2] = (uint8_t)(ReadAddr >> 8);
  123.         cmd[3] = (uint8_t)(ReadAddr);
  124.         
  125.         W25Qx_Enable();
  126.         /* Send the read ID command */
  127.         HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);        
  128.         /* Reception of the data */
  129.         if (HAL_SPI_Receive(&hspi1, pData,Size,W25Qx_TIMEOUT_VALUE) != HAL_OK)
  130.   {
  131.     return W25Qx_ERROR;
  132.   }
  133.         W25Qx_Disable();
  134.         return W25Qx_OK;
  135. }

  136. /**
  137.   * @brief  Writes an amount of data to the QSPI memory.
  138.   * @param  pData: Pointer to data to be written
  139.   * @param  WriteAddr: Write start address
  140.   * @param  Size: Size of data to write,No more than 256byte.   
  141.   * @retval QSPI memory status
  142.   */
  143. uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size)
  144. {
  145.         uint8_t cmd[4];
  146.         uint32_t end_addr, current_size, current_addr;
  147.         uint32_t tickstart = HAL_GetTick();
  148.         
  149.         /* Calculation of the size between the write address and the end of the page */
  150.   current_addr = 0;

  151.   while (current_addr <= WriteAddr)//判断地址属于哪一扇区开始
  152.   {
  153.     current_addr += W25Q128FV_PAGE_SIZE;//0x100-> 256 bytes
  154.   }
  155.   current_size = current_addr - WriteAddr;

  156.   /* Check if the size of the data is less than the remaining place in the page */
  157.   if (current_size > Size)
  158.   {
  159.     current_size = Size;
  160.   }

  161.   /* Initialize the adress variables *///写入地址大小范围
  162.   current_addr = WriteAddr;
  163.   end_addr = WriteAddr + Size;
  164.         
  165.   /* Perform the write page by page */
  166.   do
  167.   {
  168.                 /* Configure the command */
  169.                 cmd[0] = PAGE_PROG_CMD;
  170.                 cmd[1] = (uint8_t)(current_addr >> 16);
  171.                 cmd[2] = (uint8_t)(current_addr >> 8);
  172.                 cmd[3] = (uint8_t)(current_addr);

  173.                 /* Enable write operations */
  174.                 BSP_W25Qx_WriteEnable();
  175.         
  176.                 W25Qx_Enable();
  177.     /* Send the command */
  178.     if (HAL_SPI_Transmit(&hspi1,cmd, 4, W25Qx_TIMEOUT_VALUE) != HAL_OK)
  179.     {
  180.       return W25Qx_ERROR;
  181.     }

  182.     /* Transmission of the data */
  183.     if (HAL_SPI_Transmit(&hspi1, pData,current_size, W25Qx_TIMEOUT_VALUE) != HAL_OK)
  184.     {
  185.       return W25Qx_ERROR;
  186.     }
  187.                         W25Qx_Disable();
  188.             /* Wait the end of Flash writing */
  189.                 while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  190.                 {
  191.                         /* Check for the Timeout */
  192.                         if((HAL_GetTick() - tickstart) > W25Qx_TIMEOUT_VALUE)
  193.                         {        
  194.                                 return W25Qx_TIMEOUT;
  195.                         }
  196.                 }

  197.     /* Update the address and size variables for next page programming */
  198.     current_addr += current_size;
  199.     pData += current_size;
  200.     current_size = ((current_addr + W25Q128FV_PAGE_SIZE) > end_addr) ? (end_addr - current_addr) : W25Q128FV_PAGE_SIZE;
  201.   } while (current_addr < end_addr);

  202.         
  203.         return W25Qx_OK;
  204. }

  205. /**
  206.   * @brief  Erases the specified block of the QSPI memory.
  207.   * @param  BlockAddress: Block address to erase  
  208.   * @retval QSPI memory status
  209.   */
  210. uint8_t BSP_W25Qx_Erase_Block(uint32_t Address)
  211. {
  212.         uint8_t cmd[4];
  213.         uint32_t tickstart = HAL_GetTick();
  214.         cmd[0] = SECTOR_ERASE_CMD;
  215.         cmd[1] = (uint8_t)(Address >> 16);
  216.         cmd[2] = (uint8_t)(Address >> 8);
  217.         cmd[3] = (uint8_t)(Address);
  218.         
  219.         /* Enable write operations */
  220. //        BSP_W25Qx_WriteEnable();
  221.         
  222.         /*Select the FLASH: Chip Select low */
  223.         W25Qx_Enable();
  224.         /* Send the read ID command */
  225.         HAL_SPI_Transmit(&hspi1, cmd, 4, W25Qx_TIMEOUT_VALUE);        
  226.         /*Deselect the FLASH: Chip Select high */
  227.         W25Qx_Disable();
  228.         
  229.         /* Wait the end of Flash writing */
  230.         while(BSP_W25Qx_GetStatus() == W25Qx_BUSY);
  231.         {
  232.                 /* Check for the Timeout */
  233.     if((HAL_GetTick() - tickstart) > W25Q128FV_SECTOR_ERASE_MAX_TIME)
  234.     {        
  235.                         return W25Qx_TIMEOUT;
  236.     }
  237.         }
  238.         return W25Qx_OK;
  239. }

  240. /**
  241.   * @brief  Erases the entire QSPI memory.This function will take a very long time.
  242.   * @retval QSPI memory status
  243.   */
  244. uint8_t BSP_W25Qx_Erase_Chip(void)
  245. {
  246.         uint8_t cmd[4];
  247.         uint32_t tickstart = HAL_GetTick();
  248.         cmd[0] = SECTOR_ERASE_CMD;
  249.         
  250.         /* Enable write operations */
  251.         BSP_W25Qx_WriteEnable();
  252.         
  253.         /*Select the FLASH: Chip Select low */
  254.         W25Qx_Enable();
  255.         /* Send the read ID command */
  256.         HAL_SPI_Transmit(&hspi1, cmd, 1, W25Qx_TIMEOUT_VALUE);        
  257.         /*Deselect the FLASH: Chip Select high */
  258.         W25Qx_Disable();
  259.         
  260.         /* Wait the end of Flash writing */
  261.         while(BSP_W25Qx_GetStatus() != W25Qx_BUSY);
  262.         {
  263.                 /* Check for the Timeout */
  264.     if((HAL_GetTick() - tickstart) > W25Q128FV_BULK_ERASE_MAX_TIME)
  265.     {        
  266.                         return W25Qx_TIMEOUT;
  267.     }
  268.         }
  269.         return W25Qx_OK;
  270. }
复制代码

W25Qx.h
  1. /*********************************************************************************************************
  2. *
  3. * File                : W25Qx.h
  4. * Hardware Environment:
  5. * Build Environment   : RealView MDK-ARM  Version: 5.15
  6. * Version             : V1.0
  7. * By                  :
  8. *
  9. *                                  (c) Copyright 2005-2015, WaveShare
  10. *                                       <a href="http://www.waveshare.net" target="_blank">http://www.waveshare.net</a>
  11. *                                          All Rights Reserved
  12. *
  13. *********************************************************************************************************/
  14. /* Define to prevent recursive inclusion -------------------------------------*/
  15. #ifndef __W25Qx_H
  16. #define __W25Qx_H

  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif

  20. /* Includes ------------------------------------------------------------------*/
  21. #include "stm32f0xx.h"
  22. #include "spi.h"
  23.          
  24. /** @addtogroup BSP
  25.   * @{
  26.   */

  27. /** @addtogroup Components
  28.   * @{
  29.   */

  30. /** @addtogroup W25Q128FV
  31.   * @{
  32.   */

  33. /** @defgroup W25Q128FV_Exported_Types
  34.   * @{
  35.   */

  36. /**
  37.   * @}
  38.   */

  39. /** @defgroup W25Q128FV_Exported_Constants
  40.   * @{
  41.   */

  42. /**
  43.   * @brief  W25Q128FV Configuration  
  44.   */  
  45. #define W25Q128FV_FLASH_SIZE                  0x1000000 /* 128 MBits => 16MBytes */
  46. #define W25Q128FV_SECTOR_SIZE                 0x10000   /* 256 sectors of 64KBytes */
  47. #define W25Q128FV_SUBSECTOR_SIZE              0x1000    /* 4096 subsectors of 4kBytes */
  48. #define W25Q128FV_PAGE_SIZE                   0x100     /* 65536 pages of 256 bytes */

  49. #define W25Q128FV_DUMMY_CYCLES_READ           4
  50. #define W25Q128FV_DUMMY_CYCLES_READ_QUAD      10

  51. #define W25Q128FV_BULK_ERASE_MAX_TIME         250000
  52. #define W25Q128FV_SECTOR_ERASE_MAX_TIME       3000
  53. #define W25Q128FV_SUBSECTOR_ERASE_MAX_TIME    800
  54. #define W25Qx_TIMEOUT_VALUE 1000

  55. /**
  56.   * @brief  W25Q128FV Commands  
  57.   */  
  58. /* Reset Operations */
  59. #define RESET_ENABLE_CMD                     0x66
  60. #define RESET_MEMORY_CMD                     0x99

  61. #define ENTER_QPI_MODE_CMD                   0x38
  62. #define EXIT_QPI_MODE_CMD                    0xFF

  63. /* Identification Operations */
  64. #define READ_ID_CMD                          0x90
  65. #define DUAL_READ_ID_CMD                     0x92
  66. #define QUAD_READ_ID_CMD                     0x94
  67. #define READ_JEDEC_ID_CMD                    0x9F

  68. /* Read Operations */
  69. #define READ_CMD                             0x03
  70. #define FAST_READ_CMD                        0x0B
  71. #define DUAL_OUT_FAST_READ_CMD               0x3B
  72. #define DUAL_INOUT_FAST_READ_CMD             0xBB
  73. #define QUAD_OUT_FAST_READ_CMD               0x6B
  74. #define QUAD_INOUT_FAST_READ_CMD             0xEB

  75. /* Write Operations */
  76. #define WRITE_ENABLE_CMD                     0x06
  77. #define WRITE_DISABLE_CMD                    0x04

  78. /* Register Operations */
  79. #define READ_STATUS_REG1_CMD                  0x05
  80. #define READ_STATUS_REG2_CMD                  0x35
  81. #define READ_STATUS_REG3_CMD                  0x15

  82. #define WRITE_STATUS_REG1_CMD                 0x01
  83. #define WRITE_STATUS_REG2_CMD                 0x31
  84. #define WRITE_STATUS_REG3_CMD                 0x11


  85. /* Program Operations */
  86. #define PAGE_PROG_CMD                        0x02
  87. #define QUAD_INPUT_PAGE_PROG_CMD             0x32


  88. /* Erase Operations */
  89. #define SECTOR_ERASE_CMD                     0x20
  90. #define CHIP_ERASE_CMD                       0xC7

  91. #define PROG_ERASE_RESUME_CMD                0x7A
  92. #define PROG_ERASE_SUSPEND_CMD               0x75


  93. /* Flag Status Register */
  94. #define W25Q128FV_FSR_BUSY                    ((uint8_t)0x01)    /*!< busy */
  95. #define W25Q128FV_FSR_WREN                    ((uint8_t)0x02)    /*!< write enable */
  96. #define W25Q128FV_FSR_QE                      ((uint8_t)0x02)    /*!< quad enable */


  97. #define W25Qx_Enable()                         HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET)
  98. #define W25Qx_Disable()                 HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET)

  99. #define W25Qx_OK            ((uint8_t)0x00)
  100. #define W25Qx_ERROR         ((uint8_t)0x01)
  101. #define W25Qx_BUSY          ((uint8_t)0x02)
  102. #define W25Qx_TIMEOUT                                ((uint8_t)0x03)


  103. uint8_t BSP_W25Qx_Init(void);
  104. static void        BSP_W25Qx_Reset(void);
  105. static uint8_t BSP_W25Qx_GetStatus(void);
  106. uint8_t BSP_W25Qx_WriteEnable(void);
  107. void BSP_W25Qx_Read_ID(uint8_t *ID);
  108. uint8_t BSP_W25Qx_Read(uint8_t* pData, uint32_t ReadAddr, uint32_t Size);
  109. uint8_t BSP_W25Qx_Write(uint8_t* pData, uint32_t WriteAddr, uint32_t Size);
  110. uint8_t BSP_W25Qx_Erase_Block(uint32_t Address);
  111. uint8_t BSP_W25Qx_Erase_Chip(void);

  112. /**
  113.   * @}
  114.   */

  115. /** @defgroup W25Q128FV_Exported_Functions
  116.   * @{
  117.   */
  118. /**
  119.   * @}
  120.   */

  121. /**
  122.   * @}
  123.   */

  124. /**
  125.   * @}
  126.   */

  127. /**
  128.   * @}
  129.   */

  130. #ifdef __cplusplus
  131. }
  132. #endif

  133. #endif /* __W25Qx_H */
复制代码

案例
向0扇区(0块0扇区),17扇区(1块1扇区),34扇区(2块2扇区)分别写入0x200的数据。

头文件定义
  1. /* USER CODE BEGIN Includes */
  2. #include "stdio.h"

  3. #include "W25Qx.h"
  4. /* USER CODE END Includes */
复制代码

串口接收和flash数组定义
  1. /* USER CODE BEGIN PV */
  2. #define BUFFERSIZE 255           //可以接收的最大字符个数      
  3. uint8_t ReceiveBuff[BUFFERSIZE]; //接收缓冲区
  4. uint8_t recv_end_flag = 0,Rx_len;//接收完成中断标志,接收到字符长度

  5. uint8_t wData1[0x200];
  6. uint8_t wData2[0x200];
  7. uint8_t wData3[0x200];

  8. uint8_t rData1[0x200];
  9. uint8_t rData2[0x200];
  10. uint8_t rData3[0x200];
  11. uint8_t ID[4];
  12. uint32_t i;

  13. uint8_t flag[1] ;
  14. int i_flag = 0;
  15. /* USER CODE END PV */
复制代码

串口重定向
  1. /* USER CODE BEGIN PFP */
  2. void uart2_data(void);
  3. #ifdef __GNUC__                                                                        //串口重定向
  4. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  5. #else
  6. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  7. #endif
  8. PUTCHAR_PROTOTYPE
  9. {
  10.     HAL_UART_Transmit(&huart2 , (uint8_t *)&ch, 1, 0xFFFF);
  11.     return ch;
  12. }
  13. /* USER CODE END PFP */
复制代码

串口中断设置
  1. #include "stm32f1xx_it.c"文件中断外部变量引用:

  2. /* USER CODE BEGIN 0 */
  3. #define BUFFERSIZE 255        //可接收的最大数据量
  4. extern uint8_t recv_end_flag,Rx_len,bootfirst;
  5. /* USER CODE END 0 */
复制代码

串口2中断函数:

  1. /**
  2.   * @brief This function handles USART2 global interrupt.
  3.   */
  4. void USART2_IRQHandler(void)
  5. {
  6.   /* USER CODE BEGIN USART2_IRQn 0 */

  7.   /* USER CODE END USART2_IRQn 0 */
  8.   HAL_UART_IRQHandler(&huart2);
  9.   /* USER CODE BEGIN USART2_IRQn 1 */
  10.         uint32_t temp;
  11.         if(USART2 == huart2.Instance)//判断是否为串口2中断

  12.         {      
  13.                 if(RESET != __HAL_UART_GET_FLAG(&huart2,UART_FLAG_IDLE))//如果为串口2
  14.                 {
  15.                         __HAL_UART_CLEAR_IDLEFLAG(&huart2);//清除中断标志
  16.       HAL_UART_DMAStop(&huart2);//停止DMA接收
  17.                      temp  = __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);//获取DMA当前还有多少未填充
  18.                       Rx_len =  BUFFERSIZE - temp; //计算串口接收到的数据个数
  19.                       recv_end_flag = 1;
  20.              }
  21.         }

  22.   /* USER CODE END USART2_IRQn 1 */
  23. }
复制代码

主程序
读取ID和flash数据及擦除。

  1.   /* USER CODE BEGIN 2 */
  2.         printf("GD Nor Flash案例\n");
  3.          __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);//使能串口1 IDLE中断
  4.         
  5.         /*##-1- Read the device ID  ########################*/
  6.         BSP_W25Qx_Init();//初始化W25Q128
  7.         BSP_W25Qx_Read_ID(ID);//读取ID

  8.         if((ID[0] != 0xC8) | (ID[1] != 0x16))
  9.         {
  10.                 Error_Handler();//如果 ID不对打印错误
  11.         }
  12.         else//ID正确,打印ID
  13.         {
  14.                 printf("W25Q64 ID : ");
  15.                 for(i=0;i<2;i++)
  16.                 {
  17.                         printf("0x%02X ",ID);
  18.                 }
  19.                 printf("\r\n\r\n");
  20.         }


  21. /**************************读取第0扇区数据**************************************************************/

  22.         /*##-3- Read the flash     ########################*/
  23.         /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
  24.         if(BSP_W25Qx_Read(rData1,0x0,0x200)== W25Qx_OK)
  25.                 printf("读取原始的0个扇区数据成功!\n");
  26.         else
  27.                 Error_Handler();
  28.         /*打印数据*/        
  29.         printf("读取原始的0个扇区数据为: \r\n");
  30.         
  31.         for(i =0;i<0x200;i++)
  32.         {
  33.                 if(i%20==0)
  34.                         printf("\n0扇区第%d到%d的数据为:\r\n",i,i+19);
  35.                                 printf("0x%02X  ",rData1);
  36.         }

  37.         printf("\n");

  38.         
  39. /**************************读取第17扇区数据**************************************************************/
  40.         
  41.         /*##-3- Read the flash     ########################*/
  42.         /*读取数据,rData读取数据的指针,起始地址0x1000,读取数据长度0x200*/
  43.         if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
  44.                 printf("读取原始的17个扇区数据成功!\n");
  45.         else
  46.                 Error_Handler();
  47.         /*打印数据*/        
  48.         printf("读取原始的2个扇区数据为:");
  49.         
  50.         for(i =0;i<0x200;i++)
  51.         {
  52.                 if(i%20==0)
  53.                         printf("\n17扇区第%d到%d的数据为:\r\n",i,i+19);
  54.                                 printf("0x%02X  ",rData2);
  55.         }

  56.         printf("\n");        
  57.         
  58.         
  59. /**************************读取第34扇区数据**************************************************************/
  60.         
  61.         /*##-3- Read the flash     ########################*/
  62.         /*读取数据,rData读取数据的指针,起始地址0x2000,读取数据长度0x200*/
  63.         if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
  64.                 printf("读取原始的34个扇区数据成功!\n");
  65.         else
  66.                 Error_Handler();
  67.         /*打印数据*/        
  68.         printf("读取原始的34个扇区数据为: ");
  69.         
  70.         for(i =0;i<0x200;i++)
  71.         {
  72.                 if(i%20==0)
  73.                         printf("\n34扇区第%d到%d的数据为:\r\n",i,i+19);
  74.                                 printf("0x%02X  ",rData3);
  75.         }

  76.         printf("\n");        
  77.         
  78.         

  79. /**************************清除第0扇区数据为0**************************************************************/


  80.         
  81.         /*##-2- Erase Block ##################################*/
  82.         if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
  83.                 printf(" QSPI Erase Block ok\r\n");
  84.         else
  85.                 Error_Handler();
  86.         
  87.         /*##-2- Written to the flash ########################*/
  88.         /* fill buffer */
  89.         printf(" 初始化数据,清零第0扇区前0x200的数据!\r\n");
  90.         for(i =0;i<0x200;i ++)
  91.         {
  92.                         wData1 = 0;
  93.                   rData1 = 0;
  94.         }
  95.         /*写入数据,wData写入数据的指针,起始地址0x00,写入数据长度0x200*/
  96.         if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
  97.                 printf("清零第0扇区前0x200的数据成功!\r\n");
  98.         else
  99.                 Error_Handler();

  100.         
  101.         
  102.         
  103.         /*##-3- Read the flash     ########################*/
  104.         /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
  105.         if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
  106.                 printf("读取第0扇区前0x200数据成功!\r\n\r\n");
  107.         else
  108.                 Error_Handler();
  109.         /*打印数据*/        
  110.         printf("读取第0扇区前0x200数据为: \r\n");
  111.         
  112.         for(i =0;i<0x200;i++)
  113.         {
  114.                 if(i%20==0)
  115.                         printf("\n第%d到%d的数据为:\r\n",i,i+19);
  116.                                 printf("0x%02X  ",rData1);
  117.         }

  118.         printf("\n");


  119. /**************************清除第17扇区数据为0**************************************************************/


  120.         
  121.         /*##-2- Erase Block ##################################*/
  122.         if(BSP_W25Qx_Erase_Block(0x11000) == W25Qx_OK)
  123.                 printf(" QSPI Erase Block ok\r\n");
  124.         else
  125.                 Error_Handler();
  126.         
  127.         /*##-2- Written to the flash ########################*/
  128.         /* fill buffer */
  129.         printf(" 初始化数据,清零第17扇区前0x200的数据!\r\n");
  130.         for(i =0;i<0x200;i ++)
  131.         {
  132.                         wData2 = 0;
  133.                   rData2 = 0;
  134.         }
  135.         /*写入数据,wData写入数据的指针,起始地址0x1000,写入数据长度0x200*/
  136.         if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)
  137.                 printf("清零第2扇区前0x200的数据成功!\r\n");
  138.         else
  139.                 Error_Handler();

  140.         
  141.         
  142.         
  143.         /*##-3- Read the flash     ########################*/
  144.         /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
  145.         if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
  146.                 printf("读取第17扇区前0x200数据成功!\r\n\r\n");
  147.         else
  148.                 Error_Handler();
  149.         /*打印数据*/        
  150.         printf("读取第17扇区前0x200数据为: \r\n");
  151.         
  152.         for(i =0;i<0x200;i++)
  153.         {
  154.                 if(i%20==0)
  155.                         printf("\n第%d到%d的数据为:\r\n",i,i+19);
  156.                                 printf("0x%02X  ",rData2);
  157.         }

  158.         printf("\n");


  159. /**************************清除第34扇区数据为0**************************************************************/


  160.         
  161.         /*##-2- Erase Block ##################################*/
  162.         if(BSP_W25Qx_Erase_Block(0x22000) == W25Qx_OK)
  163.                 printf(" QSPI Erase Block ok\r\n");
  164.         else
  165.                 Error_Handler();
  166.         
  167.         /*##-2- Written to the flash ########################*/
  168.         /* fill buffer */
  169.         printf(" 初始化数据,清零第34扇区前0x200的数据!\r\n");
  170.         for(i =0;i<0x200;i ++)
  171.         {
  172.                         wData3 = 0;
  173.                   rData3 = 0;
  174.         }
  175.         /*写入数据,wData写入数据的指针,起始地址0x22000,写入数据长度0x200*/
  176.         if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)
  177.                 printf("清零第34扇区前0x200的数据成功!\r\n");
  178.         else
  179.                 Error_Handler();

  180.         
  181.         
  182.         
  183.         /*##-3- Read the flash     ########################*/
  184.         /*读取数据,rData读取数据的指针,起始地址0x00,读取数据长度0x200*/
  185.         if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
  186.                 printf("读取第34扇区前0x200数据成功!\r\n\r\n");
  187.         else
  188.                 Error_Handler();
  189.         /*打印数据*/        
  190.         printf("读取第34扇区前0x200数据为: \r\n");
  191.         
  192.         for(i =0;i<0x200;i++)
  193.         {
  194.                 if(i%20==0)
  195.                         printf("\n第%d到%d的数据为:\r\n",i,i+19);
  196.                                 printf("0x%02X  ",rData3);
  197.         }

  198.         printf("\n");


  199.   /* USER CODE END 2 */
复制代码

主程序。

  1.   /* Infinite loop */
  2.   /* USER CODE BEGIN WHILE */
  3.   while (1)
  4.   {
  5.     /* USER CODE END WHILE */

  6.     /* USER CODE BEGIN 3 */
  7.                 uart2_data();
  8.                 HAL_Delay(100);
  9.                
  10.   }
  11.   /* USER CODE END 3 */
复制代码

数据处理
  1. /* USER CODE BEGIN 4 */
  2. void uart2_data(void)
  3. {
  4.         if(recv_end_flag ==1)//接收完成标志
  5.         {
  6.         

  7. if(ReceiveBuff[0]==0x00)
  8.                 {
  9.                         printf("写入数据长度:%d\n",Rx_len-2);
  10.                         for(int i =0;i<Rx_len-2;i++)
  11.                         {
  12.                                 wData1[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];

  13.                         }
  14.                                 

  15.                 /*##-2- Erase Block ##################################*/
  16.                 if(BSP_W25Qx_Erase_Block(0) == W25Qx_OK)
  17.                         printf(" QSPI Erase Block ok\r\n");
  18.                 else
  19.                         Error_Handler();

  20.                
  21.                 /*写入数据,wData写入数据的指针,起始地址0x00,写入数据长度0x200*/
  22.                 if(BSP_W25Qx_Write(wData1,0x00,0x200)== W25Qx_OK)
  23.                         
  24.                         printf("扇区0数据成功~~~~~~~~~~~~~~~~~~~~~~~~~~!\r\n");
  25.                 else
  26.                         Error_Handler();
  27.                         
  28.                 if(BSP_W25Qx_Read(rData1,0x00,0x200)== W25Qx_OK)
  29.                         printf("读取扇区0前0x200数据成功!\r\n\r\n");
  30.                 else
  31.                         Error_Handler();
  32.                 /*打印数据*/        
  33.                 printf("读取扇区0前0x200数据为: \r\n");
  34.         
  35.                 for(i =0;i<0x200;i++)
  36.                 {
  37.                         if(i%20==0)
  38.                                 printf("\n第%d到%d的数据为:\r\n",i,i+19);
  39.                                         printf("0x%02X  ",wData1);
  40.                 }

  41.                 printf("\n");
  42.                
  43.         }
  44.                
  45.         
  46.         
  47.         else if(ReceiveBuff[0]==0x17)
  48.         {
  49.                         printf("写入数据长度:%d\n",Rx_len-2);
  50.                         for(int i =0;i<Rx_len-2;i++)
  51.                         {
  52. //                                Data=ReceiveBuff[i+2];
  53.                                 wData2[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];
  54.                         }
  55.                                 

  56.                 /*##-17- Erase Block ##################################*/
  57.                 if(BSP_W25Qx_Erase_Block(0x11000) == W25Qx_OK)
  58.                         printf(" QSPI Erase Block ok\r\n");
  59.                 else
  60.                         Error_Handler();

  61.                
  62.                 /*写入数据,wData写入数据的指针,起始地址0x11000,写入数据长度0x200*/
  63.                 if(BSP_W25Qx_Write(wData2,0x11000,0x200)== W25Qx_OK)
  64.                         
  65.                         printf("扇区17数据成功~~~~~~~~~~~~~~~~~~~~~~~~~~!\r\n");
  66.                 else
  67.                         Error_Handler();
  68.                         
  69.                 if(BSP_W25Qx_Read(rData2,0x11000,0x200)== W25Qx_OK)
  70.                         printf("读取扇区17前0x200数据成功!\r\n\r\n");
  71.                 else
  72.                         Error_Handler();
  73.                 /*打印数据*/        
  74.                 printf("读取扇区17前0x200数据为: \r\n");
  75.         
  76.                 for(i =0;i<0x200;i++)
  77.                 {
  78.                         if(i%20==0)
  79.                                 printf("\n第%d到%d的数据为:\r\n",i,i+19);
  80.                                         printf("0x%02X  ",rData2);
  81.                 }

  82.                 printf("\n");
  83.                
  84.         }               



  85.         else if(ReceiveBuff[0]==0x34)
  86.         {
  87.                         printf("写入数据长度:%d\n",Rx_len-2);
  88.                         for(int i =0;i<Rx_len-2;i++)
  89.                         {
  90. //                                Data=ReceiveBuff[i+2];
  91.                                 wData3[ (i+ReceiveBuff[1]) ] = ReceiveBuff[i+2];
  92.                         }
  93.                                 

  94.                 /*##-22- Erase Block ##################################*/
  95.                 if(BSP_W25Qx_Erase_Block(0x22000) == W25Qx_OK)
  96.                         printf(" QSPI Erase Block ok\r\n");
  97.                 else
  98.                         Error_Handler();

  99.                
  100.                 /*写入数据,wData写入数据的指针,起始地址0x22000,写入数据长度0x200*/
  101.                 if(BSP_W25Qx_Write(wData3,0x22000,0x200)== W25Qx_OK)
  102.                         
  103.                         printf("扇区34数据成功~~~~~~~~~~~~~~~~~~~~~~~~~~!\r\n");
  104.                 else
  105.                         Error_Handler();
  106.                         
  107.                 if(BSP_W25Qx_Read(rData3,0x22000,0x200)== W25Qx_OK)
  108.                         printf("读取扇区34前0x200数据成功!\r\n\r\n");
  109.                 else
  110.                         Error_Handler();
  111.                 /*打印数据*/        
  112.                 printf("读取扇区34前0x200数据为: \r\n");
  113.         
  114.                 for(i =0;i<0x200;i++)
  115.                 {
  116.                         if(i%20==0)
  117.                                 printf("\n第%d到%d的数据为:\r\n",i,i+19);
  118.                                         printf("0x%02X  ",rData3);
  119.                 }

  120.                 printf("\n");
  121.                
  122.         }        



  123.                
  124.                 else
  125.                         printf("输入错误!");

  126.     for(int i = 0; i < Rx_len ; i++) //清空接收缓存区
  127.     ReceiveBuff=0;//置0
  128.     Rx_len=0;//接收数据长度清零
  129.     recv_end_flag=0;//接收标志位清零
  130.                 //开启下一次接收
  131.     HAL_UART_Receive_DMA(&huart2,(uint8_t*)ReceiveBuff,BUFFERSIZE);
  132.     }

  133. }

  134. /* USER CODE END 4 */
复制代码

演示
W25Q64芯片型号的ID为0XEF17,下方读取为0XC816,所以读取成功。

724107be5c7743688035f34171618ec0.png

开机会打印出0,17,34扇区的前0x200个数据。


23708128edc5478382c20fa961eff4e8.png

打印完原始数据之后将数据全部清零,清零完成如下图所示。

0ac9a59d0a6f4138bbc3ea3e3e3c612f.png

串口定义了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。

bc97fcddb01b4e3182345641d2fa28f2.png

输入:00 28 11 12 13 14 15
向扇区0的的40(28是十六进制)号位置开始写入数据11 12 13 14 15。

4770ed6a70c849228eba1429605c74a7.png

输入:17 10 aa bb
向扇区17的的16(10是十六进制)号位置开始写入数据aa bb。

a3cb95aa3c694c7f90bf8d8b85a3d321.png

————————————————
版权声明:记帖



收藏 评论0 发布时间:2022-10-20 19:14

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版