drv_flash_f7.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. * 2019-3-2 jinsheng add Macro judgment
  10. * 2020-1-6 duminmin support single bank mode
  11. * 2021-8-11 CX fix the sector calculation error bug
  12. */
  13. #include <rtconfig.h>
  14. #include <rtdef.h>
  15. #ifdef BSP_USING_ON_CHIP_FLASH
  16. #include "drv_config.h"
  17. #include "drv_flash.h"
  18. #include <board.h>
  19. #if defined(RT_USING_FAL)
  20. #include "fal.h"
  21. #endif
  22. //#define DRV_DEBUG
  23. #define LOG_TAG "drv.flash"
  24. #include <drv_log.h>
  25. #define ADDR_FLASH_SECTOR_0 ((rt_uint32_t)0x08000000) /* Base address of Sector 0, 32 Kbytes */
  26. #define ADDR_FLASH_SECTOR_1 ((rt_uint32_t)0x08008000) /* Base address of Sector 1, 32 Kbytes */
  27. #define ADDR_FLASH_SECTOR_2 ((rt_uint32_t)0x08010000) /* Base address of Sector 2, 32 Kbytes */
  28. #define ADDR_FLASH_SECTOR_3 ((rt_uint32_t)0x08018000) /* Base address of Sector 3, 32 Kbytes */
  29. #define ADDR_FLASH_SECTOR_4 ((rt_uint32_t)0x08020000) /* Base address of Sector 4, 128 Kbytes */
  30. #define ADDR_FLASH_SECTOR_5 ((rt_uint32_t)0x08040000) /* Base address of Sector 5, 256 Kbytes */
  31. #define ADDR_FLASH_SECTOR_6 ((rt_uint32_t)0x08080000) /* Base address of Sector 6, 256 Kbytes */
  32. #define ADDR_FLASH_SECTOR_7 ((rt_uint32_t)0x080C0000) /* Base address of Sector 7, 256 Kbytes */
  33. #define ADDR_FLASH_SECTOR_8 ((rt_uint32_t)0x08100000) /* Base address of Sector 8, 256 Kbytes */
  34. #define ADDR_FLASH_SECTOR_9 ((rt_uint32_t)0x08140000) /* Base address of Sector 9, 256 Kbytes */
  35. #define ADDR_FLASH_SECTOR_10 ((rt_uint32_t)0x08180000) /* Base address of Sector 10, 256 Kbytes */
  36. #define ADDR_FLASH_SECTOR_11 ((rt_uint32_t)0x081C0000) /* Base address of Sector 11, 256 Kbytes */
  37. /**
  38. * @brief Gets the sector of a given address
  39. * @param None
  40. * @retval The sector of a given address
  41. */
  42. static rt_uint32_t GetSector(rt_uint32_t Address)
  43. {
  44. uint32_t sector = 0;
  45. #if defined (FLASH_OPTCR_nDBANK)
  46. FLASH_OBProgramInitTypeDef OBInit;
  47. uint32_t nbank = 0;
  48. //get duel bank ability:nDBANK(Bit29)
  49. HAL_FLASHEx_OBGetConfig(&OBInit);
  50. nbank = ((OBInit.USERConfig & 0x20000000U) >> 29);
  51. //1:single bank mode
  52. if (1 == nbank)
  53. {
  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. else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
  87. {
  88. sector = FLASH_SECTOR_8;
  89. }
  90. else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
  91. {
  92. sector = FLASH_SECTOR_9;
  93. }
  94. else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
  95. {
  96. sector = FLASH_SECTOR_10;
  97. }
  98. else
  99. {
  100. sector = FLASH_SECTOR_11;
  101. }
  102. }
  103. else //0:dual bank mode
  104. {
  105. LOG_E("rtthread doesn't support duel bank mode yet!");
  106. RT_ASSERT(0);
  107. }
  108. #else //no dual bank ability
  109. if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
  110. {
  111. sector = FLASH_SECTOR_0;
  112. }
  113. else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
  114. {
  115. sector = FLASH_SECTOR_1;
  116. }
  117. else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
  118. {
  119. sector = FLASH_SECTOR_2;
  120. }
  121. else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
  122. {
  123. sector = FLASH_SECTOR_3;
  124. }
  125. else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
  126. {
  127. sector = FLASH_SECTOR_4;
  128. }
  129. else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
  130. {
  131. sector = FLASH_SECTOR_5;
  132. }
  133. else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
  134. {
  135. sector = FLASH_SECTOR_6;
  136. }
  137. else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
  138. {
  139. sector = FLASH_SECTOR_7;
  140. }
  141. else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
  142. {
  143. sector = FLASH_SECTOR_8;
  144. }
  145. else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
  146. {
  147. sector = FLASH_SECTOR_9;
  148. }
  149. else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
  150. {
  151. sector = FLASH_SECTOR_10;
  152. }
  153. else
  154. {
  155. sector = FLASH_SECTOR_11;
  156. }
  157. #endif
  158. return sector;
  159. }
  160. /**
  161. * Read data from flash.
  162. * @note This operation's units is word.
  163. *
  164. * @param addr flash address
  165. * @param buf buffer to store read data
  166. * @param size read bytes size
  167. *
  168. * @return result
  169. */
  170. int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  171. {
  172. size_t i;
  173. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  174. {
  175. LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
  176. return -1;
  177. }
  178. for (i = 0; i < size; i++, buf++, addr++)
  179. {
  180. *buf = *(rt_uint8_t *) addr;
  181. }
  182. return size;
  183. }
  184. /**
  185. * Write data to flash.
  186. * @note This operation's units is word.
  187. * @note This operation must after erase. @see flash_erase.
  188. *
  189. * @param addr flash address
  190. * @param buf the write data buffer
  191. * @param size write bytes size
  192. *
  193. * @return result
  194. */
  195. int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  196. {
  197. rt_err_t result = RT_EOK;
  198. rt_uint32_t end_addr = addr + size;
  199. if ((end_addr) > STM32_FLASH_END_ADDRESS)
  200. {
  201. LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
  202. return -RT_EINVAL;
  203. }
  204. if (size < 1)
  205. {
  206. return -RT_EINVAL;
  207. }
  208. /* Unlock the Flash to enable the flash control register access */
  209. HAL_FLASH_Unlock();
  210. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR);
  211. for (size_t i = 0; i < size; i++, addr++, buf++)
  212. {
  213. /* write data to flash */
  214. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (rt_uint64_t)(*buf)) == HAL_OK)
  215. {
  216. if (*(rt_uint8_t *)addr != *buf)
  217. {
  218. result = -RT_ERROR;
  219. break;
  220. }
  221. }
  222. else
  223. {
  224. result = -RT_ERROR;
  225. break;
  226. }
  227. }
  228. HAL_FLASH_Lock();
  229. if (result != RT_EOK)
  230. {
  231. return result;
  232. }
  233. return size;
  234. }
  235. /**
  236. * Erase data on flash.
  237. * @note This operation is irreversible.
  238. * @note This operation's units is different which on many chips.
  239. *
  240. * @param addr flash address
  241. * @param size erase bytes size
  242. *
  243. * @return result
  244. */
  245. int stm32_flash_erase(rt_uint32_t addr, size_t size)
  246. {
  247. rt_err_t result = RT_EOK;
  248. rt_uint32_t FirstSector = 0, NbOfSectors = 0;
  249. rt_uint32_t SECTORError = 0;
  250. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  251. {
  252. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  253. return -RT_EINVAL;
  254. }
  255. /*Variable used for Erase procedure*/
  256. FLASH_EraseInitTypeDef EraseInitStruct;
  257. /* Unlock the Flash to enable the flash control register access */
  258. HAL_FLASH_Unlock();
  259. /* Get the 1st sector to erase */
  260. FirstSector = GetSector(addr);
  261. /* Get the number of sector to erase from 1st sector*/
  262. NbOfSectors = GetSector(addr + size - 1) - FirstSector + 1;
  263. /* Fill EraseInit structure*/
  264. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  265. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  266. EraseInitStruct.Sector = FirstSector;
  267. EraseInitStruct.NbSectors = NbOfSectors;
  268. if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
  269. {
  270. result = -RT_ERROR;
  271. goto __exit;
  272. }
  273. __exit:
  274. HAL_FLASH_Lock();
  275. if (result != RT_EOK)
  276. {
  277. return result;
  278. }
  279. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  280. return size;
  281. }
  282. #if defined(RT_USING_FAL)
  283. #define FLASH_SIZE_GRANULARITY_32K (4 * 32 * 1024)
  284. #define FLASH_SIZE_GRANULARITY_128K (128 * 1024)
  285. #define FLASH_SIZE_GRANULARITY_256K (7 * 256 *1024)
  286. #define STM32_FLASH_START_ADRESS_32K (STM32_FLASH_START_ADRESS)
  287. #define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_32K + FLASH_SIZE_GRANULARITY_32K)
  288. #define STM32_FLASH_START_ADRESS_256K (STM32_FLASH_START_ADRESS_128K + FLASH_SIZE_GRANULARITY_128K)
  289. static int fal_flash_read_32k(long offset, rt_uint8_t *buf, size_t size);
  290. static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
  291. static int fal_flash_read_256k(long offset, rt_uint8_t *buf, size_t size);
  292. static int fal_flash_write_32k(long offset, const rt_uint8_t *buf, size_t size);
  293. static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
  294. static int fal_flash_write_256k(long offset, const rt_uint8_t *buf, size_t size);
  295. static int fal_flash_erase_32k(long offset, size_t size);
  296. static int fal_flash_erase_128k(long offset, size_t size);
  297. static int fal_flash_erase_256k(long offset, size_t size);
  298. const struct fal_flash_dev stm32_onchip_flash_32k = { "onchip_flash_32k", STM32_FLASH_START_ADRESS_32K, FLASH_SIZE_GRANULARITY_32K, (32 * 1024), {NULL, fal_flash_read_32k, fal_flash_write_32k, fal_flash_erase_32k} };
  299. 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} };
  300. const struct fal_flash_dev stm32_onchip_flash_256k = { "onchip_flash_256k", STM32_FLASH_START_ADRESS_256K, FLASH_SIZE_GRANULARITY_256K, (256 * 1024), {NULL, fal_flash_read_256k, fal_flash_write_256k, fal_flash_erase_256k} };
  301. static int fal_flash_read_32k(long offset, rt_uint8_t *buf, size_t size)
  302. {
  303. return stm32_flash_read(stm32_onchip_flash_32k.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_read_256k(long offset, rt_uint8_t *buf, size_t size)
  310. {
  311. return stm32_flash_read(stm32_onchip_flash_256k.addr + offset, buf, size);
  312. }
  313. static int fal_flash_write_32k(long offset, const rt_uint8_t *buf, size_t size)
  314. {
  315. return stm32_flash_write(stm32_onchip_flash_32k.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_write_256k(long offset, const rt_uint8_t *buf, size_t size)
  322. {
  323. return stm32_flash_write(stm32_onchip_flash_256k.addr + offset, buf, size);
  324. }
  325. static int fal_flash_erase_32k(long offset, size_t size)
  326. {
  327. return stm32_flash_erase(stm32_onchip_flash_32k.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. static int fal_flash_erase_256k(long offset, size_t size)
  334. {
  335. return stm32_flash_erase(stm32_onchip_flash_256k.addr + offset, size);
  336. }
  337. #endif
  338. #endif /* BSP_USING_ON_CHIP_FLASH */