drv_flash.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * 2022-05-16 shelton first version
  9. */
  10. #include <rtthread.h>
  11. #include "drv_common.h"
  12. #ifdef BSP_USING_ON_CHIP_FLASH
  13. #include "drv_flash.h"
  14. #if defined(PKG_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 rt_uint32_t get_page(uint32_t addr)
  26. {
  27. rt_uint32_t page = 0;
  28. page = RT_ALIGN_DOWN(addr, FLASH_PAGE_SIZE);
  29. return page;
  30. }
  31. /**
  32. * @brief 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 at32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, rt_uint32_t size)
  42. {
  43. rt_uint32_t i;
  44. if ((addr + size) > AT32_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. * @brief 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 at32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, rt_uint32_t size)
  67. {
  68. rt_err_t result = RT_EOK;
  69. rt_uint32_t end_addr = addr + size;
  70. if (addr % 4 != 0)
  71. {
  72. LOG_E("write addr must be 4-byte alignment");
  73. return -RT_EINVAL;
  74. }
  75. if ((end_addr) > AT32_FLASH_END_ADDRESS)
  76. {
  77. LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
  78. return -RT_EINVAL;
  79. }
  80. flash_unlock();
  81. while (addr < end_addr)
  82. {
  83. if (flash_word_program(addr, *((rt_uint32_t *)buf)) == FLASH_OPERATE_DONE)
  84. {
  85. if (*(rt_uint32_t *)addr != *(rt_uint32_t *)buf)
  86. {
  87. result = -RT_ERROR;
  88. break;
  89. }
  90. addr += 4;
  91. buf += 4;
  92. }
  93. else
  94. {
  95. result = -RT_ERROR;
  96. break;
  97. }
  98. }
  99. flash_lock();
  100. if (result != RT_EOK)
  101. {
  102. return result;
  103. }
  104. return size;
  105. }
  106. /**
  107. * @brief erase data on flash .
  108. * @note this operation is irreversible.
  109. * @note this operation's units is different which on many chips.
  110. *
  111. * @param addr flash address
  112. * @param size erase bytes size
  113. *
  114. * @return result
  115. */
  116. int at32_flash_erase(rt_uint32_t addr, rt_uint32_t size)
  117. {
  118. rt_err_t result = RT_EOK;
  119. rt_uint32_t end_addr = addr + size;
  120. rt_uint32_t page_addr = 0;
  121. flash_unlock();
  122. if ((end_addr) > AT32_FLASH_END_ADDRESS)
  123. {
  124. LOG_E("erase outrange flash size! addr is (0x%p)", (void *)(addr + size));
  125. return -RT_EINVAL;
  126. }
  127. while(addr < end_addr)
  128. {
  129. page_addr = get_page(addr);
  130. if(flash_sector_erase(page_addr) != FLASH_OPERATE_DONE)
  131. {
  132. result = -RT_ERROR;
  133. goto __exit;
  134. }
  135. addr += FLASH_PAGE_SIZE;
  136. }
  137. flash_lock();
  138. __exit:
  139. if(result != RT_EOK)
  140. {
  141. return result;
  142. }
  143. return size;
  144. }
  145. #if defined(PKG_USING_FAL)
  146. static int fal_flash_read(long offset, rt_uint8_t *buf, rt_uint32_t size);
  147. static int fal_flash_write(long offset, const rt_uint8_t *buf, rt_uint32_t size);
  148. static int fal_flash_erase(long offset, rt_uint32_t size);
  149. const struct fal_flash_dev at32_onchip_flash =
  150. {
  151. "onchip_flash",
  152. AT32_FLASH_START_ADRESS,
  153. AT32_FLASH_SIZE,
  154. FLASH_PAGE_SIZE,
  155. {
  156. NULL,
  157. fal_flash_read,
  158. fal_flash_write,
  159. fal_flash_erase
  160. }
  161. };
  162. static int fal_flash_read(long offset, rt_uint8_t *buf, rt_uint32_t size)
  163. {
  164. return at32_flash_read(at32_onchip_flash.addr + offset, buf, size);
  165. }
  166. static int fal_flash_write(long offset, const rt_uint8_t *buf, rt_uint32_t size)
  167. {
  168. return at32_flash_write(at32_onchip_flash.addr + offset, buf, size);
  169. }
  170. static int fal_flash_erase(long offset, rt_uint32_t size)
  171. {
  172. return at32_flash_erase(at32_onchip_flash.addr + offset, size);
  173. }
  174. #endif
  175. #endif /* BSP_USING_ON_CHIP_FLASH */