drv_flash_h7.c 9.0 KB

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