drv_flash_f4.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-12-5 SummerGift first version
  9. */
  10. #include <rtconfig.h>
  11. #include <rtdef.h>
  12. #ifdef BSP_USING_ON_CHIP_FLASH
  13. #include "drv_config.h"
  14. #include "drv_flash.h"
  15. #include <board.h>
  16. #if defined(RT_USING_FAL)
  17. #include "fal.h"
  18. #endif
  19. //#define DRV_DEBUG
  20. #define LOG_TAG "drv.flash"
  21. #include <drv_log.h>
  22. /* Base address of the Flash sectors Bank 1 */
  23. #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
  24. #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
  25. #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
  26. #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
  27. #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
  28. #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
  29. #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
  30. #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
  31. #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
  32. #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
  33. #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
  34. #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
  35. /* Base address of the Flash sectors Bank 2 */
  36. #define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base @ of Sector 0, 16 Kbytes */
  37. #define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base @ of Sector 1, 16 Kbytes */
  38. #define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base @ of Sector 2, 16 Kbytes */
  39. #define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base @ of Sector 3, 16 Kbytes */
  40. #define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base @ of Sector 4, 64 Kbytes */
  41. #define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base @ of Sector 5, 128 Kbytes */
  42. #define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base @ of Sector 6, 128 Kbytes */
  43. #define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base @ of Sector 7, 128 Kbytes */
  44. #define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base @ of Sector 8, 128 Kbytes */
  45. #define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base @ of Sector 9, 128 Kbytes */
  46. #define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base @ of Sector 10, 128 Kbytes */
  47. #define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base @ of Sector 11, 128 Kbytes */
  48. /**
  49. * @brief Gets the sector of a given address
  50. * @param None
  51. * @retval The sector of a given address
  52. */
  53. static rt_uint32_t GetSector(rt_uint32_t Address)
  54. {
  55. rt_uint32_t sector = 0;
  56. if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
  57. {
  58. sector = FLASH_SECTOR_0;
  59. }
  60. else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
  61. {
  62. sector = FLASH_SECTOR_1;
  63. }
  64. else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
  65. {
  66. sector = FLASH_SECTOR_2;
  67. }
  68. else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
  69. {
  70. sector = FLASH_SECTOR_3;
  71. }
  72. else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
  73. {
  74. sector = FLASH_SECTOR_4;
  75. }
  76. else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
  77. {
  78. sector = FLASH_SECTOR_5;
  79. }
  80. else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
  81. {
  82. sector = FLASH_SECTOR_6;
  83. }
  84. else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
  85. {
  86. sector = FLASH_SECTOR_7;
  87. }
  88. #if defined(FLASH_SECTOR_8)
  89. else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
  90. {
  91. sector = FLASH_SECTOR_8;
  92. }
  93. #endif
  94. #if defined(FLASH_SECTOR_9)
  95. else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
  96. {
  97. sector = FLASH_SECTOR_9;
  98. }
  99. #endif
  100. #if defined(FLASH_SECTOR_10)
  101. else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
  102. {
  103. sector = FLASH_SECTOR_10;
  104. }
  105. #endif
  106. #if defined(FLASH_SECTOR_11)
  107. else if((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11))
  108. {
  109. sector = FLASH_SECTOR_11;
  110. }
  111. #endif
  112. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  113. else if((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12))
  114. {
  115. sector = FLASH_SECTOR_12;
  116. }
  117. else if((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13))
  118. {
  119. sector = FLASH_SECTOR_13;
  120. }
  121. else if((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14))
  122. {
  123. sector = FLASH_SECTOR_14;
  124. }
  125. else if((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15))
  126. {
  127. sector = FLASH_SECTOR_15;
  128. }
  129. else if((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16))
  130. {
  131. sector = FLASH_SECTOR_16;
  132. }
  133. else if((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17))
  134. {
  135. sector = FLASH_SECTOR_17;
  136. }
  137. else if((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18))
  138. {
  139. sector = FLASH_SECTOR_18;
  140. }
  141. else if((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19))
  142. {
  143. sector = FLASH_SECTOR_19;
  144. }
  145. else if((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20))
  146. {
  147. sector = FLASH_SECTOR_20;
  148. }
  149. else if((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21))
  150. {
  151. sector = FLASH_SECTOR_21;
  152. }
  153. else if((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22))
  154. {
  155. sector = FLASH_SECTOR_22;
  156. }
  157. else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_23) */
  158. {
  159. sector = FLASH_SECTOR_23;
  160. }
  161. #endif
  162. return sector;
  163. }
  164. /**
  165. * Read data from flash.
  166. * @note This operation's units is word.
  167. *
  168. * @param addr flash address
  169. * @param buf buffer to store read data
  170. * @param size read bytes size
  171. *
  172. * @return result
  173. */
  174. int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  175. {
  176. size_t i;
  177. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  178. {
  179. LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size));
  180. return -1;
  181. }
  182. for (i = 0; i < size; i++, buf++, addr++)
  183. {
  184. *buf = *(rt_uint8_t *) addr;
  185. }
  186. return size;
  187. }
  188. /**
  189. * Write data to flash.
  190. * @note This operation's units is word.
  191. * @note This operation must after erase. @see flash_erase.
  192. *
  193. * @param addr flash address
  194. * @param buf the write data buffer
  195. * @param size write bytes size
  196. *
  197. * @return result
  198. */
  199. int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  200. {
  201. rt_err_t result = RT_EOK;
  202. rt_uint32_t end_addr = addr + size;
  203. rt_uint32_t written_size = 0;
  204. rt_uint32_t write_size = 0;
  205. if ((end_addr) > STM32_FLASH_END_ADDRESS)
  206. {
  207. LOG_E("write outrange flash size! addr is (0x%p)", (void*)(addr + size));
  208. return -RT_EINVAL;
  209. }
  210. if (size < 1)
  211. {
  212. return -RT_EINVAL;
  213. }
  214. HAL_FLASH_Unlock();
  215. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  216. while (written_size < size)
  217. {
  218. if (((addr + written_size) % 4 == 0) && (size - written_size >= 4))
  219. {
  220. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr + written_size, *((rt_uint32_t *)(buf + written_size))) == HAL_OK)
  221. {
  222. if (*(rt_uint32_t *)(addr + written_size) != *(rt_uint32_t *)(buf + written_size))
  223. {
  224. result = -RT_ERROR;
  225. break;
  226. }
  227. }
  228. else
  229. {
  230. result = -RT_ERROR;
  231. break;
  232. }
  233. write_size = 4;
  234. }
  235. else if (((addr + written_size) % 2 == 0) && (size - written_size >= 2))
  236. {
  237. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, addr + written_size, *((rt_uint16_t *)(buf + written_size))) == HAL_OK)
  238. {
  239. if (*(rt_uint16_t *)(addr + written_size) != *(rt_uint16_t *)(buf + written_size))
  240. {
  241. result = -RT_ERROR;
  242. break;
  243. }
  244. }
  245. else
  246. {
  247. result = -RT_ERROR;
  248. break;
  249. }
  250. write_size = 2;
  251. }
  252. else
  253. {
  254. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr + written_size, *((rt_uint8_t *)(buf + written_size))) == HAL_OK)
  255. {
  256. if (*(rt_uint8_t *)(addr + written_size) != *(rt_uint8_t *)(buf + written_size))
  257. {
  258. result = -RT_ERROR;
  259. break;
  260. }
  261. }
  262. else
  263. {
  264. result = -RT_ERROR;
  265. break;
  266. }
  267. write_size = 1;
  268. }
  269. written_size += write_size;
  270. }
  271. HAL_FLASH_Lock();
  272. if (result != RT_EOK)
  273. {
  274. return result;
  275. }
  276. return size;
  277. }
  278. /**
  279. * Erase data on flash.
  280. * @note This operation is irreversible.
  281. * @note This operation's units is different which on many chips.
  282. *
  283. * @param addr flash address
  284. * @param size erase bytes size
  285. *
  286. * @return result
  287. */
  288. int stm32_flash_erase(rt_uint32_t addr, size_t size)
  289. {
  290. rt_err_t result = RT_EOK;
  291. rt_uint32_t FirstSector = 0, NbOfSectors = 0;
  292. rt_uint32_t SECTORError = 0;
  293. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  294. {
  295. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
  296. return -RT_EINVAL;
  297. }
  298. if (size < 1)
  299. {
  300. return -RT_EINVAL;
  301. }
  302. /*Variable used for Erase procedure*/
  303. FLASH_EraseInitTypeDef EraseInitStruct;
  304. /* Unlock the Flash to enable the flash control register access */
  305. HAL_FLASH_Unlock();
  306. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  307. /* Get the 1st sector to erase */
  308. FirstSector = GetSector(addr);
  309. /* Get the number of sector to erase from 1st sector*/
  310. NbOfSectors = GetSector(addr + size - 1) - FirstSector + 1;
  311. /* Fill EraseInit structure*/
  312. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  313. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  314. EraseInitStruct.Sector = FirstSector;
  315. EraseInitStruct.NbSectors = NbOfSectors;
  316. if (HAL_FLASHEx_Erase(&EraseInitStruct, (uint32_t *)&SECTORError) != HAL_OK)
  317. {
  318. result = -RT_ERROR;
  319. goto __exit;
  320. }
  321. __exit:
  322. HAL_FLASH_Lock();
  323. if (result != RT_EOK)
  324. {
  325. return result;
  326. }
  327. LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size);
  328. return size;
  329. }
  330. #if defined(RT_USING_FAL)
  331. static int fal_flash_read_16k(long offset, rt_uint8_t *buf, size_t size);
  332. static int fal_flash_read_64k(long offset, rt_uint8_t *buf, size_t size);
  333. static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
  334. static int fal_flash_write_16k(long offset, const rt_uint8_t *buf, size_t size);
  335. static int fal_flash_write_64k(long offset, const rt_uint8_t *buf, size_t size);
  336. static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
  337. static int fal_flash_erase_16k(long offset, size_t size);
  338. static int fal_flash_erase_64k(long offset, size_t size);
  339. static int fal_flash_erase_128k(long offset, size_t size);
  340. const struct fal_flash_dev stm32_onchip_flash_16k =
  341. {
  342. "onchip_flash_16k",
  343. STM32_FLASH_START_ADRESS_16K,
  344. FLASH_SIZE_GRANULARITY_16K,
  345. (16 * 1024),
  346. {
  347. NULL,
  348. fal_flash_read_16k,
  349. fal_flash_write_16k,
  350. fal_flash_erase_16k,
  351. },
  352. 8,
  353. {},
  354. };
  355. const struct fal_flash_dev stm32_onchip_flash_64k =
  356. {
  357. "onchip_flash_64k",
  358. STM32_FLASH_START_ADRESS_64K,
  359. FLASH_SIZE_GRANULARITY_64K,
  360. (64 * 1024),
  361. {
  362. NULL,
  363. fal_flash_read_64k,
  364. fal_flash_write_64k,
  365. fal_flash_erase_64k,
  366. },
  367. 8,
  368. {},
  369. };
  370. const struct fal_flash_dev stm32_onchip_flash_128k =
  371. {
  372. "onchip_flash_128k",
  373. STM32_FLASH_START_ADRESS_128K,
  374. FLASH_SIZE_GRANULARITY_128K,
  375. (128 * 1024),
  376. {
  377. NULL,
  378. fal_flash_read_128k,
  379. fal_flash_write_128k,
  380. fal_flash_erase_128k,
  381. },
  382. 8,
  383. {},
  384. };
  385. static int fal_flash_read_16k(long offset, rt_uint8_t *buf, size_t size)
  386. {
  387. return stm32_flash_read(stm32_onchip_flash_16k.addr + offset, buf, size);
  388. }
  389. static int fal_flash_read_64k(long offset, rt_uint8_t *buf, size_t size)
  390. {
  391. return stm32_flash_read(stm32_onchip_flash_64k.addr + offset, buf, size);
  392. }
  393. static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
  394. {
  395. return stm32_flash_read(stm32_onchip_flash_128k.addr + offset, buf, size);
  396. }
  397. static int fal_flash_write_16k(long offset, const rt_uint8_t *buf, size_t size)
  398. {
  399. return stm32_flash_write(stm32_onchip_flash_16k.addr + offset, buf, size);
  400. }
  401. static int fal_flash_write_64k(long offset, const rt_uint8_t *buf, size_t size)
  402. {
  403. return stm32_flash_write(stm32_onchip_flash_64k.addr + offset, buf, size);
  404. }
  405. static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
  406. {
  407. return stm32_flash_write(stm32_onchip_flash_128k.addr + offset, buf, size);
  408. }
  409. static int fal_flash_erase_16k(long offset, size_t size)
  410. {
  411. return stm32_flash_erase(stm32_onchip_flash_16k.addr + offset, size);
  412. }
  413. static int fal_flash_erase_64k(long offset, size_t size)
  414. {
  415. return stm32_flash_erase(stm32_onchip_flash_64k.addr + offset, size);
  416. }
  417. static int fal_flash_erase_128k(long offset, size_t size)
  418. {
  419. return stm32_flash_erase(stm32_onchip_flash_128k.addr + offset, size);
  420. }
  421. #endif
  422. #endif /* BSP_USING_ON_CHIP_FLASH */