drv_flash_g0.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. * 2020-06-27 NU-LL 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. page = RT_ALIGN_DOWN(addr-STM32_FLASH_START_ADRESS, FLASH_PAGE_SIZE)/FLASH_PAGE_SIZE;
  31. return page;
  32. }
  33. /**
  34. * Read data from flash.
  35. * @note This operation's units is word.
  36. *
  37. * @param addr flash address
  38. * @param buf buffer to store read data
  39. * @param size read bytes size
  40. *
  41. * @return result
  42. */
  43. int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  44. {
  45. size_t i;
  46. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  47. {
  48. LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
  49. return -RT_EINVAL;
  50. }
  51. for (i = 0; i < size; i++, buf++, addr++)
  52. {
  53. *buf = *(rt_uint8_t *) addr;
  54. }
  55. return size;
  56. }
  57. /**
  58. * Write data to flash.
  59. * @note This operation's units is word.
  60. * @note This operation must after erase. @see flash_erase.
  61. *
  62. * @param addr flash address
  63. * @param buf the write data buffer
  64. * @param size write bytes size
  65. *
  66. * @return result
  67. */
  68. int stm32_flash_write(rt_uint32_t addr, const uint8_t *buf, size_t size)
  69. {
  70. size_t i, j;
  71. rt_err_t result = 0;
  72. rt_uint64_t write_data = 0, temp_data = 0;
  73. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  74. {
  75. LOG_E("ERROR: write outrange flash size! addr is (0x%p)\n", (void*)(addr + size));
  76. return -RT_EINVAL;
  77. }
  78. if(addr % 8 != 0)
  79. {
  80. LOG_E("write addr must be 8-byte alignment");
  81. return -RT_EINVAL;
  82. }
  83. HAL_FLASH_Unlock();
  84. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGSERR);
  85. if (size < 1)
  86. {
  87. return -RT_ERROR;
  88. }
  89. for (i = 0; i < size;)
  90. {
  91. if ((size - i) < 8)
  92. {
  93. for (j = 0; (size - i) > 0; i++, j++)
  94. {
  95. temp_data = *buf;
  96. write_data = (write_data) | (temp_data << 8 * j);
  97. buf ++;
  98. }
  99. }
  100. else
  101. {
  102. for (j = 0; j < 8; j++, i++)
  103. {
  104. temp_data = *buf;
  105. write_data = (write_data) | (temp_data << 8 * j);
  106. buf ++;
  107. }
  108. }
  109. /* write data */
  110. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, addr, write_data) == HAL_OK)
  111. {
  112. /* Check the written value */
  113. if (*(uint64_t*)addr != write_data)
  114. {
  115. LOG_E("ERROR: write data != read data\n");
  116. result = -RT_ERROR;
  117. goto __exit;
  118. }
  119. }
  120. else
  121. {
  122. result = -RT_ERROR;
  123. goto __exit;
  124. }
  125. temp_data = 0;
  126. write_data = 0;
  127. addr += 8;
  128. }
  129. __exit:
  130. HAL_FLASH_Lock();
  131. if (result != 0)
  132. {
  133. return result;
  134. }
  135. return size;
  136. }
  137. /**
  138. * Erase data on flash.
  139. * @note This operation is irreversible.
  140. * @note This operation's units is different which on many chips.
  141. *
  142. * @param addr flash address
  143. * @param size erase bytes size
  144. *
  145. * @return result
  146. */
  147. int stm32_flash_erase(rt_uint32_t addr, size_t size)
  148. {
  149. rt_err_t result = RT_EOK;
  150. uint32_t PAGEError = 0;
  151. /*Variable used for Erase procedure*/
  152. FLASH_EraseInitTypeDef EraseInitStruct;
  153. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  154. {
  155. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  156. return -RT_EINVAL;
  157. }
  158. HAL_FLASH_Unlock();
  159. /* Fill EraseInit structure*/
  160. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  161. EraseInitStruct.Page = GetPage(addr);
  162. EraseInitStruct.NbPages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
  163. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  164. {
  165. result = -RT_ERROR;
  166. goto __exit;
  167. }
  168. __exit:
  169. HAL_FLASH_Lock();
  170. if (result != RT_EOK)
  171. {
  172. return result;
  173. }
  174. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  175. return size;
  176. }
  177. #if defined(RT_USING_FAL)
  178. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
  179. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
  180. static int fal_flash_erase(long offset, size_t size);
  181. 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} };
  182. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
  183. {
  184. return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size);
  185. }
  186. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  187. {
  188. return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size);
  189. }
  190. static int fal_flash_erase(long offset, size_t size)
  191. {
  192. return stm32_flash_erase(stm32_onchip_flash.addr + offset, size);
  193. }
  194. #endif
  195. #endif /* BSP_USING_ON_CHIP_FLASH */