nrfx_timer.h 15 KB

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