nrf_rtc.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**
  13. * @file
  14. * @brief RTC HAL API.
  15. */
  16. #ifndef NRF_RTC_H
  17. #define NRF_RTC_H
  18. /**
  19. * @defgroup nrf_rtc_hal RTC HAL
  20. * @{
  21. * @ingroup nrf_rtc
  22. * @brief Hardware access layer for managing the real time counter (RTC).
  23. */
  24. #include <stdint.h>
  25. #include <stddef.h>
  26. #include <stdbool.h>
  27. #include "nrf.h"
  28. #include "nrf_assert.h"
  29. /**
  30. * @brief Macro for getting the number of compare channels available
  31. * in a given RTC instance.
  32. */
  33. #ifdef NRF51
  34. #define NRF_RTC_CC_CHANNEL_COUNT(id) 4
  35. #else
  36. #define NRF_RTC_CC_CHANNEL_COUNT(id) ((id) == 0 ? 3 : 4)
  37. #endif
  38. #define RTC_INPUT_FREQ 32768 /**< Input frequency of the RTC instance. */
  39. /**< Macro for wrapping values to RTC capacity. */
  40. #define RTC_WRAP(val) (val & RTC_COUNTER_COUNTER_Msk)
  41. #define RTC_CHANNEL_INT_MASK(ch) ((uint32_t)NRF_RTC_INT_COMPARE0_MASK << ch)
  42. #define RTC_CHANNEL_EVENT_ADDR(ch) (nrf_rtc_event_t)(NRF_RTC_EVENT_COMPARE_0 + ch*sizeof(uint32_t))
  43. /**
  44. * @enum nrf_rtc_task_t
  45. * @brief RTC tasks.
  46. */
  47. typedef enum
  48. {
  49. /*lint -save -e30*/
  50. NRF_RTC_TASK_START = offsetof(NRF_RTC_Type,TASKS_START), /**< Start. */
  51. NRF_RTC_TASK_STOP = offsetof(NRF_RTC_Type,TASKS_STOP), /**< Stop. */
  52. NRF_RTC_TASK_CLEAR = offsetof(NRF_RTC_Type,TASKS_CLEAR), /**< Clear. */
  53. NRF_RTC_TASK_TRIGGER_OVERFLOW = offsetof(NRF_RTC_Type,TASKS_TRIGOVRFLW),/**< Trigger overflow. */
  54. /*lint -restore*/
  55. } nrf_rtc_task_t;
  56. /**
  57. * @enum nrf_rtc_event_t
  58. * @brief RTC events.
  59. */
  60. typedef enum
  61. {
  62. /*lint -save -e30*/
  63. NRF_RTC_EVENT_TICK = offsetof(NRF_RTC_Type,EVENTS_TICK), /**< Tick event. */
  64. NRF_RTC_EVENT_OVERFLOW = offsetof(NRF_RTC_Type,EVENTS_OVRFLW), /**< Overflow event. */
  65. NRF_RTC_EVENT_COMPARE_0 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[0]), /**< Compare 0 event. */
  66. NRF_RTC_EVENT_COMPARE_1 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[1]), /**< Compare 1 event. */
  67. NRF_RTC_EVENT_COMPARE_2 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[2]), /**< Compare 2 event. */
  68. NRF_RTC_EVENT_COMPARE_3 = offsetof(NRF_RTC_Type,EVENTS_COMPARE[3]) /**< Compare 3 event. */
  69. /*lint -restore*/
  70. } nrf_rtc_event_t;
  71. /**
  72. * @enum nrf_rtc_int_t
  73. * @brief RTC interrupts.
  74. */
  75. typedef enum
  76. {
  77. NRF_RTC_INT_TICK_MASK = RTC_INTENSET_TICK_Msk, /**< RTC interrupt from tick event. */
  78. NRF_RTC_INT_OVERFLOW_MASK = RTC_INTENSET_OVRFLW_Msk, /**< RTC interrupt from overflow event. */
  79. NRF_RTC_INT_COMPARE0_MASK = RTC_INTENSET_COMPARE0_Msk, /**< RTC interrupt from compare event on channel 0. */
  80. NRF_RTC_INT_COMPARE1_MASK = RTC_INTENSET_COMPARE1_Msk, /**< RTC interrupt from compare event on channel 1. */
  81. NRF_RTC_INT_COMPARE2_MASK = RTC_INTENSET_COMPARE2_Msk, /**< RTC interrupt from compare event on channel 2. */
  82. NRF_RTC_INT_COMPARE3_MASK = RTC_INTENSET_COMPARE3_Msk /**< RTC interrupt from compare event on channel 3. */
  83. } nrf_rtc_int_t;
  84. /**@brief Function for setting a compare value for a channel.
  85. *
  86. * @param[in] p_rtc Pointer to the instance register structure.
  87. * @param[in] ch Channel.
  88. * @param[in] cc_val Compare value to set.
  89. */
  90. __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_rtc, uint32_t ch, uint32_t cc_val);
  91. /**@brief Function for returning the compare value for a channel.
  92. *
  93. * @param[in] p_rtc Pointer to the instance register structure.
  94. * @param[in] ch Channel.
  95. *
  96. * @return COMPARE[ch] value.
  97. */
  98. __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_rtc, uint32_t ch);
  99. /**@brief Function for enabling interrupts.
  100. *
  101. * @param[in] p_rtc Pointer to the instance register structure.
  102. * @param[in] mask Interrupt mask to be enabled.
  103. */
  104. __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_rtc, uint32_t mask);
  105. /**@brief Function for disabling interrupts.
  106. *
  107. * @param[in] p_rtc Pointer to the instance register structure.
  108. * @param[in] mask Interrupt mask to be disabled.
  109. */
  110. __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_rtc, uint32_t mask);
  111. /**@brief Function for checking if interrupts are enabled.
  112. *
  113. * @param[in] p_rtc Pointer to the instance register structure.
  114. * @param[in] mask Mask of interrupt flags to check.
  115. *
  116. * @return Mask with enabled interrupts.
  117. */
  118. __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_rtc, uint32_t mask);
  119. /**@brief Function for returning the status of currently enabled interrupts.
  120. *
  121. * @param[in] p_rtc Pointer to the instance register structure.
  122. *
  123. * @return Value in INTEN register.
  124. */
  125. __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_rtc);
  126. /**@brief Function for checking if an event is pending.
  127. *
  128. * @param[in] p_rtc Pointer to the instance register structure.
  129. * @param[in] event Address of the event.
  130. *
  131. * @return Mask of pending events.
  132. */
  133. __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
  134. /**@brief Function for clearing an event.
  135. *
  136. * @param[in] p_rtc Pointer to the instance register structure.
  137. * @param[in] event Event to clear.
  138. */
  139. __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
  140. /**@brief Function for returning a counter value.
  141. *
  142. * @param[in] p_rtc Pointer to the instance register structure.
  143. *
  144. * @return Counter value.
  145. */
  146. __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_rtc);
  147. /**@brief Function for setting a prescaler value.
  148. *
  149. * @param[in] p_rtc Pointer to the instance register structure.
  150. * @param[in] val Value to set the prescaler to.
  151. */
  152. __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_rtc, uint32_t val);
  153. /**@brief Function for returning the address of an event.
  154. *
  155. * @param[in] p_rtc Pointer to the instance register structure.
  156. * @param[in] event Requested event.
  157. *
  158. * @return Address of the requested event register.
  159. */
  160. __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event);
  161. /**@brief Function for returning the address of a task.
  162. *
  163. * @param[in] p_rtc Pointer to the instance register structure.
  164. * @param[in] task Requested task.
  165. *
  166. * @return Address of the requested task register.
  167. */
  168. __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task);
  169. /**@brief Function for starting a task.
  170. *
  171. * @param[in] p_rtc Pointer to the instance register structure.
  172. * @param[in] task Requested task.
  173. */
  174. __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task);
  175. /**@brief Function for enabling events.
  176. *
  177. * @param[in] p_rtc Pointer to the instance register structure.
  178. * @param[in] mask Mask of event flags to enable.
  179. */
  180. __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_rtc, uint32_t mask);
  181. /**@brief Function for disabling an event.
  182. *
  183. * @param[in] p_rtc Pointer to the instance register structure.
  184. * @param[in] event Requested event.
  185. */
  186. __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_rtc, uint32_t event);
  187. /**
  188. *@}
  189. **/
  190. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  191. __STATIC_INLINE void nrf_rtc_cc_set(NRF_RTC_Type * p_rtc, uint32_t ch, uint32_t cc_val)
  192. {
  193. p_rtc->CC[ch] = cc_val;
  194. }
  195. __STATIC_INLINE uint32_t nrf_rtc_cc_get(NRF_RTC_Type * p_rtc, uint32_t ch)
  196. {
  197. return p_rtc->CC[ch];
  198. }
  199. __STATIC_INLINE void nrf_rtc_int_enable(NRF_RTC_Type * p_rtc, uint32_t mask)
  200. {
  201. p_rtc->INTENSET = mask;
  202. }
  203. __STATIC_INLINE void nrf_rtc_int_disable(NRF_RTC_Type * p_rtc, uint32_t mask)
  204. {
  205. p_rtc->INTENCLR = mask;
  206. }
  207. __STATIC_INLINE uint32_t nrf_rtc_int_is_enabled(NRF_RTC_Type * p_rtc, uint32_t mask)
  208. {
  209. return (p_rtc->INTENSET & mask);
  210. }
  211. __STATIC_INLINE uint32_t nrf_rtc_int_get(NRF_RTC_Type * p_rtc)
  212. {
  213. return p_rtc->INTENSET;
  214. }
  215. __STATIC_INLINE uint32_t nrf_rtc_event_pending(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
  216. {
  217. return *(volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event);
  218. }
  219. __STATIC_INLINE void nrf_rtc_event_clear(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
  220. {
  221. *((volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event)) = 0;
  222. #if __CORTEX_M == 0x04
  223. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_rtc + (uint32_t)event));
  224. (void)dummy;
  225. #endif
  226. }
  227. __STATIC_INLINE uint32_t nrf_rtc_counter_get(NRF_RTC_Type * p_rtc)
  228. {
  229. return p_rtc->COUNTER;
  230. }
  231. __STATIC_INLINE void nrf_rtc_prescaler_set(NRF_RTC_Type * p_rtc, uint32_t val)
  232. {
  233. ASSERT(val <= (RTC_PRESCALER_PRESCALER_Msk >> RTC_PRESCALER_PRESCALER_Pos));
  234. p_rtc->PRESCALER = val;
  235. }
  236. __STATIC_INLINE uint32_t rtc_prescaler_get(NRF_RTC_Type * p_rtc)
  237. {
  238. return p_rtc->PRESCALER;
  239. }
  240. __STATIC_INLINE uint32_t nrf_rtc_event_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_event_t event)
  241. {
  242. return (uint32_t)p_rtc + event;
  243. }
  244. __STATIC_INLINE uint32_t nrf_rtc_task_address_get(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task)
  245. {
  246. return (uint32_t)p_rtc + task;
  247. }
  248. __STATIC_INLINE void nrf_rtc_task_trigger(NRF_RTC_Type * p_rtc, nrf_rtc_task_t task)
  249. {
  250. *(__IO uint32_t *)((uint32_t)p_rtc + task) = 1;
  251. }
  252. __STATIC_INLINE void nrf_rtc_event_enable(NRF_RTC_Type * p_rtc, uint32_t mask)
  253. {
  254. p_rtc->EVTENSET = mask;
  255. }
  256. __STATIC_INLINE void nrf_rtc_event_disable(NRF_RTC_Type * p_rtc, uint32_t mask)
  257. {
  258. p_rtc->EVTENCLR = mask;
  259. }
  260. #endif
  261. #endif /* NRF_RTC_H */