drv_flash_f7.c 9.4 KB

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