Int_flash.c 9.4 KB

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