drv_flash.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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-04-29 supperthomas first version
  9. */
  10. #include "board.h"
  11. #include <nrfx.h>
  12. #include "nrfx_nvmc.h"
  13. #ifdef BSP_USING_ON_CHIP_FLASH
  14. #if defined(PKG_USING_FAL)
  15. #include "fal.h"
  16. #endif
  17. #include <rtdbg.h>
  18. #define LOG_TAG "drv.flash"
  19. /**
  20. * @brief Gets the page of a given address
  21. * @param Addr: Address of the FLASH Memory
  22. * @retval The page of a given address
  23. */
  24. static uint32_t GetPage(uint32_t Addr)
  25. {
  26. uint32_t page = 0;
  27. if (Addr < (MCU_FLASH_START_ADDRESS + MCU_FLASH_SIZE))
  28. {
  29. page = (Addr - MCU_FLASH_START_ADDRESS) / MCU_FLASH_PAGE_SIZE;
  30. }
  31. else
  32. {
  33. return 0xffffffff;
  34. }
  35. return page;
  36. }
  37. /**
  38. * Read data from flash.
  39. * @note This operation's units is word.
  40. *
  41. * @param addr flash address
  42. * @param buf buffer to store read data
  43. * @param size read bytes size
  44. *
  45. * @return result
  46. */
  47. int mcu_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  48. {
  49. size_t i;
  50. if ((addr + size) > MCU_FLASH_END_ADDRESS)
  51. {
  52. LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
  53. return -RT_EINVAL;
  54. }
  55. for (i = 0; i < size; i++, buf++, addr++)
  56. {
  57. *buf = *(rt_uint8_t *) addr;
  58. }
  59. return size;
  60. }
  61. /**
  62. * Write data to flash.
  63. * @note This operation's units is word.
  64. * @note This operation must after erase. @see flash_erase.
  65. *
  66. * @param addr flash address
  67. * @param buf the write data buffer
  68. * @param size write bytes size
  69. *
  70. * @return result
  71. */
  72. int mcu_flash_write(rt_uint32_t addr, const uint8_t *buf, size_t size)
  73. {
  74. if ((addr + size) > MCU_FLASH_END_ADDRESS)
  75. {
  76. LOG_E("ERROR: write outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  77. return -RT_EINVAL;
  78. }
  79. if (addr % 4 != 0)
  80. {
  81. LOG_E("write addr should be 4-byte alignment");
  82. //4byte write
  83. //else byts
  84. return -RT_EINVAL;
  85. }
  86. if (size < 1)
  87. {
  88. return -RT_ERROR;
  89. }
  90. if (size % 4 != 0)
  91. {
  92. nrfx_nvmc_bytes_write(addr, buf, size);
  93. return size;
  94. }
  95. else
  96. {
  97. nrfx_nvmc_words_write(addr, buf, size / 4);
  98. return size;
  99. }
  100. }
  101. /**
  102. * Erase data on flash.
  103. * @note This operation is irreversible.
  104. * @note This operation's units is different which on many chips.
  105. *
  106. * @param addr flash address
  107. * @param size erase bytes size
  108. *
  109. * @return result
  110. */
  111. int mcu_flash_erase(rt_uint32_t addr, size_t size)
  112. {
  113. nrfx_err_t result = NRFX_SUCCESS;
  114. uint32_t FirstPage = 0, NbOfPages = 0;
  115. if ((addr + size) > MCU_FLASH_END_ADDRESS)
  116. {
  117. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  118. return -RT_EINVAL;
  119. }
  120. FirstPage = GetPage(addr);
  121. NbOfPages = GetPage(addr + size - 1) - FirstPage + 1;
  122. for (int i = 0; i < NbOfPages ; i++)
  123. {
  124. result = nrfx_nvmc_page_erase((FirstPage + i) * MCU_FLASH_PAGE_SIZE);
  125. if (NRFX_SUCCESS != result)
  126. {
  127. LOG_E("ERROR: erase flash page %d ! error code is (%x)\n", FirstPage + i, result);
  128. return -RT_EINVAL;
  129. }
  130. }
  131. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, NbOfPages * MCU_FLASH_PAGE_SIZE);
  132. return size;
  133. }
  134. #if defined(PKG_USING_FAL)
  135. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
  136. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
  137. static int fal_flash_erase(long offset, size_t size);
  138. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
  139. {
  140. return mcu_flash_read(mcu_onchip_flash.addr + offset, buf, size);
  141. }
  142. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  143. {
  144. return mcu_flash_write(mcu_onchip_flash.addr + offset, buf, size);
  145. }
  146. static int fal_flash_erase(long offset, size_t size)
  147. {
  148. return mcu_flash_erase(mcu_onchip_flash.addr + offset, size);
  149. }
  150. const struct fal_flash_dev mcu_onchip_flash =
  151. {
  152. .name = ON_CHIP_FLASH_DEV_NAME,
  153. .addr = MCU_FLASH_START_ADDRESS,
  154. .len = MCU_FLASH_SIZE,
  155. .blk_size = MCU_FLASH_PAGE_SIZE,
  156. .ops = {NULL, fal_flash_read, fal_flash_write, fal_flash_erase},
  157. .write_gran = 8
  158. };
  159. #endif
  160. #endif /* BSP_USING_ON_CHIP_FLASH */