nrfx_rtc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 NRFX_RTC_H__
  41. #define NRFX_RTC_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_rtc.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_rtc RTC driver
  49. * @{
  50. * @ingroup nrf_rtc
  51. * @brief Real Timer Counter (RTC) peripheral driver.
  52. */
  53. /** @brief Macro for converting microseconds into ticks. */
  54. #define NRFX_RTC_US_TO_TICKS(us,freq) (((us) * (freq)) / 1000000U)
  55. /** @brief RTC driver interrupt types. */
  56. typedef enum
  57. {
  58. NRFX_RTC_INT_COMPARE0 = 0, /**< Interrupt from COMPARE0 event. */
  59. NRFX_RTC_INT_COMPARE1 = 1, /**< Interrupt from COMPARE1 event. */
  60. NRFX_RTC_INT_COMPARE2 = 2, /**< Interrupt from COMPARE2 event. */
  61. NRFX_RTC_INT_COMPARE3 = 3, /**< Interrupt from COMPARE3 event. */
  62. NRFX_RTC_INT_TICK = 4, /**< Interrupt from TICK event. */
  63. NRFX_RTC_INT_OVERFLOW = 5 /**< Interrupt from OVERFLOW event. */
  64. } nrfx_rtc_int_type_t;
  65. /** @brief RTC driver instance structure. */
  66. typedef struct
  67. {
  68. NRF_RTC_Type * p_reg; /**< Pointer to instance register set. */
  69. IRQn_Type irq; /**< Instance IRQ ID. */
  70. uint8_t instance_id; /**< Index of the driver instance. For internal use only. */
  71. uint8_t cc_channel_count; /**< Number of capture/compare channels. */
  72. } nrfx_rtc_t;
  73. /** @brief Macro for creating an RTC driver instance. */
  74. #define NRFX_RTC_INSTANCE(id) \
  75. { \
  76. .p_reg = NRFX_CONCAT_2(NRF_RTC, id), \
  77. .irq = NRFX_CONCAT_3(RTC, id, _IRQn), \
  78. .instance_id = NRFX_CONCAT_3(NRFX_RTC, id, _INST_IDX), \
  79. .cc_channel_count = NRF_RTC_CC_CHANNEL_COUNT(id), \
  80. }
  81. #ifndef __NRFX_DOXYGEN__
  82. enum {
  83. #if NRFX_CHECK(NRFX_RTC0_ENABLED)
  84. NRFX_RTC0_INST_IDX,
  85. #endif
  86. #if NRFX_CHECK(NRFX_RTC1_ENABLED)
  87. NRFX_RTC1_INST_IDX,
  88. #endif
  89. #if NRFX_CHECK(NRFX_RTC2_ENABLED)
  90. NRFX_RTC2_INST_IDX,
  91. #endif
  92. NRFX_RTC_ENABLED_COUNT
  93. };
  94. #endif
  95. /** @brief RTC driver instance configuration structure. */
  96. typedef struct
  97. {
  98. uint16_t prescaler; /**< Prescaler. */
  99. uint8_t interrupt_priority; /**< Interrupt priority. */
  100. uint8_t tick_latency; /**< Maximum length of the interrupt handler in ticks (maximum 7.7 ms). */
  101. bool reliable; /**< Reliable mode flag. */
  102. } nrfx_rtc_config_t;
  103. /** @brief RTC instance default configuration. */
  104. #define NRFX_RTC_DEFAULT_CONFIG \
  105. { \
  106. .prescaler = RTC_FREQ_TO_PRESCALER(NRFX_RTC_DEFAULT_CONFIG_FREQUENCY), \
  107. .interrupt_priority = NRFX_RTC_DEFAULT_CONFIG_IRQ_PRIORITY, \
  108. .tick_latency = NRFX_RTC_US_TO_TICKS(NRFX_RTC_MAXIMUM_LATENCY_US, \
  109. NRFX_RTC_DEFAULT_CONFIG_FREQUENCY), \
  110. .reliable = NRFX_RTC_DEFAULT_CONFIG_RELIABLE, \
  111. }
  112. /** @brief RTC driver instance handler type. */
  113. typedef void (*nrfx_rtc_handler_t)(nrfx_rtc_int_type_t int_type);
  114. /**
  115. * @brief Function for initializing the RTC driver instance.
  116. *
  117. * After initialization, the instance is in power off state.
  118. *
  119. * @param[in] p_instance Pointer to the driver instance structure.
  120. * @param[in] p_config Pointer to the structure with the initial configuration.
  121. * @param[in] handler Event handler provided by the user.
  122. * Must not be NULL.
  123. *
  124. * @retval NRFX_SUCCESS Successfully initialized.
  125. * @retval NRFX_ERROR_INVALID_STATE The instance is already initialized.
  126. */
  127. nrfx_err_t nrfx_rtc_init(nrfx_rtc_t const * const p_instance,
  128. nrfx_rtc_config_t const * p_config,
  129. nrfx_rtc_handler_t handler);
  130. /**
  131. * @brief Function for uninitializing the RTC driver instance.
  132. *
  133. * After uninitialization, the instance is in idle state. The hardware should return to the state
  134. * before initialization.
  135. *
  136. * @param[in] p_instance Pointer to the driver instance structure.
  137. */
  138. void nrfx_rtc_uninit(nrfx_rtc_t const * const p_instance);
  139. /**
  140. * @brief Function for enabling the RTC driver instance.
  141. *
  142. * @param[in] p_instance Pointer to the driver instance structure.
  143. */
  144. void nrfx_rtc_enable(nrfx_rtc_t const * const p_instance);
  145. /**
  146. * @brief Function for disabling the RTC driver instance.
  147. *
  148. * @param[in] p_instance Pointer to the driver instance structure.
  149. */
  150. void nrfx_rtc_disable(nrfx_rtc_t const * const p_instance);
  151. /**
  152. * @brief Function for setting a compare channel.
  153. *
  154. * The function powers on the instance if the instance was in power off state.
  155. *
  156. * The driver is not entering a critical section when configuring RTC, which means that it can be
  157. * preempted for a certain amount of time. When the driver was preempted and the value to be set
  158. * is short in time, there is a risk that the driver sets a compare value that is
  159. * behind. In this case, if the reliable mode is enabled for the specified instance,
  160. * the risk is handled.
  161. * However, to detect if the requested value is behind, this mode makes the following assumptions:
  162. * - The maximum preemption time in ticks (8-bit value) is known and is less than 7.7 ms
  163. * (for prescaler = 0, RTC frequency 32 kHz).
  164. * - The requested absolute compare value is not bigger than (0x00FFFFFF)-tick_latency. It is
  165. * the user's responsibility to ensure this.
  166. *
  167. * @param[in] p_instance Pointer to the driver instance structure.
  168. * @param[in] channel One of the channels of the instance.
  169. * @param[in] val Absolute value to be set in the compare register.
  170. * @param[in] enable_irq True to enable the interrupt. False to disable the interrupt.
  171. *
  172. * @retval NRFX_SUCCESS The procedure is successful.
  173. * @retval NRFX_ERROR_TIMEOUT The compare is not set because the request value is behind the
  174. * current counter value. This error can only be reported
  175. * if the reliable mode is enabled.
  176. */
  177. nrfx_err_t nrfx_rtc_cc_set(nrfx_rtc_t const * const p_instance,
  178. uint32_t channel,
  179. uint32_t val,
  180. bool enable_irq);
  181. /**
  182. * @brief Function for disabling a channel.
  183. *
  184. * This function disables channel events and channel interrupts.
  185. *
  186. * @param[in] p_instance Pointer to the driver instance structure.
  187. * @param[in] channel One of the channels of the instance.
  188. *
  189. * @retval NRFX_SUCCESS The procedure is successful.
  190. * @retval NRFX_ERROR_TIMEOUT Interrupt is pending on the requested channel.
  191. */
  192. nrfx_err_t nrfx_rtc_cc_disable(nrfx_rtc_t const * const p_instance, uint32_t channel);
  193. /**
  194. * @brief Function for enabling the TICK event.
  195. *
  196. * This function enables the tick event and optionally the interrupt.
  197. *
  198. * @param[in] p_instance Pointer to the driver instance structure.
  199. * @param[in] enable_irq True to enable the interrupt. False to disable the interrupt.
  200. */
  201. void nrfx_rtc_tick_enable(nrfx_rtc_t const * const p_instance, bool enable_irq);
  202. /**
  203. * @brief Function for disabling the TICK event.
  204. *
  205. * This function disables the TICK event and interrupt.
  206. *
  207. * @param[in] p_instance Pointer to the driver instance structure.
  208. */
  209. void nrfx_rtc_tick_disable(nrfx_rtc_t const * const p_instance);
  210. /**
  211. * @brief Function for enabling overflow.
  212. *
  213. * This function enables the overflow event and optionally the interrupt.
  214. *
  215. * @param[in] p_instance Pointer to the driver instance structure.
  216. * @param[in] enable_irq True to enable the interrupt. False to disable the interrupt.
  217. */
  218. void nrfx_rtc_overflow_enable(nrfx_rtc_t const * const p_instance, bool enable_irq);
  219. /**
  220. * @brief Function for disabling overflow.
  221. *
  222. * This function disables the overflow event and interrupt.
  223. *
  224. * @param[in] p_instance Pointer to the driver instance structure.
  225. */
  226. void nrfx_rtc_overflow_disable(nrfx_rtc_t const * const p_instance);
  227. /**
  228. * @brief Function for getting the maximum relative tick value that can be set in the compare channel.
  229. *
  230. * When a stack (for example SoftDevice) is used and it occupies high priority interrupts,
  231. * the application code can be interrupted at any moment for a certain period of time.
  232. * If the reliable mode is enabled, the provided maximum latency is taken into account
  233. * and the return value is smaller than the RTC counter resolution.
  234. * If the reliable mode is disabled, the return value equals the counter resolution.
  235. *
  236. * @param[in] p_instance Pointer to the driver instance structure.
  237. *
  238. * @return Maximum ticks value.
  239. */
  240. uint32_t nrfx_rtc_max_ticks_get(nrfx_rtc_t const * const p_instance);
  241. /**
  242. * @brief Function for disabling all instance interrupts.
  243. *
  244. * @param[in] p_instance Pointer to the driver instance structure.
  245. * @param[in] p_mask Pointer to the location where the mask is filled.
  246. */
  247. __STATIC_INLINE void nrfx_rtc_int_disable(nrfx_rtc_t const * const p_instance,
  248. uint32_t * p_mask);
  249. /**
  250. * @brief Function for enabling instance interrupts.
  251. *
  252. * @param[in] p_instance Pointer to the driver instance structure.
  253. * @param[in] mask Mask of interrupts to enable.
  254. */
  255. __STATIC_INLINE void nrfx_rtc_int_enable(nrfx_rtc_t const * const p_instance, uint32_t mask);
  256. /**
  257. * @brief Function for retrieving the current counter value.
  258. *
  259. * @param[in] p_instance Pointer to the driver instance structure.
  260. *
  261. * @return Counter value.
  262. */
  263. __STATIC_INLINE uint32_t nrfx_rtc_counter_get(nrfx_rtc_t const * const p_instance);
  264. /**
  265. * @brief Function for clearing the counter value.
  266. *
  267. * @param[in] p_instance Pointer to the driver instance structure.
  268. */
  269. __STATIC_INLINE void nrfx_rtc_counter_clear(nrfx_rtc_t const * const p_instance);
  270. /**
  271. * @brief Function for returning a requested task address for the RTC driver instance.
  272. *
  273. * The task address can be used by the PPI module.
  274. *
  275. * @param[in] p_instance Pointer to the instance.
  276. * @param[in] task One of the peripheral tasks.
  277. *
  278. * @return Address of task register.
  279. */
  280. __STATIC_INLINE uint32_t nrfx_rtc_task_address_get(nrfx_rtc_t const * const p_instance,
  281. nrf_rtc_task_t task);
  282. /**
  283. * @brief Function for returning a requested event address for the RTC driver instance.
  284. *
  285. * The event address can be used by the PPI module.
  286. *
  287. * @param[in] p_instance Pointer to the driver instance structure.
  288. * @param[in] event One of the peripheral events.
  289. *
  290. * @return Address of event register.
  291. */
  292. __STATIC_INLINE uint32_t nrfx_rtc_event_address_get(nrfx_rtc_t const * const p_instance,
  293. nrf_rtc_event_t event);
  294. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  295. __STATIC_INLINE void nrfx_rtc_int_disable(nrfx_rtc_t const * const p_instance,
  296. uint32_t * p_mask)
  297. {
  298. *p_mask = nrf_rtc_int_get(p_instance->p_reg);
  299. nrf_rtc_int_disable(p_instance->p_reg, NRF_RTC_INT_TICK_MASK |
  300. NRF_RTC_INT_OVERFLOW_MASK |
  301. NRF_RTC_INT_COMPARE0_MASK |
  302. NRF_RTC_INT_COMPARE1_MASK |
  303. NRF_RTC_INT_COMPARE2_MASK |
  304. NRF_RTC_INT_COMPARE3_MASK);
  305. }
  306. __STATIC_INLINE void nrfx_rtc_int_enable(nrfx_rtc_t const * const p_instance, uint32_t mask)
  307. {
  308. nrf_rtc_int_enable(p_instance->p_reg, mask);
  309. }
  310. __STATIC_INLINE uint32_t nrfx_rtc_counter_get(nrfx_rtc_t const * const p_instance)
  311. {
  312. return nrf_rtc_counter_get(p_instance->p_reg);
  313. }
  314. __STATIC_INLINE void nrfx_rtc_counter_clear(nrfx_rtc_t const * const p_instance)
  315. {
  316. nrf_rtc_task_trigger(p_instance->p_reg, NRF_RTC_TASK_CLEAR);
  317. }
  318. __STATIC_INLINE uint32_t nrfx_rtc_task_address_get(nrfx_rtc_t const * const p_instance,
  319. nrf_rtc_task_t task)
  320. {
  321. return nrf_rtc_task_address_get(p_instance->p_reg, task);
  322. }
  323. __STATIC_INLINE uint32_t nrfx_rtc_event_address_get(nrfx_rtc_t const * const p_instance,
  324. nrf_rtc_event_t event)
  325. {
  326. return nrf_rtc_event_address_get(p_instance->p_reg, event);
  327. }
  328. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  329. /** @} */
  330. void nrfx_rtc_0_irq_handler(void);
  331. void nrfx_rtc_1_irq_handler(void);
  332. void nrfx_rtc_2_irq_handler(void);
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif // NRFX_RTC_H__