drv_flash_f0.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-22 zylx 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, 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 rt_uint8_t *buf, size_t size)
  69. {
  70. rt_err_t result = RT_EOK;
  71. rt_uint32_t end_addr = addr + size;
  72. if (addr % 4 != 0)
  73. {
  74. LOG_E("write addr must be 4-byte alignment");
  75. return -RT_EINVAL;
  76. }
  77. if ((end_addr) > STM32_FLASH_END_ADDRESS)
  78. {
  79. LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
  80. return -RT_EINVAL;
  81. }
  82. HAL_FLASH_Unlock();
  83. while (addr < end_addr)
  84. {
  85. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, *((rt_uint32_t *)buf)) == HAL_OK)
  86. {
  87. if (*(rt_uint32_t *)addr != *(rt_uint32_t *)buf)
  88. {
  89. result = -RT_ERROR;
  90. break;
  91. }
  92. addr += 4;
  93. buf += 4;
  94. }
  95. else
  96. {
  97. result = -RT_ERROR;
  98. break;
  99. }
  100. }
  101. HAL_FLASH_Lock();
  102. if (result != RT_EOK)
  103. {
  104. return result;
  105. }
  106. return size;
  107. }
  108. /**
  109. * Erase data on flash.
  110. * @note This operation is irreversible.
  111. * @note This operation's units is different which on many chips.
  112. *
  113. * @param addr flash address
  114. * @param size erase bytes size
  115. *
  116. * @return result
  117. */
  118. int stm32_flash_erase(rt_uint32_t addr, size_t size)
  119. {
  120. rt_err_t result = RT_EOK;
  121. uint32_t PAGEError = 0;
  122. /*Variable used for Erase procedure*/
  123. FLASH_EraseInitTypeDef EraseInitStruct;
  124. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  125. {
  126. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  127. return -RT_EINVAL;
  128. }
  129. HAL_FLASH_Unlock();
  130. /* Fill EraseInit structure*/
  131. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  132. EraseInitStruct.PageAddress = GetPage(addr);
  133. EraseInitStruct.NbPages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
  134. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  135. {
  136. result = -RT_ERROR;
  137. goto __exit;
  138. }
  139. __exit:
  140. HAL_FLASH_Lock();
  141. if (result != RT_EOK)
  142. {
  143. return result;
  144. }
  145. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  146. return size;
  147. }
  148. #if defined(RT_USING_FAL)
  149. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
  150. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
  151. static int fal_flash_erase(long offset, size_t size);
  152. 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} };
  153. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
  154. {
  155. return stm32_flash_read(stm32_onchip_flash.addr + offset, buf, size);
  156. }
  157. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  158. {
  159. return stm32_flash_write(stm32_onchip_flash.addr + offset, buf, size);
  160. }
  161. static int fal_flash_erase(long offset, size_t size)
  162. {
  163. return stm32_flash_erase(stm32_onchip_flash.addr + offset, size);
  164. }
  165. #endif
  166. #endif /* BSP_USING_ON_CHIP_FLASH */