drv_flash_e1.c 4.5 KB

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