nrf_timer.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. * Copyright (c) 2014 - 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 NRF_TIMER_H__
  41. #define NRF_TIMER_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_timer_hal TIMER HAL
  48. * @{
  49. * @ingroup nrf_timer
  50. * @brief Hardware access layer for managing the TIMER peripheral.
  51. */
  52. /**
  53. * @brief Macro for getting the maximum bit resolution of the specified timer instance.
  54. *
  55. * @param[in] id Index of the specified timer instance.
  56. *
  57. * @retval Maximum bit resolution of the specified timer instance.
  58. */
  59. #define TIMER_MAX_SIZE(id) NRFX_CONCAT_3(TIMER, id, _MAX_SIZE)
  60. /**
  61. * @brief Macro for validating the correctness of the bit width resolution setting.
  62. *
  63. * @param[in] id Index of the specified timer instance.
  64. * @param[in] bit_width Bit width resolution value to be checked.
  65. *
  66. * @retval true Timer instance supports the specified bit width resolution value.
  67. * @retval false Timer instance does not support the specified bit width resolution value.
  68. */
  69. #define TIMER_BIT_WIDTH_MAX(id, bit_width) \
  70. (TIMER_MAX_SIZE(id) == 8 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) : \
  71. (TIMER_MAX_SIZE(id) == 16 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) || \
  72. (bit_width == NRF_TIMER_BIT_WIDTH_16) : \
  73. (TIMER_MAX_SIZE(id) == 24 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) || \
  74. (bit_width == NRF_TIMER_BIT_WIDTH_16) || \
  75. (bit_width == NRF_TIMER_BIT_WIDTH_24) : \
  76. (TIMER_MAX_SIZE(id) == 32 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) || \
  77. (bit_width == NRF_TIMER_BIT_WIDTH_16) || \
  78. (bit_width == NRF_TIMER_BIT_WIDTH_24) || \
  79. (bit_width == NRF_TIMER_BIT_WIDTH_32) : \
  80. false))))
  81. /**
  82. * @brief Macro for checking correctness of bit width configuration for the specified timer.
  83. *
  84. * @param[in] p_reg Timer instance register.
  85. * @param[in] bit_width Bit width resolution value to be checked.
  86. *
  87. * @retval true Timer instance supports the specified bit width resolution value.
  88. * @retval false Timer instance does not support the specified bit width resolution value.
  89. */
  90. #if (TIMER_COUNT == 3) || defined(__NRFX_DOXYGEN__)
  91. #define NRF_TIMER_IS_BIT_WIDTH_VALID(p_reg, bit_width) ( \
  92. ((p_reg == NRF_TIMER0) && TIMER_BIT_WIDTH_MAX(0, bit_width)) \
  93. || ((p_reg == NRF_TIMER1) && TIMER_BIT_WIDTH_MAX(1, bit_width)) \
  94. || ((p_reg == NRF_TIMER2) && TIMER_BIT_WIDTH_MAX(2, bit_width)))
  95. #elif (TIMER_COUNT == 4)
  96. #define NRF_TIMER_IS_BIT_WIDTH_VALID(p_reg, bit_width) ( \
  97. ((p_reg == NRF_TIMER0) && TIMER_BIT_WIDTH_MAX(0, bit_width)) \
  98. || ((p_reg == NRF_TIMER1) && TIMER_BIT_WIDTH_MAX(1, bit_width)) \
  99. || ((p_reg == NRF_TIMER2) && TIMER_BIT_WIDTH_MAX(2, bit_width)) \
  100. || ((p_reg == NRF_TIMER3) && TIMER_BIT_WIDTH_MAX(3, bit_width)))
  101. #elif (TIMER_COUNT == 5)
  102. #define NRF_TIMER_IS_BIT_WIDTH_VALID(p_reg, bit_width) ( \
  103. ((p_reg == NRF_TIMER0) && TIMER_BIT_WIDTH_MAX(0, bit_width)) \
  104. || ((p_reg == NRF_TIMER1) && TIMER_BIT_WIDTH_MAX(1, bit_width)) \
  105. || ((p_reg == NRF_TIMER2) && TIMER_BIT_WIDTH_MAX(2, bit_width)) \
  106. || ((p_reg == NRF_TIMER3) && TIMER_BIT_WIDTH_MAX(3, bit_width)) \
  107. || ((p_reg == NRF_TIMER4) && TIMER_BIT_WIDTH_MAX(4, bit_width)))
  108. #else
  109. #error "Not supported timer count"
  110. #endif
  111. /**
  112. * @brief Macro for getting the number of capture/compare channels available
  113. * in a given timer instance.
  114. *
  115. * @param[in] id Index of the specified timer instance.
  116. */
  117. #define NRF_TIMER_CC_CHANNEL_COUNT(id) NRFX_CONCAT_3(TIMER, id, _CC_NUM)
  118. /** @brief Timer tasks. */
  119. typedef enum
  120. {
  121. NRF_TIMER_TASK_START = offsetof(NRF_TIMER_Type, TASKS_START), ///< Task for starting the timer.
  122. NRF_TIMER_TASK_STOP = offsetof(NRF_TIMER_Type, TASKS_STOP), ///< Task for stopping the timer.
  123. NRF_TIMER_TASK_COUNT = offsetof(NRF_TIMER_Type, TASKS_COUNT), ///< Task for incrementing the timer (in counter mode).
  124. NRF_TIMER_TASK_CLEAR = offsetof(NRF_TIMER_Type, TASKS_CLEAR), ///< Task for resetting the timer value.
  125. NRF_TIMER_TASK_SHUTDOWN = offsetof(NRF_TIMER_Type, TASKS_SHUTDOWN), ///< Task for powering off the timer.
  126. NRF_TIMER_TASK_CAPTURE0 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[0]), ///< Task for capturing the timer value on channel 0.
  127. NRF_TIMER_TASK_CAPTURE1 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[1]), ///< Task for capturing the timer value on channel 1.
  128. NRF_TIMER_TASK_CAPTURE2 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[2]), ///< Task for capturing the timer value on channel 2.
  129. NRF_TIMER_TASK_CAPTURE3 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[3]), ///< Task for capturing the timer value on channel 3.
  130. #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__)
  131. NRF_TIMER_TASK_CAPTURE4 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[4]), ///< Task for capturing the timer value on channel 4.
  132. #endif
  133. #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__)
  134. NRF_TIMER_TASK_CAPTURE5 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[5]), ///< Task for capturing the timer value on channel 5.
  135. #endif
  136. } nrf_timer_task_t;
  137. /** @brief Timer events. */
  138. typedef enum
  139. {
  140. NRF_TIMER_EVENT_COMPARE0 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[0]), ///< Event from compare channel 0.
  141. NRF_TIMER_EVENT_COMPARE1 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[1]), ///< Event from compare channel 1.
  142. NRF_TIMER_EVENT_COMPARE2 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[2]), ///< Event from compare channel 2.
  143. NRF_TIMER_EVENT_COMPARE3 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[3]), ///< Event from compare channel 3.
  144. #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__)
  145. NRF_TIMER_EVENT_COMPARE4 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[4]), ///< Event from compare channel 4.
  146. #endif
  147. #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__)
  148. NRF_TIMER_EVENT_COMPARE5 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[5]), ///< Event from compare channel 5.
  149. #endif
  150. } nrf_timer_event_t;
  151. /** @brief Types of timer shortcuts. */
  152. typedef enum
  153. {
  154. NRF_TIMER_SHORT_COMPARE0_STOP_MASK = TIMER_SHORTS_COMPARE0_STOP_Msk, ///< Shortcut for stopping the timer based on compare 0.
  155. NRF_TIMER_SHORT_COMPARE1_STOP_MASK = TIMER_SHORTS_COMPARE1_STOP_Msk, ///< Shortcut for stopping the timer based on compare 1.
  156. NRF_TIMER_SHORT_COMPARE2_STOP_MASK = TIMER_SHORTS_COMPARE2_STOP_Msk, ///< Shortcut for stopping the timer based on compare 2.
  157. NRF_TIMER_SHORT_COMPARE3_STOP_MASK = TIMER_SHORTS_COMPARE3_STOP_Msk, ///< Shortcut for stopping the timer based on compare 3.
  158. #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__)
  159. NRF_TIMER_SHORT_COMPARE4_STOP_MASK = TIMER_SHORTS_COMPARE4_STOP_Msk, ///< Shortcut for stopping the timer based on compare 4.
  160. #endif
  161. #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__)
  162. NRF_TIMER_SHORT_COMPARE5_STOP_MASK = TIMER_SHORTS_COMPARE5_STOP_Msk, ///< Shortcut for stopping the timer based on compare 5.
  163. #endif
  164. NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK = TIMER_SHORTS_COMPARE0_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 0.
  165. NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK = TIMER_SHORTS_COMPARE1_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 1.
  166. NRF_TIMER_SHORT_COMPARE2_CLEAR_MASK = TIMER_SHORTS_COMPARE2_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 2.
  167. NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK = TIMER_SHORTS_COMPARE3_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 3.
  168. #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__)
  169. NRF_TIMER_SHORT_COMPARE4_CLEAR_MASK = TIMER_SHORTS_COMPARE4_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 4.
  170. #endif
  171. #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__)
  172. NRF_TIMER_SHORT_COMPARE5_CLEAR_MASK = TIMER_SHORTS_COMPARE5_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 5.
  173. #endif
  174. } nrf_timer_short_mask_t;
  175. /** @brief Timer modes. */
  176. typedef enum
  177. {
  178. NRF_TIMER_MODE_TIMER = TIMER_MODE_MODE_Timer, ///< Timer mode: timer.
  179. NRF_TIMER_MODE_COUNTER = TIMER_MODE_MODE_Counter, ///< Timer mode: counter.
  180. #if defined(TIMER_MODE_MODE_LowPowerCounter) || defined(__NRFX_DOXYGEN__)
  181. NRF_TIMER_MODE_LOW_POWER_COUNTER = TIMER_MODE_MODE_LowPowerCounter, ///< Timer mode: low-power counter.
  182. #endif
  183. } nrf_timer_mode_t;
  184. /** @brief Timer bit width. */
  185. typedef enum
  186. {
  187. NRF_TIMER_BIT_WIDTH_8 = TIMER_BITMODE_BITMODE_08Bit, ///< Timer bit width 8 bit.
  188. NRF_TIMER_BIT_WIDTH_16 = TIMER_BITMODE_BITMODE_16Bit, ///< Timer bit width 16 bit.
  189. NRF_TIMER_BIT_WIDTH_24 = TIMER_BITMODE_BITMODE_24Bit, ///< Timer bit width 24 bit.
  190. NRF_TIMER_BIT_WIDTH_32 = TIMER_BITMODE_BITMODE_32Bit ///< Timer bit width 32 bit.
  191. } nrf_timer_bit_width_t;
  192. /** @brief Timer prescalers. */
  193. typedef enum
  194. {
  195. NRF_TIMER_FREQ_16MHz = 0, ///< Timer frequency 16 MHz.
  196. NRF_TIMER_FREQ_8MHz, ///< Timer frequency 8 MHz.
  197. NRF_TIMER_FREQ_4MHz, ///< Timer frequency 4 MHz.
  198. NRF_TIMER_FREQ_2MHz, ///< Timer frequency 2 MHz.
  199. NRF_TIMER_FREQ_1MHz, ///< Timer frequency 1 MHz.
  200. NRF_TIMER_FREQ_500kHz, ///< Timer frequency 500 kHz.
  201. NRF_TIMER_FREQ_250kHz, ///< Timer frequency 250 kHz.
  202. NRF_TIMER_FREQ_125kHz, ///< Timer frequency 125 kHz.
  203. NRF_TIMER_FREQ_62500Hz, ///< Timer frequency 62500 Hz.
  204. NRF_TIMER_FREQ_31250Hz ///< Timer frequency 31250 Hz.
  205. } nrf_timer_frequency_t;
  206. /** @brief Timer capture/compare channels. */
  207. typedef enum
  208. {
  209. NRF_TIMER_CC_CHANNEL0 = 0, ///< Timer capture/compare channel 0.
  210. NRF_TIMER_CC_CHANNEL1, ///< Timer capture/compare channel 1.
  211. NRF_TIMER_CC_CHANNEL2, ///< Timer capture/compare channel 2.
  212. NRF_TIMER_CC_CHANNEL3, ///< Timer capture/compare channel 3.
  213. #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__)
  214. NRF_TIMER_CC_CHANNEL4, ///< Timer capture/compare channel 4.
  215. #endif
  216. #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__)
  217. NRF_TIMER_CC_CHANNEL5, ///< Timer capture/compare channel 5.
  218. #endif
  219. } nrf_timer_cc_channel_t;
  220. /** @brief Timer interrupts. */
  221. typedef enum
  222. {
  223. NRF_TIMER_INT_COMPARE0_MASK = TIMER_INTENSET_COMPARE0_Msk, ///< Timer interrupt from compare event on channel 0.
  224. NRF_TIMER_INT_COMPARE1_MASK = TIMER_INTENSET_COMPARE1_Msk, ///< Timer interrupt from compare event on channel 1.
  225. NRF_TIMER_INT_COMPARE2_MASK = TIMER_INTENSET_COMPARE2_Msk, ///< Timer interrupt from compare event on channel 2.
  226. NRF_TIMER_INT_COMPARE3_MASK = TIMER_INTENSET_COMPARE3_Msk, ///< Timer interrupt from compare event on channel 3.
  227. #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__)
  228. NRF_TIMER_INT_COMPARE4_MASK = TIMER_INTENSET_COMPARE4_Msk, ///< Timer interrupt from compare event on channel 4.
  229. #endif
  230. #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__)
  231. NRF_TIMER_INT_COMPARE5_MASK = TIMER_INTENSET_COMPARE5_Msk, ///< Timer interrupt from compare event on channel 5.
  232. #endif
  233. } nrf_timer_int_mask_t;
  234. /**
  235. * @brief Function for activating the specified timer task.
  236. *
  237. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  238. * @param[in] task Task to be activated.
  239. */
  240. __STATIC_INLINE void nrf_timer_task_trigger(NRF_TIMER_Type * p_reg,
  241. nrf_timer_task_t task);
  242. /**
  243. * @brief Function for getting the address of the specified timer task register.
  244. *
  245. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  246. * @param[in] task The specified task.
  247. *
  248. * @return Address of the specified task register.
  249. */
  250. __STATIC_INLINE uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_reg,
  251. nrf_timer_task_t task);
  252. /**
  253. * @brief Function for clearing the specified timer event.
  254. *
  255. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  256. * @param[in] event Event to clear.
  257. */
  258. __STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_reg,
  259. nrf_timer_event_t event);
  260. /**
  261. * @brief Function for retrieving the state of the TIMER event.
  262. *
  263. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  264. * @param[in] event Event to be checked.
  265. *
  266. * @retval true The event has been generated.
  267. * @retval false The event has not been generated.
  268. */
  269. __STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type * p_reg,
  270. nrf_timer_event_t event);
  271. /**
  272. * @brief Function for getting the address of the specified timer event register.
  273. *
  274. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  275. * @param[in] event The specified event.
  276. *
  277. * @return Address of the specified event register.
  278. */
  279. __STATIC_INLINE uint32_t * nrf_timer_event_address_get(NRF_TIMER_Type * p_reg,
  280. nrf_timer_event_t event);
  281. /**
  282. * @brief Function for enabling the specified shortcuts.
  283. *
  284. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  285. * @param[in] mask Shortcuts to be enabled.
  286. */
  287. __STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_reg,
  288. uint32_t mask);
  289. /**
  290. * @brief Function for disabling the specified shortcuts.
  291. *
  292. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  293. * @param[in] mask Shortcuts to be disabled.
  294. */
  295. __STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_reg,
  296. uint32_t mask);
  297. /**
  298. * @brief Function for enabling the specified interrupts.
  299. *
  300. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  301. * @param[in] mask Mask of interrupts to be enabled.
  302. */
  303. __STATIC_INLINE void nrf_timer_int_enable(NRF_TIMER_Type * p_reg,
  304. uint32_t mask);
  305. /**
  306. * @brief Function for disabling the specified interrupts.
  307. *
  308. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  309. * @param[in] mask Mask of interrupts to be disabled.
  310. */
  311. __STATIC_INLINE void nrf_timer_int_disable(NRF_TIMER_Type * p_reg,
  312. uint32_t mask);
  313. /**
  314. * @brief Function for retrieving the state of a given interrupt.
  315. *
  316. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  317. * @param[in] timer_int Interrupt to be checked.
  318. *
  319. * @retval true The interrupt is enabled.
  320. * @retval false The interrupt is not enabled.
  321. */
  322. __STATIC_INLINE bool nrf_timer_int_enable_check(NRF_TIMER_Type * p_reg,
  323. uint32_t timer_int);
  324. #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  325. /**
  326. * @brief Function for setting the subscribe configuration for a given
  327. * TIMER task.
  328. *
  329. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  330. * @param[in] task Task for which to set the configuration.
  331. * @param[in] channel Channel through which to subscribe events.
  332. */
  333. __STATIC_INLINE void nrf_timer_subscribe_set(NRF_TIMER_Type * p_reg,
  334. nrf_timer_task_t task,
  335. uint8_t channel);
  336. /**
  337. * @brief Function for clearing the subscribe configuration for a given
  338. * TIMER task.
  339. *
  340. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  341. * @param[in] task Task for which to clear the configuration.
  342. */
  343. __STATIC_INLINE void nrf_timer_subscribe_clear(NRF_TIMER_Type * p_reg,
  344. nrf_timer_task_t task);
  345. /**
  346. * @brief Function for setting the publish configuration for a given
  347. * TIMER event.
  348. *
  349. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  350. * @param[in] event Event for which to set the configuration.
  351. * @param[in] channel Channel through which to publish the event.
  352. */
  353. __STATIC_INLINE void nrf_timer_publish_set(NRF_TIMER_Type * p_reg,
  354. nrf_timer_event_t event,
  355. uint8_t channel);
  356. /**
  357. * @brief Function for clearing the publish configuration for a given
  358. * TIMER event.
  359. *
  360. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  361. * @param[in] event Event for which to clear the configuration.
  362. */
  363. __STATIC_INLINE void nrf_timer_publish_clear(NRF_TIMER_Type * p_reg,
  364. nrf_timer_event_t event);
  365. #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__)
  366. /**
  367. * @brief Function for setting the timer mode.
  368. *
  369. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  370. * @param[in] mode Timer mode.
  371. */
  372. __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_reg,
  373. nrf_timer_mode_t mode);
  374. /**
  375. * @brief Function for retrieving the timer mode.
  376. *
  377. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  378. *
  379. * @return Timer mode.
  380. */
  381. __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_reg);
  382. /**
  383. * @brief Function for setting the timer bit width.
  384. *
  385. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  386. * @param[in] bit_width Timer bit width.
  387. */
  388. __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg,
  389. nrf_timer_bit_width_t bit_width);
  390. /**
  391. * @brief Function for retrieving the timer bit width.
  392. *
  393. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  394. *
  395. * @return Timer bit width.
  396. */
  397. __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_reg);
  398. /**
  399. * @brief Function for setting the timer frequency.
  400. *
  401. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  402. * @param[in] frequency Timer frequency.
  403. */
  404. __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg,
  405. nrf_timer_frequency_t frequency);
  406. /**
  407. * @brief Function for retrieving the timer frequency.
  408. *
  409. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  410. *
  411. * @return Timer frequency.
  412. */
  413. __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_reg);
  414. /**
  415. * @brief Function for writing the capture/compare register for the specified channel.
  416. *
  417. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  418. * @param[in] cc_channel The specified capture/compare channel.
  419. * @param[in] cc_value Value to write to the capture/compare register.
  420. */
  421. __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_reg,
  422. nrf_timer_cc_channel_t cc_channel,
  423. uint32_t cc_value);
  424. /**
  425. * @brief Function for retrieving the capture/compare value for a specified channel.
  426. *
  427. * @param[in] p_reg Pointer to the structure of registers of the peripheral.
  428. * @param[in] cc_channel The specified capture/compare channel.
  429. *
  430. * @return Value from the specified capture/compare register.
  431. */
  432. __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_reg,
  433. nrf_timer_cc_channel_t cc_channel);
  434. /**
  435. * @brief Function for getting the specified timer capture task.
  436. *
  437. * @param[in] channel Capture channel.
  438. *
  439. * @return Capture task.
  440. */
  441. __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel);
  442. /**
  443. * @brief Function for getting the specified timer compare event.
  444. *
  445. * @param[in] channel Compare channel.
  446. *
  447. * @return Compare event.
  448. */
  449. __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel);
  450. /**
  451. * @brief Function for getting the specified timer compare interrupt.
  452. *
  453. * @param[in] channel Compare channel.
  454. *
  455. * @return Compare interrupt.
  456. */
  457. __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel);
  458. /**
  459. * @brief Function for calculating the number of timer ticks for a given time
  460. * (in microseconds) and timer frequency.
  461. *
  462. * @param[in] time_us Time in microseconds.
  463. * @param[in] frequency Timer frequency.
  464. *
  465. * @return Number of timer ticks.
  466. */
  467. __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us,
  468. nrf_timer_frequency_t frequency);
  469. /**
  470. * @brief Function for calculating the number of timer ticks for a given time
  471. * (in milliseconds) and timer frequency.
  472. *
  473. * @param[in] time_ms Time in milliseconds.
  474. * @param[in] frequency Timer frequency.
  475. *
  476. * @return Number of timer ticks.
  477. */
  478. __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms,
  479. nrf_timer_frequency_t frequency);
  480. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  481. __STATIC_INLINE void nrf_timer_task_trigger(NRF_TIMER_Type * p_reg,
  482. nrf_timer_task_t task)
  483. {
  484. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  485. }
  486. __STATIC_INLINE uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_reg,
  487. nrf_timer_task_t task)
  488. {
  489. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)task);
  490. }
  491. __STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_reg,
  492. nrf_timer_event_t event)
  493. {
  494. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  495. #if __CORTEX_M == 0x04
  496. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  497. (void)dummy;
  498. #endif
  499. }
  500. __STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type * p_reg,
  501. nrf_timer_event_t event)
  502. {
  503. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  504. }
  505. __STATIC_INLINE uint32_t * nrf_timer_event_address_get(NRF_TIMER_Type * p_reg,
  506. nrf_timer_event_t event)
  507. {
  508. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  509. }
  510. __STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_reg,
  511. uint32_t mask)
  512. {
  513. p_reg->SHORTS |= mask;
  514. }
  515. __STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_reg,
  516. uint32_t mask)
  517. {
  518. p_reg->SHORTS &= ~(mask);
  519. }
  520. __STATIC_INLINE void nrf_timer_int_enable(NRF_TIMER_Type * p_reg,
  521. uint32_t mask)
  522. {
  523. p_reg->INTENSET = mask;
  524. }
  525. __STATIC_INLINE void nrf_timer_int_disable(NRF_TIMER_Type * p_reg,
  526. uint32_t mask)
  527. {
  528. p_reg->INTENCLR = mask;
  529. }
  530. __STATIC_INLINE bool nrf_timer_int_enable_check(NRF_TIMER_Type * p_reg,
  531. uint32_t timer_int)
  532. {
  533. return (bool)(p_reg->INTENSET & timer_int);
  534. }
  535. #if defined(DPPI_PRESENT)
  536. __STATIC_INLINE void nrf_timer_subscribe_set(NRF_TIMER_Type * p_reg,
  537. nrf_timer_task_t task,
  538. uint8_t channel)
  539. {
  540. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) =
  541. ((uint32_t)channel | TIMER_SUBSCRIBE_START_EN_Msk);
  542. }
  543. __STATIC_INLINE void nrf_timer_subscribe_clear(NRF_TIMER_Type * p_reg,
  544. nrf_timer_task_t task)
  545. {
  546. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0;
  547. }
  548. __STATIC_INLINE void nrf_timer_publish_set(NRF_TIMER_Type * p_reg,
  549. nrf_timer_event_t event,
  550. uint8_t channel)
  551. {
  552. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) =
  553. ((uint32_t)channel | TIMER_PUBLISH_COMPARE_EN_Msk);
  554. }
  555. __STATIC_INLINE void nrf_timer_publish_clear(NRF_TIMER_Type * p_reg,
  556. nrf_timer_event_t event)
  557. {
  558. *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0;
  559. }
  560. #endif // defined(DPPI_PRESENT)
  561. __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_reg,
  562. nrf_timer_mode_t mode)
  563. {
  564. p_reg->MODE = (p_reg->MODE & ~TIMER_MODE_MODE_Msk) |
  565. ((mode << TIMER_MODE_MODE_Pos) & TIMER_MODE_MODE_Msk);
  566. }
  567. __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_reg)
  568. {
  569. return (nrf_timer_mode_t)(p_reg->MODE);
  570. }
  571. __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg,
  572. nrf_timer_bit_width_t bit_width)
  573. {
  574. p_reg->BITMODE = (p_reg->BITMODE & ~TIMER_BITMODE_BITMODE_Msk) |
  575. ((bit_width << TIMER_BITMODE_BITMODE_Pos) &
  576. TIMER_BITMODE_BITMODE_Msk);
  577. }
  578. __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_reg)
  579. {
  580. return (nrf_timer_bit_width_t)(p_reg->BITMODE);
  581. }
  582. __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg,
  583. nrf_timer_frequency_t frequency)
  584. {
  585. p_reg->PRESCALER = (p_reg->PRESCALER & ~TIMER_PRESCALER_PRESCALER_Msk) |
  586. ((frequency << TIMER_PRESCALER_PRESCALER_Pos) &
  587. TIMER_PRESCALER_PRESCALER_Msk);
  588. }
  589. __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_reg)
  590. {
  591. return (nrf_timer_frequency_t)(p_reg->PRESCALER);
  592. }
  593. __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_reg,
  594. nrf_timer_cc_channel_t cc_channel,
  595. uint32_t cc_value)
  596. {
  597. p_reg->CC[cc_channel] = cc_value;
  598. }
  599. __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_reg,
  600. nrf_timer_cc_channel_t cc_channel)
  601. {
  602. return (uint32_t)p_reg->CC[cc_channel];
  603. }
  604. __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel)
  605. {
  606. return (nrf_timer_task_t)NRFX_OFFSETOF(NRF_TIMER_Type, TASKS_CAPTURE[channel]);
  607. }
  608. __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel)
  609. {
  610. return (nrf_timer_event_t)NRFX_OFFSETOF(NRF_TIMER_Type, EVENTS_COMPARE[channel]);
  611. }
  612. __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel)
  613. {
  614. return (nrf_timer_int_mask_t)
  615. ((uint32_t)NRF_TIMER_INT_COMPARE0_MASK << channel);
  616. }
  617. __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us,
  618. nrf_timer_frequency_t frequency)
  619. {
  620. // The "frequency" parameter here is actually the prescaler value, and the
  621. // timer runs at the following frequency: f = 16 MHz / 2^prescaler.
  622. uint32_t prescaler = (uint32_t)frequency;
  623. uint64_t ticks = ((time_us * 16ULL) >> prescaler);
  624. NRFX_ASSERT(ticks <= UINT32_MAX);
  625. return (uint32_t)ticks;
  626. }
  627. __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms,
  628. nrf_timer_frequency_t frequency)
  629. {
  630. // The "frequency" parameter here is actually the prescaler value, and the
  631. // timer runs at the following frequency: f = 16000 kHz / 2^prescaler.
  632. uint32_t prescaler = (uint32_t)frequency;
  633. uint64_t ticks = ((time_ms * 16000ULL) >> prescaler);
  634. NRFX_ASSERT(ticks <= UINT32_MAX);
  635. return (uint32_t)ticks;
  636. }
  637. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  638. /** @} */
  639. #ifdef __cplusplus
  640. }
  641. #endif
  642. #endif // NRF_TIMER_H__