drv_flash_h7.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Copyright (c) 2006-2023, 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. * 2021-3-3 zhuyf233 fix some bugs
  13. */
  14. #include <rtconfig.h>
  15. #include <rtdef.h>
  16. #ifdef BSP_USING_ON_CHIP_FLASH
  17. #include "drv_config.h"
  18. #include "drv_flash.h"
  19. #include <board.h>
  20. #if defined(RT_USING_FAL)
  21. #include "fal.h"
  22. #endif
  23. //#define DRV_DEBUG
  24. #define LOG_TAG "drv.flash"
  25. #include <drv_log.h>
  26. /**
  27. * Read data from flash.
  28. * @note This operation's units is word.
  29. *
  30. * @param addr flash address
  31. * @param buf buffer to store read data
  32. * @param size read bytes size
  33. *
  34. * @retval The length of bytes that have been read
  35. */
  36. int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  37. {
  38. size_t i;
  39. if ((addr + size - 1) > FLASH_END)
  40. {
  41. LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
  42. return -RT_ERROR;
  43. }
  44. for (i = 0; i < size; i++, buf++, addr++)
  45. {
  46. *buf = *(rt_uint8_t *) addr;
  47. }
  48. return size;
  49. }
  50. /**
  51. * Write data to flash.
  52. * @note This operation's units is word.
  53. * @note This operation must after erase. @see flash_erase.
  54. *
  55. * @param addr flash address
  56. * @param buf the write data buffer
  57. * @param size write bytes size
  58. *
  59. * @return The length of bytes that have been written
  60. */
  61. int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  62. {
  63. rt_err_t result = RT_EOK;
  64. rt_uint32_t end_addr = addr + size - 1, write_addr;
  65. rt_uint32_t write_granularity = FLASH_NB_32BITWORD_IN_FLASHWORD * 4;
  66. rt_uint32_t write_size = write_granularity;
  67. rt_uint8_t write_buffer[32] = {0};
  68. if ((end_addr) > FLASH_END)
  69. {
  70. LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
  71. return -RT_EINVAL;
  72. }
  73. if(addr % 32 != 0)
  74. {
  75. LOG_E("write addr must be 32-byte alignment");
  76. return -RT_EINVAL;
  77. }
  78. if (size < 1)
  79. {
  80. return -RT_EINVAL;
  81. }
  82. HAL_FLASH_Unlock();
  83. write_addr = (uint32_t)buf;
  84. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR);
  85. while (addr < end_addr)
  86. {
  87. if(end_addr - addr + 1 < write_granularity)
  88. {
  89. write_size = end_addr - addr + 1;
  90. for(size_t i = 0; i < write_size; i++)
  91. {
  92. write_buffer[i] = *((uint8_t *)(write_addr + i));
  93. }
  94. write_addr = (uint32_t)((rt_uint32_t *)write_buffer);
  95. }
  96. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, addr, write_addr) == HAL_OK)
  97. {
  98. for(rt_uint8_t i = 0; i < write_size; i++)
  99. {
  100. if (*(rt_uint8_t *)(addr + i) != *(rt_uint8_t *)(write_addr + i))
  101. {
  102. result = -RT_ERROR;
  103. goto __exit;
  104. }
  105. }
  106. addr += write_granularity;
  107. write_addr += write_granularity;
  108. }
  109. else
  110. {
  111. result = -RT_ERROR;
  112. goto __exit;
  113. }
  114. }
  115. __exit:
  116. HAL_FLASH_Lock();
  117. if (result != RT_EOK)
  118. {
  119. return result;
  120. }
  121. return size;
  122. }
  123. /**
  124. * Erase data on flash.
  125. * @note This operation is irreversible.
  126. * @note This operation's units is different which on many chips.
  127. *
  128. * @param addr flash address
  129. * @param size erase bytes size
  130. *
  131. * @return result
  132. */
  133. int stm32_flash_erase(rt_uint32_t addr, size_t size)
  134. {
  135. rt_err_t result = RT_EOK;
  136. rt_uint32_t SECTORError = 0;
  137. if ((addr + size - 1) > FLASH_END)
  138. {
  139. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  140. return -RT_EINVAL;
  141. }
  142. rt_uint32_t addr_bank1 = 0;
  143. rt_uint32_t size_bank1 = 0;
  144. #ifdef FLASH_BANK_2
  145. rt_uint32_t addr_bank2 = 0;
  146. rt_uint32_t size_bank2 = 0;
  147. #endif
  148. if((addr + size) < FLASH_BANK2_BASE)
  149. {
  150. addr_bank1 = addr;
  151. size_bank1 = size;
  152. #ifdef FLASH_BANK_2
  153. size_bank2 = 0;
  154. #endif
  155. }
  156. else if(addr >= FLASH_BANK2_BASE)
  157. {
  158. size_bank1 = 0;
  159. #ifdef FLASH_BANK_2
  160. addr_bank2 = addr;
  161. size_bank2 = size;
  162. #endif
  163. }
  164. else
  165. {
  166. addr_bank1 = addr;
  167. size_bank1 = FLASH_BANK2_BASE - addr_bank1;
  168. #ifdef FLASH_BANK_2
  169. addr_bank2 = FLASH_BANK2_BASE;
  170. size_bank2 = addr + size - FLASH_BANK2_BASE;
  171. #endif
  172. }
  173. /*Variable used for Erase procedure*/
  174. FLASH_EraseInitTypeDef EraseInitStruct;
  175. /* Unlock the Flash to enable the flash control register access */
  176. HAL_FLASH_Unlock();
  177. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  178. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  179. SCB_DisableDCache();
  180. if(size_bank1)
  181. {
  182. EraseInitStruct.Sector = (addr_bank1 - FLASH_BANK1_BASE) / FLASH_SECTOR_SIZE;
  183. EraseInitStruct.NbSectors = (addr_bank1 + size_bank1 -1 - FLASH_BANK1_BASE) / FLASH_SECTOR_SIZE - EraseInitStruct.Sector + 1;
  184. EraseInitStruct.Banks = FLASH_BANK_1;
  185. if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
  186. {
  187. result = -RT_ERROR;
  188. goto __exit;
  189. }
  190. }
  191. #ifdef FLASH_BANK_2
  192. if(size_bank2)
  193. {
  194. EraseInitStruct.Sector = (addr_bank2 - FLASH_BANK2_BASE) / FLASH_SECTOR_SIZE;
  195. EraseInitStruct.NbSectors = (addr_bank2 + size_bank2 -1 - FLASH_BANK2_BASE) / FLASH_SECTOR_SIZE - EraseInitStruct.Sector + 1;
  196. EraseInitStruct.Banks = FLASH_BANK_2;
  197. if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
  198. {
  199. result = -RT_ERROR;
  200. goto __exit;
  201. }
  202. }
  203. #endif
  204. __exit:
  205. SCB_EnableDCache();
  206. HAL_FLASH_Lock();
  207. if (result != RT_EOK)
  208. {
  209. return result;
  210. }
  211. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  212. return size;
  213. }
  214. #if defined(RT_USING_FAL)
  215. static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
  216. static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
  217. static int fal_flash_erase_128k(long offset, size_t size);
  218. 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} };
  219. static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
  220. {
  221. return stm32_flash_read(stm32_onchip_flash_128k.addr + offset, buf, size);
  222. }
  223. static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
  224. {
  225. return stm32_flash_write(stm32_onchip_flash_128k.addr + offset, buf, size);
  226. }
  227. static int fal_flash_erase_128k(long offset, size_t size)
  228. {
  229. return stm32_flash_erase(stm32_onchip_flash_128k.addr + offset, size);
  230. }
  231. #endif
  232. #endif /* BSP_USING_ON_CHIP_FLASH */