drv_flash_f0.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. * 2023-03-16 luobeihai first version
  9. *
  10. */
  11. #include "board.h"
  12. #ifdef BSP_USING_ON_CHIP_FLASH
  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. #if (defined(APM32F030x6) || defined(APM32F030x8) || defined(APM32F051))
  21. #define FLASH_PAGE_SIZE 0x400U
  22. #elif (defined(APM32F030xC) || defined(APM32F070xB) || defined(APM32F071) || defined(APM32F072) || defined(APM32F091))
  23. #define FLASH_PAGE_SIZE 0x800U
  24. #endif
  25. /**
  26. * @brief Gets the page of a given address
  27. * @param Addr: Address of the FLASH Memory
  28. * @retval The page of a given address
  29. */
  30. static uint32_t GetPage(uint32_t addr)
  31. {
  32. uint32_t page = 0;
  33. page = RT_ALIGN_DOWN(addr, FLASH_PAGE_SIZE);
  34. return page;
  35. }
  36. /**
  37. * Read data from flash.
  38. * @note This operation's units is word.
  39. *
  40. * @param addr flash address
  41. * @param buf buffer to store read data
  42. * @param size read bytes size
  43. *
  44. * @return result
  45. */
  46. int apm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  47. {
  48. size_t i;
  49. if ((addr + size) > APM32_FLASH_END_ADDRESS)
  50. {
  51. LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
  52. return -RT_EINVAL;
  53. }
  54. for (i = 0; i < size; i++, buf++, addr++)
  55. {
  56. *buf = *(rt_uint8_t *) addr;
  57. }
  58. return size;
  59. }
  60. /**
  61. * Write data to flash.
  62. * @note This operation's units is word.
  63. * @note This operation must after erase. @see flash_erase.
  64. *
  65. * @param addr flash address
  66. * @param buf the write data buffer
  67. * @param size write bytes size
  68. *
  69. * @return result
  70. */
  71. int apm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  72. {
  73. rt_err_t result = RT_EOK;
  74. rt_uint32_t end_addr = addr + size;
  75. if (addr % 4 != 0)
  76. {
  77. LOG_E("write addr must be 4-byte alignment");
  78. return -RT_EINVAL;
  79. }
  80. if ((end_addr) > APM32_FLASH_END_ADDRESS)
  81. {
  82. LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
  83. return -RT_EINVAL;
  84. }
  85. FMC_Unlock();
  86. while (addr < end_addr)
  87. {
  88. if (FMC_ProgramWord(addr, *((rt_uint32_t *)buf)) == FMC_STATE_COMPLETE)
  89. {
  90. if (*(rt_uint32_t *)addr != *(rt_uint32_t *)buf)
  91. {
  92. result = -RT_ERROR;
  93. break;
  94. }
  95. addr += 4;
  96. buf += 4;
  97. }
  98. else
  99. {
  100. result = -RT_ERROR;
  101. break;
  102. }
  103. }
  104. FMC_Lock();
  105. if (result != RT_EOK)
  106. {
  107. return result;
  108. }
  109. return size;
  110. }
  111. /**
  112. * @brief erase data on flash .
  113. * @note this operation is irreversible.
  114. * @note this operation's units is different which on many chips.
  115. *
  116. * @param addr flash address
  117. * @param size erase bytes size
  118. *
  119. * @return result
  120. */
  121. int apm32_flash_erase(rt_uint32_t addr, rt_uint32_t size)
  122. {
  123. rt_err_t result = RT_EOK;
  124. rt_uint32_t start_addr = addr;
  125. rt_uint32_t end_addr = addr + size;
  126. rt_uint32_t page_addr = 0;
  127. FMC_Unlock();
  128. if ((end_addr) > APM32_FLASH_END_ADDRESS)
  129. {
  130. LOG_E("erase outrange flash size! addr is (0x%p)", (void *)(addr + size));
  131. return -RT_EINVAL;
  132. }
  133. /* clear program error flag */
  134. if (FMC_ReadStatusFlag(FMC_FLAG_PE) == SET)
  135. {
  136. FMC_ClearStatusFlag(FMC_FLAG_PE);
  137. }
  138. while(addr < end_addr)
  139. {
  140. page_addr = GetPage(addr);
  141. if(FMC_ErasePage(page_addr) != FMC_STATE_COMPLETE)
  142. {
  143. result = -RT_ERROR;
  144. goto __exit;
  145. }
  146. addr += FLASH_PAGE_SIZE;
  147. }
  148. __exit:
  149. FMC_Lock();
  150. if(result != RT_EOK)
  151. {
  152. return result;
  153. }
  154. LOG_D("erase done: addr (0x%p), size %d", (void *)start_addr, size);
  155. return size;
  156. }
  157. #if defined(RT_USING_FAL)
  158. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
  159. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
  160. static int fal_flash_erase(long offset, size_t size);
  161. const struct fal_flash_dev apm32_onchip_flash = { "onchip_flash", APM32_FLASH_START_ADRESS, APM32_FLASH_SIZE, FLASH_PAGE_SIZE, {NULL, fal_flash_read, fal_flash_write, fal_flash_erase} };
  162. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
  163. {
  164. return apm32_flash_read(apm32_onchip_flash.addr + offset, buf, size);
  165. }
  166. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  167. {
  168. return apm32_flash_write(apm32_onchip_flash.addr + offset, buf, size);
  169. }
  170. static int fal_flash_erase(long offset, size_t size)
  171. {
  172. return apm32_flash_erase(apm32_onchip_flash.addr + offset, size);
  173. }
  174. #endif
  175. #endif /* BSP_USING_ON_CHIP_FLASH */