nrf_rtc.h 15 KB

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