drv_flash_h7.c 6.7 KB

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