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