nrf_rtc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * Copyright (c) 2014 - 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_RTC_H
  41. #define NRF_RTC_H
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_rtc_hal RTC HAL
  48. * @{
  49. * @ingroup nrf_rtc
  50. * @brief Hardware access layer for managing the Real Time Counter (RTC) peripheral.
  51. */
  52. /** @brief Macro for getting the number of compare channels available in a given RTC instance. */
  53. #define NRF_RTC_CC_CHANNEL_COUNT(id) NRFX_CONCAT_3(RTC, id, _CC_NUM)
  54. #define RTC_INPUT_FREQ 32768 /**< Input frequency of the RTC instance. */
  55. /** @brief Macro for converting expected frequency to prescaler setting. */
  56. #define RTC_FREQ_TO_PRESCALER(FREQ) (uint16_t)(((RTC_INPUT_FREQ) / (FREQ)) - 1)
  57. /**< Macro for wrapping values to RTC capacity. */
  58. #define RTC_WRAP(val) ((val) & RTC_COUNTER_COUNTER_Msk)
  59. #define RTC_CHANNEL_INT_MASK(ch) \
  60. ((uint32_t)(NRF_RTC_INT_COMPARE0_MASK) << (ch))
  61. #define RTC_CHANNEL_EVENT_ADDR(ch) \
  62. (nrf_rtc_event_t)((NRF_RTC_EVENT_COMPARE_0) + (ch) * sizeof(uint32_t))
  63. /** @brief RTC tasks. */
  64. typedef enum
  65. {
  66. /*lint -save -e30*/
  67. NRF_RTC_TASK_START = offsetof(NRF_RTC_Type,TASKS_START), /**< Start. */
  68. NRF_RTC_TASK_STOP = offsetof(NRF_RTC_Type,TASKS_STOP), /**< Stop. */
  69. NRF_RTC_TASK_CLEAR = offsetof(NRF_RTC_Type,TASKS_CLEAR), /**< Clear. */
  70. NRF_RTC_TASK_TRIGGER_OVERFLOW = offsetof(NRF_RTC_Type,TASKS_TRIGOVRFLW),/**< Trigger overflow. */
  71. /*lint -restore*/
  72. } nrf_rtc_task_t;
  73. /** @brief RTC events. */
  74. typedef enum
  75. {
  76. /*lint -save -e30*/
  77. NRF_RTC_EVENT_TICK = offsetof(NRF_RTC_Type,EVENTS_TICK), /**< Tick event. */
  78. NRF_RTC_EVENT_OVERFLOW = offsetof(NRF_RTC_Type,EVENTS_OVRFLW), /**< Overflow event. */
  79. NRF_RTC_EVENT_COMPARE_0 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[0]), /**< Compare 0 event. */
  80. NRF_RTC_EVENT_COMPARE_1 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[1]), /**< Compare 1 event. */
  81. NRF_RTC_EVENT_COMPARE_2 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[2]), /**< Compare 2 event. */
  82. NRF_RTC_EVENT_COMPARE_3 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[3]) /**< Compare 3 event. */
  83. /*lint -restore*/
  84. } nrf_rtc_event_t;
  85. /** @brief RTC interrupts. */
  86. typedef enum
  87. {
  88. NRF_RTC_INT_TICK_MASK = RTC_INTENSET_TICK_Msk, /**< RTC interrupt from tick event. */
  89. NRF_RTC_INT_OVERFLOW_MASK = RTC_INTENSET_OVRFLW_Msk, /**< RTC interrupt from overflow event. */
  90. NRF_RTC_INT_COMPARE0_MASK = RTC_INTENSET_COMPARE0_Msk, /**< RTC interrupt from compare event on channel 0. */
  91. NRF_RTC_INT_COMPARE1_MASK = RTC_INTENSET_COMPARE1_Msk, /**< RTC interrupt from compare event on channel 1. */
  92. NRF_RTC_INT_COMPARE2_MASK = RTC_INTENSET_COMPARE2_Msk, /**< RTC interrupt from compare event on channel 2. */
  93. NRF_RTC_INT_COMPARE3_MASK = RTC_INTENSET_COMPARE3_Msk /**< RTC interrupt from compare event on channel 3. */
  94. } nrf_rtc_int_t;
  95. /**
  96. * @brief Function for setting a compare value for a channel.
  97. *
  98. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  99. * @param[in] ch Channel.
  100. * @param[in] cc_val Compare value to set.
  101. */
  102. __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_reg, uint32_t ch, uint32_t cc_val);
  103. /**
  104. * @brief Function for returning the compare value for a channel.
  105. *
  106. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  107. * @param[in] ch Channel.
  108. *
  109. * @return COMPARE[ch] value.
  110. */
  111. __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_reg, uint32_t ch);
  112. /**
  113. * @brief Function for enabling interrupts.
  114. *
  115. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  116. * @param[in] mask Interrupt mask to be enabled.
  117. */
  118. __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_reg, uint32_t mask);
  119. /**
  120. * @brief Function for disabling interrupts.
  121. *
  122. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  123. * @param[in] mask Interrupt mask to be disabled.
  124. */
  125. __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_reg, uint32_t mask);
  126. /**
  127. * @brief Function for checking if interrupts are enabled.
  128. *
  129. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  130. * @param[in] mask Mask of interrupt flags to check.
  131. *
  132. * @return Mask with enabled interrupts.
  133. */
  134. __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_reg, uint32_t mask);
  135. /**
  136. * @brief Function for returning the status of currently enabled interrupts.
  137. *
  138. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  139. *
  140. * @return Value in INTEN register.
  141. */
  142. __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_reg);
  143. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  144. /**
  145. * @brief Function for setting the subscribe configuration for a given
  146. * RTC task.
  147. *
  148. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  149. * @param[in] task Task for which to set the configuration.
  150. * @param[in] channel Channel through which to subscribe events.
  151. */
  152. __STATIC_INLINE void nrf_rtc_subscribe_set(NRF_RTC_Type * p_reg,
  153. nrf_rtc_task_t task,
  154. uint8_t channel);
  155. /**
  156. * @brief Function for clearing the subscribe configuration for a given
  157. * RTC task.
  158. *
  159. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  160. * @param[in] task Task for which to clear the configuration.
  161. */
  162. __STATIC_INLINE void nrf_rtc_subscribe_clear(NRF_RTC_Type * p_reg,
  163. nrf_rtc_task_t task);
  164. /**
  165. * @brief Function for setting the publish configuration for a given
  166. * RTC event.
  167. *
  168. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  169. * @param[in] event Event for which to set the configuration.
  170. * @param[in] channel Channel through which to publish the event.
  171. */
  172. __STATIC_INLINE void nrf_rtc_publish_set(NRF_RTC_Type * p_reg,
  173. nrf_rtc_event_t event,
  174. uint8_t channel);
  175. /**
  176. * @brief Function for clearing the publish configuration for a given
  177. * RTC event.
  178. *
  179. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  180. * @param[in] event Event for which to clear the configuration.
  181. */
  182. __STATIC_INLINE void nrf_rtc_publish_clear(NRF_RTC_Type * p_reg,
  183. nrf_rtc_event_t event);
  184. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  185. /**
  186. * @brief Function for checking if an event is pending.
  187. *
  188. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  189. * @param[in] event Address of the event.
  190. *
  191. * @return Mask of pending events.
  192. */
  193. __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_reg, nrf_rtc_event_t event);
  194. /**
  195. * @brief Function for clearing an event.
  196. *
  197. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  198. * @param[in] event Event to clear.
  199. */
  200. __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_reg, nrf_rtc_event_t event);
  201. /**
  202. * @brief Function for returning a counter value.
  203. *
  204. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  205. *
  206. * @return Counter value.
  207. */
  208. __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_reg);
  209. /**
  210. * @brief Function for setting a prescaler value.
  211. *
  212. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  213. * @param[in] val Value to set the prescaler to.
  214. */
  215. __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_reg, uint32_t val);
  216. /**
  217. * @brief Function for returning the address of an event.
  218. *
  219. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  220. * @param[in] event Requested event.
  221. *
  222. * @return Address of the requested event register.
  223. */
  224. __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_reg, nrf_rtc_event_t event);
  225. /**
  226. * @brief Function for returning the address of a task.
  227. *
  228. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  229. * @param[in] task Requested task.
  230. *
  231. * @return Address of the requested task register.
  232. */
  233. __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_reg, nrf_rtc_task_t task);
  234. /**
  235. * @brief Function for starting a task.
  236. *
  237. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  238. * @param[in] task Requested task.
  239. */
  240. __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_reg, nrf_rtc_task_t task);
  241. /**
  242. * @brief Function for enabling events.
  243. *
  244. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  245. * @param[in] mask Mask of event flags to enable.
  246. */
  247. __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_reg, uint32_t mask);
  248. /**
  249. * @brief Function for disabling an event.
  250. *
  251. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  252. * @param[in] event Requested event.
  253. */
  254. __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_reg, uint32_t event);
  255. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  256. __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_reg, uint32_t ch, uint32_t cc_val)
  257. {
  258. p_reg->CC[ch] = cc_val;
  259. }
  260. __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_reg, uint32_t ch)
  261. {
  262. return p_reg->CC[ch];
  263. }
  264. __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_reg, uint32_t mask)
  265. {
  266. p_reg->INTENSET = mask;
  267. }
  268. __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_reg, uint32_t mask)
  269. {
  270. p_reg->INTENCLR = mask;
  271. }
  272. __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_reg, uint32_t mask)
  273. {
  274. return (p_reg->INTENSET & mask);
  275. }
  276. __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_reg)
  277. {
  278. return p_reg->INTENSET;
  279. }
  280. #if defined(DPPI_PRESENT)
  281. __STATIC_INLINE void nrf_rtc_subscribe_set(NRF_RTC_Type * p_reg,
  282. nrf_rtc_task_t task,
  283. uint8_t channel)
  284. {
  285. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  286. ((uint32_t)channel | RTC_SUBSCRIBE_START_EN_Msk);
  287. }
  288. __STATIC_INLINE void nrf_rtc_subscribe_clear(NRF_RTC_Type * p_reg,
  289. nrf_rtc_task_t task)
  290. {
  291. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  292. }
  293. __STATIC_INLINE void nrf_rtc_publish_set(NRF_RTC_Type * p_reg,
  294. nrf_rtc_event_t event,
  295. uint8_t channel)
  296. {
  297. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  298. ((uint32_t)channel | RTC_PUBLISH_TICK_EN_Msk);
  299. }
  300. __STATIC_INLINE void nrf_rtc_publish_clear(NRF_RTC_Type * p_reg,
  301. nrf_rtc_event_t event)
  302. {
  303. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  304. }
  305. #endif // defined(DPPI_PRESENT)
  306. __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_reg, nrf_rtc_event_t event)
  307. {
  308. return *(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  309. }
  310. __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_reg, nrf_rtc_event_t event)
  311. {
  312. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0;
  313. #if __CORTEX_M == 0x04
  314. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  315. (void)dummy;
  316. #endif
  317. }
  318. __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_reg)
  319. {
  320. return p_reg->COUNTER;
  321. }
  322. __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_reg, uint32_t val)
  323. {
  324. NRFX_ASSERT(val <= (RTC_PRESCALER_PRESCALER_Msk >> RTC_PRESCALER_PRESCALER_Pos));
  325. p_reg->PRESCALER = val;
  326. }
  327. __STATIC_INLINE uint32_t rtc_prescaler_get(NRF_RTC_Type * p_reg)
  328. {
  329. return p_reg->PRESCALER;
  330. }
  331. __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_reg, nrf_rtc_event_t event)
  332. {
  333. return (uint32_t)p_reg + event;
  334. }
  335. __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_reg, nrf_rtc_task_t task)
  336. {
  337. return (uint32_t)p_reg + task;
  338. }
  339. __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_reg, nrf_rtc_task_t task)
  340. {
  341. *(__IO uint32_t *)((uint32_t)p_reg + task) = 1;
  342. }
  343. __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_reg, uint32_t mask)
  344. {
  345. p_reg->EVTENSET = mask;
  346. }
  347. __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_reg, uint32_t mask)
  348. {
  349. p_reg->EVTENCLR = mask;
  350. }
  351. #endif
  352. /** @} */
  353. #ifdef __cplusplus
  354. }
  355. #endif
  356. #endif /* NRF_RTC_H */