func_spi_w25qxx.c 6.4 KB

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