nrf_nvmc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /**
  2. * Copyright (c) 2012 - 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 NRF_NVMC_H__
  41. #define NRF_NVMC_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_nvmc_hal_deprecated NVMC HAL (deprecated)
  48. * @{
  49. * @ingroup nrf_nvmc
  50. * @brief Hardware access layer (HAL) for managing the Non-Volatile Memory Controller (NVMC) peripheral.
  51. *
  52. * This driver allows writing to the non-volatile memory (NVM) regions
  53. * of the chip. To write to NVM, the controller must be powered
  54. * on and the relevant page must be erased.
  55. */
  56. /**
  57. * @brief Erase a page in flash. This is required before writing to any address in the page.
  58. *
  59. * @param address Start address of the page.
  60. */
  61. void nrf_nvmc_page_erase(uint32_t address);
  62. /**
  63. * @brief Write a single byte to flash.
  64. *
  65. * The function reads the word containing the byte, and then rewrites the entire word.
  66. *
  67. * @param address Address to write to.
  68. * @param value Value to write.
  69. */
  70. void nrf_nvmc_write_byte(uint32_t address , uint8_t value);
  71. /**
  72. * @brief Write a 32-bit word to flash.
  73. *
  74. * @param address Address to write to.
  75. * @param value Value to write.
  76. */
  77. void nrf_nvmc_write_word(uint32_t address, uint32_t value);
  78. /**
  79. * @brief Write consecutive bytes to flash.
  80. *
  81. * @param address Address to write to.
  82. * @param src Pointer to data to copy from.
  83. * @param num_bytes Number of bytes in src to write.
  84. */
  85. void nrf_nvmc_write_bytes(uint32_t address, const uint8_t * src, uint32_t num_bytes);
  86. /**
  87. * @brief Write consecutive words to flash.
  88. *
  89. * @param address Address to write to.
  90. * @param src Pointer to data to copy from.
  91. * @param num_words Number of words in src to write.
  92. */
  93. void nrf_nvmc_write_words(uint32_t address, const uint32_t * src, uint32_t num_words);
  94. /** @} */
  95. /**
  96. * @defgroup nrf_nvmc_hal NVMC HAL
  97. * @{
  98. * @ingroup nrf_nvmc
  99. * @brief Hardware access layer (HAL) for managing the Non-Volatile Memory Controller (NVMC) peripheral.
  100. */
  101. #if defined(NVMC_ICACHECNF_CACHEEN_Msk) || defined(__NRFX_DOXYGEN__)
  102. /** @brief Symbol indicating whether Instruction Cache (ICache) is present. */
  103. #define NRF_NVMC_ICACHE_PRESENT
  104. #endif
  105. #if defined(NVMC_ERASEPAGEPARTIALCFG_DURATION_Msk) || defined(__NRFX_DOXYGEN__)
  106. /** @brief Symbol indicating whether the option of page partial erase is present. */
  107. #define NRF_NVMC_PARTIAL_ERASE_PRESENT
  108. #endif
  109. /** @brief NVMC modes. */
  110. typedef enum
  111. {
  112. NRF_NVMC_MODE_READONLY = NVMC_CONFIG_WEN_Ren, ///< NVMC in read-only mode.
  113. NRF_NVMC_MODE_WRITE = NVMC_CONFIG_WEN_Wen, ///< NVMC in read and write mode.
  114. NRF_NVMC_MODE_ERASE = NVMC_CONFIG_WEN_Een, ///< NVMC in read and erase mode.
  115. #if defined(NVMC_CONFIG_WEN_PEen)
  116. NRF_NVMC_MODE_PARTIAL_ERASE = NVMC_CONFIG_WEN_PEen ///< NVMC in read and partial erase mode.
  117. #endif
  118. } nrf_nvmc_mode_t;
  119. #if defined(NVMC_CONFIGNS_WEN_Msk) || defined(__NRFX_DOXYGEN__)
  120. /** @brief Non-secure NVMC modes. */
  121. typedef enum
  122. {
  123. NRF_NVMC_NS_MODE_READONLY = NVMC_CONFIGNS_WEN_Ren, ///< Non-secure NVMC in read-only mode.
  124. NRF_NVMC_NS_MODE_WRITE = NVMC_CONFIGNS_WEN_Wen, ///< Non-secure NVMC in read and write mode.
  125. NRF_NVMC_NS_MODE_ERASE = NVMC_CONFIGNS_WEN_Een, ///< Non-secure NVMC in read and erase mode.
  126. } nrf_nvmc_ns_mode_t;
  127. #endif
  128. #if defined(NRF_NVMC_ICACHE_PRESENT)
  129. /** @brief NVMC ICache configuration. */
  130. typedef enum
  131. {
  132. NRF_NVMC_ICACHE_DISABLE = NVMC_ICACHECNF_CACHEEN_Disabled, ///< Instruction Cache disabled.
  133. NRF_NVMC_ICACHE_ENABLE = NVMC_ICACHECNF_CACHEEN_Enabled, ///< Instruction Cache enabled.
  134. NRF_NVMC_ICACHE_ENABLE_WITH_PROFILING = NVMC_ICACHECNF_CACHEEN_Enabled | ///< Instruction Cache with cache profiling enabled.
  135. NVMC_ICACHECNF_CACHEPROFEN_Msk
  136. } nrf_nvmc_icache_config_t;
  137. #endif // defined(NRF_NVMC_ICACHE_PRESENT)
  138. /**
  139. * @brief Function for checking if NVMC is ready to perform write or erase operation.
  140. *
  141. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  142. *
  143. * @retval true NVMC can perform write or erase.
  144. * @retval false NVMC is busy and cannot perform next operation yet.
  145. */
  146. __STATIC_INLINE bool nrf_nvmc_ready_check(NRF_NVMC_Type const * p_reg);
  147. #if defined(NVMC_READYNEXT_READYNEXT_Msk) || defined(__NRFX_DOXYGEN__)
  148. /**
  149. * @brief Function for checking if NVMC is ready to accept the next write operation.
  150. *
  151. * NVM writing time can be reduced by using this function.
  152. *
  153. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  154. *
  155. * @retval true NVMC can accept the next write. It will be buffered and will be taken
  156. * into account as soon as the ongoing write operation is completed.
  157. * @retval false NVMC is busy and cannot accept the next write yet.
  158. */
  159. __STATIC_INLINE bool nrf_nvmc_write_ready_check(NRF_NVMC_Type const * p_reg);
  160. #endif // defined(NVMC_READYNEXT_READYNEXT_Msk) || defined(__NRFX_DOXYGEN__)
  161. /**
  162. * @brief Function for setting the NVMC mode.
  163. *
  164. * Only activate erase and write modes when they are actively used.
  165. * If Instruction Cache (ICache) is present, enabling write or erase will
  166. * invalidate the cache and keep it invalidated.
  167. *
  168. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  169. * @param[in] mode Desired operating mode for NVMC.
  170. */
  171. __STATIC_INLINE void nrf_nvmc_mode_set(NRF_NVMC_Type * p_reg,
  172. nrf_nvmc_mode_t mode);
  173. #if defined(NVMC_CONFIGNS_WEN_Msk) || defined(__NRFX_DOXYGEN__)
  174. /**
  175. * @brief Function for setting the NVMC mode for non-secure Flash page operations.
  176. *
  177. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  178. * @param[in] mode Desired operating mode for NVMC.
  179. */
  180. __STATIC_INLINE void nrf_nvmc_nonsecure_mode_set(NRF_NVMC_Type * p_reg,
  181. nrf_nvmc_ns_mode_t mode);
  182. #endif
  183. /**
  184. * @brief Function for starting a single page erase in the Flash memory.
  185. *
  186. * The NVMC mode must be correctly configured with @ref nrf_nvmc_mode_set
  187. * before starting the erase operation.
  188. *
  189. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  190. * @param[in] page_addr Address of the first word of the page to erase.
  191. */
  192. __STATIC_INLINE void nrf_nvmc_page_erase_start(NRF_NVMC_Type * p_reg,
  193. uint32_t page_addr);
  194. #if defined(NVMC_ERASEUICR_ERASEUICR_Msk) || defined(__NRFX_DOXYGEN__)
  195. /**
  196. * @brief Function for starting the user information configuration registers (UICR) erase.
  197. *
  198. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  199. */
  200. __STATIC_INLINE void nrf_nvmc_uicr_erase_start(NRF_NVMC_Type * p_reg);
  201. #endif
  202. /**
  203. * @brief Function for starting the erase of the whole NVM, including UICR.
  204. *
  205. * This function purges all user code.
  206. *
  207. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  208. */
  209. __STATIC_INLINE void nrf_nvmc_erase_all_start(NRF_NVMC_Type * p_reg);
  210. #if defined(NRF_NVMC_PARTIAL_ERASE_PRESENT)
  211. /**
  212. * @brief Function for configuring the page partial erase duration in milliseconds.
  213. *
  214. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  215. * @param[in] duration_ms Page partial erase duration in milliseconds.
  216. */
  217. __STATIC_INLINE void nrf_nvmc_partial_erase_duration_set(NRF_NVMC_Type * p_reg,
  218. uint32_t duration_ms);
  219. /**
  220. * @brief Function for getting the current setting for the page partial erase duration.
  221. *
  222. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  223. *
  224. * @retval Interval duration setting in milliseconds.
  225. */
  226. __STATIC_INLINE uint32_t nrf_nvmc_partial_erase_duration_get(NRF_NVMC_Type const * p_reg);
  227. /**
  228. * @brief Function for starting a partial erase operation.
  229. *
  230. * It must be called successively until the page erase time is reached.
  231. *
  232. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  233. * @param[in] page_addr Address of the first word of the page to erase.
  234. */
  235. __STATIC_INLINE void nrf_nvmc_page_partial_erase_start(NRF_NVMC_Type * p_reg,
  236. uint32_t page_addr);
  237. #endif // defined(NRF_NVMC_PARTIAL_ERASE_PRESENT)
  238. #if defined(NRF_NVMC_ICACHE_PRESENT)
  239. /**
  240. * @brief Function for applying the Instruction Cache (ICache) configuration.
  241. *
  242. * Enabling the cache can increase CPU performance and reduce power
  243. * consumption by reducing the number of wait cycles and the number
  244. * of flash accesses.
  245. *
  246. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  247. * @param[in] config ICache configuration.
  248. */
  249. __STATIC_INLINE void nrf_nvmc_icache_config_set(NRF_NVMC_Type * p_reg,
  250. nrf_nvmc_icache_config_t config);
  251. /**
  252. * @brief Function for checking if ICache is enabled.
  253. *
  254. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  255. *
  256. * @retval true ICache enabled.
  257. * @retval false ICache disabled.
  258. */
  259. __STATIC_INLINE bool nrf_nvmc_icache_enable_check(NRF_NVMC_Type const * p_reg);
  260. /**
  261. * @brief Function for checking if the ICache profiling option is enabled.
  262. *
  263. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  264. *
  265. * @retval true ICache profiling enabled.
  266. * @retval false ICache profiling disabled.
  267. */
  268. __STATIC_INLINE bool nrf_nvmc_icache_profiling_enable_check(NRF_NVMC_Type const * p_reg);
  269. /**
  270. * @brief Function for getting the number of ICache hits.
  271. *
  272. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  273. *
  274. * @retval Number of the ICache hits.
  275. */
  276. __STATIC_INLINE uint32_t nrf_nvmc_icache_hit_get(NRF_NVMC_Type const * p_reg);
  277. /**
  278. * @brief Function for getting the number of ICache misses.
  279. *
  280. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  281. *
  282. * @retval Number of the ICache misses.
  283. */
  284. __STATIC_INLINE uint32_t nrf_nvmc_icache_miss_get(NRF_NVMC_Type const * p_reg);
  285. /**
  286. * @brief Function for resetting the ICache hit and miss counters.
  287. *
  288. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  289. */
  290. __STATIC_INLINE void nrf_nvmc_icache_hit_miss_reset(NRF_NVMC_Type * p_reg);
  291. #endif // defined(NRF_NVMC_ICACHE_PRESENT)
  292. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  293. __STATIC_INLINE bool nrf_nvmc_ready_check(NRF_NVMC_Type const * p_reg)
  294. {
  295. return (bool)(p_reg->READY & NVMC_READY_READY_Msk);
  296. }
  297. #if defined(NVMC_READYNEXT_READYNEXT_Msk)
  298. __STATIC_INLINE bool nrf_nvmc_write_ready_check(NRF_NVMC_Type const * p_reg)
  299. {
  300. return (bool)(p_reg->READYNEXT & NVMC_READYNEXT_READYNEXT_Msk);
  301. }
  302. #endif
  303. __STATIC_INLINE void nrf_nvmc_mode_set(NRF_NVMC_Type * p_reg,
  304. nrf_nvmc_mode_t mode)
  305. {
  306. p_reg->CONFIG = (uint32_t)mode;
  307. }
  308. #if defined(NVMC_CONFIGNS_WEN_Msk)
  309. __STATIC_INLINE void nrf_nvmc_nonsecure_mode_set(NRF_NVMC_Type * p_reg,
  310. nrf_nvmc_ns_mode_t mode)
  311. {
  312. p_reg->CONFIGNS = (uint32_t)mode;
  313. }
  314. #endif
  315. __STATIC_INLINE void nrf_nvmc_page_erase_start(NRF_NVMC_Type * p_reg,
  316. uint32_t page_addr)
  317. {
  318. #if defined(NRF51)
  319. /* On nRF51, the code area can be divided into two regions: CR0 and CR1.
  320. * The length of CR0 is specified in the CLENR0 register of UICR.
  321. * If CLENR0 contains the 0xFFFFFFFF value, CR0 is not set.
  322. * Moreover, the page from CR0 can be written or erased only from code
  323. * running in CR0.*/
  324. uint32_t cr0_len = NRF_UICR->CLENR0 == 0xFFFFFFFF ? 0 : NRF_UICR->CLENR0;
  325. if (page_addr < cr0_len)
  326. {
  327. p_reg->ERASEPCR0 = page_addr;
  328. }
  329. else
  330. {
  331. p_reg->ERASEPCR1 = page_addr;
  332. }
  333. #elif defined(NRF52_SERIES)
  334. p_reg->ERASEPAGE = page_addr;
  335. #elif defined(NRF9160_XXAA)
  336. *(volatile uint32_t *)page_addr = 0xFFFFFFFF;
  337. (void)p_reg;
  338. #else
  339. #error "Unknown device."
  340. #endif
  341. }
  342. #if defined(NVMC_ERASEUICR_ERASEUICR_Msk)
  343. __STATIC_INLINE void nrf_nvmc_uicr_erase_start(NRF_NVMC_Type * p_reg)
  344. {
  345. p_reg->ERASEUICR = 1;
  346. }
  347. #endif
  348. __STATIC_INLINE void nrf_nvmc_erase_all_start(NRF_NVMC_Type * p_reg)
  349. {
  350. p_reg->ERASEALL = 1;
  351. }
  352. #if defined(NRF_NVMC_PARTIAL_ERASE_PRESENT)
  353. __STATIC_INLINE void nrf_nvmc_partial_erase_duration_set(NRF_NVMC_Type * p_reg,
  354. uint32_t duration_ms)
  355. {
  356. p_reg->ERASEPAGEPARTIALCFG = duration_ms;
  357. }
  358. __STATIC_INLINE uint32_t nrf_nvmc_partial_erase_duration_get(NRF_NVMC_Type const * p_reg)
  359. {
  360. return p_reg->ERASEPAGEPARTIALCFG;
  361. }
  362. __STATIC_INLINE void nrf_nvmc_page_partial_erase_start(NRF_NVMC_Type * p_reg,
  363. uint32_t page_addr)
  364. {
  365. #if defined(NVMC_ERASEPAGEPARTIAL_ERASEPAGEPARTIAL_Msk)
  366. p_reg->ERASEPAGEPARTIAL = page_addr;
  367. #elif defined(NRF9160_XXAA)
  368. nrf_nvmc_page_erase_start(p_reg, page_addr);
  369. #else
  370. #error "Unknown device."
  371. #endif
  372. }
  373. #endif // defined(NRF_NVMC_PARTIAL_ERASE_PRESENT)
  374. #if defined(NRF_NVMC_ICACHE_PRESENT)
  375. __STATIC_INLINE void nrf_nvmc_icache_config_set(NRF_NVMC_Type * p_reg,
  376. nrf_nvmc_icache_config_t config)
  377. {
  378. p_reg->ICACHECNF = (uint32_t)config;
  379. }
  380. __STATIC_INLINE bool nrf_nvmc_icache_enable_check(NRF_NVMC_Type const * p_reg)
  381. {
  382. return (bool)(p_reg->ICACHECNF & NVMC_ICACHECNF_CACHEEN_Msk);
  383. }
  384. __STATIC_INLINE bool nrf_nvmc_icache_profiling_enable_check(NRF_NVMC_Type const * p_reg)
  385. {
  386. return (bool)(p_reg->ICACHECNF & NVMC_ICACHECNF_CACHEPROFEN_Msk);
  387. }
  388. __STATIC_INLINE uint32_t nrf_nvmc_icache_hit_get(NRF_NVMC_Type const * p_reg)
  389. {
  390. return p_reg->IHIT;
  391. }
  392. __STATIC_INLINE uint32_t nrf_nvmc_icache_miss_get(NRF_NVMC_Type const * p_reg)
  393. {
  394. return p_reg->IMISS;
  395. }
  396. __STATIC_INLINE void nrf_nvmc_icache_hit_miss_reset(NRF_NVMC_Type * p_reg)
  397. {
  398. p_reg->IHIT = 0;
  399. p_reg->IMISS = 0;
  400. }
  401. #endif // defined(NRF_NVMC_ICACHE_PRESENT)
  402. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  403. /** @} */
  404. #ifdef __cplusplus
  405. }
  406. #endif
  407. #endif // NRF_NVMC_H__