drv_rtc.h 11 KB

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