nrf_drv_rtc.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. #ifndef NRF_DRV_RTC_H
  13. #define NRF_DRV_RTC_H
  14. #include "nordic_common.h"
  15. #include "nrf_drv_config.h"
  16. #include "nrf_drv_common.h"
  17. #include "nrf_rtc.h"
  18. #include "sdk_errors.h"
  19. /**
  20. * @addtogroup nrf_rtc RTC HAL and driver
  21. * @ingroup nrf_drivers
  22. * @brief Real timer counter (RTC) APIs.
  23. * @details The RTC HAL provides basic APIs for accessing the registers of the real time counter (RTC).
  24. * The RTC driver provides APIs on a higher level.
  25. *
  26. * @defgroup nrf_drv_rtc RTC driver
  27. * @{
  28. * @ingroup nrf_rtc
  29. * @brief Real timer counter (RTC) driver.
  30. */
  31. /**@brief Macro to convert microseconds into ticks. */
  32. #define RTC_US_TO_TICKS(us,freq) ((us*freq)/1000000)
  33. /**
  34. * @enum nrf_drv_rtc_int_type_t
  35. * @brief RTC driver interrupt types.
  36. */
  37. typedef enum
  38. {
  39. NRF_DRV_RTC_INT_COMPARE0 = 0, /**< Interrupt from COMPARE0 event. */
  40. NRF_DRV_RTC_INT_COMPARE1 = 1, /**< Interrupt from COMPARE1 event. */
  41. NRF_DRV_RTC_INT_COMPARE2 = 2, /**< Interrupt from COMPARE2 event. */
  42. NRF_DRV_RTC_INT_COMPARE3 = 3, /**< Interrupt from COMPARE3 event. */
  43. NRF_DRV_RTC_INT_TICK = 4, /**< Interrupt from TICK event. */
  44. NRF_DRV_RTC_INT_OVERFLOW = 5 /**< Interrupt from OVERFLOW event. */
  45. } nrf_drv_rtc_int_type_t;
  46. /**@brief RTC driver instance structure. */
  47. typedef struct
  48. {
  49. NRF_RTC_Type * p_reg; /**< Pointer to instance register set. */
  50. IRQn_Type irq; /**< Instance IRQ ID. */
  51. uint8_t instance_id; /**< Instance index. */
  52. uint8_t cc_channel_count; /**< Number of capture/compare channels. */
  53. } nrf_drv_rtc_t;
  54. /**@brief Macro for creating RTC driver instance.*/
  55. #define NRF_DRV_RTC_INSTANCE(id) \
  56. { \
  57. .p_reg = CONCAT_2(NRF_RTC, id), \
  58. .irq = CONCAT_3(RTC, id, _IRQn), \
  59. .instance_id = CONCAT_3(RTC, id, _INSTANCE_INDEX),\
  60. .cc_channel_count = NRF_RTC_CC_CHANNEL_COUNT(id), \
  61. }
  62. /**@brief RTC driver instance configuration structure. */
  63. typedef struct
  64. {
  65. uint16_t prescaler; /**< Prescaler. */
  66. uint8_t interrupt_priority; /**< Interrupt priority. */
  67. uint8_t tick_latency; /**< Maximum length of interrupt handler in ticks (max 7.7 ms). */
  68. bool reliable; /**< Reliable mode flag. */
  69. } nrf_drv_rtc_config_t;
  70. /**@brief RTC instance default configuration. */
  71. #define NRF_DRV_RTC_DEFAULT_CONFIG(id) \
  72. { \
  73. .prescaler = (uint16_t)(RTC_INPUT_FREQ / CONCAT_3(RTC, id, _CONFIG_FREQUENCY))-1, \
  74. .interrupt_priority = CONCAT_3(RTC, id, _CONFIG_IRQ_PRIORITY), \
  75. .reliable = CONCAT_3(RTC, id, _CONFIG_RELIABLE), \
  76. .tick_latency = RTC_US_TO_TICKS(NRF_MAXIMUM_LATENCY_US, CONCAT_3(RTC, id, _CONFIG_FREQUENCY)), \
  77. }
  78. /**@brief RTC driver instance handler type. */
  79. typedef void (*nrf_drv_rtc_handler_t)(nrf_drv_rtc_int_type_t int_type);
  80. /**@brief Function for initializing the RTC driver instance.
  81. *
  82. * After initialization, the instance is in power off state.
  83. *
  84. * @param[in] p_instance Pointer to the instance.
  85. * @param[in] p_config Initial configuration. Default configuration used if NULL.
  86. * @param[in] handler User's event handler.
  87. *
  88. * @retval NRF_SUCCESS If successfully initialized.
  89. * @retval NRF_ERROR_INVALID_PARAM If no handler was provided.
  90. * @retval NRF_ERROR_INVALID_STATE If the instance is already initialized.
  91. */
  92. ret_code_t nrf_drv_rtc_init(nrf_drv_rtc_t const * const p_instance,
  93. nrf_drv_rtc_config_t const * p_config,
  94. nrf_drv_rtc_handler_t handler);
  95. /**@brief Function for uninitializing the RTC driver instance.
  96. *
  97. * After uninitialization, the instance is in idle state. The hardware should return to the state
  98. * before initialization. The function asserts if the instance is in idle state.
  99. *
  100. * @param[in] p_instance Pointer to the instance.
  101. */
  102. void nrf_drv_rtc_uninit(nrf_drv_rtc_t const * const p_instance);
  103. /**@brief Function for enabling the RTC driver instance.
  104. *
  105. * @note Function asserts if instance is enabled.
  106. *
  107. * @param[in] p_instance Pointer to the instance.
  108. */
  109. void nrf_drv_rtc_enable(nrf_drv_rtc_t const * const p_instance);
  110. /**@brief Function for disabling the RTC driver instance.
  111. *
  112. * @note Function asserts if instance is disabled.
  113. *
  114. * @param[in] p_instance Pointer to instance.
  115. */
  116. void nrf_drv_rtc_disable(nrf_drv_rtc_t const * const p_instance);
  117. /**@brief Function for setting a compare channel.
  118. *
  119. * The function asserts if the instance is not initialized or if the channel parameter is
  120. * wrong. The function powers on the instance if the instance was in power off state.
  121. *
  122. * The driver is not entering a critical section when configuring RTC, which means that it can be
  123. * preempted for a certain amount of time. When the driver was preempted and the value to be set
  124. * is short in time, there is a risk that the driver sets a compare value that is
  125. * behind. If RTCn_CONFIG_RELIABLE is 1 for the given instance, the Reliable mode handles that case.
  126. * However, to detect if the requested value is behind, this mode makes the following assumptions:
  127. * - The maximum preemption time in ticks (8-bit value) is known and is less than 7.7 ms
  128. * (for prescaler = 0, RTC frequency 32 kHz).
  129. * - The requested absolute compare value is not bigger than (0x00FFFFFF)-tick_latency. It is
  130. * the user's responsibility to ensure that.
  131. *
  132. * @param[in] p_instance Pointer to the instance.
  133. * @param[in] channel One of the instance's channels.
  134. * @param[in] val Absolute value to be set in the compare register.
  135. * @param[in] enable_irq True to enable the interrupt. False to disable the interrupt.
  136. *
  137. * @retval NRF_SUCCESS If the procedure was successful.
  138. * @retval NRF_ERROR_TIMEOUT If the compare was not set because the request value is behind the current counter
  139. * value. This error can only be reported if RTCn_CONFIG_RELIABLE = 1.
  140. */
  141. ret_code_t nrf_drv_rtc_cc_set(nrf_drv_rtc_t const * const p_instance,
  142. uint32_t channel,
  143. uint32_t val,
  144. bool enable_irq);
  145. /**@brief Function for disabling a channel.
  146. *
  147. * This function disables channel events and channel interrupts. The function asserts if the instance is not
  148. * initialized or if the channel parameter is wrong.
  149. *
  150. * @param[in] p_instance Pointer to the instance.
  151. * @param[in] channel One of the instance's channels.
  152. *
  153. * @retval NRF_SUCCESS If the procedure was successful.
  154. * @retval NRF_ERROR_TIMEOUT If an interrupt was pending on the requested channel.
  155. */
  156. ret_code_t nrf_drv_rtc_cc_disable(nrf_drv_rtc_t const * const p_instance, uint32_t channel);
  157. /**@brief Function for enabling tick.
  158. *
  159. * This function enables the tick event and optionally the interrupt. The function asserts if the instance is not
  160. * powered on.
  161. *
  162. * @param[in] p_instance Pointer to the instance.
  163. * @param[in] enable_irq True to enable the interrupt. False to disable the interrupt.
  164. */
  165. void nrf_drv_rtc_tick_enable(nrf_drv_rtc_t const * const p_instance, bool enable_irq);
  166. /**@brief Function for disabling tick.
  167. *
  168. * This function disables the tick event and interrupt.
  169. *
  170. * @param[in] p_instance Pointer to the instance.
  171. */
  172. void nrf_drv_rtc_tick_disable(nrf_drv_rtc_t const * const p_instance);
  173. /**@brief Function for enabling overflow.
  174. *
  175. * This function enables the overflow event and optionally the interrupt. The function asserts if the instance is
  176. * not powered on.
  177. *
  178. * @param[in] p_instance Pointer to the instance.
  179. * @param[in] enable_irq True to enable the interrupt. False to disable the interrupt.
  180. */
  181. void nrf_drv_rtc_overflow_enable(nrf_drv_rtc_t const * const p_instance, bool enable_irq);
  182. /**@brief Function for disabling overflow.
  183. *
  184. * This function disables the overflow event and interrupt.
  185. *
  186. * @param[in] p_instance Pointer to the instance.
  187. */
  188. void nrf_drv_rtc_overflow_disable(nrf_drv_rtc_t const * const p_instance);
  189. /**@brief Function for getting the maximum relative ticks value that can be set in the compare channel.
  190. *
  191. * When a SoftDevice is used, it occupies the highest level interrupt, so that the application code can be
  192. * interrupted at any moment for a certain period of time. If Reliable mode is enabled, the provided
  193. * maximum latency is taken into account and the return value is smaller than the RTC counter
  194. * resolution. If Reliable mode is disabled, the return value equals the counter resolution.
  195. *
  196. * @param[in] p_instance Pointer to the instance.
  197. *
  198. * @retval ticks Maximum ticks value.
  199. */
  200. uint32_t nrf_drv_rtc_max_ticks_get(nrf_drv_rtc_t const * const p_instance);
  201. /**@brief Function for disabling all instance interrupts.
  202. *
  203. * @param[in] p_instance Pointer to the instance.
  204. * @param[in] p_mask Pointer to the location where the mask is filled.
  205. */
  206. __STATIC_INLINE void nrf_drv_rtc_int_disable(nrf_drv_rtc_t const * const p_instance,
  207. uint32_t * p_mask);
  208. /**@brief Function for enabling instance interrupts.
  209. *
  210. * @param[in] p_instance Pointer to the instance.
  211. * @param[in] mask Mask of interrupts to enable.
  212. */
  213. __STATIC_INLINE void nrf_drv_rtc_int_enable(nrf_drv_rtc_t const * const p_instance, uint32_t mask);
  214. /**@brief Function for retrieving the current counter value.
  215. *
  216. * This function asserts if the instance is not powered on or if p_val is NULL.
  217. *
  218. * @param[in] p_instance Pointer to the instance.
  219. *
  220. * @retval value Counter value.
  221. */
  222. __STATIC_INLINE uint32_t nrf_drv_rtc_counter_get(nrf_drv_rtc_t const * const p_instance);
  223. /**@brief Function for clearing the counter value.
  224. *
  225. * This function asserts if the instance is not powered on.
  226. *
  227. * @param[in] p_instance Pointer to the instance.
  228. */
  229. __STATIC_INLINE void nrf_drv_rtc_counter_clear(nrf_drv_rtc_t const * const p_instance);
  230. /**@brief Function for returning a requested task address for the RTC driver instance.
  231. *
  232. * This function asserts if the output pointer is NULL. The task address can be used by the PPI module.
  233. *
  234. * @param[in] p_instance Pointer to the instance.
  235. * @param[in] task One of the peripheral tasks.
  236. *
  237. * @retval Address of task register.
  238. */
  239. __STATIC_INLINE uint32_t nrf_drv_rtc_task_address_get(nrf_drv_rtc_t const * const p_instance,
  240. nrf_rtc_task_t task);
  241. /**@brief Function for returning a requested event address for the RTC driver instance.
  242. *
  243. * This function asserts if the output pointer is NULL. The event address can be used by the PPI module.
  244. *
  245. * @param[in] p_instance Pointer to the instance.
  246. * @param[in] event One of the peripheral events.
  247. *
  248. * @retval Address of event register.
  249. */
  250. __STATIC_INLINE uint32_t nrf_drv_rtc_event_address_get(nrf_drv_rtc_t const * const p_instance,
  251. nrf_rtc_event_t event);
  252. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  253. __STATIC_INLINE void nrf_drv_rtc_int_disable(nrf_drv_rtc_t const * const p_instance,
  254. uint32_t * p_mask)
  255. {
  256. *p_mask = nrf_rtc_int_get(p_instance->p_reg);
  257. nrf_rtc_int_disable(p_instance->p_reg, NRF_RTC_INT_TICK_MASK |
  258. NRF_RTC_INT_OVERFLOW_MASK |
  259. NRF_RTC_INT_COMPARE0_MASK |
  260. NRF_RTC_INT_COMPARE1_MASK |
  261. NRF_RTC_INT_COMPARE2_MASK |
  262. NRF_RTC_INT_COMPARE3_MASK);
  263. }
  264. __STATIC_INLINE void nrf_drv_rtc_int_enable(nrf_drv_rtc_t const * const p_instance, uint32_t mask)
  265. {
  266. nrf_rtc_int_enable(p_instance->p_reg, mask);
  267. }
  268. __STATIC_INLINE uint32_t nrf_drv_rtc_counter_get(nrf_drv_rtc_t const * const p_instance)
  269. {
  270. return nrf_rtc_counter_get(p_instance->p_reg);
  271. }
  272. __STATIC_INLINE void nrf_drv_rtc_counter_clear(nrf_drv_rtc_t const * const p_instance)
  273. {
  274. nrf_rtc_task_trigger(p_instance->p_reg,NRF_RTC_TASK_CLEAR);
  275. }
  276. __STATIC_INLINE uint32_t nrf_drv_rtc_task_address_get(nrf_drv_rtc_t const * const p_instance,
  277. nrf_rtc_task_t task)
  278. {
  279. return nrf_rtc_task_address_get(p_instance->p_reg, task);
  280. }
  281. __STATIC_INLINE uint32_t nrf_drv_rtc_event_address_get(nrf_drv_rtc_t const * const p_instance,
  282. nrf_rtc_event_t event)
  283. {
  284. return nrf_rtc_event_address_get(p_instance->p_reg, event);
  285. }
  286. #endif /* SUPPRESS_INLINE_IMPLEMENTATION */
  287. /**
  288. *@}
  289. **/
  290. #endif /* NRF_DRV_RTC_H */