nrf_timer.h 28 KB

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