nrf_timer.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  1. /**
  2. * Copyright (c) 2014 - 2018, 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. /**
  313. * @brief Function for setting the timer mode.
  314. *
  315. * @param[in] p_reg Pointer to the peripheral registers structure.
  316. * @param[in] mode Timer mode.
  317. */
  318. __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_reg,
  319. nrf_timer_mode_t mode);
  320. /**
  321. * @brief Function for retrieving the timer mode.
  322. *
  323. * @param[in] p_reg Pointer to the peripheral registers structure.
  324. *
  325. * @return Timer mode.
  326. */
  327. __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_reg);
  328. /**
  329. * @brief Function for setting the timer bit width.
  330. *
  331. * @param[in] p_reg Pointer to the peripheral registers structure.
  332. * @param[in] bit_width Timer bit width.
  333. */
  334. __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg,
  335. nrf_timer_bit_width_t bit_width);
  336. /**
  337. * @brief Function for retrieving the timer bit width.
  338. *
  339. * @param[in] p_reg Pointer to the peripheral registers structure.
  340. *
  341. * @return Timer bit width.
  342. */
  343. __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_reg);
  344. /**
  345. * @brief Function for setting the timer frequency.
  346. *
  347. * @param[in] p_reg Pointer to the peripheral registers structure.
  348. * @param[in] frequency Timer frequency.
  349. */
  350. __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg,
  351. nrf_timer_frequency_t frequency);
  352. /**
  353. * @brief Function for retrieving the timer frequency.
  354. *
  355. * @param[in] p_reg Pointer to the peripheral registers structure.
  356. *
  357. * @return Timer frequency.
  358. */
  359. __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_reg);
  360. /**
  361. * @brief Function for writing the capture/compare register for a specified channel.
  362. *
  363. * @param[in] p_reg Pointer to the peripheral registers structure.
  364. * @param[in] cc_channel Requested capture/compare channel.
  365. * @param[in] cc_value Value to write to the capture/compare register.
  366. */
  367. __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_reg,
  368. nrf_timer_cc_channel_t cc_channel,
  369. uint32_t cc_value);
  370. /**
  371. * @brief Function for retrieving the capture/compare value for a specified channel.
  372. *
  373. * @param[in] p_reg Pointer to the peripheral registers structure.
  374. * @param[in] cc_channel Requested capture/compare channel.
  375. *
  376. * @return Value from the requested capture/compare register.
  377. */
  378. __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_reg,
  379. nrf_timer_cc_channel_t cc_channel);
  380. /**
  381. * @brief Function for getting a specific timer capture task.
  382. *
  383. * @param[in] channel Capture channel.
  384. *
  385. * @return Capture task.
  386. */
  387. __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel);
  388. /**
  389. * @brief Function for getting a specific timer compare event.
  390. *
  391. * @param[in] channel Compare channel.
  392. *
  393. * @return Compare event.
  394. */
  395. __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel);
  396. /**
  397. * @brief Function for getting a specific timer compare interrupt.
  398. *
  399. * @param[in] channel Compare channel.
  400. *
  401. * @return Compare interrupt.
  402. */
  403. __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel);
  404. /**
  405. * @brief Function for calculating the number of timer ticks for a given time
  406. * (in microseconds) and timer frequency.
  407. *
  408. * @param[in] time_us Time in microseconds.
  409. * @param[in] frequency Timer frequency.
  410. *
  411. * @return Number of timer ticks.
  412. */
  413. __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us,
  414. nrf_timer_frequency_t frequency);
  415. /**
  416. * @brief Function for calculating the number of timer ticks for a given time
  417. * (in milliseconds) and timer frequency.
  418. *
  419. * @param[in] time_ms Time in milliseconds.
  420. * @param[in] frequency Timer frequency.
  421. *
  422. * @return Number of timer ticks.
  423. */
  424. __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms,
  425. nrf_timer_frequency_t frequency);
  426. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  427. __STATIC_INLINE void nrf_timer_task_trigger(NRF_TIMER_Type * p_reg,
  428. nrf_timer_task_t task)
  429. {
  430. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  431. }
  432. __STATIC_INLINE uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_reg,
  433. nrf_timer_task_t task)
  434. {
  435. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)task);
  436. }
  437. __STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_reg,
  438. nrf_timer_event_t event)
  439. {
  440. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  441. #if __CORTEX_M == 0x04
  442. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  443. (void)dummy;
  444. #endif
  445. }
  446. __STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type * p_reg,
  447. nrf_timer_event_t event)
  448. {
  449. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  450. }
  451. __STATIC_INLINE uint32_t * nrf_timer_event_address_get(NRF_TIMER_Type * p_reg,
  452. nrf_timer_event_t event)
  453. {
  454. return (uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  455. }
  456. __STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_reg,
  457. uint32_t timer_shorts_mask)
  458. {
  459. p_reg->SHORTS |= timer_shorts_mask;
  460. }
  461. __STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_reg,
  462. uint32_t timer_shorts_mask)
  463. {
  464. p_reg->SHORTS &= ~(timer_shorts_mask);
  465. }
  466. __STATIC_INLINE void nrf_timer_int_enable(NRF_TIMER_Type * p_reg,
  467. uint32_t timer_int_mask)
  468. {
  469. p_reg->INTENSET = timer_int_mask;
  470. }
  471. __STATIC_INLINE void nrf_timer_int_disable(NRF_TIMER_Type * p_reg,
  472. uint32_t timer_int_mask)
  473. {
  474. p_reg->INTENCLR = timer_int_mask;
  475. }
  476. __STATIC_INLINE bool nrf_timer_int_enable_check(NRF_TIMER_Type * p_reg,
  477. uint32_t timer_int)
  478. {
  479. return (bool)(p_reg->INTENSET & timer_int);
  480. }
  481. __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_reg,
  482. nrf_timer_mode_t mode)
  483. {
  484. p_reg->MODE = (p_reg->MODE & ~TIMER_MODE_MODE_Msk) |
  485. ((mode << TIMER_MODE_MODE_Pos) & TIMER_MODE_MODE_Msk);
  486. }
  487. __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_reg)
  488. {
  489. return (nrf_timer_mode_t)(p_reg->MODE);
  490. }
  491. __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg,
  492. nrf_timer_bit_width_t bit_width)
  493. {
  494. p_reg->BITMODE = (p_reg->BITMODE & ~TIMER_BITMODE_BITMODE_Msk) |
  495. ((bit_width << TIMER_BITMODE_BITMODE_Pos) &
  496. TIMER_BITMODE_BITMODE_Msk);
  497. }
  498. __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_reg)
  499. {
  500. return (nrf_timer_bit_width_t)(p_reg->BITMODE);
  501. }
  502. __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg,
  503. nrf_timer_frequency_t frequency)
  504. {
  505. p_reg->PRESCALER = (p_reg->PRESCALER & ~TIMER_PRESCALER_PRESCALER_Msk) |
  506. ((frequency << TIMER_PRESCALER_PRESCALER_Pos) &
  507. TIMER_PRESCALER_PRESCALER_Msk);
  508. }
  509. __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_reg)
  510. {
  511. return (nrf_timer_frequency_t)(p_reg->PRESCALER);
  512. }
  513. __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_reg,
  514. nrf_timer_cc_channel_t cc_channel,
  515. uint32_t cc_value)
  516. {
  517. p_reg->CC[cc_channel] = cc_value;
  518. }
  519. __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_reg,
  520. nrf_timer_cc_channel_t cc_channel)
  521. {
  522. return (uint32_t)p_reg->CC[cc_channel];
  523. }
  524. __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel)
  525. {
  526. return (nrf_timer_task_t)
  527. ((uint32_t)NRF_TIMER_TASK_CAPTURE0 + (channel * sizeof(uint32_t)));
  528. }
  529. __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel)
  530. {
  531. return (nrf_timer_event_t)
  532. ((uint32_t)NRF_TIMER_EVENT_COMPARE0 + (channel * sizeof(uint32_t)));
  533. }
  534. __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel)
  535. {
  536. return (nrf_timer_int_mask_t)
  537. ((uint32_t)NRF_TIMER_INT_COMPARE0_MASK << channel);
  538. }
  539. __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us,
  540. nrf_timer_frequency_t frequency)
  541. {
  542. // The "frequency" parameter here is actually the prescaler value, and the
  543. // timer runs at the following frequency: f = 16 MHz / 2^prescaler.
  544. uint32_t prescaler = (uint32_t)frequency;
  545. NRFX_ASSERT(time_us <= (UINT32_MAX / 16UL));
  546. return ((time_us * 16UL) >> prescaler);
  547. }
  548. __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms,
  549. nrf_timer_frequency_t frequency)
  550. {
  551. // The "frequency" parameter here is actually the prescaler value, and the
  552. // timer runs at the following frequency: f = 16000 kHz / 2^prescaler.
  553. uint32_t prescaler = (uint32_t)frequency;
  554. NRFX_ASSERT(time_ms <= (UINT32_MAX / 16000UL));
  555. return ((time_ms * 16000UL) >> prescaler);
  556. }
  557. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  558. /** @} */
  559. #ifdef __cplusplus
  560. }
  561. #endif
  562. #endif // NRF_TIMER_H__