drv_rtc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**
  2. * Copyright (c) 2018 - 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 DRV_RTC_H__
  41. #define DRV_RTC_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_rtc.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup drv_rtc RTC driver
  49. * @{
  50. * @ingroup app_timer
  51. * @brief Real Timer Counter (RTC) peripheral driver for app_timer.
  52. */
  53. /** @brief Maximum RTC counter value. */
  54. #define DRV_RTC_MAX_CNT RTC_COUNTER_COUNTER_Msk
  55. /** @brief Time requires to update registers between RTC and MCU domains. */
  56. #define DRV_RTC_CONFIG_APPLY_TIME_US 33
  57. /**
  58. * @brief Minimum delta value between set value and counter value.
  59. *
  60. * RTC peripheral requires two ticks to be sure that value it properly set in RTC value. Compare
  61. * channel function requires additional one tick to avoid problematic situations (lack or additional
  62. * unspecified event) when Compare Channel register is reseting or setting to N+2 value.
  63. */
  64. #define DRV_RTC_MIN_TICK_HANDLED 3
  65. /** @brief Macro to convert microseconds into ticks. */
  66. #define DRV_RTC_US_TO_TICKS(us,freq) (us >= 2^17 ? \
  67. ((((us)/1000)*(freq))/1000U) : (((us)*(freq))/1000000U) )
  68. /** @brief RTC driver instance structure. */
  69. typedef struct
  70. {
  71. NRF_RTC_Type * p_reg; /**< Pointer to instance register set. */
  72. IRQn_Type irq; /**< Instance IRQ ID. */
  73. uint8_t instance_id; /**< Instance index. */
  74. uint8_t cc_channel_count; /**< Number of capture/compare channels. */
  75. } drv_rtc_t;
  76. /** @brief Macro for creating RTC driver instance.*/
  77. #define DRV_RTC_INSTANCE(id) \
  78. { \
  79. .p_reg = NRFX_CONCAT_2(NRF_RTC, id), \
  80. .irq = NRFX_CONCAT_3(RTC, id, _IRQn), \
  81. .instance_id = NRFX_CONCAT_3(DRV_RTC_RTC, id, _INST_IDX), \
  82. .cc_channel_count = NRF_RTC_CC_CHANNEL_COUNT(id), \
  83. }
  84. enum {
  85. #if defined(APP_TIMER_V2_RTC0_ENABLED)
  86. DRV_RTC_RTC0_INST_IDX,
  87. #endif
  88. #if defined(APP_TIMER_V2_RTC1_ENABLED)
  89. DRV_RTC_RTC1_INST_IDX,
  90. #endif
  91. #if defined(APP_TIMER_V2_RTC2_ENABLED)
  92. DRV_RTC_RTC2_INST_IDX,
  93. #endif
  94. DRV_RTC_ENABLED_COUNT
  95. };
  96. /** @brief RTC driver instance configuration structure. */
  97. typedef struct
  98. {
  99. uint16_t prescaler; /**< Prescaler. */
  100. uint8_t interrupt_priority; /**< Interrupt priority. */
  101. } drv_rtc_config_t;
  102. /** @brief RTC instance default configuration. */
  103. #define DRV_RTC_DEFAULT_CONFIG \
  104. { \
  105. .prescaler = RTC_FREQ_TO_PRESCALER(DRV_RTC_DEFAULT_CONFIG_FREQUENCY), \
  106. .interrupt_priority = DRV_RTC_DEFAULT_CONFIG_IRQ_PRIORITY, \
  107. }
  108. /** @brief RTC driver instance handler type. */
  109. typedef void (*drv_rtc_handler_t)(drv_rtc_t const * const p_instance);
  110. /**
  111. * @brief Function for initializing the RTC driver instance.
  112. *
  113. * After initialization, the instance is in power off state. The LFCLK (@ref nrfx_clock)
  114. * has to be started before using @ref drv_rtc.
  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. Must not be NULL.
  119. *
  120. * @retval NRF_SUCCESS If successfully initialized.
  121. * @retval NRF_ERROR_INVALID_STATE If the instance is already initialized.
  122. */
  123. ret_code_t drv_rtc_init(drv_rtc_t const * const p_instance,
  124. drv_rtc_config_t const * p_config,
  125. drv_rtc_handler_t handler);
  126. /**
  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 drv_rtc_uninit(drv_rtc_t const * const p_instance);
  135. /**
  136. * @brief Function for starting RTC clock.
  137. *
  138. * @param[in] p_instance Pointer to the driver instance structure.
  139. */
  140. void drv_rtc_start(drv_rtc_t const * const p_instance);
  141. /**
  142. * @brief Function for stopping RTC clock.
  143. *
  144. * @param[in] p_instance Pointer to the driver instance structure.
  145. */
  146. void drv_rtc_stop(drv_rtc_t const * const p_instance);
  147. /**
  148. * @brief Function for configuring compare channel.
  149. *
  150. * @note Function disables interrupts and only enable compare events. Remember to enable interrupt
  151. * using @ref drv_rtc_compare_enable in case of using it.
  152. *
  153. * @param[in] p_instance Pointer to the driver instance structure.
  154. * @param[in] cc Compare channel index.
  155. * @param[in] abs_value Absolute value to be set in the compare register.
  156. * @param[in] irq_enable True to enable interrupt.
  157. */
  158. void drv_rtc_compare_set(drv_rtc_t const * const p_instance,
  159. uint32_t cc,
  160. uint32_t abs_value,
  161. bool irq_enable);
  162. /**
  163. * @brief Function for configuring compare channel with safe window.
  164. *
  165. * Maximum possible relative value is limited by safe window to detect
  166. * cases when requested compare event has already occured.
  167. *
  168. * @param[in] p_instance Pointer to the driver instance structure.
  169. * @param[in] cc Compare channel index.
  170. * @param[in] abs_value Absolute value to be set in the compare register.
  171. * @param[in] safe_window Width of the safe window.
  172. *
  173. * @retval NRF_ERROR_TIMEOUT If @par abs_value is in safe window of event occured before
  174. * enabling compare channel intterupt.
  175. * @retval NRF_SUCCESS If successfully set.
  176. */
  177. ret_code_t drv_rtc_windowed_compare_set(drv_rtc_t const * const p_instance,
  178. uint32_t cc,
  179. uint32_t abs_value,
  180. uint32_t safe_window);
  181. /**
  182. * @brief Function for enabling overflow event and interrupt.
  183. *
  184. * @param[in] p_instance Pointer to the driver instance structure.
  185. * @param[in] irq_enable True to enable interrupt.
  186. */
  187. void drv_rtc_overflow_enable(drv_rtc_t const * const p_instance, bool irq_enable);
  188. /**
  189. * @brief Function for diabling overflow event and interrupt.
  190. *
  191. * @param[in] p_instance Pointer to the driver instance structure.
  192. */
  193. void drv_rtc_overflow_disable(drv_rtc_t const * const p_instance);
  194. /**
  195. * @brief Function for checking if overflow event has occured.
  196. *
  197. * @note Event is cleared after reading.
  198. *
  199. * @param[in] p_instance Pointer to the driver instance structure.
  200. *
  201. * @return True if interrupt pending, false otherwise.
  202. */
  203. bool drv_rtc_overflow_pending(drv_rtc_t const * const p_instance);
  204. /**
  205. * @brief Function for enabling tick event and interrupt.
  206. *
  207. * @param[in] p_instance Pointer to the driver instance structure.
  208. * @param[in] irq_enable True to enable interrupt.
  209. */
  210. void drv_rtc_tick_enable(drv_rtc_t const * const p_instance, bool irq_enable);
  211. /**
  212. * @brief Function for disabling tick event and interrupt.
  213. *
  214. * @param[in] p_instance Pointer to the driver instance structure.
  215. */
  216. void drv_rtc_tick_disable(drv_rtc_t const * const p_instance);
  217. /**
  218. * @brief Function for checking if tick event has occured.
  219. *
  220. * @param[in] p_instance Pointer to the driver instance structure.
  221. *
  222. * @return True if interrupt pending, false otherwise.
  223. */
  224. bool drv_rtc_tick_pending(drv_rtc_t const * const p_instance);
  225. /**
  226. * @brief Function for enabling compare channel event and interrupt.
  227. *
  228. * @param[in] p_instance Pointer to the driver instance structure.
  229. * @param[in] cc Compare channel index.
  230. * @param[in] irq_enable True to enable interrupt.
  231. */
  232. void drv_rtc_compare_enable(drv_rtc_t const * const p_instance,
  233. uint32_t cc,
  234. bool irq_enable);
  235. /**
  236. * @brief Function for disabling compare channel event and interrupt.
  237. *
  238. * @param[in] p_instance Pointer to the driver instance structure.
  239. * @param[in] cc Compare channel index.
  240. */
  241. void drv_rtc_compare_disable(drv_rtc_t const * const p_instance, uint32_t cc);
  242. /**
  243. * @brief Function for checking if compare channel event has occured.
  244. *
  245. * @param[in] p_instance Pointer to the driver instance structure.
  246. * @param[in] cc Compare channel index.
  247. *
  248. * @return True if interrupt pending, false otherwise.
  249. */
  250. bool drv_rtc_compare_pending(drv_rtc_t const * const p_instance, uint32_t cc);
  251. /**
  252. * @brief Function for getting current value of RTC counter.
  253. *
  254. * @param[in] p_instance Pointer to the driver instance structure.
  255. *
  256. * @return Counter value.
  257. */
  258. uint32_t drv_rtc_counter_get(drv_rtc_t const * const p_instance);
  259. /**
  260. * @brief Function for triggering RTC interrupt.
  261. *
  262. * @param[in] p_instance Pointer to the driver instance structure.
  263. */
  264. void drv_rtc_irq_trigger(drv_rtc_t const * const p_instance);
  265. /** @} */
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif // DRV_RTC_H__