driver_spiflash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #include <rtthread.h>
  2. #include "stm32g0xx_hal.h"
  3. #include "driver_spiflash.h"
  4. /* 私有类型定义 --------------------------------------------------------------*/
  5. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
  6. /* 私有宏定义 ----------------------------------------------------------------*/
  7. /* 获取缓冲区的长度 */
  8. #define countof(a) (sizeof(a) / sizeof(*(a)))
  9. #define TxBufferSize1 (countof(TxBuffer1) - 1)
  10. #define RxBufferSize1 (countof(TxBuffer1) - 1)
  11. #define BufferSize (countof(Tx_Buffer)-1)
  12. #define FLASH_WriteAddress 0x00000
  13. #define FLASH_ReadAddress FLASH_WriteAddress
  14. #define FLASH_SectorToErase FLASH_WriteAddress
  15. #define SPI_FLASH_CS_H() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET)
  16. #define SPI_FLASH_CS_L() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET)
  17. /* 私有变量 ------------------------------------------------------------------*/
  18. uint8_t Tx_Buffer[] = " test data is right or not test123456";
  19. uint8_t Rx_Buffer[BufferSize];
  20. __IO uint32_t DeviceID = 0;
  21. __IO uint32_t FlashID = 0;
  22. __IO TestStatus TransferStatus1 = FAILED;
  23. /* 扩展变量 ------------------------------------------------------------------*/
  24. /* 私有函数原形 --------------------------------------------------------------*/
  25. static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
  26. /* 包含头文件 ----------------------------------------------------------------*/
  27. #include "driver_spiflash.h"
  28. /* 私有类型定义 --------------------------------------------------------------*/
  29. /* 私有宏定义 ----------------------------------------------------------------*/
  30. #define SPI_FLASH_PageSize 256
  31. #define SPI_FLASH_PerWritePageSize 256
  32. #define W25X_WriteEnable 0x06
  33. #define W25X_WriteDisable 0x04
  34. #define W25X_ReadStatusReg 0x05
  35. #define W25X_WriteStatusReg 0x01
  36. #define W25X_ReadData 0x03
  37. #define W25X_FastReadData 0x0B
  38. #define W25X_FastReadDual 0x3B
  39. #define W25X_PageProgram 0x02
  40. #define W25X_BlockErase 0xD8
  41. #define W25X_SectorErase 0x20
  42. #define W25X_ChipErase 0xC7
  43. #define W25X_PowerDown 0xB9
  44. #define W25X_ReleasePowerDown 0xAB
  45. #define W25X_DeviceID 0xAB
  46. #define W25X_ManufactDeviceID 0x90
  47. #define W25X_JedecDeviceID 0x9F
  48. #define WIP_Flag 0x01 /* Write In Progress (WIP) flag */
  49. #define Dummy_Byte 0xFF
  50. /* 私有变量 ------------------------------------------------------------------*/
  51. SPI_HandleTypeDef hspiflash;
  52. /* 扩展变量 ------------------------------------------------------------------*/
  53. /* 私有函数原形 --------------------------------------------------------------*/
  54. /* 函数体 --------------------------------------------------------------------*/
  55. /**
  56. * 函数功能: 串行FLASH初始化
  57. * 输入参数: huart:串口句柄类型指针
  58. * 返 回 值: 无
  59. * 说 明: 该函数被HAL库内部调用
  60. */
  61. int MX_SPIFlash_Init(void)
  62. {
  63. hspiflash.Instance = FLASH_SPIx;
  64. hspiflash.Init.Mode = SPI_MODE_MASTER;
  65. hspiflash.Init.Direction = SPI_DIRECTION_2LINES;
  66. hspiflash.Init.DataSize = SPI_DATASIZE_8BIT;
  67. hspiflash.Init.CLKPolarity = SPI_POLARITY_HIGH;
  68. hspiflash.Init.CLKPhase = SPI_PHASE_2EDGE;
  69. hspiflash.Init.NSS = SPI_NSS_SOFT;
  70. hspiflash.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  71. hspiflash.Init.FirstBit = SPI_FIRSTBIT_MSB;
  72. hspiflash.Init.TIMode = SPI_TIMODE_DISABLE;
  73. hspiflash.Init.CRCCalculation = SPI_CRCCALCULATION_ENABLE;
  74. hspiflash.Init.CRCPolynomial = 7;
  75. hspiflash.Init.CRCLength = SPI_CRC_LENGTH_8BIT;
  76. hspiflash.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
  77. if (HAL_SPI_Init(&hspiflash) != HAL_OK)
  78. {
  79. rt_kprintf("Error_Handler HAL_SPI_Init\r\n");
  80. }
  81. // rt_kprintf("MX_SPIFlash_Init \r\n");
  82. return 0;
  83. }
  84. /**
  85. * 函数功能: SPI外设系统级初始化
  86. * 输入参数: hspi:SPI句柄类型指针
  87. * 返 回 值: 无
  88. * 说 明: 该函数被HAL库内部调用
  89. */
  90. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  91. {
  92. GPIO_InitTypeDef GPIO_InitStruct = {0};
  93. if(hspi->Instance==FLASH_SPIx)
  94. {
  95. __HAL_RCC_SPI1_CLK_ENABLE();
  96. __HAL_RCC_GPIOA_CLK_ENABLE();
  97. /**SPI1 GPIO Configuration
  98. PA5 ------> SPI1_SCK
  99. PA6 ------> SPI1_MISO
  100. PA7 ------> SPI1_MOSI
  101. */
  102. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  103. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  104. GPIO_InitStruct.Pull = GPIO_NOPULL;
  105. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  106. GPIO_InitStruct.Alternate = GPIO_AF0_SPI1;
  107. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  108. //rt_kprintf("HAL_SPI_MspInit \n\n");// 会到这里
  109. }
  110. }
  111. /**
  112. * 函数功能: SPI外设系统级反初始化
  113. * 输入参数: hspi:SPI句柄类型指针
  114. * 返 回 值: 无
  115. * 说 明: 该函数被HAL库内部调用
  116. */
  117. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
  118. {
  119. if(hspi->Instance==FLASH_SPIx)
  120. {
  121. /* SPI外设时钟禁用 */
  122. FLASH_SPIx_RCC_CLK_DISABLE();
  123. /**SPI1 GPIO Configuration
  124. PA4 ------> SPI1_NSS
  125. PA5 ------> SPI1_SCK
  126. PA6 ------> SPI1_MISO
  127. PA7 ------> SPI1_MOSI
  128. */
  129. HAL_GPIO_DeInit(FLASH_SPI_GPIO_PORT, FLASH_SPI_SCK_PIN|FLASH_SPI_MOSI_PIN|FLASH_SPI_MISO_PIN);
  130. HAL_GPIO_DeInit(FLASH_SPI_CS_PORT, FLASH_SPI_CS_PIN);
  131. }
  132. }
  133. /**
  134. * 函数功能: 擦除扇区
  135. * 输入参数: SectorAddr:待擦除扇区地址,要求为4096倍数
  136. * 返 回 值: 无
  137. * 说 明:串行Flash最小擦除块大小为4KB(4096字节),即一个扇区大小,要求输入参数
  138. * 为4096倍数。在往串行Flash芯片写入数据之前要求先擦除空间。
  139. */
  140. void SPI_FLASH_SectorErase(uint32_t SectorAddr)
  141. {
  142. /* 发送FLASH写使能命令 */
  143. SPI_FLASH_WriteEnable();
  144. SPI_FLASH_WaitForWriteEnd();
  145. /* 擦除扇区 */
  146. /* 选择串行FLASH: CS低电平 */
  147. FLASH_SPI_CS_ENABLE();
  148. /* 发送扇区擦除指令*/
  149. SPI_FLASH_SendByte(W25X_SectorErase);
  150. /*发送擦除扇区地址的高位*/
  151. SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
  152. /* 发送擦除扇区地址的中位 */
  153. SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);
  154. /* 发送擦除扇区地址的低位 */
  155. SPI_FLASH_SendByte(SectorAddr & 0xFF);
  156. /* 禁用串行FLASH: CS 高电平 */
  157. FLASH_SPI_CS_DISABLE();
  158. /* 等待擦除完毕*/
  159. SPI_FLASH_WaitForWriteEnd();
  160. }
  161. /**
  162. * 函数功能: 擦除整片
  163. * 输入参数: 无
  164. * 返 回 值: 无
  165. * 说 明:擦除串行Flash整片空间
  166. */
  167. void SPI_FLASH_BulkErase(void)
  168. {
  169. /* 发送FLASH写使能命令 */
  170. SPI_FLASH_WriteEnable();
  171. /* 整片擦除 Erase */
  172. /* 选择串行FLASH: CS低电平 */
  173. FLASH_SPI_CS_ENABLE();
  174. /* 发送整片擦除指令*/
  175. SPI_FLASH_SendByte(W25X_ChipErase);
  176. /* 禁用串行FLASH: CS高电平 */
  177. FLASH_SPI_CS_DISABLE();
  178. /* 等待擦除完毕*/
  179. SPI_FLASH_WaitForWriteEnd();
  180. }
  181. /**
  182. * 函数功能: 往串行FLASH按页写入数据,调用本函数写入数据前需要先擦除扇区
  183. * 输入参数: pBuffer:待写入数据的指针
  184. * WriteAddr:写入地址
  185. * NumByteToWrite:写入数据长度,必须小于等于SPI_FLASH_PerWritePageSize
  186. * 返 回 值: 无
  187. * 说 明:串行Flash每页大小为256个字节
  188. */
  189. void SPI_FLASH_PageWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
  190. {
  191. /* 发送FLASH写使能命令 */
  192. SPI_FLASH_WriteEnable();
  193. /* 寻找串行FLASH: CS低电平 */
  194. FLASH_SPI_CS_ENABLE();
  195. /* 写送写指令*/
  196. SPI_FLASH_SendByte(W25X_PageProgram);
  197. /*发送写地址的高位*/
  198. SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16);
  199. /*发送写地址的中位*/
  200. SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8);
  201. /*发送写地址的低位*/
  202. SPI_FLASH_SendByte(WriteAddr & 0xFF);
  203. if(NumByteToWrite > SPI_FLASH_PerWritePageSize)
  204. {
  205. NumByteToWrite = SPI_FLASH_PerWritePageSize;
  206. //rt_kprintf("Err: SPI_FLASH_PageWrite too large!\n");
  207. }
  208. /* 写入数据*/
  209. while (NumByteToWrite--)
  210. {
  211. /* 发送当前要写入的字节数据 */
  212. SPI_FLASH_SendByte(*pBuffer);
  213. /* 指向下一字节数据 */
  214. pBuffer++;
  215. }
  216. /* 禁用串行FLASH: CS 高电平 */
  217. FLASH_SPI_CS_DISABLE();
  218. /* 等待写入完毕*/
  219. SPI_FLASH_WaitForWriteEnd();
  220. }
  221. /**
  222. * 函数功能: 往串行FLASH写入数据,调用本函数写入数据前需要先擦除扇区
  223. * 输入参数: pBuffer:待写入数据的指针
  224. * WriteAddr:写入地址
  225. * NumByteToWrite:写入数据长度
  226. * 返 回 值: 无
  227. * 说 明:该函数可以设置任意写入数据长度
  228. */
  229. void SPI_FLASH_BufferWrite(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
  230. {
  231. uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;
  232. Addr = WriteAddr % SPI_FLASH_PageSize;
  233. count = SPI_FLASH_PageSize - Addr;
  234. NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
  235. NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
  236. if (Addr == 0) /* 若地址与 SPI_FLASH_PageSize 对齐 */
  237. {
  238. if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
  239. {
  240. SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
  241. }
  242. else /* NumByteToWrite > SPI_FLASH_PageSize */
  243. {
  244. while (NumOfPage--)
  245. {
  246. SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
  247. WriteAddr += SPI_FLASH_PageSize;
  248. pBuffer += SPI_FLASH_PageSize;
  249. }
  250. SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  251. }
  252. }
  253. else /* 若地址与 SPI_FLASH_PageSize 不对齐 */
  254. {
  255. if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */
  256. {
  257. if (NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */
  258. {
  259. temp = NumOfSingle - count;
  260. SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
  261. WriteAddr += count;
  262. pBuffer += count;
  263. SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp);
  264. }
  265. else
  266. {
  267. SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite);
  268. }
  269. }
  270. else /* NumByteToWrite > SPI_FLASH_PageSize */
  271. {
  272. NumByteToWrite -= count;
  273. NumOfPage = NumByteToWrite / SPI_FLASH_PageSize;
  274. NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize;
  275. SPI_FLASH_PageWrite(pBuffer, WriteAddr, count);
  276. WriteAddr += count;
  277. pBuffer += count;
  278. while (NumOfPage--)
  279. {
  280. SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize);
  281. WriteAddr += SPI_FLASH_PageSize;
  282. pBuffer += SPI_FLASH_PageSize;
  283. }
  284. if (NumOfSingle != 0)
  285. {
  286. SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle);
  287. }
  288. }
  289. }
  290. }
  291. /**
  292. * 函数功能: 从串行Flash读取数据
  293. * 输入参数: pBuffer:存放读取到数据的指针
  294. * ReadAddr:读取数据目标地址
  295. * NumByteToRead:读取数据长度
  296. * 返 回 值: 无
  297. * 说 明:该函数可以设置任意读取数据长度
  298. */
  299. void SPI_FLASH_BufferRead(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead)
  300. {
  301. /* 选择串行FLASH: CS低电平 */
  302. FLASH_SPI_CS_ENABLE();
  303. /* 发送 读 指令 */
  304. SPI_FLASH_SendByte(W25X_ReadData);
  305. /* 发送 读 地址高位 */
  306. SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  307. /* 发送 读 地址中位 */
  308. SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  309. /* 发送 读 地址低位 */
  310. SPI_FLASH_SendByte(ReadAddr & 0xFF);
  311. while (NumByteToRead--) /* 读取数据 */
  312. {
  313. /* 读取一个字节*/
  314. *pBuffer = SPI_FLASH_SendByte(Dummy_Byte);
  315. /* 指向下一个字节缓冲区 */
  316. pBuffer++;
  317. }
  318. /* 禁用串行FLASH: CS 高电平 */
  319. FLASH_SPI_CS_DISABLE();
  320. }
  321. void SPI1_WriteByte(uint8_t TxData)
  322. {
  323. HAL_SPI_Transmit(&hspiflash, &TxData, 1, 20); //通过外设SPIx发送一个数据
  324. }
  325. uint8_t SPI1_ReadByte(void)
  326. {
  327. uint8_t RxData;
  328. HAL_SPI_Receive(&hspiflash, &RxData, 1, 20);
  329. return RxData; //返回通过SPIx最近接收的数据
  330. }
  331. /**
  332. * 函数功能: 读取串行Flash设备ID
  333. * 输入参数: 无
  334. * 返 回 值: uint32_t:串行Flash的设备ID
  335. * 说 明:
  336. */
  337. uint32_t SPI_FLASH_ReadDeviceID(void)
  338. {
  339. uint32_t Temp = 0;
  340. /* 选择串行FLASH: CS低电平 */
  341. SPI_FLASH_CS_L();
  342. /* 发送命令:读取芯片设备ID * */
  343. SPI1_WriteByte(0xAB);
  344. SPI1_WriteByte(0x00);
  345. SPI1_WriteByte(0x00);
  346. SPI1_WriteByte(0x00);
  347. Temp|=SPI1_ReadByte()<<8;
  348. Temp|=SPI1_ReadByte();
  349. /* 禁用串行Flash:CS高电平 */
  350. SPI_FLASH_CS_H();
  351. return Temp;
  352. }
  353. /**
  354. * 函数功能: 读取串行Flash型号的ID
  355. * 输入参数: 无
  356. * 返 回 值: uint32_t:串行Flash的型号ID
  357. * 说 明: FLASH_ID IC型号 存储空间大小
  358. 0xEF3015 W25X16 2M byte
  359. 0xEF4015 W25Q16 4M byte
  360. 0XEF4017 W25Q64 8M byte
  361. 0XEF4018 W25Q128 16M byte (YS-F1Pro开发板默认配置)
  362. */
  363. uint32_t SPI_Flash_ReadID(void)
  364. {
  365. uint32_t Temp = 0;
  366. SPI_FLASH_CS_L();
  367. SPI1_WriteByte(0x90);//发送读取ID命令
  368. SPI1_WriteByte(0x00);
  369. SPI1_WriteByte(0x00);
  370. SPI1_WriteByte(0x00);
  371. Temp|=SPI1_ReadByte()<<8;
  372. Temp|=SPI1_ReadByte();
  373. SPI_FLASH_CS_H();
  374. return Temp;
  375. }
  376. /**
  377. * 函数功能: 启动连续读取数据串
  378. * 输入参数: ReadAddr:读取地址
  379. * 返 回 值: 无
  380. * 说 明:Initiates a read data byte (READ) sequence from the Flash.
  381. * This is done by driving the /CS line low to select the device,
  382. * then the READ instruction is transmitted followed by 3 bytes
  383. * address. This function exit and keep the /CS line low, so the
  384. * Flash still being selected. With this technique the whole
  385. * content of the Flash is read with a single READ instruction.
  386. */
  387. void SPI_FLASH_StartReadSequence(uint32_t ReadAddr)
  388. {
  389. /* Select the FLASH: Chip Select low */
  390. FLASH_SPI_CS_ENABLE();
  391. /* Send "Read from Memory " instruction */
  392. SPI_FLASH_SendByte(W25X_ReadData);
  393. /* Send the 24-bit address of the address to read from -----------------------*/
  394. /* Send ReadAddr high nibble address byte */
  395. SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16);
  396. /* Send ReadAddr medium nibble address byte */
  397. SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8);
  398. /* Send ReadAddr low nibble address byte */
  399. SPI_FLASH_SendByte(ReadAddr & 0xFF);
  400. }
  401. /**
  402. * 函数功能: 从串行Flash读取一个字节数据
  403. * 输入参数: 无
  404. * 返 回 值: uint8_t:读取到的数据
  405. * 说 明:This function must be used only if the Start_Read_Sequence
  406. * function has been previously called.
  407. */
  408. uint8_t SPI_FLASH_ReadByte(void)
  409. {
  410. uint8_t d_read,d_send=Dummy_Byte;
  411. if(HAL_SPI_TransmitReceive(&hspiflash,&d_send,&d_read,1,0xFFFFFF)!=HAL_OK)
  412. d_read=Dummy_Byte;
  413. return d_read;
  414. }
  415. /**
  416. * 函数功能: 往串行Flash读取写入一个字节数据并接收一个字节数据
  417. * 输入参数: byte:待发送数据
  418. * 返 回 值: uint8_t:接收到的数据
  419. * 说 明:无
  420. */
  421. uint8_t SPI_FLASH_SendByte(uint8_t byte)
  422. {
  423. uint8_t d_read,d_send=byte;
  424. if(HAL_SPI_TransmitReceive(&hspiflash,&d_send,&d_read,1,0xFFFFFF)!=HAL_OK)
  425. d_read=Dummy_Byte;
  426. return d_read;
  427. }
  428. /**
  429. * 函数功能: 使能串行Flash写操作
  430. * 输入参数: 无
  431. * 返 回 值: 无
  432. * 说 明:无
  433. */
  434. void SPI_FLASH_WriteEnable(void)
  435. {
  436. /* 选择串行FLASH: CS低电平 */
  437. FLASH_SPI_CS_ENABLE();
  438. /* 发送命令:写使能 */
  439. SPI_FLASH_SendByte(W25X_WriteEnable);
  440. /* 禁用串行Flash:CS高电平 */
  441. FLASH_SPI_CS_DISABLE();
  442. }
  443. /**
  444. * 函数功能: 等待数据写入完成
  445. * 输入参数: 无
  446. * 返 回 值: 无
  447. * 说 明:Polls the status of the Write In Progress (WIP) flag in the
  448. * FLASH's status register and loop until write opertaion
  449. * has completed.
  450. */
  451. void SPI_FLASH_WaitForWriteEnd(void)
  452. {
  453. uint8_t FLASH_Status = 0;
  454. /* Select the FLASH: Chip Select low */
  455. FLASH_SPI_CS_ENABLE();
  456. /* Send "Read Status Register" instruction */
  457. SPI_FLASH_SendByte(W25X_ReadStatusReg);
  458. /* Loop as long as the memory is busy with a write cycle */
  459. do
  460. {
  461. /* Send a dummy byte to generate the clock needed by the FLASH
  462. and put the value of the status register in FLASH_Status variable */
  463. FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte);
  464. }
  465. while ((FLASH_Status & WIP_Flag) == SET); /* Write in progress */
  466. /* Deselect the FLASH: Chip Select high */
  467. FLASH_SPI_CS_DISABLE();
  468. }
  469. /**
  470. * 函数功能: 进入掉电模式
  471. * 输入参数: 无
  472. * 返 回 值: 无
  473. * 说 明:无
  474. */
  475. void SPI_Flash_PowerDown(void)
  476. {
  477. /* Select the FLASH: Chip Select low */
  478. FLASH_SPI_CS_ENABLE();
  479. /* Send "Power Down" instruction */
  480. SPI_FLASH_SendByte(W25X_PowerDown);
  481. /* Deselect the FLASH: Chip Select high */
  482. FLASH_SPI_CS_DISABLE();
  483. }
  484. /**
  485. * 函数功能: 唤醒串行Flash
  486. * 输入参数: 无
  487. * 返 回 值: 无
  488. * 说 明:无
  489. */
  490. void SPI_Flash_WAKEUP(void)
  491. {
  492. /* Select the FLASH: Chip Select low */
  493. FLASH_SPI_CS_ENABLE();
  494. /* Send "Power Down" instruction */
  495. SPI_FLASH_SendByte(W25X_ReleasePowerDown);
  496. /* Deselect the FLASH: Chip Select high */
  497. FLASH_SPI_CS_DISABLE();
  498. }
  499. int test_flash(void)
  500. {
  501. //MX_SPIFlash_Init();
  502. /* Get SPI Flash Device ID */
  503. DeviceID = SPI_Flash_ReadID();
  504. rt_thread_mdelay(100);
  505. /* Get SPI Flash ID */
  506. FlashID = SPI_Flash_ReadID();
  507. rt_kprintf("FlashID is 0x%X, Manufacturer Device ID is 0x%X\n", FlashID, DeviceID);
  508. /* Check the SPI Flash ID */
  509. if (FlashID == SPI_FLASH_ID || 1) /* #define sFLASH_ID 0XEF4018 */
  510. {
  511. rt_kprintf("check winband flash W25Q32 !\n");
  512. /* 擦除SPI的扇区以写入 */
  513. SPI_FLASH_SectorErase(FLASH_SectorToErase);
  514. /* 将发送缓冲区的数据写到flash中 */
  515. SPI_FLASH_BufferWrite(Tx_Buffer, FLASH_WriteAddress, BufferSize);
  516. SPI_FLASH_BufferWrite(Tx_Buffer, 252, BufferSize);
  517. rt_kprintf("write data:\n%s \n", Tx_Buffer);
  518. /* 将刚刚写入的数据读出来放到接收缓冲区中 */
  519. SPI_FLASH_BufferRead(Rx_Buffer, FLASH_ReadAddress, BufferSize);
  520. rt_kprintf("read data:\n %s\n", Rx_Buffer);
  521. /* 检查写入的数据与读出的数据是否相等 */
  522. TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize);
  523. if( PASSED == TransferStatus1 )
  524. {
  525. rt_kprintf("serial flash test sucess\r\n");
  526. }
  527. else
  528. {
  529. rt_kprintf("serial flash test fail\r\n");
  530. }
  531. }
  532. else
  533. {
  534. rt_kprintf(" W25Q128 ID!\n");
  535. }
  536. return 0;
  537. }
  538. MSH_CMD_EXPORT(test_flash, test_flash);
  539. /**
  540. * 函数功能: 比较两个缓冲区中的数据是否相等
  541. * 输入参数: pBuffer1:要比较的缓冲区1的指针
  542. * pBuffer2:要比较的缓冲区2的指针
  543. * BufferLength:缓冲区长度
  544. * 返 回 值: PASSED:相等
  545. * FAILED:不等
  546. * 说 明: 无
  547. */
  548. static TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
  549. {
  550. while(BufferLength--)
  551. {
  552. if(*pBuffer1 != *pBuffer2)
  553. {
  554. return FAILED;
  555. }
  556. pBuffer1++;
  557. pBuffer2++;
  558. }
  559. return PASSED;
  560. }
  561. //INIT_APP_EXPORT(MX_SPIFlash_Init);
  562. /******************* (C) COPYRIGHT 2015-2020 硬石嵌入式开发团队 *****END OF FILE****/