drv_flash_f4.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. 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 if((Address < ADDR_FLASH_SECTOR_12) && (Address >= ADDR_FLASH_SECTOR_11))
  99. {
  100. sector = FLASH_SECTOR_11;
  101. }
  102. #if defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx)|| defined(STM32F439xx) || defined(STM32F469xx) || defined(STM32F479xx)
  103. else if((Address < ADDR_FLASH_SECTOR_13) && (Address >= ADDR_FLASH_SECTOR_12))
  104. {
  105. sector = FLASH_SECTOR_12;
  106. }
  107. else if((Address < ADDR_FLASH_SECTOR_14) && (Address >= ADDR_FLASH_SECTOR_13))
  108. {
  109. sector = FLASH_SECTOR_13;
  110. }
  111. else if((Address < ADDR_FLASH_SECTOR_15) && (Address >= ADDR_FLASH_SECTOR_14))
  112. {
  113. sector = FLASH_SECTOR_14;
  114. }
  115. else if((Address < ADDR_FLASH_SECTOR_16) && (Address >= ADDR_FLASH_SECTOR_15))
  116. {
  117. sector = FLASH_SECTOR_15;
  118. }
  119. else if((Address < ADDR_FLASH_SECTOR_17) && (Address >= ADDR_FLASH_SECTOR_16))
  120. {
  121. sector = FLASH_SECTOR_16;
  122. }
  123. else if((Address < ADDR_FLASH_SECTOR_18) && (Address >= ADDR_FLASH_SECTOR_17))
  124. {
  125. sector = FLASH_SECTOR_17;
  126. }
  127. else if((Address < ADDR_FLASH_SECTOR_19) && (Address >= ADDR_FLASH_SECTOR_18))
  128. {
  129. sector = FLASH_SECTOR_18;
  130. }
  131. else if((Address < ADDR_FLASH_SECTOR_20) && (Address >= ADDR_FLASH_SECTOR_19))
  132. {
  133. sector = FLASH_SECTOR_19;
  134. }
  135. else if((Address < ADDR_FLASH_SECTOR_21) && (Address >= ADDR_FLASH_SECTOR_20))
  136. {
  137. sector = FLASH_SECTOR_20;
  138. }
  139. else if((Address < ADDR_FLASH_SECTOR_22) && (Address >= ADDR_FLASH_SECTOR_21))
  140. {
  141. sector = FLASH_SECTOR_21;
  142. }
  143. else if((Address < ADDR_FLASH_SECTOR_23) && (Address >= ADDR_FLASH_SECTOR_22))
  144. {
  145. sector = FLASH_SECTOR_22;
  146. }
  147. else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_23) */
  148. {
  149. sector = FLASH_SECTOR_23;
  150. }
  151. #endif
  152. return sector;
  153. }
  154. /**
  155. * Read data from flash.
  156. * @note This operation's units is word.
  157. *
  158. * @param addr flash address
  159. * @param buf buffer to store read data
  160. * @param size read bytes size
  161. *
  162. * @return result
  163. */
  164. int stm32_flash_read(long offset, rt_uint8_t *buf, size_t size)
  165. {
  166. size_t i;
  167. rt_uint32_t addr = STM32_FLASH_START_ADRESS + offset;
  168. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  169. {
  170. LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size));
  171. return -1;
  172. }
  173. for (i = 0; i < size; i++, buf++, addr++)
  174. {
  175. *buf = *(rt_uint8_t *) addr;
  176. }
  177. return size;
  178. }
  179. /**
  180. * Write data to flash.
  181. * @note This operation's units is word.
  182. * @note This operation must after erase. @see flash_erase.
  183. *
  184. * @param addr flash address
  185. * @param buf the write data buffer
  186. * @param size write bytes size
  187. *
  188. * @return result
  189. */
  190. int stm32_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  191. {
  192. rt_err_t result = RT_EOK;
  193. rt_uint32_t addr = STM32_FLASH_START_ADRESS + offset;
  194. rt_uint32_t end_addr = addr + size;
  195. if ((end_addr) > STM32_FLASH_END_ADDRESS)
  196. {
  197. LOG_E("write outrange flash size! addr is (0x%p)", (void*)(addr + size));
  198. return -RT_EINVAL;
  199. }
  200. if (size < 1)
  201. {
  202. return -RT_EINVAL;
  203. }
  204. HAL_FLASH_Unlock();
  205. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);
  206. for (size_t i = 0; i < size; i++, addr++, buf++)
  207. {
  208. /* write data to flash */
  209. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (rt_uint64_t)(*buf)) == HAL_OK)
  210. {
  211. if (*(rt_uint8_t *)addr != *buf)
  212. {
  213. result = -RT_ERROR;
  214. break;
  215. }
  216. }
  217. else
  218. {
  219. result = -RT_ERROR;
  220. break;
  221. }
  222. }
  223. HAL_FLASH_Lock();
  224. if (result != RT_EOK)
  225. {
  226. return result;
  227. }
  228. return size;
  229. }
  230. /**
  231. * Erase data on flash.
  232. * @note This operation is irreversible.
  233. * @note This operation's units is different which on many chips.
  234. *
  235. * @param addr flash address
  236. * @param size erase bytes size
  237. *
  238. * @return result
  239. */
  240. int stm32_flash_erase(long offset, size_t size)
  241. {
  242. rt_err_t result = RT_EOK;
  243. rt_uint32_t addr = STM32_FLASH_START_ADRESS + offset;
  244. rt_uint32_t FirstSector = 0, NbOfSectors = 0;
  245. rt_uint32_t SECTORError = 0;
  246. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  247. {
  248. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
  249. return -RT_EINVAL;
  250. }
  251. /*Variable used for Erase procedure*/
  252. FLASH_EraseInitTypeDef EraseInitStruct;
  253. /* Unlock the Flash to enable the flash control register access */
  254. HAL_FLASH_Unlock();
  255. /* Get the 1st sector to erase */
  256. FirstSector = GetSector(addr);
  257. /* Get the number of sector to erase from 1st sector*/
  258. NbOfSectors = GetSector(addr + size) - FirstSector + 1;
  259. /* Fill EraseInit structure*/
  260. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  261. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  262. EraseInitStruct.Sector = FirstSector;
  263. EraseInitStruct.NbSectors = NbOfSectors;
  264. if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
  265. {
  266. result = -RT_ERROR;
  267. goto __exit;
  268. }
  269. __exit:
  270. HAL_FLASH_Lock();
  271. if (result != RT_EOK)
  272. {
  273. return result;
  274. }
  275. LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size);
  276. return result;
  277. }
  278. #if defined(PKG_USING_FAL)
  279. const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE, (128 * 1024), {NULL, stm32_flash_read, stm32_flash_write, stm32_flash_erase} };
  280. #endif
  281. #endif /* BSP_USING_ON_CHIP_FLASH */