nrf_drv_timer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* Copyright (c) 2015 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. /**@file
  13. * @addtogroup nrf_timer Timer HAL and driver
  14. * @ingroup nrf_drivers
  15. * @brief Timer APIs.
  16. * @details The timer HAL provides basic APIs for accessing the registers
  17. * of the timer. The timer driver provides APIs on a higher level.
  18. *
  19. * @defgroup lib_driver_timer Timer driver
  20. * @{
  21. * @ingroup nrf_timer
  22. * @brief Multi-instance timer driver.
  23. */
  24. #ifndef NRF_DRV_TIMER_H__
  25. #define NRF_DRV_TIMER_H__
  26. #include "nordic_common.h"
  27. #include "nrf_drv_config.h"
  28. #include "nrf_timer.h"
  29. #include "sdk_errors.h"
  30. #include "nrf_assert.h"
  31. /**
  32. * @brief Timer driver instance data structure.
  33. */
  34. typedef struct
  35. {
  36. NRF_TIMER_Type * p_reg; ///< Pointer to the structure with TIMER peripheral instance registers.
  37. uint8_t instance_id; ///< Driver instance index.
  38. uint8_t cc_channel_count; ///< Number of capture/compare channels.
  39. } nrf_drv_timer_t;
  40. /**
  41. * @brief Macro for creating a timer driver instance.
  42. */
  43. #define NRF_DRV_TIMER_INSTANCE(id) \
  44. { \
  45. .p_reg = CONCAT_2(NRF_TIMER, id), \
  46. .instance_id = CONCAT_3(TIMER, id, _INSTANCE_INDEX), \
  47. .cc_channel_count = NRF_TIMER_CC_CHANNEL_COUNT(id), \
  48. }
  49. /**
  50. * @brief Timer driver instance configuration structure.
  51. */
  52. typedef struct
  53. {
  54. nrf_timer_frequency_t frequency; ///< Frequency.
  55. nrf_timer_mode_t mode; ///< Mode of operation.
  56. nrf_timer_bit_width_t bit_width; ///< Bit width.
  57. uint8_t interrupt_priority; ///< Interrupt priority.
  58. void * p_context; ///< Context passed to interrupt handler.
  59. } nrf_drv_timer_config_t;
  60. #define TIMER_CONFIG_FREQUENCY(id) CONCAT_3(TIMER, id, _CONFIG_FREQUENCY)
  61. #define TIMER_CONFIG_MODE(id) CONCAT_3(TIMER, id, _CONFIG_MODE)
  62. #define TIMER_CONFIG_BIT_WIDTH(id) CONCAT_3(TIMER, id, _CONFIG_BIT_WIDTH)
  63. #define TIMER_CONFIG_IRQ_PRIORITY(id) CONCAT_3(TIMER, id, _CONFIG_IRQ_PRIORITY)
  64. /**
  65. * @brief Timer driver instance default configuration.
  66. */
  67. #define NRF_DRV_TIMER_DEFAULT_CONFIG(id) \
  68. { \
  69. .frequency = TIMER_CONFIG_FREQUENCY(id), \
  70. .mode = (nrf_timer_mode_t)TIMER_CONFIG_MODE(id), \
  71. .bit_width = (nrf_timer_bit_width_t)TIMER_CONFIG_BIT_WIDTH(id), \
  72. .interrupt_priority = TIMER_CONFIG_IRQ_PRIORITY(id), \
  73. .p_context = NULL \
  74. }
  75. /**
  76. * @brief Timer driver event handler type.
  77. *
  78. * @param[in] event_type Timer event.
  79. * @param[in] p_context General purpose parameter set during initialization of
  80. * the timer. This parameter can be used to pass
  81. * additional information to the handler function, for
  82. * example, the timer ID.
  83. */
  84. typedef void (* nrf_timer_event_handler_t)(nrf_timer_event_t event_type,
  85. void * p_context);
  86. /**
  87. * @brief Function for initializing the timer.
  88. *
  89. * @param[in] p_instance Timer instance.
  90. * @param[in] p_config Initial configuration.
  91. * If NULL, the default configuration is used.
  92. * @param[in] timer_event_handler Event handler provided by the user.
  93. * Must not be NULL.
  94. *
  95. * @retval NRF_SUCCESS If initialization was successful.
  96. * @retval NRF_ERROR_INVALID_STATE If the instance is already initialized.
  97. * @retval NRF_ERROR_INVALID_PARAM If no handler was provided.
  98. */
  99. ret_code_t nrf_drv_timer_init(nrf_drv_timer_t const * const p_instance,
  100. nrf_drv_timer_config_t const * p_config,
  101. nrf_timer_event_handler_t timer_event_handler);
  102. /**
  103. * @brief Function for uninitializing the timer.
  104. *
  105. * @param[in] p_instance Timer instance.
  106. */
  107. void nrf_drv_timer_uninit(nrf_drv_timer_t const * const p_instance);
  108. /**
  109. * @brief Function for turning on the timer.
  110. *
  111. * @param[in] p_instance Timer instance.
  112. */
  113. void nrf_drv_timer_enable(nrf_drv_timer_t const * const p_instance);
  114. /**
  115. * @brief Function for turning off the timer.
  116. *
  117. * Note that the timer will allow to enter the lowest possible SYSTEM_ON state
  118. * only after this function is called.
  119. *
  120. * @param[in] p_instance Timer instance.
  121. */
  122. void nrf_drv_timer_disable(nrf_drv_timer_t const * const p_instance);
  123. /**
  124. * @brief Function for pausing the timer.
  125. *
  126. * @param[in] p_instance Timer instance.
  127. */
  128. void nrf_drv_timer_pause(nrf_drv_timer_t const * const p_instance);
  129. /**
  130. * @brief Function for resuming the timer.
  131. *
  132. * @param[in] p_instance Timer instance.
  133. */
  134. void nrf_drv_timer_resume(nrf_drv_timer_t const * const p_instance);
  135. /**
  136. * @brief Function for clearing the timer.
  137. *
  138. * @param[in] p_instance Timer instance.
  139. */
  140. void nrf_drv_timer_clear(nrf_drv_timer_t const * const p_instance);
  141. /**
  142. * @brief Function for incrementing the timer.
  143. *
  144. * @param[in] p_instance Timer instance.
  145. */
  146. void nrf_drv_timer_increment(nrf_drv_timer_t const * const p_instance);
  147. /**
  148. * @brief Function for returning the address of a specific timer task.
  149. *
  150. * @param[in] p_instance Timer instance.
  151. * @param[in] timer_task Timer task.
  152. *
  153. * @return Task address.
  154. */
  155. __STATIC_INLINE uint32_t nrf_drv_timer_task_address_get(
  156. nrf_drv_timer_t const * const p_instance,
  157. nrf_timer_task_t timer_task);
  158. /**
  159. * @brief Function for returning the address of a specific timer capture task.
  160. *
  161. * @param[in] p_instance Timer instance.
  162. * @param[in] channel Capture channel number.
  163. *
  164. * @return Task address.
  165. */
  166. __STATIC_INLINE uint32_t nrf_drv_timer_capture_task_address_get(
  167. nrf_drv_timer_t const * const p_instance,
  168. uint32_t channel);
  169. /**
  170. * @brief Function for returning the address of a specific timer event.
  171. *
  172. * @param[in] p_instance Timer instance.
  173. * @param[in] timer_event Timer event.
  174. *
  175. * @return Event address.
  176. */
  177. __STATIC_INLINE uint32_t nrf_drv_timer_event_address_get(
  178. nrf_drv_timer_t const * const p_instance,
  179. nrf_timer_event_t timer_event);
  180. /**
  181. * @brief Function for returning the address of a specific timer compare event.
  182. *
  183. * @param[in] p_instance Timer instance.
  184. * @param[in] channel Compare channel number.
  185. *
  186. * @return Event address.
  187. */
  188. __STATIC_INLINE uint32_t nrf_drv_timer_compare_event_address_get(
  189. nrf_drv_timer_t const * const p_instance,
  190. uint32_t channel);
  191. /**
  192. * @brief Function for capturing the timer value.
  193. *
  194. * @param[in] p_instance Timer instance.
  195. * @param[in] cc_channel Capture channel number.
  196. *
  197. * @return Captured value.
  198. */
  199. uint32_t nrf_drv_timer_capture(nrf_drv_timer_t const * const p_instance,
  200. nrf_timer_cc_channel_t cc_channel);
  201. /**
  202. * @brief Function for returning the capture value from a specific channel.
  203. *
  204. * Use this function to read channel values when PPI is used for capturing.
  205. *
  206. * @param[in] p_instance Timer instance.
  207. * @param[in] cc_channel Capture channel number.
  208. *
  209. * @return Captured value.
  210. */
  211. __STATIC_INLINE uint32_t nrf_drv_timer_capture_get(
  212. nrf_drv_timer_t const * const p_instance,
  213. nrf_timer_cc_channel_t cc_channel);
  214. /**
  215. * @brief Function for setting the timer channel in compare mode.
  216. *
  217. * @param[in] p_instance Timer instance.
  218. * @param[in] cc_channel Compare channel number.
  219. * @param[in] cc_value Compare value.
  220. * @param[in] enable_int Enable or disable the interrupt for the compare channel.
  221. */
  222. void nrf_drv_timer_compare(nrf_drv_timer_t const * const p_instance,
  223. nrf_timer_cc_channel_t cc_channel,
  224. uint32_t cc_value,
  225. bool enable_int);
  226. /**
  227. * @brief Function for setting the timer channel in extended compare mode.
  228. *
  229. * @param[in] p_instance Timer instance.
  230. * @param[in] cc_channel Compare channel number.
  231. * @param[in] cc_value Compare value.
  232. * @param[in] timer_short_mask Shortcut between the compare event on the channel
  233. * and the timer task (STOP or CLEAR).
  234. * @param[in] enable_int Enable or disable the interrupt for the compare
  235. * channel.
  236. */
  237. void nrf_drv_timer_extended_compare(nrf_drv_timer_t const * const p_instance,
  238. nrf_timer_cc_channel_t cc_channel,
  239. uint32_t cc_value,
  240. nrf_timer_short_mask_t timer_short_mask,
  241. bool enable_int);
  242. /**
  243. * @brief Function for converting time in microseconds to timer ticks.
  244. *
  245. * @param[in] p_instance Timer instance.
  246. * @param[in] time_us Time in microseconds.
  247. *
  248. * @return Number of ticks.
  249. */
  250. __STATIC_INLINE uint32_t nrf_drv_timer_us_to_ticks(
  251. nrf_drv_timer_t const * const p_instance,
  252. uint32_t time_us);
  253. /**
  254. * @brief Function for converting time in milliseconds to timer ticks.
  255. *
  256. * @param[in] p_instance Timer instance.
  257. * @param[in] time_ms Time in milliseconds.
  258. *
  259. * @return Number of ticks.
  260. */
  261. __STATIC_INLINE uint32_t nrf_drv_timer_ms_to_ticks(
  262. nrf_drv_timer_t const * const p_instance,
  263. uint32_t time_ms);
  264. /**
  265. * @brief Function for enabling timer compare interrupt.
  266. *
  267. * @param[in] p_instance Timer instance.
  268. * @param[in] channel Compare channel.
  269. */
  270. void nrf_drv_timer_compare_int_enable(nrf_drv_timer_t const * const p_instance,
  271. uint32_t channel);
  272. /**
  273. * @brief Function for disabling timer compare interrupt.
  274. *
  275. * @param[in] p_instance Timer instance.
  276. * @param[in] channel Compare channel.
  277. */
  278. void nrf_drv_timer_compare_int_disable(nrf_drv_timer_t const * const p_instance,
  279. uint32_t channel);
  280. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  281. __STATIC_INLINE uint32_t nrf_drv_timer_task_address_get(
  282. nrf_drv_timer_t const * const p_instance,
  283. nrf_timer_task_t timer_task)
  284. {
  285. return (uint32_t)nrf_timer_task_address_get(p_instance->p_reg, timer_task);
  286. }
  287. __STATIC_INLINE uint32_t nrf_drv_timer_capture_task_address_get(
  288. nrf_drv_timer_t const * const p_instance,
  289. uint32_t channel)
  290. {
  291. ASSERT(channel < p_instance->cc_channel_count);
  292. return (uint32_t)nrf_timer_task_address_get(p_instance->p_reg,
  293. nrf_timer_capture_task_get(channel));
  294. }
  295. __STATIC_INLINE uint32_t nrf_drv_timer_event_address_get(
  296. nrf_drv_timer_t const * const p_instance,
  297. nrf_timer_event_t timer_event)
  298. {
  299. return (uint32_t)nrf_timer_event_address_get(p_instance->p_reg, timer_event);
  300. }
  301. __STATIC_INLINE uint32_t nrf_drv_timer_compare_event_address_get(
  302. nrf_drv_timer_t const * const p_instance,
  303. uint32_t channel)
  304. {
  305. ASSERT(channel < p_instance->cc_channel_count);
  306. return (uint32_t)nrf_timer_event_address_get(p_instance->p_reg,
  307. nrf_timer_compare_event_get(channel));
  308. }
  309. __STATIC_INLINE uint32_t nrf_drv_timer_capture_get(
  310. nrf_drv_timer_t const * const p_instance,
  311. nrf_timer_cc_channel_t cc_channel)
  312. {
  313. return nrf_timer_cc_read(p_instance->p_reg, cc_channel);
  314. }
  315. __STATIC_INLINE uint32_t nrf_drv_timer_us_to_ticks(
  316. nrf_drv_timer_t const * const p_instance,
  317. uint32_t timer_us)
  318. {
  319. return nrf_timer_us_to_ticks(timer_us,
  320. nrf_timer_frequency_get(p_instance->p_reg));
  321. }
  322. __STATIC_INLINE uint32_t nrf_drv_timer_ms_to_ticks(
  323. nrf_drv_timer_t const * const p_instance,
  324. uint32_t timer_ms)
  325. {
  326. return nrf_timer_ms_to_ticks(timer_ms,
  327. nrf_timer_frequency_get(p_instance->p_reg));
  328. }
  329. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  330. #endif // NRF_DRV_TIMER_H__
  331. /** @} */