nrfx_timer.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /**
  2. * Copyright (c) 2015 - 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_TIMER_H__
  41. #define NRFX_TIMER_H__
  42. #include <nrfx.h>
  43. #include <hal/nrf_timer.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * @defgroup nrfx_timer Timer driver
  49. * @{
  50. * @ingroup nrf_timer
  51. * @brief TIMER peripheral driver.
  52. */
  53. /**
  54. * @brief Timer driver instance data structure.
  55. */
  56. typedef struct
  57. {
  58. NRF_TIMER_Type * p_reg; ///< Pointer to the structure with TIMER peripheral instance registers.
  59. uint8_t instance_id; ///< Index of the driver instance. For internal use only.
  60. uint8_t cc_channel_count; ///< Number of capture/compare channels.
  61. } nrfx_timer_t;
  62. /** @brief Macro for creating a timer driver instance. */
  63. #define NRFX_TIMER_INSTANCE(id) \
  64. { \
  65. .p_reg = NRFX_CONCAT_2(NRF_TIMER, id), \
  66. .instance_id = NRFX_CONCAT_3(NRFX_TIMER, id, _INST_IDX), \
  67. .cc_channel_count = NRF_TIMER_CC_CHANNEL_COUNT(id), \
  68. }
  69. #ifndef __NRFX_DOXYGEN__
  70. enum {
  71. #if NRFX_CHECK(NRFX_TIMER0_ENABLED)
  72. NRFX_TIMER0_INST_IDX,
  73. #endif
  74. #if NRFX_CHECK(NRFX_TIMER1_ENABLED)
  75. NRFX_TIMER1_INST_IDX,
  76. #endif
  77. #if NRFX_CHECK(NRFX_TIMER2_ENABLED)
  78. NRFX_TIMER2_INST_IDX,
  79. #endif
  80. #if NRFX_CHECK(NRFX_TIMER3_ENABLED)
  81. NRFX_TIMER3_INST_IDX,
  82. #endif
  83. #if NRFX_CHECK(NRFX_TIMER4_ENABLED)
  84. NRFX_TIMER4_INST_IDX,
  85. #endif
  86. NRFX_TIMER_ENABLED_COUNT
  87. };
  88. #endif
  89. /** @brief The configuration structure of the timer driver instance. */
  90. typedef struct
  91. {
  92. nrf_timer_frequency_t frequency; ///< Frequency.
  93. nrf_timer_mode_t mode; ///< Mode of operation.
  94. nrf_timer_bit_width_t bit_width; ///< Bit width.
  95. uint8_t interrupt_priority; ///< Interrupt priority.
  96. void * p_context; ///< Context passed to interrupt handler.
  97. } nrfx_timer_config_t;
  98. /** @brief Timer driver instance default configuration. */
  99. #define NRFX_TIMER_DEFAULT_CONFIG \
  100. { \
  101. .frequency = (nrf_timer_frequency_t)NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY,\
  102. .mode = (nrf_timer_mode_t)NRFX_TIMER_DEFAULT_CONFIG_MODE, \
  103. .bit_width = (nrf_timer_bit_width_t)NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH,\
  104. .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, \
  105. .p_context = NULL \
  106. }
  107. /**
  108. * @brief Timer driver event handler type.
  109. *
  110. * @param[in] event_type Timer event.
  111. * @param[in] p_context General purpose parameter set during initialization of
  112. * the timer. This parameter can be used to pass
  113. * additional information to the handler function, for
  114. * example, the timer ID.
  115. */
  116. typedef void (* nrfx_timer_event_handler_t)(nrf_timer_event_t event_type,
  117. void * p_context);
  118. /**
  119. * @brief Function for initializing the timer.
  120. *
  121. * @param[in] p_instance Pointer to the driver instance structure.
  122. * @param[in] p_config Pointer to the structure with the initial configuration.
  123. * @param[in] timer_event_handler Event handler provided by the user.
  124. * Must not be NULL.
  125. *
  126. * @retval NRFX_SUCCESS Initialization was successful.
  127. * @retval NRFX_ERROR_INVALID_STATE The instance is already initialized.
  128. */
  129. nrfx_err_t nrfx_timer_init(nrfx_timer_t const * const p_instance,
  130. nrfx_timer_config_t const * p_config,
  131. nrfx_timer_event_handler_t timer_event_handler);
  132. /**
  133. * @brief Function for uninitializing the timer.
  134. *
  135. * @param[in] p_instance Pointer to the driver instance structure.
  136. */
  137. void nrfx_timer_uninit(nrfx_timer_t const * const p_instance);
  138. /**
  139. * @brief Function for turning on the timer.
  140. *
  141. * @param[in] p_instance Pointer to the driver instance structure.
  142. */
  143. void nrfx_timer_enable(nrfx_timer_t const * const p_instance);
  144. /**
  145. * @brief Function for turning off the timer.
  146. *
  147. * The timer will allow to enter the lowest possible SYSTEM_ON state
  148. * only after this function is called.
  149. *
  150. * @param[in] p_instance Pointer to the driver instance structure.
  151. */
  152. void nrfx_timer_disable(nrfx_timer_t const * const p_instance);
  153. /**
  154. * @brief Function for checking the timer state.
  155. *
  156. * @param[in] p_instance Pointer to the driver instance structure.
  157. *
  158. * @retval true Timer is enabled.
  159. * @retval false Timer is not enabled.
  160. */
  161. bool nrfx_timer_is_enabled(nrfx_timer_t const * const p_instance);
  162. /**
  163. * @brief Function for pausing the timer.
  164. *
  165. * @param[in] p_instance Pointer to the driver instance structure.
  166. */
  167. void nrfx_timer_pause(nrfx_timer_t const * const p_instance);
  168. /**
  169. * @brief Function for resuming the timer.
  170. *
  171. * @param[in] p_instance Pointer to the driver instance structure.
  172. */
  173. void nrfx_timer_resume(nrfx_timer_t const * const p_instance);
  174. /**
  175. * @brief Function for clearing the timer.
  176. *
  177. * @param[in] p_instance Pointer to the driver instance structure.
  178. */
  179. void nrfx_timer_clear(nrfx_timer_t const * const p_instance);
  180. /**
  181. * @brief Function for incrementing the timer.
  182. *
  183. * @param[in] p_instance Pointer to the driver instance structure.
  184. */
  185. void nrfx_timer_increment(nrfx_timer_t const * const p_instance);
  186. /**
  187. * @brief Function for returning the address of the specified timer task.
  188. *
  189. * @param[in] p_instance Pointer to the driver instance structure.
  190. * @param[in] timer_task Timer task.
  191. *
  192. * @return Task address.
  193. */
  194. __STATIC_INLINE uint32_t nrfx_timer_task_address_get(nrfx_timer_t const * const p_instance,
  195. nrf_timer_task_t timer_task);
  196. /**
  197. * @brief Function for returning the address of the specified timer capture task.
  198. *
  199. * @param[in] p_instance Pointer to the driver instance structure.
  200. * @param[in] channel Capture channel number.
  201. *
  202. * @return Task address.
  203. */
  204. __STATIC_INLINE uint32_t nrfx_timer_capture_task_address_get(nrfx_timer_t const * const p_instance,
  205. uint32_t channel);
  206. /**
  207. * @brief Function for returning the address of the specified timer event.
  208. *
  209. * @param[in] p_instance Pointer to the driver instance structure.
  210. * @param[in] timer_event Timer event.
  211. *
  212. * @return Event address.
  213. */
  214. __STATIC_INLINE uint32_t nrfx_timer_event_address_get(nrfx_timer_t const * const p_instance,
  215. nrf_timer_event_t timer_event);
  216. /**
  217. * @brief Function for returning the address of the specified timer compare event.
  218. *
  219. * @param[in] p_instance Pointer to the driver instance structure.
  220. * @param[in] channel Compare channel number.
  221. *
  222. * @return Event address.
  223. */
  224. __STATIC_INLINE uint32_t nrfx_timer_compare_event_address_get(nrfx_timer_t const * const p_instance,
  225. uint32_t channel);
  226. /**
  227. * @brief Function for capturing the timer value.
  228. *
  229. * @param[in] p_instance Pointer to the driver instance structure.
  230. * @param[in] cc_channel Capture channel number.
  231. *
  232. * @return Captured value.
  233. */
  234. uint32_t nrfx_timer_capture(nrfx_timer_t const * const p_instance,
  235. nrf_timer_cc_channel_t cc_channel);
  236. /**
  237. * @brief Function for returning the capture value from the specified channel.
  238. *
  239. * Use this function to read channel values when PPI is used for capturing.
  240. *
  241. * @param[in] p_instance Pointer to the driver instance structure.
  242. * @param[in] cc_channel Capture channel number.
  243. *
  244. * @return Captured value.
  245. */
  246. __STATIC_INLINE uint32_t nrfx_timer_capture_get(nrfx_timer_t const * const p_instance,
  247. nrf_timer_cc_channel_t cc_channel);
  248. /**
  249. * @brief Function for setting the timer channel in compare mode.
  250. *
  251. * @param[in] p_instance Pointer to the driver instance structure.
  252. * @param[in] cc_channel Compare channel number.
  253. * @param[in] cc_value Compare value.
  254. * @param[in] enable_int Enable or disable the interrupt for the compare channel.
  255. */
  256. void nrfx_timer_compare(nrfx_timer_t const * const p_instance,
  257. nrf_timer_cc_channel_t cc_channel,
  258. uint32_t cc_value,
  259. bool enable_int);
  260. /**
  261. * @brief Function for setting the timer channel in the extended compare mode.
  262. *
  263. * @param[in] p_instance Pointer to the driver instance structure.
  264. * @param[in] cc_channel Compare channel number.
  265. * @param[in] cc_value Compare value.
  266. * @param[in] timer_short_mask Shortcut between the compare event on the channel
  267. * and the timer task (STOP or CLEAR).
  268. * @param[in] enable_int Enable or disable the interrupt for the compare channel.
  269. */
  270. void nrfx_timer_extended_compare(nrfx_timer_t const * const p_instance,
  271. nrf_timer_cc_channel_t cc_channel,
  272. uint32_t cc_value,
  273. nrf_timer_short_mask_t timer_short_mask,
  274. bool enable_int);
  275. /**
  276. * @brief Function for converting time in microseconds to timer ticks.
  277. *
  278. * @param[in] p_instance Pointer to the driver instance structure.
  279. * @param[in] time_us Time in microseconds.
  280. *
  281. * @return Number of ticks.
  282. */
  283. __STATIC_INLINE uint32_t nrfx_timer_us_to_ticks(nrfx_timer_t const * const p_instance,
  284. uint32_t time_us);
  285. /**
  286. * @brief Function for converting time in milliseconds to timer ticks.
  287. *
  288. * @param[in] p_instance Pointer to the driver instance structure.
  289. * @param[in] time_ms Time in milliseconds.
  290. *
  291. * @return Number of ticks.
  292. */
  293. __STATIC_INLINE uint32_t nrfx_timer_ms_to_ticks(nrfx_timer_t const * const p_instance,
  294. uint32_t time_ms);
  295. /**
  296. * @brief Function for enabling timer compare interrupt.
  297. *
  298. * @param[in] p_instance Pointer to the driver instance structure.
  299. * @param[in] channel Compare channel.
  300. */
  301. void nrfx_timer_compare_int_enable(nrfx_timer_t const * const p_instance,
  302. uint32_t channel);
  303. /**
  304. * @brief Function for disabling timer compare interrupt.
  305. *
  306. * @param[in] p_instance Pointer to the driver instance structure.
  307. * @param[in] channel Compare channel.
  308. */
  309. void nrfx_timer_compare_int_disable(nrfx_timer_t const * const p_instance,
  310. uint32_t channel);
  311. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  312. __STATIC_INLINE uint32_t nrfx_timer_task_address_get(nrfx_timer_t const * const p_instance,
  313. nrf_timer_task_t timer_task)
  314. {
  315. return (uint32_t)nrf_timer_task_address_get(p_instance->p_reg, timer_task);
  316. }
  317. __STATIC_INLINE uint32_t nrfx_timer_capture_task_address_get(nrfx_timer_t const * const p_instance,
  318. uint32_t channel)
  319. {
  320. NRFX_ASSERT(channel < p_instance->cc_channel_count);
  321. return (uint32_t)nrf_timer_task_address_get(p_instance->p_reg,
  322. nrf_timer_capture_task_get(channel));
  323. }
  324. __STATIC_INLINE uint32_t nrfx_timer_event_address_get(nrfx_timer_t const * const p_instance,
  325. nrf_timer_event_t timer_event)
  326. {
  327. return (uint32_t)nrf_timer_event_address_get(p_instance->p_reg, timer_event);
  328. }
  329. __STATIC_INLINE uint32_t nrfx_timer_compare_event_address_get(nrfx_timer_t const * const p_instance,
  330. uint32_t channel)
  331. {
  332. NRFX_ASSERT(channel < p_instance->cc_channel_count);
  333. return (uint32_t)nrf_timer_event_address_get(p_instance->p_reg,
  334. nrf_timer_compare_event_get(channel));
  335. }
  336. __STATIC_INLINE uint32_t nrfx_timer_capture_get(nrfx_timer_t const * const p_instance,
  337. nrf_timer_cc_channel_t cc_channel)
  338. {
  339. return nrf_timer_cc_read(p_instance->p_reg, cc_channel);
  340. }
  341. __STATIC_INLINE uint32_t nrfx_timer_us_to_ticks(nrfx_timer_t const * const p_instance,
  342. uint32_t timer_us)
  343. {
  344. return nrf_timer_us_to_ticks(timer_us, nrf_timer_frequency_get(p_instance->p_reg));
  345. }
  346. __STATIC_INLINE uint32_t nrfx_timer_ms_to_ticks(nrfx_timer_t const * const p_instance,
  347. uint32_t timer_ms)
  348. {
  349. return nrf_timer_ms_to_ticks(timer_ms, nrf_timer_frequency_get(p_instance->p_reg));
  350. }
  351. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  352. /** @} */
  353. void nrfx_timer_0_irq_handler(void);
  354. void nrfx_timer_1_irq_handler(void);
  355. void nrfx_timer_2_irq_handler(void);
  356. void nrfx_timer_3_irq_handler(void);
  357. void nrfx_timer_4_irq_handler(void);
  358. #ifdef __cplusplus
  359. }
  360. #endif
  361. #endif // NRFX_TIMER_H__