Int_flash.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. #include "Int_flash.h"
  2. #include "quadspi.h"
  3. #include <string.h>
  4. extern QSPI_HandleTypeDef hqspi;
  5. /* SPI Flash 地址位数:2 = 24位地址,3 = 32位地址(探测 ID 后更新) */
  6. static volatile uint8_t g_int_flash_addrw = 2;
  7. /* 4KB 扇区读-改-写缓冲区 */
  8. static uint8_t g_int_flash_buf[4096];
  9. /*
  10. * 发送一条 QSPI 命令
  11. * mode 位域定义:
  12. * [1:0] 指令模式 (0=无, 1=单线, 2=双线, 3=四线)
  13. * [3:2] 地址模式 (0=无, 1=单线, 2=双线, 3=四线)
  14. * [5:4] 地址宽度 (0=8位, 1=16位, 2=24位, 3=32位)
  15. * [7:6] 数据模式 (0=无, 1=单线, 2=双线, 3=四线)
  16. */
  17. static void Int_flash_send_cmd(uint8_t cmd, uint32_t addr, uint8_t mode, uint8_t dummy)
  18. {
  19. QSPI_CommandTypeDef qc = {0};
  20. qc.Instruction = cmd;
  21. qc.Address = addr;
  22. qc.DummyCycles = dummy;
  23. switch ((mode >> 0) & 0x03)
  24. {
  25. case 1: qc.InstructionMode = QSPI_INSTRUCTION_1_LINE; break;
  26. case 2: qc.InstructionMode = QSPI_INSTRUCTION_2_LINES; break;
  27. case 3: qc.InstructionMode = QSPI_INSTRUCTION_4_LINES; break;
  28. default: qc.InstructionMode = QSPI_INSTRUCTION_NONE; break;
  29. }
  30. switch ((mode >> 2) & 0x03)
  31. {
  32. case 1: qc.AddressMode = QSPI_ADDRESS_1_LINE; break;
  33. case 2: qc.AddressMode = QSPI_ADDRESS_2_LINES; break;
  34. case 3: qc.AddressMode = QSPI_ADDRESS_4_LINES; break;
  35. default: qc.AddressMode = QSPI_ADDRESS_NONE; break;
  36. }
  37. switch ((mode >> 4) & 0x03)
  38. {
  39. case 0: qc.AddressSize = QSPI_ADDRESS_8_BITS; break;
  40. case 1: qc.AddressSize = QSPI_ADDRESS_16_BITS; break;
  41. case 2: qc.AddressSize = QSPI_ADDRESS_24_BITS; break;
  42. default: qc.AddressSize = QSPI_ADDRESS_32_BITS; break;
  43. }
  44. switch ((mode >> 6) & 0x03)
  45. {
  46. case 1: qc.DataMode = QSPI_DATA_1_LINE; break;
  47. case 2: qc.DataMode = QSPI_DATA_2_LINES; break;
  48. case 3: qc.DataMode = QSPI_DATA_4_LINES; break;
  49. default: qc.DataMode = QSPI_DATA_NONE; break;
  50. }
  51. qc.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
  52. qc.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
  53. qc.DdrMode = QSPI_DDR_MODE_DISABLE;
  54. qc.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
  55. HAL_QSPI_Command(&hqspi, &qc, 5000);
  56. }
  57. /* QSPI 接收 datalen 个字节存入 buf */
  58. static uint8_t Int_flash_receive(uint8_t *buf, uint32_t datalen)
  59. {
  60. hqspi.Instance->DLR = datalen - 1;
  61. if (HAL_QSPI_Receive(&hqspi, buf, 5000) == HAL_OK)
  62. {
  63. return 0;
  64. }
  65. return 1;
  66. }
  67. /* QSPI 从 buf 发送 datalen 个字节 */
  68. static uint8_t Int_flash_transmit(uint8_t *buf, uint32_t datalen)
  69. {
  70. hqspi.Instance->DLR = datalen - 1;
  71. if (HAL_QSPI_Transmit(&hqspi, buf, 5000) == HAL_OK)
  72. {
  73. return 0;
  74. }
  75. return 1;
  76. }
  77. /* 写使能(置位 SR1 的 WEL 位) */
  78. static void Int_flash_write_enable(void)
  79. {
  80. Int_flash_send_cmd(INT_FLASH_CMD_WRITE_ENABLE, 0, 0x01, 0);
  81. }
  82. /* 写禁止(清除 SR1 的 WEL 位) */
  83. //static void Int_flash_write_disable(void)
  84. //{
  85. // Int_flash_send_cmd(INT_FLASH_CMD_WRITE_DISABLE, 0, 0x01, 0);
  86. //}
  87. /* 读取状态寄存器(1~3),数据单线模式 */
  88. static uint8_t Int_flash_read_sr(uint8_t regno)
  89. {
  90. uint8_t byte = 0;
  91. uint8_t command = INT_FLASH_CMD_READ_STATUS_1;
  92. switch (regno)
  93. {
  94. case 2: command = INT_FLASH_CMD_READ_STATUS_2; break;
  95. case 3: command = INT_FLASH_CMD_READ_STATUS_3; break;
  96. default: command = INT_FLASH_CMD_READ_STATUS_1; break;
  97. }
  98. Int_flash_send_cmd(command, 0, 0x41, 0); /* 单线数据,无地址,单线指令 */
  99. Int_flash_receive(&byte, 1);
  100. return byte;
  101. }
  102. /* 写状态寄存器(1~3),数据单线模式 */
  103. static void Int_flash_write_sr(uint8_t regno, uint8_t sr)
  104. {
  105. uint8_t command = INT_FLASH_CMD_WRITE_STATUS_1;
  106. switch (regno)
  107. {
  108. case 2: command = INT_FLASH_CMD_WRITE_STATUS_2; break;
  109. case 3: command = INT_FLASH_CMD_WRITE_STATUS_3; break;
  110. default: command = INT_FLASH_CMD_WRITE_STATUS_1; break;
  111. }
  112. Int_flash_send_cmd(command, 0, 0x41, 0); /* 单线数据,无地址,单线指令 */
  113. Int_flash_transmit(&sr, 1);
  114. }
  115. /* 使能 QE 位(SR2 bit1),以使用 IO2/IO3 四线模式 */
  116. static void Int_flash_qe_enable(void)
  117. {
  118. uint8_t stareg2 = Int_flash_read_sr(2);
  119. if ((stareg2 & 0x02) == 0)
  120. {
  121. Int_flash_write_enable();
  122. stareg2 |= (1 << 1);
  123. Int_flash_write_sr(2, stareg2);
  124. }
  125. }
  126. /* 编程一个页(最多 256 字节),四线 I/O,不跨页边界 */
  127. static void Int_flash_write_page(uint8_t *pbuf, uint32_t addr, uint16_t datalen)
  128. {
  129. Int_flash_write_enable();
  130. /* 四线数据 | 24/32位单线地址 | 单线指令 */
  131. Int_flash_send_cmd(INT_FLASH_CMD_PAGE_PROGRAM_QUAD, addr,
  132. (3 << 6) | (g_int_flash_addrw << 4) | (1 << 2) | (1 << 0), 0);
  133. Int_flash_transmit(pbuf, datalen);
  134. Int_flash_wait_busy();
  135. }
  136. void Int_flash_wait_busy(void)
  137. {
  138. while ((Int_flash_read_sr(1) & 0x01) == 0x01);
  139. }
  140. uint8_t Int_flash_init(void)
  141. {
  142. /* QSPI 外设已由 MX_QUADSPI_Init() 提前初始化 */
  143. Int_flash_qe_enable();
  144. g_int_flash_addrw = 2;
  145. if (Int_flash_read_id() == INT_FLASH_ID_GD25Q128)
  146. {
  147. return 0;
  148. }
  149. return 1;
  150. }
  151. uint16_t Int_flash_read_id(void)
  152. {
  153. uint8_t temp[2];
  154. uint16_t deviceid;
  155. /* 单线数据 2B | 24位单线地址 | 单线指令 */
  156. Int_flash_send_cmd(INT_FLASH_CMD_MANUF_DEVICE_ID, 0,
  157. (1 << 6) | (2 << 4) | (1 << 2) | (1 << 0), 0);
  158. Int_flash_receive(temp, 2);
  159. deviceid = (uint16_t)((temp[0] << 8) | temp[1]);
  160. return deviceid;
  161. }
  162. uint8_t Int_flash_read(uint8_t *pbuf, uint32_t addr, uint32_t datalen)
  163. {
  164. /* 四线数据 | 24/32位四线地址 | 单线指令,6 个空转周期 */
  165. Int_flash_send_cmd(INT_FLASH_CMD_FAST_READ_QUAD, addr,
  166. (3 << 6) | (g_int_flash_addrw << 4) | (3 << 2) | (1 << 0), 6);
  167. return Int_flash_receive(pbuf, datalen);
  168. }
  169. uint8_t Int_flash_erase_sector(uint32_t saddr)
  170. {
  171. uint32_t byteaddr = saddr * INT_FLASH_SECTOR_SIZE;
  172. Int_flash_write_enable();
  173. Int_flash_wait_busy();
  174. /* 无数据 | 24/32位单线地址 | 单线指令 */
  175. Int_flash_send_cmd(INT_FLASH_CMD_SECTOR_ERASE, byteaddr,
  176. (0 << 6) | (g_int_flash_addrw << 4) | (1 << 2) | (1 << 0), 0);
  177. Int_flash_wait_busy();
  178. return 0;
  179. }
  180. uint8_t Int_flash_erase_block(uint32_t baddr)
  181. {
  182. uint32_t byteaddr = baddr * INT_FLASH_BLOCK_SIZE;
  183. Int_flash_write_enable();
  184. Int_flash_wait_busy();
  185. /* 无数据 | 24/32位单线地址 | 单线指令 */
  186. Int_flash_send_cmd(INT_FLASH_CMD_BLOCK_ERASE, byteaddr,
  187. (0 << 6) | (g_int_flash_addrw << 4) | (1 << 2) | (1 << 0), 0);
  188. Int_flash_wait_busy();
  189. return 0;
  190. }
  191. uint8_t Int_flash_erase_chip(void)
  192. {
  193. Int_flash_write_enable();
  194. Int_flash_wait_busy();
  195. /* 无数据 | 无地址 | 单线指令 */
  196. Int_flash_send_cmd(INT_FLASH_CMD_CHIP_ERASE, 0, 0x01, 0);
  197. Int_flash_wait_busy();
  198. return 0;
  199. }
  200. /* 按页边界逐段写入(不跨页),确保每个 PAGE_PROGRAM 都在单个 256 字节页内 */
  201. static void Int_flash_write_nocheck(uint8_t *pbuf, uint32_t addr, uint32_t datalen)
  202. {
  203. uint16_t pagerem;
  204. pagerem = INT_FLASH_PAGE_SIZE - (addr % INT_FLASH_PAGE_SIZE);
  205. if (datalen <= pagerem)
  206. {
  207. pagerem = datalen;
  208. }
  209. while (1)
  210. {
  211. Int_flash_write_page(pbuf, addr, pagerem);
  212. if (datalen == pagerem)
  213. {
  214. break;
  215. }
  216. pbuf += pagerem;
  217. addr += pagerem;
  218. datalen -= pagerem;
  219. pagerem = (datalen > INT_FLASH_PAGE_SIZE) ? INT_FLASH_PAGE_SIZE : datalen;
  220. }
  221. }
  222. uint8_t Int_flash_write(uint8_t *pbuf, uint32_t addr, uint32_t datalen)
  223. {
  224. uint32_t secpos;
  225. uint16_t secoff;
  226. uint16_t secremain;
  227. uint16_t i;
  228. uint8_t *flash_buf = g_int_flash_buf;
  229. secpos = addr / INT_FLASH_SECTOR_SIZE;
  230. secoff = addr % INT_FLASH_SECTOR_SIZE;
  231. secremain = INT_FLASH_SECTOR_SIZE - secoff;
  232. if (datalen <= secremain)
  233. {
  234. secremain = datalen;
  235. }
  236. while (1)
  237. {
  238. Int_flash_read(flash_buf, secpos * INT_FLASH_SECTOR_SIZE, INT_FLASH_SECTOR_SIZE);
  239. for (i = 0; i < secremain; i++)
  240. {
  241. if (flash_buf[secoff + i] != 0xFF)
  242. {
  243. break;
  244. }
  245. }
  246. if (i < secremain)
  247. {
  248. Int_flash_erase_sector(secpos);
  249. for (i = 0; i < secremain; i++)
  250. {
  251. flash_buf[secoff + i] = pbuf[i];
  252. }
  253. Int_flash_write_nocheck(flash_buf, secpos * INT_FLASH_SECTOR_SIZE,
  254. INT_FLASH_SECTOR_SIZE);
  255. }
  256. else
  257. {
  258. Int_flash_write_nocheck(pbuf, addr, secremain);
  259. }
  260. if (datalen == secremain)
  261. {
  262. break;
  263. }
  264. secpos++;
  265. secoff = 0;
  266. pbuf += secremain;
  267. addr += secremain;
  268. datalen -= secremain;
  269. secremain = (datalen > INT_FLASH_SECTOR_SIZE) ? INT_FLASH_SECTOR_SIZE : datalen;
  270. }
  271. return 0;
  272. }