nrf_aar.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 NRF_AAR_H__
  41. #define NRF_AAR_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_aar_hal AAR HAL
  48. * @{
  49. * @ingroup nrf_aar
  50. * @brief Hardware access layer for managing the Accelerated Address Resolver (AAR) peripheral.
  51. */
  52. /** @brief AAR events. */
  53. typedef enum
  54. {
  55. NRF_AAR_EVENT_END = offsetof(NRF_AAR_Type, EVENTS_END), ///< Address resolution procedure complete.
  56. NRF_AAR_EVENT_RESOLVED = offsetof(NRF_AAR_Type, EVENTS_RESOLVED), ///< Address resolved.
  57. NRF_AAR_EVENT_NOTRESOLVED = offsetof(NRF_AAR_Type, EVENTS_NOTRESOLVED), ///< Address not resolved.
  58. } nrf_aar_event_t;
  59. /** @brief AAR interrupts. */
  60. typedef enum
  61. {
  62. NRF_AAR_INT_END_MASK = AAR_INTENSET_END_Msk, ///< Interrupt on END event.
  63. NRF_AAR_INT_RESOLVED_MASK = AAR_INTENSET_RESOLVED_Msk, ///< Interrupt on RESOLVED event.
  64. NRF_AAR_INT_NOTRESOLVED_MASK = AAR_INTENSET_NOTRESOLVED_Msk, ///< Interrupt on NOTRESOLVED event.
  65. } nrf_aar_int_mask_t;
  66. /** @brief AAR tasks. */
  67. typedef enum
  68. {
  69. NRF_AAR_TASK_START = offsetof(NRF_AAR_Type, TASKS_START), ///< Start address resolution procedure.
  70. NRF_AAR_TASK_STOP = offsetof(NRF_AAR_Type, TASKS_STOP), ///< Stop address resolution procedure.
  71. } nrf_aar_task_t;
  72. /**
  73. * @brief Function for retrieving the state of the AAR event.
  74. *
  75. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  76. * @param[in] event Event to be checked.
  77. *
  78. * @retval true Event is set.
  79. * @retval false Event is not set.
  80. */
  81. __STATIC_INLINE bool nrf_aar_event_check(NRF_AAR_Type const * p_reg,
  82. nrf_aar_event_t event);
  83. /**
  84. * @brief Function for clearing the specified AAR event.
  85. *
  86. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  87. * @param[in] event Event to be cleared.
  88. */
  89. __STATIC_INLINE void nrf_aar_event_clear(NRF_AAR_Type * p_reg,
  90. nrf_aar_event_t event);
  91. /**
  92. * @brief Function for getting the address of the specified AAR event register.
  93. *
  94. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  95. * @param[in] event Event to get the address of.
  96. *
  97. * @return Address of the specified event register.
  98. */
  99. __STATIC_INLINE uint32_t nrf_aar_event_address_get(NRF_AAR_Type const * p_reg,
  100. nrf_aar_event_t event);
  101. /**
  102. * @brief Function for enabling the specified interrupts.
  103. *
  104. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  105. * @param[in] mask Mask of interrupts to be enabled.
  106. */
  107. __STATIC_INLINE void nrf_aar_int_enable(NRF_AAR_Type * p_reg, uint32_t mask);
  108. /**
  109. * @brief Function for retrieving the state of the specified interrupt.
  110. *
  111. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  112. * @param[in] mask Mask of the interrupt to be checked.
  113. *
  114. * @retval true Interrupt is enabled.
  115. * @retval false Interrupt is not enabled.
  116. */
  117. __STATIC_INLINE bool nrf_aar_int_enable_check(NRF_AAR_Type const * p_reg,
  118. nrf_aar_int_mask_t mask);
  119. /**
  120. * @brief Function for disabling the specified interrupts.
  121. *
  122. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  123. * @param[in] mask Mask of interrupts to be disabled.
  124. */
  125. __STATIC_INLINE void nrf_aar_int_disable(NRF_AAR_Type * p_reg, uint32_t mask);
  126. /**
  127. * @brief Function for starting an AAR task.
  128. *
  129. * @param p_reg Pointer to the structure of registers of the peripheral.
  130. * @param task Task to be activated.
  131. */
  132. __STATIC_INLINE void nrf_aar_task_trigger(NRF_AAR_Type * p_reg, nrf_aar_task_t task);
  133. /**
  134. * @brief Function for getting the address of a specific AAR task register.
  135. *
  136. * @param p_reg Pointer to the structure of registers of the peripheral.
  137. * @param task Requested AAR task.
  138. *
  139. * @return Address of the specified task register.
  140. */
  141. __STATIC_INLINE uint32_t nrf_aar_task_address_get(NRF_AAR_Type const * p_reg,
  142. nrf_aar_task_t task);
  143. /**
  144. * @brief Function for enabling AAR.
  145. *
  146. * @param p_reg Pointer to the structure of registers of the peripheral.
  147. */
  148. __STATIC_INLINE void nrf_aar_enable(NRF_AAR_Type * p_reg);
  149. /**
  150. * @brief Function for disabling AAR.
  151. *
  152. * @param p_reg Pointer to the structure of registers of the peripheral.
  153. */
  154. __STATIC_INLINE void nrf_aar_disable(NRF_AAR_Type * p_reg);
  155. /**
  156. * @brief Function for setting the pointer to the Identity Resolving Keys (IRK) data structure.
  157. *
  158. * The size of the provided data structure must correspond to the number of keys available.
  159. * Each key occupies 16 bytes.
  160. *
  161. * @param p_reg Pointer to the structure of registers of the peripheral.
  162. * @param irk_ptr Pointer to the IRK data structure. Must point to the Data RAM region.
  163. *
  164. * @sa nrf_aar_irk_number_set
  165. */
  166. __STATIC_INLINE void nrf_aar_irk_pointer_set(NRF_AAR_Type * p_reg, uint8_t const * irk_ptr);
  167. /**
  168. * @brief Function for getting the pointer to the Identity Resolving Keys
  169. * data structure.
  170. *
  171. * @param p_reg Pointer to the structure of registers of the peripheral.
  172. *
  173. * @return Pointer to the IRK data structure.
  174. */
  175. __STATIC_INLINE uint8_t const * nrf_aar_irk_pointer_get(NRF_AAR_Type const * p_reg);
  176. /**
  177. * @brief Function for setting the number of keys available in the Identity Resolving Keys
  178. * data structure.
  179. *
  180. * @param p_reg Pointer to the structure of registers of the peripheral.
  181. * @param irk_num Number of keys available in the IRK data structure. Maximum is 16.
  182. * Must correspond to the size of the provided IRK data structure.
  183. *
  184. * @sa nrf_aar_irk_pointer_set
  185. */
  186. __STATIC_INLINE void nrf_aar_irk_number_set(NRF_AAR_Type * p_reg, uint8_t irk_num);
  187. /**
  188. * @brief Function for getting the number of keys available in the Identity Resolving Keys
  189. * data structure.
  190. *
  191. * @param p_reg Pointer to the structure of registers of the peripheral.
  192. *
  193. * @return Number of keys in the IRK data structure.
  194. */
  195. __STATIC_INLINE uint8_t nrf_aar_irk_number_get(NRF_AAR_Type const * p_reg);
  196. /**
  197. * @brief Function for setting the pointer to the resolvable address.
  198. *
  199. * The resolvable address must consist of 6 bytes.
  200. *
  201. * @param p_reg Pointer to the structure of registers of the peripheral.
  202. * @param addr_ptr Pointer to the address to resolve using the available IRK keys.
  203. * Must point to the Data RAM region.
  204. */
  205. __STATIC_INLINE void nrf_aar_addr_pointer_set(NRF_AAR_Type * p_reg, uint8_t const * addr_ptr);
  206. /**
  207. * @brief Function for getting the pointer to the resolvable address.
  208. *
  209. * @param p_reg Pointer to the structure of registers of the peripheral.
  210. *
  211. * @return Pointer to the address to resolve.
  212. */
  213. __STATIC_INLINE uint8_t const * nrf_aar_addr_pointer_get(NRF_AAR_Type const * p_reg);
  214. /**
  215. * @brief Function for setting the pointer to the scratch data area.
  216. *
  217. * The scratch data area is used for temporary storage during the address resolution procedure.
  218. * A space of minimum 3 bytes must be reserved for the scratch data area.
  219. *
  220. * @param p_reg Pointer to the structure of registers of the peripheral.
  221. * @param scratch_ptr Pointer to the scratch data area. Must point to the Data RAM region.
  222. */
  223. __STATIC_INLINE void nrf_aar_scratch_pointer_set(NRF_AAR_Type * p_reg, uint8_t * scratch_ptr);
  224. /**
  225. * @brief Function for getting the pointer to the scratch data area.
  226. *
  227. * @param p_reg Pointer to the structure of registers of the peripheral.
  228. *
  229. * @return Pointer to the scratch data area.
  230. */
  231. __STATIC_INLINE uint8_t * nrf_aar_scratch_pointer_get(NRF_AAR_Type const * p_reg);
  232. /**
  233. * @brief Function for getting the index of the Identity Resolving Key that was used
  234. * the last time an address was resolved.
  235. *
  236. * This function can be used to get the IRK index that matched the resolvable address,
  237. * provided that @ref NRF_AAR_EVENT_RESOLVED occured. Otherwise, it will return
  238. * the index of the last IRK stored in the IRK data structure.
  239. *
  240. * @param p_reg Pointer to the structure of registers of the peripheral.
  241. *
  242. * @return The index of the IRK that was used the last time an address was resolved.
  243. */
  244. __STATIC_INLINE uint8_t nrf_aar_resolution_status_get(NRF_AAR_Type const * p_reg);
  245. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  246. __STATIC_INLINE bool nrf_aar_event_check(NRF_AAR_Type const * p_reg,
  247. nrf_aar_event_t aar_event)
  248. {
  249. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)aar_event);
  250. }
  251. __STATIC_INLINE void nrf_aar_event_clear(NRF_AAR_Type * p_reg,
  252. nrf_aar_event_t aar_event)
  253. {
  254. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)aar_event)) = 0;
  255. #if __CORTEX_M == 0x04
  256. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)aar_event));
  257. (void)dummy;
  258. #endif
  259. }
  260. __STATIC_INLINE uint32_t nrf_aar_event_address_get(NRF_AAR_Type const * p_reg,
  261. nrf_aar_event_t aar_event)
  262. {
  263. return (uint32_t)((uint8_t *)p_reg + (uint32_t)aar_event);
  264. }
  265. __STATIC_INLINE void nrf_aar_int_enable(NRF_AAR_Type * p_reg, uint32_t mask)
  266. {
  267. p_reg->INTENSET = mask;
  268. }
  269. __STATIC_INLINE bool nrf_aar_int_enable_check(NRF_AAR_Type const * p_reg,
  270. nrf_aar_int_mask_t mask)
  271. {
  272. return (bool)(p_reg->INTENSET & mask);
  273. }
  274. __STATIC_INLINE void nrf_aar_int_disable(NRF_AAR_Type * p_reg, uint32_t mask)
  275. {
  276. p_reg->INTENCLR = mask;
  277. }
  278. __STATIC_INLINE void nrf_aar_task_trigger(NRF_AAR_Type * p_reg, nrf_aar_task_t task)
  279. {
  280. *(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task) = 1;
  281. }
  282. __STATIC_INLINE uint32_t nrf_aar_task_address_get(NRF_AAR_Type const * p_reg,
  283. nrf_aar_task_t task)
  284. {
  285. return (uint32_t)((uint8_t *)p_reg + (uint32_t)task);
  286. }
  287. __STATIC_INLINE void nrf_aar_enable(NRF_AAR_Type * p_reg)
  288. {
  289. p_reg->ENABLE = AAR_ENABLE_ENABLE_Enabled << AAR_ENABLE_ENABLE_Pos;
  290. }
  291. __STATIC_INLINE void nrf_aar_disable(NRF_AAR_Type * p_reg)
  292. {
  293. p_reg->ENABLE = AAR_ENABLE_ENABLE_Disabled << AAR_ENABLE_ENABLE_Pos;
  294. }
  295. __STATIC_INLINE void nrf_aar_irk_pointer_set(NRF_AAR_Type * p_reg, uint8_t const * irk_ptr)
  296. {
  297. p_reg->IRKPTR = (uint32_t)irk_ptr;
  298. }
  299. __STATIC_INLINE uint8_t const * nrf_aar_irk_pointer_get(NRF_AAR_Type const * p_reg)
  300. {
  301. return (uint8_t const *)(p_reg->IRKPTR);
  302. }
  303. __STATIC_INLINE void nrf_aar_irk_number_set(NRF_AAR_Type * p_reg, uint8_t irk_num)
  304. {
  305. p_reg->NIRK = irk_num;
  306. }
  307. __STATIC_INLINE uint8_t nrf_aar_irk_number_get(NRF_AAR_Type const * p_reg)
  308. {
  309. return (uint8_t)(p_reg->NIRK);
  310. }
  311. __STATIC_INLINE void nrf_aar_addr_pointer_set(NRF_AAR_Type * p_reg, uint8_t const * addr_ptr)
  312. {
  313. p_reg->ADDRPTR = (uint32_t)addr_ptr;
  314. }
  315. __STATIC_INLINE uint8_t const * nrf_aar_addr_pointer_get(NRF_AAR_Type const * p_reg)
  316. {
  317. return (uint8_t const *)(p_reg->ADDRPTR);
  318. }
  319. __STATIC_INLINE void nrf_aar_scratch_pointer_set(NRF_AAR_Type * p_reg, uint8_t * scratch_ptr)
  320. {
  321. p_reg->SCRATCHPTR = (uint32_t)scratch_ptr;
  322. }
  323. __STATIC_INLINE uint8_t * nrf_aar_scratch_pointer_get(NRF_AAR_Type const * p_reg)
  324. {
  325. return (uint8_t *)(p_reg->SCRATCHPTR);
  326. }
  327. __STATIC_INLINE uint8_t nrf_aar_resolution_status_get(NRF_AAR_Type const * p_reg)
  328. {
  329. return (uint8_t)(p_reg->STATUS);
  330. }
  331. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  332. /** @} */
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif // NRF_AAR_H__