drv_flash_l4.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. */
  10. #include <rtconfig.h>
  11. #include <rtdef.h>
  12. #ifdef BSP_USING_ON_CHIP_FLASH
  13. #include "drv_config.h"
  14. #include "drv_flash.h"
  15. #include <board.h>
  16. #if defined(RT_USING_FAL)
  17. #include "fal.h"
  18. #endif
  19. //#define DRV_DEBUG
  20. #define LOG_TAG "drv.flash"
  21. #include <drv_log.h>
  22. /**
  23. * @brief Gets the page of a given address
  24. * @param Addr: Address of the FLASH Memory
  25. * @retval The page of a given address
  26. */
  27. static uint32_t GetPage(uint32_t Addr)
  28. {
  29. uint32_t page = 0;
  30. if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
  31. {
  32. /* Bank 1 */
  33. page = (Addr - FLASH_BASE) / FLASH_PAGE_SIZE;
  34. }
  35. else
  36. {
  37. /* Bank 2 */
  38. page = (Addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
  39. }
  40. return page;
  41. }
  42. /**
  43. * @brief Gets the bank of a given address
  44. * @param Addr: Address of the FLASH Memory
  45. * @retval The bank of a given address
  46. */
  47. static uint32_t GetBank(uint32_t Addr)
  48. {
  49. uint32_t bank = 0;
  50. #ifndef FLASH_BANK_2
  51. bank = FLASH_BANK_1;
  52. #else
  53. if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0)
  54. {
  55. /* No Bank swap */
  56. if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
  57. {
  58. bank = FLASH_BANK_1;
  59. }
  60. else
  61. {
  62. bank = FLASH_BANK_2;
  63. }
  64. }
  65. else
  66. {
  67. /* Bank swap */
  68. if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
  69. {
  70. bank = FLASH_BANK_2;
  71. }
  72. else
  73. {
  74. bank = FLASH_BANK_1;
  75. }
  76. }
  77. #endif
  78. return bank;
  79. }
  80. /**
  81. * Read data from flash.
  82. * @note This operation's units is word.
  83. *
  84. * @param addr flash address
  85. * @param buf buffer to store read data
  86. * @param size read bytes size
  87. *
  88. * @return result
  89. */
  90. int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  91. {
  92. size_t i;
  93. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  94. {
  95. LOG_E("read outrange flash size! addr is (0x%p)", (void*)(addr + size));
  96. return -RT_EINVAL;
  97. }
  98. for (i = 0; i < size; i++, buf++, addr++)
  99. {
  100. *buf = *(rt_uint8_t *) addr;
  101. }
  102. return size;
  103. }
  104. /**
  105. * Write data to flash.
  106. * @note This operation's units is word.
  107. * @note This operation must after erase. @see flash_erase.
  108. *
  109. * @param addr flash address
  110. * @param buf the write data buffer
  111. * @param size write bytes size
  112. *
  113. * @return result
  114. */
  115. int stm32_flash_write(rt_uint32_t addr, const uint8_t *buf, size_t size)
  116. {
  117. size_t i, j;
  118. rt_err_t result = 0;
  119. rt_uint64_t write_data = 0, temp_data = 0;
  120. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  121. {
  122. LOG_E("ERROR: write outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
  123. return -RT_EINVAL;
  124. }
  125. if(addr % 8 != 0)
  126. {
  127. LOG_E("write addr must be 8-byte alignment");
  128. return -RT_EINVAL;
  129. }
  130. HAL_FLASH_Unlock();
  131. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);
  132. if (size < 1)
  133. {
  134. return -RT_ERROR;
  135. }
  136. for (i = 0; i < size;)
  137. {
  138. if ((size - i) < 8)
  139. {
  140. for (j = 0; (size - i) > 0; i++, j++)
  141. {
  142. temp_data = *buf;
  143. write_data = (write_data) | (temp_data << 8 * j);
  144. buf ++;
  145. }
  146. }
  147. else
  148. {
  149. for (j = 0; j < 8; j++, i++)
  150. {
  151. temp_data = *buf;
  152. write_data = (write_data) | (temp_data << 8 * j);
  153. buf ++;
  154. }
  155. }
  156. /* write data */
  157. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, write_data) == HAL_OK)
  158. {
  159. /* Check the written value */
  160. if (*(uint64_t*)addr != write_data)
  161. {
  162. LOG_E("ERROR: write data != read data\n");
  163. result = -RT_ERROR;
  164. goto __exit;
  165. }
  166. }
  167. else
  168. {
  169. result = -RT_ERROR;
  170. goto __exit;
  171. }
  172. temp_data = 0;
  173. write_data = 0;
  174. addr += 8;
  175. }
  176. __exit:
  177. HAL_FLASH_Lock();
  178. if (result != 0)
  179. {
  180. return result;
  181. }
  182. return size;
  183. }
  184. /**
  185. * Erase data on flash.
  186. * @note This operation is irreversible.
  187. * @note This operation's units is different which on many chips.
  188. *
  189. * @param addr flash address
  190. * @param size erase bytes size
  191. *
  192. * @return result
  193. */
  194. int stm32_flash_erase(rt_uint32_t addr, size_t size)
  195. {
  196. rt_err_t result = RT_EOK;
  197. uint32_t FirstPage = 0, NbOfPages = 0, BankNumber = 0;
  198. uint32_t PAGEError = 0;
  199. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  200. {
  201. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
  202. return -RT_EINVAL;
  203. }
  204. /*Variable used for Erase procedure*/
  205. FLASH_EraseInitTypeDef EraseInitStruct;
  206. /* Unlock the Flash to enable the flash control register access *************/
  207. HAL_FLASH_Unlock();
  208. /* Clear OPTVERR bit set on virgin samples */
  209. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPTVERR | FLASH_FLAG_PGSERR);
  210. /* Get the 1st page to erase */
  211. FirstPage = GetPage(addr);
  212. /* Get the number of pages to erase from 1st page */
  213. NbOfPages = GetPage(addr + size - 1) - FirstPage + 1;
  214. /* Get the bank */
  215. BankNumber = GetBank(addr);
  216. /* Fill EraseInit structure*/
  217. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  218. EraseInitStruct.Banks = BankNumber;
  219. EraseInitStruct.Page = FirstPage;
  220. EraseInitStruct.NbPages = NbOfPages;
  221. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  222. {
  223. result = -RT_ERROR;
  224. goto __exit;
  225. }
  226. __exit:
  227. HAL_FLASH_Lock();
  228. if (result != RT_EOK)
  229. {
  230. return result;
  231. }
  232. LOG_D("erase done: addr (0x%p), size %d", (void*)addr, size);
  233. return size;
  234. }
  235. #if defined(RT_USING_FAL)
  236. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
  237. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
  238. static int fal_flash_erase(long offset, size_t size);
  239. const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE, FLASH_PAGE_SIZE, {NULL, fal_flash_read, fal_flash_write, fal_flash_erase} };
  240. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
  241. {
  242. return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size);
  243. }
  244. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  245. {
  246. return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size);
  247. }
  248. static int fal_flash_erase(long offset, size_t size)
  249. {
  250. return stm32_flash_erase(stm32_onchip_flash.addr + offset, size);
  251. }
  252. #endif
  253. #endif /* BSP_USING_ON_CHIP_FLASH */