drv_flash_wb.c 5.3 KB

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