drv_flash_f2.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * Copyright (c) 2006-2021, 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. /**
  34. * @brief Gets the sector of a given address
  35. * @param None
  36. * @retval The sector of a given address
  37. */
  38. static rt_uint32_t GetSector(rt_uint32_t Address)
  39. {
  40. rt_uint32_t sector = 0;
  41. if((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
  42. {
  43. sector = FLASH_SECTOR_0;
  44. }
  45. else if((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
  46. {
  47. sector = FLASH_SECTOR_1;
  48. }
  49. else if((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
  50. {
  51. sector = FLASH_SECTOR_2;
  52. }
  53. else if((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
  54. {
  55. sector = FLASH_SECTOR_3;
  56. }
  57. else if((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
  58. {
  59. sector = FLASH_SECTOR_4;
  60. }
  61. else if((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
  62. {
  63. sector = FLASH_SECTOR_5;
  64. }
  65. else if((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
  66. {
  67. sector = FLASH_SECTOR_6;
  68. }
  69. else if((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
  70. {
  71. sector = FLASH_SECTOR_7;
  72. }
  73. #if defined(FLASH_SECTOR_8)
  74. else if((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
  75. {
  76. sector = FLASH_SECTOR_8;
  77. }
  78. #endif
  79. #if defined(FLASH_SECTOR_9)
  80. else if((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
  81. {
  82. sector = FLASH_SECTOR_9;
  83. }
  84. #endif
  85. #if defined(FLASH_SECTOR_10)
  86. else if((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
  87. {
  88. sector = FLASH_SECTOR_10;
  89. }
  90. #endif
  91. #if defined(FLASH_SECTOR_11)
  92. else /* (Address < FLASH_END_ADDR) && (Address >= ADDR_FLASH_SECTOR_11) */
  93. {
  94. sector = FLASH_SECTOR_11;
  95. }
  96. #endif
  97. return sector;
  98. }
  99. /**
  100. * Read data from flash.
  101. * @note This operation's units is word.
  102. *
  103. * @param addr flash address
  104. * @param buf buffer to store read data
  105. * @param size read bytes size
  106. *
  107. * @return result
  108. */
  109. int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  110. {
  111. size_t i;
  112. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  113. {
  114. LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size));
  115. return -1;
  116. }
  117. for (i = 0; i < size; i++, buf++, addr++)
  118. {
  119. *buf = *(rt_uint8_t *) addr;
  120. }
  121. return size;
  122. }
  123. /**
  124. * Write data to flash.
  125. * @note This operation's units is word.
  126. * @note This operation must after erase. @see flash_erase.
  127. *
  128. * @param addr flash address
  129. * @param buf the write data buffer
  130. * @param size write bytes size
  131. *
  132. * @return result
  133. */
  134. int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  135. {
  136. rt_err_t result = RT_EOK;
  137. rt_uint32_t end_addr = addr + size;
  138. if ((end_addr) > STM32_FLASH_END_ADDRESS)
  139. {
  140. LOG_E("write outrange flash size! addr is (0x%p)", (void*)(addr + size));
  141. return -RT_EINVAL;
  142. }
  143. if (size < 1)
  144. {
  145. return -RT_EINVAL;
  146. }
  147. HAL_FLASH_Unlock();
  148. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  149. for (size_t i = 0; i < size; i++, addr++, buf++)
  150. {
  151. /* write data to flash */
  152. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (rt_uint64_t)(*buf)) == HAL_OK)
  153. {
  154. if (*(rt_uint8_t *)addr != *buf)
  155. {
  156. result = -RT_ERROR;
  157. break;
  158. }
  159. }
  160. else
  161. {
  162. result = -RT_ERROR;
  163. break;
  164. }
  165. }
  166. HAL_FLASH_Lock();
  167. if (result != RT_EOK)
  168. {
  169. return result;
  170. }
  171. return size;
  172. }
  173. /**
  174. * Erase data on flash.
  175. * @note This operation is irreversible.
  176. * @note This operation's units is different which on many chips.
  177. *
  178. * @param addr flash address
  179. * @param size erase bytes size
  180. *
  181. * @return result
  182. */
  183. int stm32_flash_erase(rt_uint32_t addr, size_t size)
  184. {
  185. rt_err_t result = RT_EOK;
  186. rt_uint32_t FirstSector = 0, NbOfSectors = 0;
  187. rt_uint32_t SECTORError = 0;
  188. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  189. {
  190. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
  191. return -RT_EINVAL;
  192. }
  193. /*Variable used for Erase procedure*/
  194. FLASH_EraseInitTypeDef EraseInitStruct;
  195. /* Unlock the Flash to enable the flash control register access */
  196. HAL_FLASH_Unlock();
  197. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  198. /* Get the 1st sector to erase */
  199. FirstSector = GetSector(addr);
  200. /* Get the number of sector to erase from 1st sector*/
  201. NbOfSectors = GetSector(addr + size - 1) - FirstSector + 1;
  202. /* Fill EraseInit structure*/
  203. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  204. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  205. EraseInitStruct.Sector = FirstSector;
  206. EraseInitStruct.NbSectors = NbOfSectors;
  207. if (HAL_FLASHEx_Erase(&EraseInitStruct, (uint32_t *)&SECTORError) != HAL_OK)
  208. {
  209. result = -RT_ERROR;
  210. goto __exit;
  211. }
  212. __exit:
  213. HAL_FLASH_Lock();
  214. if (result != RT_EOK)
  215. {
  216. return result;
  217. }
  218. LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size);
  219. return result;
  220. }
  221. #if defined(PKG_USING_FAL)
  222. static int fal_flash_read_16k(long offset, rt_uint8_t *buf, size_t size);
  223. static int fal_flash_read_64k(long offset, rt_uint8_t *buf, size_t size);
  224. static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
  225. static int fal_flash_write_16k(long offset, const rt_uint8_t *buf, size_t size);
  226. static int fal_flash_write_64k(long offset, const rt_uint8_t *buf, size_t size);
  227. static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
  228. static int fal_flash_erase_16k(long offset, size_t size);
  229. static int fal_flash_erase_64k(long offset, size_t size);
  230. static int fal_flash_erase_128k(long offset, size_t size);
  231. 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} };
  232. 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} };
  233. 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} };
  234. static int fal_flash_read_16k(long offset, rt_uint8_t *buf, size_t size)
  235. {
  236. return stm32_flash_read(stm32_onchip_flash_16k.addr + offset, buf, size);
  237. }
  238. static int fal_flash_read_64k(long offset, rt_uint8_t *buf, size_t size)
  239. {
  240. return stm32_flash_read(stm32_onchip_flash_64k.addr + offset, buf, size);
  241. }
  242. static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
  243. {
  244. return stm32_flash_read(stm32_onchip_flash_128k.addr + offset, buf, size);
  245. }
  246. static int fal_flash_write_16k(long offset, const rt_uint8_t *buf, size_t size)
  247. {
  248. return stm32_flash_write(stm32_onchip_flash_16k.addr + offset, buf, size);
  249. }
  250. static int fal_flash_write_64k(long offset, const rt_uint8_t *buf, size_t size)
  251. {
  252. return stm32_flash_write(stm32_onchip_flash_64k.addr + offset, buf, size);
  253. }
  254. static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
  255. {
  256. return stm32_flash_write(stm32_onchip_flash_128k.addr + offset, buf, size);
  257. }
  258. static int fal_flash_erase_16k(long offset, size_t size)
  259. {
  260. return stm32_flash_erase(stm32_onchip_flash_16k.addr + offset, size);
  261. }
  262. static int fal_flash_erase_64k(long offset, size_t size)
  263. {
  264. return stm32_flash_erase(stm32_onchip_flash_64k.addr + offset, size);
  265. }
  266. static int fal_flash_erase_128k(long offset, size_t size)
  267. {
  268. return stm32_flash_erase(stm32_onchip_flash_128k.addr + offset, size);
  269. }
  270. #endif
  271. #endif /* BSP_USING_ON_CHIP_FLASH */