nrf_rtc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /**
  2. * Copyright (c) 2014 - 2018, 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. /**
  53. * @brief Macro for getting the number of compare channels available
  54. * in a given RTC instance.
  55. */
  56. #define NRF_RTC_CC_CHANNEL_COUNT(id) NRFX_CONCAT_3(RTC, id, _CC_NUM)
  57. #define RTC_INPUT_FREQ 32768 /**< Input frequency of the RTC instance. */
  58. /**
  59. * @brief Macro for converting expected frequency to prescaler setting.
  60. */
  61. #define RTC_FREQ_TO_PRESCALER(FREQ) (uint16_t)(((RTC_INPUT_FREQ) / (FREQ)) - 1)
  62. /**< Macro for wrapping values to RTC capacity. */
  63. #define RTC_WRAP(val) ((val) & RTC_COUNTER_COUNTER_Msk)
  64. #define RTC_CHANNEL_INT_MASK(ch) ((uint32_t)(NRF_RTC_INT_COMPARE0_MASK) << (ch))
  65. #define RTC_CHANNEL_EVENT_ADDR(ch) (nrf_rtc_event_t)((NRF_RTC_EVENT_COMPARE_0) + (ch) * sizeof(uint32_t))
  66. /**
  67. * @enum nrf_rtc_task_t
  68. * @brief RTC tasks.
  69. */
  70. typedef enum
  71. {
  72. /*lint -save -e30*/
  73. NRF_RTC_TASK_START = offsetof(NRF_RTC_Type,TASKS_START), /**< Start. */
  74. NRF_RTC_TASK_STOP = offsetof(NRF_RTC_Type,TASKS_STOP), /**< Stop. */
  75. NRF_RTC_TASK_CLEAR = offsetof(NRF_RTC_Type,TASKS_CLEAR), /**< Clear. */
  76. NRF_RTC_TASK_TRIGGER_OVERFLOW = offsetof(NRF_RTC_Type,TASKS_TRIGOVRFLW),/**< Trigger overflow. */
  77. /*lint -restore*/
  78. } nrf_rtc_task_t;
  79. /**
  80. * @enum nrf_rtc_event_t
  81. * @brief RTC events.
  82. */
  83. typedef enum
  84. {
  85. /*lint -save -e30*/
  86. NRF_RTC_EVENT_TICK = offsetof(NRF_RTC_Type,EVENTS_TICK), /**< Tick event. */
  87. NRF_RTC_EVENT_OVERFLOW = offsetof(NRF_RTC_Type,EVENTS_OVRFLW), /**< Overflow event. */
  88. NRF_RTC_EVENT_COMPARE_0 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[0]), /**< Compare 0 event. */
  89. NRF_RTC_EVENT_COMPARE_1 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[1]), /**< Compare 1 event. */
  90. NRF_RTC_EVENT_COMPARE_2 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[2]), /**< Compare 2 event. */
  91. NRF_RTC_EVENT_COMPARE_3 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[3]) /**< Compare 3 event. */
  92. /*lint -restore*/
  93. } nrf_rtc_event_t;
  94. /**
  95. * @enum nrf_rtc_int_t
  96. * @brief RTC interrupts.
  97. */
  98. typedef enum
  99. {
  100. NRF_RTC_INT_TICK_MASK = RTC_INTENSET_TICK_Msk, /**< RTC interrupt from tick event. */
  101. NRF_RTC_INT_OVERFLOW_MASK = RTC_INTENSET_OVRFLW_Msk, /**< RTC interrupt from overflow event. */
  102. NRF_RTC_INT_COMPARE0_MASK = RTC_INTENSET_COMPARE0_Msk, /**< RTC interrupt from compare event on channel 0. */
  103. NRF_RTC_INT_COMPARE1_MASK = RTC_INTENSET_COMPARE1_Msk, /**< RTC interrupt from compare event on channel 1. */
  104. NRF_RTC_INT_COMPARE2_MASK = RTC_INTENSET_COMPARE2_Msk, /**< RTC interrupt from compare event on channel 2. */
  105. NRF_RTC_INT_COMPARE3_MASK = RTC_INTENSET_COMPARE3_Msk /**< RTC interrupt from compare event on channel 3. */
  106. } nrf_rtc_int_t;
  107. /**@brief Function for setting a compare value for a channel.
  108. *
  109. * @param[in] p_rtc Pointer to the peripheral registers structure.
  110. * @param[in] ch Channel.
  111. * @param[in] cc_val Compare value to set.
  112. */
  113. __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_rtc, uint32_t ch, uint32_t cc_val);
  114. /**@brief Function for returning the compare value for a channel.
  115. *
  116. * @param[in] p_rtc Pointer to the peripheral registers structure.
  117. * @param[in] ch Channel.
  118. *
  119. * @return COMPARE[ch] value.
  120. */
  121. __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_rtc, uint32_t ch);
  122. /**@brief Function for enabling interrupts.
  123. *
  124. * @param[in] p_rtc Pointer to the peripheral registers structure.
  125. * @param[in] mask Interrupt mask to be enabled.
  126. */
  127. __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_rtc, uint32_t mask);
  128. /**@brief Function for disabling interrupts.
  129. *
  130. * @param[in] p_rtc Pointer to the peripheral registers structure.
  131. * @param[in] mask Interrupt mask to be disabled.
  132. */
  133. __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_rtc, uint32_t mask);
  134. /**@brief Function for checking if interrupts are enabled.
  135. *
  136. * @param[in] p_rtc Pointer to the peripheral registers structure.
  137. * @param[in] mask Mask of interrupt flags to check.
  138. *
  139. * @return Mask with enabled interrupts.
  140. */
  141. __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_rtc, uint32_t mask);
  142. /**@brief Function for returning the status of currently enabled interrupts.
  143. *
  144. * @param[in] p_rtc Pointer to the peripheral registers structure.
  145. *
  146. * @return Value in INTEN register.
  147. */
  148. __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_rtc);
  149. /**@brief Function for checking if an event is pending.
  150. *
  151. * @param[in] p_rtc Pointer to the peripheral registers structure.
  152. * @param[in] event Address of the event.
  153. *
  154. * @return Mask of pending events.
  155. */
  156. __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
  157. /**@brief Function for clearing an event.
  158. *
  159. * @param[in] p_rtc Pointer to the peripheral registers structure.
  160. * @param[in] event Event to clear.
  161. */
  162. __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
  163. /**@brief Function for returning a counter value.
  164. *
  165. * @param[in] p_rtc Pointer to the peripheral registers structure.
  166. *
  167. * @return Counter value.
  168. */
  169. __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_rtc);
  170. /**@brief Function for setting a prescaler value.
  171. *
  172. * @param[in] p_rtc Pointer to the peripheral registers structure.
  173. * @param[in] val Value to set the prescaler to.
  174. */
  175. __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_rtc, uint32_t val);
  176. /**@brief Function for returning the address of an event.
  177. *
  178. * @param[in] p_rtc Pointer to the peripheral registers structure.
  179. * @param[in] event Requested event.
  180. *
  181. * @return Address of the requested event register.
  182. */
  183. __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
  184. /**@brief Function for returning the address of a task.
  185. *
  186. * @param[in] p_rtc Pointer to the peripheral registers structure.
  187. * @param[in] task Requested task.
  188. *
  189. * @return Address of the requested task register.
  190. */
  191. __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task);
  192. /**@brief Function for starting a task.
  193. *
  194. * @param[in] p_rtc Pointer to the peripheral registers structure.
  195. * @param[in] task Requested task.
  196. */
  197. __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task);
  198. /**@brief Function for enabling events.
  199. *
  200. * @param[in] p_rtc Pointer to the peripheral registers structure.
  201. * @param[in] mask Mask of event flags to enable.
  202. */
  203. __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_rtc, uint32_t mask);
  204. /**@brief Function for disabling an event.
  205. *
  206. * @param[in] p_rtc Pointer to the peripheral registers structure.
  207. * @param[in] event Requested event.
  208. */
  209. __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_rtc, uint32_t event);
  210. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  211. __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_rtc, uint32_t ch, uint32_t cc_val)
  212. {
  213. p_rtc->CC[ch] = cc_val;
  214. }
  215. __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_rtc, uint32_t ch)
  216. {
  217. return p_rtc->CC[ch];
  218. }
  219. __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_rtc, uint32_t mask)
  220. {
  221. p_rtc->INTENSET = mask;
  222. }
  223. __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_rtc, uint32_t mask)
  224. {
  225. p_rtc->INTENCLR = mask;
  226. }
  227. __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_rtc, uint32_t mask)
  228. {
  229. return (p_rtc->INTENSET & mask);
  230. }
  231. __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_rtc)
  232. {
  233. return p_rtc->INTENSET;
  234. }
  235. __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
  236. {
  237. return *(volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event);
  238. }
  239. __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
  240. {
  241. *((volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event)) = 0;
  242. #if __CORTEX_M == 0x04
  243. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event));
  244. (void)dummy;
  245. #endif
  246. }
  247. __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_rtc)
  248. {
  249. return p_rtc->COUNTER;
  250. }
  251. __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_rtc, uint32_t val)
  252. {
  253. NRFX_ASSERT(val <= (RTC_PRESCALER_PRESCALER_Msk >> RTC_PRESCALER_PRESCALER_Pos));
  254. p_rtc->PRESCALER = val;
  255. }
  256. __STATIC_INLINE uint32_t rtc_prescaler_get(NRF_RTC_Type * p_rtc)
  257. {
  258. return p_rtc->PRESCALER;
  259. }
  260. __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
  261. {
  262. return (uint32_t)p_rtc + event;
  263. }
  264. __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task)
  265. {
  266. return (uint32_t)p_rtc + task;
  267. }
  268. __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task)
  269. {
  270. *(__IO uint32_t *)((uint32_t)p_rtc + task) = 1;
  271. }
  272. __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_rtc, uint32_t mask)
  273. {
  274. p_rtc->EVTENSET = mask;
  275. }
  276. __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_rtc, uint32_t mask)
  277. {
  278. p_rtc->EVTENCLR = mask;
  279. }
  280. #endif
  281. /** @} */
  282. #ifdef __cplusplus
  283. }
  284. #endif
  285. #endif /* NRF_RTC_H */