drv_flash_f4.c 12 KB

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