nrf_mwu.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * Copyright (c) 2019, 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_MWU_H__
  41. #define NRF_MWU_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_mwu_hal MWU HAL
  48. * @{
  49. * @ingroup nrf_mwu
  50. * @brief Hardware access layer for managing the Memory Watch Unit (MWU) peripheral.
  51. */
  52. /** @brief MWU events. */
  53. typedef enum
  54. {
  55. NRF_MWU_EVENT_REGION0_WRITE = offsetof(NRF_MWU_Type, EVENTS_REGION[0].WA), ///< Write access to region 0 detected.
  56. NRF_MWU_EVENT_REGION0_READ = offsetof(NRF_MWU_Type, EVENTS_REGION[0].RA), ///< Read access to region 0 detected.
  57. NRF_MWU_EVENT_REGION1_WRITE = offsetof(NRF_MWU_Type, EVENTS_REGION[1].WA), ///< Write access to region 1 detected.
  58. NRF_MWU_EVENT_REGION1_READ = offsetof(NRF_MWU_Type, EVENTS_REGION[1].RA), ///< Read access to region 1 detected.
  59. NRF_MWU_EVENT_REGION2_WRITE = offsetof(NRF_MWU_Type, EVENTS_REGION[2].WA), ///< Write access to region 2 detected.
  60. NRF_MWU_EVENT_REGION2_READ = offsetof(NRF_MWU_Type, EVENTS_REGION[2].RA), ///< Read access to region 2 detected.
  61. NRF_MWU_EVENT_REGION3_WRITE = offsetof(NRF_MWU_Type, EVENTS_REGION[3].WA), ///< Write access to region 3 detected.
  62. NRF_MWU_EVENT_REGION3_READ = offsetof(NRF_MWU_Type, EVENTS_REGION[3].RA), ///< Read access to region 3 detected.
  63. NRF_MWU_EVENT_PREGION0_WRITE = offsetof(NRF_MWU_Type, EVENTS_PREGION[0].WA), ///< Write access to peripheral region 0 detected.
  64. NRF_MWU_EVENT_PREGION0_READ = offsetof(NRF_MWU_Type, EVENTS_PREGION[0].RA), ///< Read access to peripheral region 0 detected.
  65. NRF_MWU_EVENT_PREGION1_WRITE = offsetof(NRF_MWU_Type, EVENTS_PREGION[1].WA), ///< Write access to peripheral region 1 detected.
  66. NRF_MWU_EVENT_PREGION1_READ = offsetof(NRF_MWU_Type, EVENTS_PREGION[1].RA), ///< Read access to peripheral region 1 detected.
  67. } nrf_mwu_event_t;
  68. /** @brief MWU interrupt masks. */
  69. typedef enum
  70. {
  71. NRF_MWU_INT_REGION0_WRITE_MASK = MWU_INTEN_REGION0WA_Msk, ///< Interrupt on REGION[0].WA event.
  72. NRF_MWU_INT_REGION0_READ_MASK = MWU_INTEN_REGION0RA_Msk, ///< Interrupt on REGION[0].RA event.
  73. NRF_MWU_INT_REGION1_WRITE_MASK = MWU_INTEN_REGION1WA_Msk, ///< Interrupt on REGION[1].WA event.
  74. NRF_MWU_INT_REGION1_READ_MASK = MWU_INTEN_REGION1RA_Msk, ///< Interrupt on REGION[1].RA event.
  75. NRF_MWU_INT_REGION2_WRITE_MASK = MWU_INTEN_REGION2WA_Msk, ///< Interrupt on REGION[2].WA event.
  76. NRF_MWU_INT_REGION2_READ_MASK = MWU_INTEN_REGION2RA_Msk, ///< Interrupt on REGION[2].RA event.
  77. NRF_MWU_INT_REGION3_WRITE_MASK = MWU_INTEN_REGION3WA_Msk, ///< Interrupt on REGION[3].WA event.
  78. NRF_MWU_INT_REGION3_READ_MASK = MWU_INTEN_REGION3RA_Msk, ///< Interrupt on REGION[3].RA event.
  79. NRF_MWU_INT_PREGION0_WRITE_MASK = MWU_INTEN_PREGION0WA_Msk, ///< Interrupt on PREGION[0].WA event.
  80. NRF_MWU_INT_PREGION0_READ_MASK = MWU_INTEN_PREGION0RA_Msk, ///< Interrupt on PREGION[0].RA event.
  81. NRF_MWU_INT_PREGION1_WRITE_MASK = MWU_INTEN_PREGION1WA_Msk, ///< Interrupt on PREGION[1].WA event.
  82. NRF_MWU_INT_PREGION1_READ_MASK = MWU_INTEN_PREGION1RA_Msk, ///< Interrupt on PREGION[1].RA event.
  83. } nrf_mwu_int_mask_t;
  84. /** @brief MWU region watch masks. */
  85. typedef enum
  86. {
  87. NRF_MWU_WATCH_REGION0_WRITE = MWU_REGIONEN_RGN0WA_Msk, ///< Region 0 write access watch mask
  88. NRF_MWU_WATCH_REGION0_READ = MWU_REGIONEN_RGN0RA_Msk, ///< Region 0 read access watch mask
  89. NRF_MWU_WATCH_REGION1_WRITE = MWU_REGIONEN_RGN1WA_Msk, ///< Region 1 write access watch mask
  90. NRF_MWU_WATCH_REGION1_READ = MWU_REGIONEN_RGN1RA_Msk, ///< Region 1 read access watch mask
  91. NRF_MWU_WATCH_REGION2_WRITE = MWU_REGIONEN_RGN2WA_Msk, ///< Region 2 write access watch mask
  92. NRF_MWU_WATCH_REGION2_READ = MWU_REGIONEN_RGN2RA_Msk, ///< Region 2 read access watch mask
  93. NRF_MWU_WATCH_REGION3_WRITE = MWU_REGIONEN_RGN3WA_Msk, ///< Region 3 write access watch mask
  94. NRF_MWU_WATCH_REGION3_READ = MWU_REGIONEN_RGN3RA_Msk, ///< Region 3 read access watch mask
  95. NRF_MWU_WATCH_PREGION0_WRITE = MWU_REGIONEN_PRGN0WA_Msk, ///< Peripheral region 0 write access watch mask
  96. NRF_MWU_WATCH_PREGION0_READ = MWU_REGIONEN_PRGN0RA_Msk, ///< Peripheral region 0 read access watch mask
  97. NRF_MWU_WATCH_PREGION1_WRITE = MWU_REGIONEN_PRGN1WA_Msk, ///< Peripheral region 1 write access watch mask
  98. NRF_MWU_WATCH_PREGION1_READ = MWU_REGIONEN_PRGN1RA_Msk, ///< Peripheral region 1 read access watch mask
  99. } nrf_mwu_region_watch_t;
  100. /**
  101. * @brief Function for checking the state of a specific MWU event.
  102. *
  103. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  104. * @param[in] mwu_event Event to check.
  105. *
  106. * @retval true If the event is set.
  107. * @retval false If the event is not set.
  108. */
  109. __STATIC_INLINE bool nrf_mwu_event_check(NRF_MWU_Type const * p_reg,
  110. nrf_mwu_event_t mwu_event);
  111. /**
  112. * @brief Function for clearing a specific MWU event.
  113. *
  114. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  115. * @param[in] mwu_event Event to clear.
  116. */
  117. __STATIC_INLINE void nrf_mwu_event_clear(NRF_MWU_Type * p_reg,
  118. nrf_mwu_event_t mwu_event);
  119. /**
  120. * @brief Function for getting the address of a specific MWU event register.
  121. *
  122. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  123. * @param[in] mwu_event Requested event.
  124. *
  125. * @return Address of the specified event register.
  126. */
  127. __STATIC_INLINE uint32_t nrf_mwu_event_address_get(NRF_MWU_Type const * p_reg,
  128. nrf_mwu_event_t mwu_event);
  129. /**
  130. * @brief Function for enabling specified interrupts.
  131. *
  132. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  133. * @param[in] int_mask Interrupts to enable.
  134. */
  135. __STATIC_INLINE void nrf_mwu_int_enable(NRF_MWU_Type * p_reg, uint32_t int_mask);
  136. /**
  137. * @brief Function for retrieving the state of a specific interrupt.
  138. *
  139. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  140. * @param[in] mwu_int Interrupt to check.
  141. *
  142. * @retval true If the interrupt is enabled.
  143. * @retval false If the interrupt is not enabled.
  144. */
  145. __STATIC_INLINE bool nrf_mwu_int_enable_check(NRF_MWU_Type const * p_reg,
  146. nrf_mwu_int_mask_t mwu_int);
  147. /**
  148. * @brief Function for disabling specified interrupts.
  149. *
  150. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  151. * @param[in] int_mask Interrupts to disable.
  152. */
  153. __STATIC_INLINE void nrf_mwu_int_disable(NRF_MWU_Type * p_reg, uint32_t int_mask);
  154. /**
  155. * @brief Function for enabling specified non-maskable interrupts.
  156. *
  157. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  158. * @param[in] int_mask Interrupts to enable.
  159. */
  160. __STATIC_INLINE void nrf_mwu_nmi_enable(NRF_MWU_Type * p_reg, uint32_t int_mask);
  161. /**
  162. * @brief Function for retrieving the state of a specific non-maskable interrupt.
  163. *
  164. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  165. * @param[in] mwu_int Interrupt to check.
  166. *
  167. * @retval true If the interrupt is enabled.
  168. * @retval false If the interrupt is not enabled.
  169. */
  170. __STATIC_INLINE bool nrf_mwu_nmi_enable_check(NRF_MWU_Type const * p_reg,
  171. nrf_mwu_int_mask_t mwu_int);
  172. /**
  173. * @brief Function for disabling specified non-maskable interrupts.
  174. *
  175. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  176. * @param[in] int_mask Interrupts to disable.
  177. */
  178. __STATIC_INLINE void nrf_mwu_nmi_disable(NRF_MWU_Type * p_reg, uint32_t int_mask);
  179. /**
  180. * @brief Function for setting address range of the specified user region.
  181. *
  182. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  183. * @param[in] region_idx Region number to configure.
  184. * @param[in] start_addr Memory address defining the beginning of the region.
  185. * @param[in] end_addr Memory address defining the end of the region.
  186. */
  187. __STATIC_INLINE void nrf_mwu_user_region_range_set(NRF_MWU_Type * p_reg,
  188. uint8_t region_idx,
  189. uint32_t start_addr,
  190. uint32_t end_addr);
  191. /**
  192. * @brief Function for enabling memory access watch mechanism.
  193. *
  194. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  195. * @param[in] reg_watch_mask Mask that defines regions and access types to watch.
  196. * Compose this mask from @ref nrf_mwu_region_watch_t values.
  197. */
  198. __STATIC_INLINE void nrf_mwu_region_watch_enable(NRF_MWU_Type * p_reg, uint32_t reg_watch_mask);
  199. /**
  200. * @brief Function for disabling memory access watch mechanism.
  201. *
  202. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  203. * @param[in] reg_watch_mask Mask that defines regions and access types to stop watching.
  204. * Compose this mask from @ref nrf_mwu_region_watch_t values.
  205. */
  206. __STATIC_INLINE void nrf_mwu_region_watch_disable(NRF_MWU_Type * p_reg, uint32_t reg_watch_mask);
  207. /**
  208. * @brief Function for getting memory access watch configuration mask.
  209. *
  210. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  211. *
  212. * @return Mask that defines regions and access types being watched.
  213. */
  214. __STATIC_INLINE uint32_t nrf_mwu_region_watch_get(NRF_MWU_Type const * p_reg);
  215. /**
  216. * @brief Function for configuring peripheral subregions for watching.
  217. *
  218. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  219. * @param[in] per_reg_idx Peripheral region containing specified subregions.
  220. * @param[in] subregion_mask Mask that defines subregions to include into the specified peripheral region.
  221. */
  222. __STATIC_INLINE void nrf_mwu_subregions_configure(NRF_MWU_Type * p_reg,
  223. uint8_t per_reg_idx,
  224. uint32_t subregion_mask);
  225. /**
  226. * @brief Function for getting the mask of the write access flags of peripheral subregions
  227. *
  228. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  229. * @param[in] per_reg_idx Peripheral region containing subregions to check.
  230. *
  231. * @return Mask specifying subregions that were write accessed.
  232. */
  233. __STATIC_INLINE uint32_t nrf_mwu_subregions_write_accesses_get(NRF_MWU_Type const * p_reg,
  234. uint8_t per_reg_idx);
  235. /**
  236. * @brief Function for clearing write access flags of peripheral subregions.
  237. *
  238. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  239. * @param[in] per_reg_idx Peripheral region containing subregion accesses to clear.
  240. * @param[in] subregion_mask Mask that defines subregion write accesses to clear.
  241. */
  242. __STATIC_INLINE void nrf_mwu_subregions_write_accesses_clear(NRF_MWU_Type * p_reg,
  243. uint8_t per_reg_idx,
  244. uint32_t subregion_mask);
  245. /**
  246. * @brief Function for getting the mask of the read access flags of peripheral subregions
  247. *
  248. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  249. * @param[in] per_reg_idx Peripheral region containing subregions to check.
  250. *
  251. * @return Mask specifying subregions that were read accessed.
  252. */
  253. __STATIC_INLINE uint32_t nrf_mwu_subregions_read_accesses_get(NRF_MWU_Type const * p_reg,
  254. uint8_t per_reg_idx);
  255. /**
  256. * @brief Function for clearing read access flags of peripheral subregions.
  257. *
  258. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  259. * @param[in] per_reg_idx Peripheral region containing subregion accesses to clear.
  260. * @param[in] subregion_mask Mask that defines subregion read accesses to clear.
  261. */
  262. __STATIC_INLINE void nrf_mwu_subregions_read_accesses_clear(NRF_MWU_Type * p_reg,
  263. uint8_t per_reg_idx,
  264. uint32_t subregion_mask);
  265. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  266. __STATIC_INLINE bool nrf_mwu_event_check(NRF_MWU_Type const * p_reg,
  267. nrf_mwu_event_t mwu_event)
  268. {
  269. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)mwu_event);
  270. }
  271. __STATIC_INLINE void nrf_mwu_event_clear(NRF_MWU_Type * p_reg,
  272. nrf_mwu_event_t mwu_event)
  273. {
  274. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)mwu_event)) = 0;
  275. #if __CORTEX_M == 0x04
  276. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)mwu_event));
  277. (void)dummy;
  278. #endif
  279. }
  280. __STATIC_INLINE uint32_t nrf_mwu_event_address_get(NRF_MWU_Type const * p_reg,
  281. nrf_mwu_event_t mwu_event)
  282. {
  283. return (uint32_t)((uint8_t *)p_reg + (uint32_t)mwu_event);
  284. }
  285. __STATIC_INLINE void nrf_mwu_int_enable(NRF_MWU_Type * p_reg, uint32_t int_mask)
  286. {
  287. p_reg->INTENSET = int_mask;
  288. }
  289. __STATIC_INLINE bool nrf_mwu_int_enable_check(NRF_MWU_Type const * p_reg,
  290. nrf_mwu_int_mask_t mwu_int)
  291. {
  292. return (bool)(p_reg->INTENSET & mwu_int);
  293. }
  294. __STATIC_INLINE void nrf_mwu_int_disable(NRF_MWU_Type * p_reg, uint32_t int_mask)
  295. {
  296. p_reg->INTENCLR = int_mask;
  297. }
  298. __STATIC_INLINE void nrf_mwu_nmi_enable(NRF_MWU_Type * p_reg, uint32_t int_mask)
  299. {
  300. p_reg->NMIENSET = int_mask;
  301. }
  302. __STATIC_INLINE bool nrf_mwu_nmi_enable_check(NRF_MWU_Type const * p_reg,
  303. nrf_mwu_int_mask_t mwu_int)
  304. {
  305. return (bool)(p_reg->NMIENSET & mwu_int);
  306. }
  307. __STATIC_INLINE void nrf_mwu_nmi_disable(NRF_MWU_Type * p_reg, uint32_t int_mask)
  308. {
  309. p_reg->NMIENCLR = int_mask;
  310. }
  311. __STATIC_INLINE void nrf_mwu_user_region_range_set(NRF_MWU_Type * p_reg,
  312. uint8_t region_idx,
  313. uint32_t start_addr,
  314. uint32_t end_addr)
  315. {
  316. NRFX_ASSERT(region_idx < NRFX_ARRAY_SIZE(NRF_MWU->REGION));
  317. NRFX_ASSERT(end_addr >= start_addr);
  318. p_reg->REGION[region_idx].START = start_addr;
  319. p_reg->REGION[region_idx].END = end_addr;
  320. }
  321. __STATIC_INLINE void nrf_mwu_region_watch_enable(NRF_MWU_Type * p_reg, uint32_t reg_watch_mask)
  322. {
  323. p_reg->REGIONENSET = reg_watch_mask;
  324. }
  325. __STATIC_INLINE void nrf_mwu_region_watch_disable(NRF_MWU_Type * p_reg, uint32_t reg_watch_mask)
  326. {
  327. p_reg->REGIONENCLR = reg_watch_mask;
  328. }
  329. __STATIC_INLINE uint32_t nrf_mwu_region_watch_get(NRF_MWU_Type const * p_reg)
  330. {
  331. return p_reg->REGIONENSET;
  332. }
  333. __STATIC_INLINE void nrf_mwu_subregions_configure(NRF_MWU_Type * p_reg,
  334. uint8_t per_reg_idx,
  335. uint32_t subregion_mask)
  336. {
  337. NRFX_ASSERT(per_reg_idx < NRFX_ARRAY_SIZE(NRF_MWU->PREGION));
  338. p_reg->PREGION[per_reg_idx].SUBS = subregion_mask;
  339. }
  340. __STATIC_INLINE uint32_t nrf_mwu_subregions_write_accesses_get(NRF_MWU_Type const * p_reg,
  341. uint8_t per_reg_idx)
  342. {
  343. NRFX_ASSERT(per_reg_idx < NRFX_ARRAY_SIZE(NRF_MWU->PREGION));
  344. return p_reg->PERREGION[per_reg_idx].SUBSTATWA;
  345. }
  346. __STATIC_INLINE void nrf_mwu_subregions_write_accesses_clear(NRF_MWU_Type * p_reg,
  347. uint8_t per_reg_idx,
  348. uint32_t subregion_mask)
  349. {
  350. NRFX_ASSERT(per_reg_idx < NRFX_ARRAY_SIZE(NRF_MWU->PREGION));
  351. p_reg->PERREGION[per_reg_idx].SUBSTATWA = subregion_mask;
  352. }
  353. __STATIC_INLINE uint32_t nrf_mwu_subregions_read_accesses_get(NRF_MWU_Type const * p_reg,
  354. uint8_t per_reg_idx)
  355. {
  356. NRFX_ASSERT(per_reg_idx < NRFX_ARRAY_SIZE(NRF_MWU->PREGION));
  357. return p_reg->PERREGION[per_reg_idx].SUBSTATRA;
  358. }
  359. __STATIC_INLINE void nrf_mwu_subregions_read_accesses_clear(NRF_MWU_Type * p_reg,
  360. uint8_t per_reg_idx,
  361. uint32_t subregion_mask)
  362. {
  363. NRFX_ASSERT(per_reg_idx < NRFX_ARRAY_SIZE(NRF_MWU->PREGION));
  364. p_reg->PERREGION[per_reg_idx].SUBSTATRA = subregion_mask;
  365. }
  366. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  367. /** @} */
  368. #ifdef __cplusplus
  369. }
  370. #endif
  371. #endif // NRF_MWU_H__