nrfx_rtc.h 15 KB

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