w25qxx.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #include "includes.h"
  2. #include "w25qxx.h"
  3. //w25Q128
  4. static __IO uint32_t SPITimeout = SPIT_LONG_TIMEOUT;
  5. /*
  6. 容量 16页/扇区 128页/半块(32kB) 256页/块(64kB)
  7. w25q16 16M-bit 2MByte 8192编程页/256B 512扇区 64半块 32块
  8. */
  9. static uint32_t SPI_TIMEOUT_UserCallback(uint8_t errorCode);
  10. /**
  11. * @brief SPII/O配置
  12. * @param 无
  13. * @retval 无
  14. */
  15. static void SPI_GPIO_Config(void)
  16. {
  17. GPIO_InitTypeDef GPIO_InitStructure;
  18. /* 使能与SPI 有关的时钟 */
  19. FLASH_SPI_APBxClock_FUN ( FLASH_SPI_CLK, ENABLE );
  20. FLASH_SPI_GPIO_APBxClock_FUN ( FLASH_SPI_GPIO_CLK, ENABLE );
  21. /* MISO MOSI SCK*/
  22. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
  23. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  25. GPIO_Init(FLASH_SPI_SCK_PORT, &GPIO_InitStructure);
  26. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
  27. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  28. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  29. GPIO_Init(FLASH_SPI_MOSI_PORT, &GPIO_InitStructure);
  30. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
  31. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  32. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  33. GPIO_Init(FLASH_SPI_MISO_PORT, &GPIO_InitStructure);
  34. //初始化CS引脚,使用软件控制,所以直接设置成推挽输出
  35. GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
  36. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  37. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  38. GPIO_Init(FLASH_SPI_CS_PORT, &GPIO_InitStructure);
  39. FLASH_SPI_CS_HIGH;
  40. }
  41. /**
  42. * @brief SPI 工作模式配置
  43. * @param 无
  44. * @retval 无
  45. */
  46. static void SPI_Mode_Config(void)
  47. {
  48. SPI_InitTypeDef SPI_InitStructure;
  49. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2 ;
  50. //SPI 使用模式3
  51. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge ;
  52. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High ;
  53. SPI_InitStructure.SPI_CRCPolynomial = 0;//不使用CRC功能,数值随便写
  54. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  55. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex ;//双线全双工
  56. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB ;
  57. SPI_InitStructure.SPI_Mode = SPI_Mode_Master ;
  58. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft ;
  59. SPI_Init(FLASH_SPIx,&SPI_InitStructure); //写入配置到寄存器
  60. SPI_Cmd(FLASH_SPIx,ENABLE);//使能SPI
  61. }
  62. /**
  63. * @brief SPI 初始化
  64. * @param 无
  65. * @retval 无
  66. */
  67. void SPI_USER_Init(void)
  68. {
  69. SPI_GPIO_Config();
  70. SPI_Mode_Config();
  71. }
  72. //发送并接收一个字节
  73. uint8_t SPI_USER_Send_Byte(uint8_t data)
  74. {
  75. SPITimeout = SPIT_FLAG_TIMEOUT;
  76. //检查并等待至TX缓冲区为空
  77. while(SPI_I2S_GetFlagStatus(FLASH_SPIx,SPI_I2S_FLAG_TXE) == RESET)
  78. {
  79. if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0);
  80. }
  81. //程序执行到此处,TX缓冲区已空
  82. SPI_I2S_SendData (FLASH_SPIx,data);
  83. SPITimeout = SPIT_FLAG_TIMEOUT;
  84. //检查并等待至RX缓冲区为非空
  85. while(SPI_I2S_GetFlagStatus(FLASH_SPIx,SPI_I2S_FLAG_RXNE) == RESET)
  86. {
  87. if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0);
  88. }
  89. //程序执行到此处,说明数据发送完毕,并接收到一字字节
  90. return SPI_I2S_ReceiveData(FLASH_SPIx);
  91. }
  92. uint8_t SPI_USER_Read_Byte(void)
  93. {
  94. return SPI_USER_Send_Byte(DUMMY);
  95. }
  96. //读取ID号
  97. uint32_t SPI_FLASH_Read_ID(void)
  98. {
  99. uint32_t flash_id;
  100. //片选使能
  101. FLASH_SPI_CS_LOW;
  102. SPI_USER_Send_Byte(READ_JEDEC_ID);
  103. flash_id = SPI_USER_Send_Byte(DUMMY);
  104. flash_id <<= 8;
  105. flash_id |= SPI_USER_Send_Byte(DUMMY);
  106. flash_id <<= 8;
  107. flash_id |= SPI_USER_Send_Byte(DUMMY);
  108. FLASH_SPI_CS_HIGH;
  109. return flash_id;
  110. }
  111. //FLASH写入使能
  112. void SPI_FLASH_Write_Enable(void)
  113. {
  114. //片选使能
  115. FLASH_SPI_CS_LOW;
  116. SPI_USER_Send_Byte(WRITE_ENABLE);
  117. FLASH_SPI_CS_HIGH;
  118. }
  119. //擦除FLASH指定扇区
  120. void SPI_FLASH_Erase_Sector(uint32_t addr)
  121. {
  122. SPI_FLASH_Write_Enable();
  123. //片选使能
  124. FLASH_SPI_CS_LOW;
  125. SPI_USER_Send_Byte(ERASE_SECTOR);
  126. SPI_USER_Send_Byte((addr>>16)&0xff);
  127. SPI_USER_Send_Byte((addr>>8)&0xff);
  128. SPI_USER_Send_Byte(addr&0xff);
  129. FLASH_SPI_CS_HIGH;
  130. SPI_FLASH_WaitForWriteEnd();
  131. }
  132. //读取FLASH的内容
  133. void SPI_FLASH_Read_Data(uint32_t addr,uint8_t *readBuff,uint32_t numByteToRead)
  134. {
  135. //片选使能
  136. FLASH_SPI_CS_LOW;
  137. SPI_USER_Send_Byte(READ_DATA);
  138. SPI_USER_Send_Byte((addr>>16)&0xff);
  139. SPI_USER_Send_Byte((addr>>8)&0xff);
  140. SPI_USER_Send_Byte(addr&0xff);
  141. while(numByteToRead--)
  142. {
  143. *readBuff = SPI_USER_Send_Byte(DUMMY);
  144. readBuff++;
  145. }
  146. FLASH_SPI_CS_HIGH;
  147. }
  148. //向FLASH写入内容
  149. void SPI_FLASH_Write_Data(uint32_t addr,uint8_t *writeBuff,uint32_t numByteToWrite)
  150. {
  151. SPI_FLASH_Write_Enable();
  152. //片选使能
  153. FLASH_SPI_CS_LOW;
  154. SPI_USER_Send_Byte(WRITE_DATA);
  155. SPI_USER_Send_Byte((addr>>16)&0xff);
  156. SPI_USER_Send_Byte((addr>>8)&0xff);
  157. SPI_USER_Send_Byte(addr&0xff);
  158. while(numByteToWrite--)
  159. {
  160. SPI_USER_Send_Byte(*writeBuff);
  161. writeBuff++;
  162. }
  163. FLASH_SPI_CS_HIGH;
  164. SPI_FLASH_WaitForWriteEnd();
  165. }
  166. //连续写入多字节:不用考虑是否达到边界的问题,写入字节数也没有限制(当然要小于FLASH容量)
  167. void SPI_FLASH_Write_Datas(uint32_t addr,uint8_t *writeBuff,uint32_t numByteToWrite)
  168. {
  169. uint32_t page_offset = 0; //距离下一个页地址边界的偏移(距离)
  170. uint32_t write_len = 0; //每次要写入的字节数量
  171. int page_size = 256; //页大小
  172. SPI_FLASH_Write_Enable();
  173. //片选使能
  174. FLASH_SPI_CS_LOW;
  175. while(numByteToWrite > 0)
  176. {
  177. page_offset = page_size - (numByteToWrite % page_size); //计算页偏移
  178. write_len = numByteToWrite>page_offset ? page_offset : numByteToWrite; //选择较小的作为本次写入的数据字节长度
  179. SPI_FLASH_Write_Data( addr,writeBuff,write_len);
  180. //if(numByteToWrite)
  181. //改变相应参数
  182. numByteToWrite = numByteToWrite - write_len; //减少写入数量
  183. writeBuff = writeBuff + write_len; //增加写入缓冲地址
  184. addr = addr+write_len; //增加FLASH写入的地址
  185. }
  186. FLASH_SPI_CS_HIGH;
  187. }
  188. //等待FLASH内部时序操作完成
  189. void SPI_FLASH_WaitForWriteEnd(void)
  190. {
  191. uint8_t status_reg = 0;
  192. //片选使能
  193. FLASH_SPI_CS_LOW;
  194. SPI_USER_Send_Byte(READ_STATUS);
  195. do
  196. {
  197. status_reg = SPI_USER_Send_Byte(DUMMY);
  198. }
  199. while((status_reg & 0x01) == 1);
  200. FLASH_SPI_CS_HIGH;
  201. }
  202. /**
  203. * @brief Basic management of the timeout situation.
  204. * @param errorCode:错误代码,可以用来定位是哪个环节出错.
  205. * @retval 返回0,表示SPI读取失败.
  206. */
  207. static uint32_t SPI_TIMEOUT_UserCallback(uint8_t errorCode)
  208. {
  209. /* Block communication and all processes */
  210. FLASH_ERROR("SPI 等待超时!errorCode = %d",errorCode);
  211. return 0;
  212. }
  213. static u8 Spi_Buf_Write[256], Spi_Buf_Read[256];
  214. uint8_t SPI_FLASH_Test(void)
  215. {
  216. u16 i;
  217. u32 id;
  218. SPI_USER_Init();
  219. id = SPI_FLASH_Read_ID();
  220. memset(Spi_Buf_Write, 0, 256);
  221. for ( i=0; i<=255; i++ ) {//填充缓冲
  222. Spi_Buf_Write[i] = i+50;
  223. }
  224. data_dump("Flash Write", Spi_Buf_Write, sizeof(Spi_Buf_Write));
  225. SPI_FLASH_Erase_Sector(0);
  226. //字节写入方式
  227. SPI_FLASH_Write_Data(0, Spi_Buf_Write, 128);
  228. //将 EEPROM 读出数据顺序保持到 I2c_Buf_Read 中
  229. memset(Spi_Buf_Read, 0, 256);
  230. SPI_FLASH_Read_Data(0, Spi_Buf_Read, 128);
  231. //将 I2c_Buf_Read 中的数据通过串口打印
  232. data_dump("Flash Read", Spi_Buf_Read, sizeof(Spi_Buf_Read));
  233. printf("\r\nSPI(W25qxx, id = %d)rw success\r\n", id);
  234. return 1;
  235. }