func_spi_w25qxx.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*********************************************************
  2. //file :hd_dev_gpio.c
  3. //author :libo
  4. //date :2020/05/10
  5. //version :V1.0
  6. //brief :GSP HARD层GPIO接口C文件
  7. *********************************************************/
  8. /* Includes-----------------------------------------------------------------------------------*/
  9. //#define FUNC_SPI_W25QXX_USE
  10. #include <string.h>
  11. #include <stdbool.h>
  12. #include "cmsis_os.h"
  13. #include "func_spi_w25qxx.h"
  14. #include "obj_hal_w25qxx.h"
  15. #include "obj_soft_w25qxx.h"
  16. #define SPI_FLASH_SECT_BOOT 0
  17. #ifdef FUNC_SPI_W25QXX_USE
  18. /* Includes-----------------------------------------------------------------------------------*/
  19. #include <string.h>
  20. #include "dev_spi_sdcard.h"
  21. #include "obj_spi_w25qxx.h"
  22. /* Private macro------------------------------------------------------------------------------*/
  23. #define domain_add 0x000000
  24. #define phone_add 0x001000
  25. /* Private typedef----------------------------------------------------------------------------*/
  26. /* Private define-----------------------------------------------------------------------------*/
  27. /* Private variables--------------------------------------------------------------------------*/
  28. /* Private function prototypes----------------------------------------------------------------*/
  29. uint8_t func_w25q_disk_ioctl (
  30. BYTE pdrv, /* Physical drive nmuber (0..) */
  31. BYTE cmd, /* Control code */
  32. void *buff /* Buffer to send/receive control data */
  33. )
  34. {
  35. uint8_t res = RES_PARERR;
  36. switch(cmd)
  37. {
  38. case CTRL_SYNC:
  39. res = RES_OK;
  40. break;
  41. case GET_SECTOR_SIZE:
  42. *(WORD*)buff = w25qxx.SectorSize;
  43. res = RES_OK;
  44. break;
  45. case GET_BLOCK_SIZE:
  46. *(WORD*)buff = w25qxx.BlockSize;
  47. res = RES_OK;
  48. break;
  49. case GET_SECTOR_COUNT:
  50. *(DWORD*)buff = w25qxx.SectorCount;
  51. res = RES_OK;
  52. break;
  53. default:
  54. res = RES_PARERR;
  55. break;
  56. }
  57. return res;
  58. }
  59. void func_w25q_init(void)
  60. {
  61. W25qxx_Cs_Init();
  62. W25qxx_Spi_Init();
  63. W25qxx_Init();
  64. while(w25qxx.ID != ID_W25Q128) //检测不到ID_W25Q128
  65. {
  66. W25qxx_Init();
  67. HAL_Delay(100);
  68. }
  69. return;
  70. }
  71. void func_w25q_spi_flash_bootcnt(void)
  72. {
  73. uint8_t key;
  74. uint32_t boot_cnt = 0;
  75. uint8_t write_buff[] = "6686666666";
  76. uint8_t read_buff[20] = {0};
  77. UNUSED(key);
  78. UNUSED(write_buff);
  79. UNUSED(read_buff);
  80. func_w25q_init();
  81. while(w25qxx.ID != ID_W25Q128) //检测不到W25Q256
  82. {
  83. W25qxx_Init();
  84. HAL_Delay(100);
  85. }
  86. while(1)
  87. {
  88. W25qxx_ReadSector((uint8_t *)&boot_cnt,phone_add,0,sizeof(boot_cnt));
  89. boot_cnt++;
  90. W25qxx_EraseSector(phone_add);
  91. W25qxx_WriteSector((uint8_t *)&boot_cnt,phone_add,0,sizeof(boot_cnt));
  92. // if(key)
  93. // {
  94. // HAL_Delay(100);
  95. // W25qxx_WritePage(phone_buff,phone_add,0,strlen((char*)phone_buff));
  96. // }
  97. // else
  98. // {
  99. // HAL_Delay(100);
  100. // W25qxx_ReadPage(read_buff,phone_add,0,20);
  101. // }
  102. osDelay(1000);
  103. }
  104. }
  105. void func_w25q_spi_flash_buffer(void)
  106. {
  107. uint8_t key;
  108. uint8_t phone_buff[] = "6686666666";
  109. uint8_t read_buff[20] = {0};
  110. func_w25q_init();
  111. while(w25qxx.ID != ID_W25Q128) //检测不到W25Q256
  112. {
  113. W25qxx_Init();
  114. HAL_Delay(100);
  115. }
  116. while(1)
  117. {
  118. if(key)
  119. {
  120. HAL_Delay(100);
  121. W25qxx_WritePage(phone_buff,phone_add,0,strlen((char*)phone_buff));
  122. }
  123. else
  124. {
  125. HAL_Delay(100);
  126. W25qxx_ReadPage(read_buff,phone_add,0,20);
  127. }
  128. osDelay(1000);
  129. }
  130. }
  131. #endif /*************FUNC_SPI_W25QXX_USE*******************/
  132. #if defined(USE_OBJ_HAL_W25QXX) || defined(USE_OBJ_SOFT_W25QXX)
  133. //要写入到W25Q16的字符串数组
  134. const uint8_t TEXT_Buffer[]={"STM32 SPI TEST"};
  135. #define SIZE sizeof(TEXT_Buffer)
  136. void func_w25q_id_check(void)
  137. {
  138. uint32_t W25Q_ReadID = 0xffffffff;
  139. while((W25Q_ReadID <= W25Q512)
  140. &&(W25Q_ReadID >= W25Q32)) //检测不到W25Q128
  141. {
  142. W25Q_ReadID = W25QXX_ReadID();
  143. HAL_Delay(100);
  144. }
  145. }
  146. void func_w25q_init(void)
  147. {
  148. W25QXX_Init(); //W25QXX初始化
  149. W25QXX_WAKEUP();
  150. HAL_Delay(100);
  151. uint8_t erase_flag = 0;
  152. if(erase_flag)
  153. {
  154. W25QXX_Erase_Chip();
  155. }
  156. return;
  157. }
  158. #if 0
  159. uint8_t func_w25q_disk_ioctl (
  160. BYTE pdrv, /* Physical drive nmuber (0..) */
  161. BYTE cmd, /* Control code */
  162. void *buff /* Buffer to send/receive control data */
  163. )
  164. {
  165. uint8_t res = RES_PARERR;
  166. switch(cmd)
  167. {
  168. case CTRL_SYNC:
  169. res = RES_OK;
  170. break;
  171. case GET_SECTOR_SIZE:
  172. *(WORD*)buff = SPI_FLASH_SECTOR_SIZE;
  173. res = RES_OK;
  174. break;
  175. case GET_BLOCK_SIZE:
  176. *(WORD*)buff = SPI_FLASH_BLOCK_SIZE;
  177. res = RES_OK;
  178. break;
  179. case GET_SECTOR_COUNT:
  180. *(DWORD*)buff = SPI_FLASH_SECTOR_COUNT;
  181. res = RES_OK;
  182. break;
  183. default:
  184. res = RES_PARERR;
  185. break;
  186. }
  187. return res;
  188. }
  189. #endif
  190. uint32_t boot_cnt[2] = {0};
  191. uint8_t func_w25q_bootcnt(void)
  192. {
  193. uint32_t falsh_addr = 0;
  194. uint8_t ret = false;
  195. falsh_addr = SPI_FLASH_SECT_BOOT * w25qxx_obj.SectorSize;
  196. W25QXX_Read((uint8_t*)&boot_cnt[0], falsh_addr, sizeof(uint32_t));
  197. boot_cnt[0]++;
  198. W25QXX_Write((uint8_t*)&boot_cnt[0], falsh_addr, sizeof(uint32_t));
  199. W25QXX_Read((uint8_t*)&boot_cnt[1], falsh_addr, sizeof(uint32_t));
  200. if(boot_cnt[0] == boot_cnt[1])
  201. {
  202. ret = true;
  203. }
  204. return ret;
  205. }
  206. uint32_t FLASH_ReadID = 0xffffffff;
  207. int func_w25q_hal_main(void)
  208. {
  209. //uint8_t key;
  210. uint8_t idx=0;
  211. uint32_t FLASH_SIZE;
  212. uint8_t TXT_WR[64] = {0};
  213. uint8_t TXT_RD[64] = {0};
  214. W25QXX_Init(); //W25QXX初始化
  215. HAL_Delay(100);
  216. FLASH_SIZE=1*1024*1024; //FLASH 大小为32M字节
  217. W25QXX_Read(&idx,FLASH_SIZE-100,sizeof(idx));
  218. while(1)
  219. {
  220. FLASH_ReadID = W25QXX_ReadID();
  221. idx++;
  222. memset((void *)TXT_WR,idx,sizeof(TXT_WR));
  223. W25QXX_Write((uint8_t*)TXT_WR,FLASH_SIZE-100,sizeof(TXT_WR)); //从倒数第100个地址处开始,写入SIZE长度的数据
  224. osDelay(100);
  225. memset((void *)TXT_RD,0x00,sizeof(TXT_RD));
  226. W25QXX_Read(TXT_RD,FLASH_SIZE-100,sizeof(TXT_RD)); //从倒数第100个地址处开始,读出SIZE个字节
  227. if(memcmp(TXT_RD,TXT_WR,sizeof(TXT_RD)) != 0 )
  228. {
  229. osDelay(2000);
  230. memset((void *)TXT_RD,0x00,sizeof(TXT_RD));
  231. W25QXX_Read(TXT_RD,FLASH_SIZE-100,sizeof(TXT_RD)); //从倒数第100个地址处开始,读出SIZE个字节
  232. }
  233. osDelay(1000);
  234. }
  235. }
  236. #endif
  237. uint16_t func_w25q_stat(void)
  238. {
  239. return W25QXX_ID_OK();
  240. }
  241. void func_w25q_erase_ship(void)
  242. {
  243. W25QXX_Erase_Chip();
  244. }
  245. void func_w25q_test(void)
  246. {
  247. uint8_t erase_ship = 0;
  248. if(erase_ship)
  249. {
  250. func_w25q_erase_ship();
  251. }
  252. #if 0
  253. while(1)
  254. {
  255. //add boly 20221107 W25QXX件初始化
  256. //func_w25q_spi_flash_bootcnt();
  257. //func_w25q_spi_flash_buffer();
  258. func_w25q_hal_main();
  259. osDelay(1000);
  260. //end boly
  261. }
  262. #endif
  263. }