drv_flash_l4.c 6.8 KB

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