nrfx_nvmc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /**
  2. * Copyright (c) 2019 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRFX_NVMC_H__
  41. #define NRFX_NVMC_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_nvmc.h>
  44. #include <hal/nrf_ficr.h>
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @defgroup nrfx_nvmc NVMC driver
  50. * @{
  51. * @ingroup nrf_nvmc
  52. * @brief Non-Volatile Memory Controller (NVMC) peripheral driver.
  53. */
  54. /**
  55. * @brief Function for erasing a page in flash.
  56. *
  57. * This function blocks until the erase operation finishes.
  58. *
  59. * @note Depending on the source of the code being executed,
  60. * the CPU may be halted during the operation.
  61. * Refer to the Product Specification for more information.
  62. *
  63. * @param address Address of the first word in the page to erase.
  64. *
  65. * @retval NRFX_SUCCESS Page erase complete.
  66. * @retval NRFX_ERROR_INVALID_ADDR Address is not aligned to the size of the page.
  67. */
  68. nrfx_err_t nrfx_nvmc_page_erase(uint32_t address);
  69. /**
  70. * @brief Function for erasing the user information configuration register (UICR).
  71. *
  72. * @note Depending on the source of the code being executed,
  73. * the CPU may be halted during the operation.
  74. * Refer to the Product Specification for more information.
  75. *
  76. * @retval NRFX_SUCCESS UICR has been successfully erased.
  77. * @retval NRFX_ERROR_NOT_SUPPORTED UICR erase is not supported.
  78. */
  79. nrfx_err_t nrfx_nvmc_uicr_erase(void);
  80. /**
  81. * @brief Function for erasing the whole flash memory.
  82. *
  83. * @note All user code and UICR will be erased.
  84. */
  85. void nrfx_nvmc_all_erase(void);
  86. #if defined(NRF_NVMC_PARTIAL_ERASE_PRESENT)
  87. /**
  88. * @brief Function for initiating a complete page erase split into parts (also known as partial erase).
  89. *
  90. * This function initiates a partial erase with the specified duration.
  91. * To execute each part of the partial erase, use @ref nrfx_nvmc_page_partial_erase_continue.
  92. *
  93. * @param address Address of the first word in the page to erase.
  94. * @param duration_ms Time in milliseconds that each partial erase will take.
  95. *
  96. * @retval NRFX_SUCCESS Partial erase started.
  97. * @retval NRFX_ERROR_INVALID_ADDR Address is not aligned to the size of the page.
  98. *
  99. * @sa nrfx_nvmc_page_partial_erase_continue()
  100. */
  101. nrfx_err_t nrfx_nvmc_page_partial_erase_init(uint32_t address, uint32_t duration_ms);
  102. /**
  103. * @brief Function for performing a part of the complete page erase (also known as partial erase).
  104. *
  105. * This function must be called several times to erase the whole page, once for each erase part.
  106. *
  107. * @note The actual time needed to perform each part of the page erase is longer than the partial
  108. * erase duration specified in the call to @ref nrfx_nvmc_page_partial_erase_init,
  109. * since the NVMC peripheral needs certain additional amount of time to handle the process.
  110. * For details regarding this additional time, see the "Electrical specification" section
  111. * for the NVMC peripheral in the Product Specification.
  112. *
  113. * @note Using a page that was not completely erased leads to undefined behavior.
  114. * Depending on the source of the code being executed,
  115. * the CPU may be halted during the operation.
  116. * Refer to the Product Specification for more information.
  117. *
  118. * @retval true Partial erase finished.
  119. * @retval false Partial erase not finished.
  120. * Call the function again to process the next part.
  121. */
  122. bool nrfx_nvmc_page_partial_erase_continue(void);
  123. #endif // defined(NRF_NVMC_PARTIAL_ERASE_PRESENT)
  124. /**
  125. * @brief Function for checking whether a byte is writable at the specified address.
  126. *
  127. * The NVMC is only able to write '0' to bits in the flash that are erased (set to '1').
  128. * It cannot rewrite a bit back to '1'. This function checks if the value currently
  129. * residing at the specified address can be transformed to the desired value
  130. * without any '0' to '1' transitions.
  131. *
  132. * @param address Address to be checked.
  133. * @param value Value to be checked.
  134. *
  135. * @retval true Byte can be written at the specified address.
  136. * @retval false Byte cannot be written at the specified address.
  137. * Erase the page or change the address.
  138. */
  139. bool nrfx_nvmc_byte_writable_check(uint32_t address, uint8_t value);
  140. /**
  141. * @brief Function for writing a single byte to flash.
  142. *
  143. * To determine if the flash write has been completed, use @ref nrfx_nvmc_write_done_check().
  144. *
  145. * @note Depending on the source of the code being executed,
  146. * the CPU may be halted during the operation.
  147. * Refer to the Product Specification for more information.
  148. *
  149. * @param address Address to write to.
  150. * @param value Value to write.
  151. */
  152. void nrfx_nvmc_byte_write(uint32_t address, uint8_t value);
  153. /**
  154. * @brief Function for checking whether a word is writable at the specified address.
  155. *
  156. * The NVMC is only able to write '0' to bits in the Flash that are erased (set to '1').
  157. * It cannot rewrite a bit back to '1'. This function checks if the value currently
  158. * residing at the specified address can be transformed to the desired value
  159. * without any '0' to '1' transitions.
  160. *
  161. * @param address Address to be checked. Must be word-aligned.
  162. * @param value Value to be checked.
  163. *
  164. * @retval true Word can be written at the specified address.
  165. * @retval false Word cannot be written at the specified address.
  166. * Erase page or change address.
  167. */
  168. bool nrfx_nvmc_word_writable_check(uint32_t address, uint32_t value);
  169. /**
  170. * @brief Function for writing a 32-bit word to flash.
  171. *
  172. * To determine if the flash write has been completed, use @ref nrfx_nvmc_write_done_check().
  173. *
  174. * @note Depending on the source of the code being executed,
  175. * the CPU may be halted during the operation.
  176. * Refer to the Product Specification for more information.
  177. *
  178. * @param address Address to write to. Must be word-aligned.
  179. * @param value Value to write.
  180. */
  181. void nrfx_nvmc_word_write(uint32_t address, uint32_t value);
  182. /**
  183. * @brief Function for writing consecutive bytes to flash.
  184. *
  185. * To determine if the last flash write has been completed, use @ref nrfx_nvmc_write_done_check().
  186. *
  187. * @note Depending on the source of the code being executed,
  188. * the CPU may be halted during the operation.
  189. * Refer to the Product Specification for more information.
  190. *
  191. * @param address Address to write to.
  192. * @param src Pointer to the data to copy from.
  193. * @param num_bytes Number of bytes to write.
  194. */
  195. void nrfx_nvmc_bytes_write(uint32_t address, void const * src, uint32_t num_bytes);
  196. /**
  197. * @brief Function for writing consecutive words to flash.
  198. *
  199. * To determine if the last flash write has been completed, use @ref nrfx_nvmc_write_done_check().
  200. *
  201. * @note Depending on the source of the code being executed,
  202. * the CPU may be halted during the operation.
  203. * Refer to the Product Specification for more information.
  204. *
  205. * @param address Address to write to. Must be word-aligned.
  206. * @param src Pointer to data to copy from. Must be word-aligned.
  207. * @param num_words Number of words to write.
  208. */
  209. void nrfx_nvmc_words_write(uint32_t address, void const * src, uint32_t num_words);
  210. /**
  211. * @brief Function for getting the total flash size in bytes.
  212. *
  213. * @return Flash total size in bytes.
  214. */
  215. uint32_t nrfx_nvmc_flash_size_get(void);
  216. /**
  217. * @brief Function for getting the flash page size in bytes.
  218. *
  219. * @return Flash page size in bytes.
  220. */
  221. uint32_t nrfx_nvmc_flash_page_size_get(void);
  222. /**
  223. * @brief Function for getting the flash page count.
  224. *
  225. * @return Flash page count.
  226. */
  227. uint32_t nrfx_nvmc_flash_page_count_get(void);
  228. /**
  229. * @brief Function for checking if the last flash write has been completed.
  230. *
  231. * @retval true Last write completed successfully.
  232. * @retval false Last write is still in progress.
  233. */
  234. __STATIC_INLINE bool nrfx_nvmc_write_done_check(void);
  235. #if defined(NRF_NVMC_ICACHE_PRESENT)
  236. /**
  237. * @brief Function for enabling the Instruction Cache (ICache).
  238. *
  239. * Enabling ICache reduces the amount of accesses to flash memory,
  240. * which can boost performance and lower power consumption.
  241. */
  242. __STATIC_INLINE void nrfx_nvmc_icache_enable(void);
  243. /** @brief Function for disabling ICache. */
  244. __STATIC_INLINE void nrfx_nvmc_icache_disable(void);
  245. #endif // defined(NRF_NVMC_ICACHE_PRESENT)
  246. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  247. __STATIC_INLINE bool nrfx_nvmc_write_done_check(void)
  248. {
  249. return nrf_nvmc_ready_check(NRF_NVMC);
  250. }
  251. #if defined(NRF_NVMC_ICACHE_PRESENT)
  252. __STATIC_INLINE void nrfx_nvmc_icache_enable(void)
  253. {
  254. nrf_nvmc_icache_config_set(NRF_NVMC, NRF_NVMC_ICACHE_ENABLE_WITH_PROFILING);
  255. }
  256. __STATIC_INLINE void nrfx_nvmc_icache_disable(void)
  257. {
  258. nrf_nvmc_icache_config_set(NRF_NVMC, NRF_NVMC_ICACHE_DISABLE);
  259. }
  260. #endif // defined(NRF_NVMC_ICACHE_PRESENT)
  261. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  262. /** @} */
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #endif // NRF_NVMC_H__