nrf_timer.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /* Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. /**
  13. * @defgroup nrf_timer_hal Timer HAL
  14. * @{
  15. * @ingroup nrf_timer
  16. *
  17. * @brief Hardware access layer for accessing the timer peripheral.
  18. */
  19. #ifndef NRF_TIMER_H__
  20. #define NRF_TIMER_H__
  21. #include <stddef.h>
  22. #include <stdbool.h>
  23. #include <stdint.h>
  24. #include "nrf.h"
  25. #include "nrf_assert.h"
  26. /**
  27. * @brief Macro for validating the correctness of the BIT_WIDTH setting.
  28. */
  29. #ifdef NRF51
  30. /**
  31. * In the nRF51 Series, timer instance 0 supports all available bit widths.
  32. * The other two instances support only 8 and 16 bits.
  33. */
  34. #define NRF_TIMER_IS_BIT_WIDTH_VALID(p_timer, bit_width) \
  35. ((p_timer == NRF_TIMER0) || (bit_width <= NRF_TIMER_BIT_WIDTH_16))
  36. #else
  37. /**
  38. * In the nRF52 Series, all timer instances support all available bit widths.
  39. */
  40. #define NRF_TIMER_IS_BIT_WIDTH_VALID(p_timer, bit_width) true
  41. #endif
  42. /**
  43. * @brief Macro for getting the number of capture/compare channels available
  44. * in a given timer instance.
  45. */
  46. #ifdef NRF51
  47. #define NRF_TIMER_CC_CHANNEL_COUNT(id) 4
  48. #else
  49. #define NRF_TIMER_CC_CHANNEL_COUNT(id) ((id) <= 2 ? 4 : 6)
  50. #endif
  51. /**
  52. * @brief Timer tasks.
  53. */
  54. typedef enum
  55. {
  56. /*lint -save -e30 -esym(628,__INTADDR__)*/
  57. NRF_TIMER_TASK_START = offsetof(NRF_TIMER_Type, TASKS_START), ///< Task for starting the timer.
  58. NRF_TIMER_TASK_STOP = offsetof(NRF_TIMER_Type, TASKS_STOP), ///< Task for stopping the timer.
  59. NRF_TIMER_TASK_COUNT = offsetof(NRF_TIMER_Type, TASKS_COUNT), ///< Task for incrementing the timer (in counter mode).
  60. NRF_TIMER_TASK_CLEAR = offsetof(NRF_TIMER_Type, TASKS_CLEAR), ///< Task for resetting the timer value.
  61. NRF_TIMER_TASK_SHUTDOWN = offsetof(NRF_TIMER_Type, TASKS_SHUTDOWN), ///< Task for powering off the timer.
  62. NRF_TIMER_TASK_CAPTURE0 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[0]), ///< Task for capturing the timer value on channel 0.
  63. NRF_TIMER_TASK_CAPTURE1 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[1]), ///< Task for capturing the timer value on channel 1.
  64. NRF_TIMER_TASK_CAPTURE2 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[2]), ///< Task for capturing the timer value on channel 2.
  65. NRF_TIMER_TASK_CAPTURE3 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[3]), ///< Task for capturing the timer value on channel 3.
  66. #ifdef NRF52
  67. NRF_TIMER_TASK_CAPTURE4 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[4]), ///< Task for capturing the timer value on channel 4.
  68. NRF_TIMER_TASK_CAPTURE5 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[5]), ///< Task for capturing the timer value on channel 5.
  69. #endif
  70. /*lint -restore*/
  71. } nrf_timer_task_t;
  72. /**
  73. * @brief Timer events.
  74. */
  75. typedef enum
  76. {
  77. /*lint -save -e30*/
  78. NRF_TIMER_EVENT_COMPARE0 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[0]), ///< Event from compare channel 0.
  79. NRF_TIMER_EVENT_COMPARE1 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[1]), ///< Event from compare channel 1.
  80. NRF_TIMER_EVENT_COMPARE2 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[2]), ///< Event from compare channel 2.
  81. NRF_TIMER_EVENT_COMPARE3 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[3]), ///< Event from compare channel 3.
  82. #ifdef NRF52
  83. NRF_TIMER_EVENT_COMPARE4 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[4]), ///< Event from compare channel 4.
  84. NRF_TIMER_EVENT_COMPARE5 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[5]), ///< Event from compare channel 5.
  85. #endif
  86. /*lint -restore*/
  87. } nrf_timer_event_t;
  88. /**
  89. * @brief Types of timer shortcuts.
  90. */
  91. typedef enum
  92. {
  93. NRF_TIMER_SHORT_COMPARE0_STOP_MASK = TIMER_SHORTS_COMPARE0_STOP_Msk, ///< Shortcut for stopping the timer based on compare 0.
  94. NRF_TIMER_SHORT_COMPARE1_STOP_MASK = TIMER_SHORTS_COMPARE1_STOP_Msk, ///< Shortcut for stopping the timer based on compare 1.
  95. NRF_TIMER_SHORT_COMPARE2_STOP_MASK = TIMER_SHORTS_COMPARE2_STOP_Msk, ///< Shortcut for stopping the timer based on compare 2.
  96. NRF_TIMER_SHORT_COMPARE3_STOP_MASK = TIMER_SHORTS_COMPARE3_STOP_Msk, ///< Shortcut for stopping the timer based on compare 3.
  97. #ifdef NRF52
  98. NRF_TIMER_SHORT_COMPARE4_STOP_MASK = TIMER_SHORTS_COMPARE4_STOP_Msk, ///< Shortcut for stopping the timer based on compare 4.
  99. NRF_TIMER_SHORT_COMPARE5_STOP_MASK = TIMER_SHORTS_COMPARE5_STOP_Msk, ///< Shortcut for stopping the timer based on compare 5.
  100. #endif
  101. NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK = TIMER_SHORTS_COMPARE0_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 0.
  102. NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK = TIMER_SHORTS_COMPARE1_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 1.
  103. NRF_TIMER_SHORT_COMPARE2_CLEAR_MASK = TIMER_SHORTS_COMPARE2_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 2.
  104. NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK = TIMER_SHORTS_COMPARE3_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 3.
  105. #ifdef NRF52
  106. NRF_TIMER_SHORT_COMPARE4_CLEAR_MASK = TIMER_SHORTS_COMPARE4_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 4.
  107. NRF_TIMER_SHORT_COMPARE5_CLEAR_MASK = TIMER_SHORTS_COMPARE5_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 5.
  108. #endif
  109. } nrf_timer_short_mask_t;
  110. /**
  111. * @brief Timer modes.
  112. */
  113. typedef enum
  114. {
  115. NRF_TIMER_MODE_TIMER = TIMER_MODE_MODE_Timer, ///< Timer mode: timer.
  116. NRF_TIMER_MODE_COUNTER = TIMER_MODE_MODE_Counter, ///< Timer mode: counter.
  117. #ifdef NRF52
  118. NRF_TIMER_MODE_LOW_POWER_COUNTER = TIMER_MODE_MODE_LowPowerCounter, ///< Timer mode: low-power counter.
  119. #endif
  120. } nrf_timer_mode_t;
  121. /**
  122. * @brief Timer bit width.
  123. */
  124. typedef enum
  125. {
  126. NRF_TIMER_BIT_WIDTH_8 = TIMER_BITMODE_BITMODE_08Bit, ///< Timer bit width 8 bit.
  127. NRF_TIMER_BIT_WIDTH_16 = TIMER_BITMODE_BITMODE_16Bit, ///< Timer bit width 16 bit.
  128. NRF_TIMER_BIT_WIDTH_24 = TIMER_BITMODE_BITMODE_24Bit, ///< Timer bit width 24 bit.
  129. NRF_TIMER_BIT_WIDTH_32 = TIMER_BITMODE_BITMODE_32Bit ///< Timer bit width 32 bit.
  130. } nrf_timer_bit_width_t;
  131. /**
  132. * @brief Timer prescalers.
  133. */
  134. typedef enum
  135. {
  136. NRF_TIMER_FREQ_16MHz = 0, ///< Timer frequency 16 MHz.
  137. NRF_TIMER_FREQ_8MHz, ///< Timer frequency 8 MHz.
  138. NRF_TIMER_FREQ_4MHz, ///< Timer frequency 4 MHz.
  139. NRF_TIMER_FREQ_2MHz, ///< Timer frequency 2 MHz.
  140. NRF_TIMER_FREQ_1MHz, ///< Timer frequency 1 MHz.
  141. NRF_TIMER_FREQ_500kHz, ///< Timer frequency 500 kHz.
  142. NRF_TIMER_FREQ_250kHz, ///< Timer frequency 250 kHz.
  143. NRF_TIMER_FREQ_125kHz, ///< Timer frequency 125 kHz.
  144. NRF_TIMER_FREQ_62500Hz, ///< Timer frequency 62500 Hz.
  145. NRF_TIMER_FREQ_31250Hz ///< Timer frequency 31250 Hz.
  146. } nrf_timer_frequency_t;
  147. /**
  148. * @brief Timer capture/compare channels.
  149. */
  150. typedef enum
  151. {
  152. NRF_TIMER_CC_CHANNEL0 = 0, ///< Timer capture/compare channel 0.
  153. NRF_TIMER_CC_CHANNEL1, ///< Timer capture/compare channel 1.
  154. NRF_TIMER_CC_CHANNEL2, ///< Timer capture/compare channel 2.
  155. NRF_TIMER_CC_CHANNEL3, ///< Timer capture/compare channel 3.
  156. #ifdef NRF52
  157. NRF_TIMER_CC_CHANNEL4, ///< Timer capture/compare channel 4.
  158. NRF_TIMER_CC_CHANNEL5, ///< Timer capture/compare channel 5.
  159. #endif
  160. } nrf_timer_cc_channel_t;
  161. /**
  162. * @brief Timer interrupts.
  163. */
  164. typedef enum
  165. {
  166. NRF_TIMER_INT_COMPARE0_MASK = TIMER_INTENSET_COMPARE0_Msk, ///< Timer interrupt from compare event on channel 0.
  167. NRF_TIMER_INT_COMPARE1_MASK = TIMER_INTENSET_COMPARE1_Msk, ///< Timer interrupt from compare event on channel 1.
  168. NRF_TIMER_INT_COMPARE2_MASK = TIMER_INTENSET_COMPARE2_Msk, ///< Timer interrupt from compare event on channel 2.
  169. NRF_TIMER_INT_COMPARE3_MASK = TIMER_INTENSET_COMPARE3_Msk, ///< Timer interrupt from compare event on channel 3.
  170. #ifdef NRF52
  171. NRF_TIMER_INT_COMPARE4_MASK = TIMER_INTENSET_COMPARE4_Msk, ///< Timer interrupt from compare event on channel 4.
  172. NRF_TIMER_INT_COMPARE5_MASK = TIMER_INTENSET_COMPARE5_Msk, ///< Timer interrupt from compare event on channel 5.
  173. #endif
  174. } nrf_timer_int_mask_t;
  175. /**
  176. * @brief Function for activating a specific timer task.
  177. *
  178. * @param[in] p_timer Timer instance.
  179. * @param[in] task Task to activate.
  180. */
  181. __STATIC_INLINE void nrf_timer_task_trigger(NRF_TIMER_Type * p_timer,
  182. nrf_timer_task_t task);
  183. /**
  184. * @brief Function for getting the address of a specific timer task register.
  185. *
  186. * @param[in] p_timer Timer instance.
  187. * @param[in] task Requested task.
  188. *
  189. * @return Address of the specified task register.
  190. */
  191. __STATIC_INLINE uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_timer,
  192. nrf_timer_task_t task);
  193. /**
  194. * @brief Function for clearing a specific timer event.
  195. *
  196. * @param[in] p_timer Timer instance.
  197. * @param[in] event Event to clear.
  198. */
  199. __STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_timer,
  200. nrf_timer_event_t event);
  201. /**
  202. * @brief Function for checking the state of a specific timer event.
  203. *
  204. * @param[in] p_timer Timer instance.
  205. * @param[in] event Event to check.
  206. *
  207. * @retval true If the event is set.
  208. * @retval false If the event is not set.
  209. */
  210. __STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type * p_timer,
  211. nrf_timer_event_t event);
  212. /**
  213. * @brief Function for getting the address of a specific timer event register.
  214. *
  215. * @param[in] p_timer Timer instance.
  216. * @param[in] event Requested event.
  217. *
  218. * @return Address of the specified event register.
  219. */
  220. __STATIC_INLINE uint32_t * nrf_timer_event_address_get(NRF_TIMER_Type * p_timer,
  221. nrf_timer_event_t event);
  222. /**
  223. * @brief Function for enabling specified shortcuts.
  224. *
  225. * @param[in] p_timer Timer instance.
  226. * @param[in] timer_shorts_mask Shortcuts to enable.
  227. */
  228. __STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_timer,
  229. uint32_t timer_shorts_mask);
  230. /**
  231. * @brief Function for disabling specified shortcuts.
  232. *
  233. * @param[in] p_timer Timer instance.
  234. * @param[in] timer_shorts_mask Shortcuts to disable.
  235. */
  236. __STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_timer,
  237. uint32_t timer_shorts_mask);
  238. /**
  239. * @brief Function for enabling specified interrupts.
  240. *
  241. * @param[in] p_timer Timer instance.
  242. * @param[in] timer_int_mask Interrupts to enable.
  243. */
  244. __STATIC_INLINE void nrf_timer_int_enable(NRF_TIMER_Type * p_timer,
  245. uint32_t timer_int_mask);
  246. /**
  247. * @brief Function for disabling specified interrupts.
  248. *
  249. * @param[in] p_timer Timer instance.
  250. * @param[in] timer_int_mask Interrupts to disable.
  251. */
  252. __STATIC_INLINE void nrf_timer_int_disable(NRF_TIMER_Type * p_timer,
  253. uint32_t timer_int_mask);
  254. /**
  255. * @brief Function for retrieving the state of a given interrupt.
  256. *
  257. * @param[in] p_timer Timer instance.
  258. * @param[in] timer_int Interrupt to check.
  259. *
  260. * @retval true If the interrupt is enabled.
  261. * @retval false If the interrupt is not enabled.
  262. */
  263. __STATIC_INLINE bool nrf_timer_int_enable_check(NRF_TIMER_Type * p_timer,
  264. uint32_t timer_int);
  265. /**
  266. * @brief Function for setting the timer mode.
  267. *
  268. * @param[in] p_timer Timer instance.
  269. * @param[in] mode Timer mode.
  270. */
  271. __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_timer,
  272. nrf_timer_mode_t mode);
  273. /**
  274. * @brief Function for retrieving the timer mode.
  275. *
  276. * @param[in] p_timer Timer instance.
  277. *
  278. * @return Timer mode.
  279. */
  280. __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_timer);
  281. /**
  282. * @brief Function for setting the timer bit width.
  283. *
  284. * @param[in] p_timer Timer instance.
  285. * @param[in] bit_width Timer bit width.
  286. */
  287. __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_timer,
  288. nrf_timer_bit_width_t bit_width);
  289. /**
  290. * @brief Function for retrieving the timer bit width.
  291. *
  292. * @param[in] p_timer Timer instance.
  293. *
  294. * @return Timer bit width.
  295. */
  296. __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_timer);
  297. /**
  298. * @brief Function for setting the timer frequency.
  299. *
  300. * @param[in] p_timer Timer instance.
  301. * @param[in] frequency Timer frequency.
  302. */
  303. __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_timer,
  304. nrf_timer_frequency_t frequency);
  305. /**
  306. * @brief Function for retrieving the timer frequency.
  307. *
  308. * @param[in] p_timer Timer instance.
  309. *
  310. * @return Timer frequency.
  311. */
  312. __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_timer);
  313. /**
  314. * @brief Function for writing the capture/compare register for a specified channel.
  315. *
  316. * @param[in] p_timer Timer instance.
  317. * @param[in] cc_channel Requested capture/compare channel.
  318. * @param[in] cc_value Value to write to the capture/compare register.
  319. */
  320. __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_timer,
  321. nrf_timer_cc_channel_t cc_channel,
  322. uint32_t cc_value);
  323. /**
  324. * @brief Function for retrieving the capture/compare value for a specified channel.
  325. *
  326. * @param[in] p_timer Timer instance.
  327. * @param[in] cc_channel Requested capture/compare channel.
  328. *
  329. * @return Value from the requested capture/compare register.
  330. */
  331. __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_timer,
  332. nrf_timer_cc_channel_t cc_channel);
  333. /**
  334. * @brief Function for getting a specific timer capture task.
  335. *
  336. * @param[in] channel Capture channel.
  337. *
  338. * @return Capture task.
  339. */
  340. __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel);
  341. /**
  342. * @brief Function for getting a specific timer compare event.
  343. *
  344. * @param[in] channel Compare channel.
  345. *
  346. * @return Compare event.
  347. */
  348. __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel);
  349. /**
  350. * @brief Function for getting a specific timer compare interrupt.
  351. *
  352. * @param[in] channel Compare channel.
  353. *
  354. * @return Compare interrupt.
  355. */
  356. __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel);
  357. /**
  358. * @brief Function for calculating the number of timer ticks for a given time
  359. * (in microseconds) and timer frequency.
  360. *
  361. * @param[in] time_us Time in microseconds.
  362. * @param[in] frequency Timer frequency.
  363. *
  364. * @return Number of timer ticks.
  365. */
  366. __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us,
  367. nrf_timer_frequency_t frequency);
  368. /**
  369. * @brief Function for calculating the number of timer ticks for a given time
  370. * (in milliseconds) and timer frequency.
  371. *
  372. * @param[in] time_ms Time in milliseconds.
  373. * @param[in] frequency Timer frequency.
  374. *
  375. * @return Number of timer ticks.
  376. */
  377. __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms,
  378. nrf_timer_frequency_t frequency);
  379. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  380. __STATIC_INLINE void nrf_timer_task_trigger(NRF_TIMER_Type * p_timer,
  381. nrf_timer_task_t task)
  382. {
  383. *((volatile uint32_t *)((uint8_t *)p_timer + (uint32_t)task)) = 0x1UL;
  384. }
  385. __STATIC_INLINE uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_timer,
  386. nrf_timer_task_t task)
  387. {
  388. return (uint32_t *)((uint8_t *)p_timer + (uint32_t)task);
  389. }
  390. __STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_timer,
  391. nrf_timer_event_t event)
  392. {
  393. *((volatile uint32_t *)((uint8_t *)p_timer + (uint32_t)event)) = 0x0UL;
  394. }
  395. __STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type * p_timer,
  396. nrf_timer_event_t event)
  397. {
  398. return (bool)*(volatile uint32_t *)((uint8_t *)p_timer + (uint32_t)event);
  399. }
  400. __STATIC_INLINE uint32_t * nrf_timer_event_address_get(NRF_TIMER_Type * p_timer,
  401. nrf_timer_event_t event)
  402. {
  403. return (uint32_t *)((uint8_t *)p_timer + (uint32_t)event);
  404. }
  405. __STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_timer,
  406. uint32_t timer_shorts_mask)
  407. {
  408. p_timer->SHORTS |= timer_shorts_mask;
  409. }
  410. __STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_timer,
  411. uint32_t timer_shorts_mask)
  412. {
  413. p_timer->SHORTS &= ~(timer_shorts_mask);
  414. }
  415. __STATIC_INLINE void nrf_timer_int_enable(NRF_TIMER_Type * p_timer,
  416. uint32_t timer_int_mask)
  417. {
  418. p_timer->INTENSET = timer_int_mask;
  419. }
  420. __STATIC_INLINE void nrf_timer_int_disable(NRF_TIMER_Type * p_timer,
  421. uint32_t timer_int_mask)
  422. {
  423. p_timer->INTENCLR = timer_int_mask;
  424. }
  425. __STATIC_INLINE bool nrf_timer_int_enable_check(NRF_TIMER_Type * p_timer,
  426. uint32_t timer_int)
  427. {
  428. return (bool)(p_timer->INTENSET & timer_int);
  429. }
  430. __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_timer,
  431. nrf_timer_mode_t mode)
  432. {
  433. p_timer->MODE = (p_timer->MODE & ~TIMER_MODE_MODE_Msk) |
  434. ((mode << TIMER_MODE_MODE_Pos) & TIMER_MODE_MODE_Msk);
  435. }
  436. __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_timer)
  437. {
  438. return (nrf_timer_mode_t)(p_timer->MODE);
  439. }
  440. __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_timer,
  441. nrf_timer_bit_width_t bit_width)
  442. {
  443. p_timer->BITMODE = (p_timer->BITMODE & ~TIMER_BITMODE_BITMODE_Msk) |
  444. ((bit_width << TIMER_BITMODE_BITMODE_Pos) &
  445. TIMER_BITMODE_BITMODE_Msk);
  446. }
  447. __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_timer)
  448. {
  449. return (nrf_timer_bit_width_t)(p_timer->BITMODE);
  450. }
  451. __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_timer,
  452. nrf_timer_frequency_t frequency)
  453. {
  454. p_timer->PRESCALER = (p_timer->PRESCALER & ~TIMER_PRESCALER_PRESCALER_Msk) |
  455. ((frequency << TIMER_PRESCALER_PRESCALER_Pos) &
  456. TIMER_PRESCALER_PRESCALER_Msk);
  457. }
  458. __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_timer)
  459. {
  460. return (nrf_timer_frequency_t)(p_timer->PRESCALER);
  461. }
  462. __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_timer,
  463. nrf_timer_cc_channel_t cc_channel,
  464. uint32_t cc_value)
  465. {
  466. p_timer->CC[cc_channel] = cc_value;
  467. }
  468. __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_timer,
  469. nrf_timer_cc_channel_t cc_channel)
  470. {
  471. return (uint32_t)p_timer->CC[cc_channel];
  472. }
  473. __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel)
  474. {
  475. return (nrf_timer_task_t)
  476. ((uint32_t)NRF_TIMER_TASK_CAPTURE0 + (channel * sizeof(uint32_t)));
  477. }
  478. __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel)
  479. {
  480. return (nrf_timer_event_t)
  481. ((uint32_t)NRF_TIMER_EVENT_COMPARE0 + (channel * sizeof(uint32_t)));
  482. }
  483. __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel)
  484. {
  485. return (nrf_timer_int_mask_t)
  486. ((uint32_t)NRF_TIMER_INT_COMPARE0_MASK << channel);
  487. }
  488. __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us,
  489. nrf_timer_frequency_t frequency)
  490. {
  491. // The "frequency" parameter here is actually the prescaler value, and the
  492. // timer runs at the following frequency: f = 16 MHz / 2^prescaler.
  493. uint32_t prescaler = (uint32_t)frequency;
  494. ASSERT(time_us <= (UINT32_MAX / 16UL));
  495. return ((time_us * 16UL) >> prescaler);
  496. }
  497. __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms,
  498. nrf_timer_frequency_t frequency)
  499. {
  500. // The "frequency" parameter here is actually the prescaler value, and the
  501. // timer runs at the following frequency: f = 16000 kHz / 2^prescaler.
  502. uint32_t prescaler = (uint32_t)frequency;
  503. ASSERT(time_ms <= (UINT32_MAX / 16000UL));
  504. return ((time_ms * 16000UL) >> prescaler);
  505. }
  506. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  507. #endif // NRF_TIMER_H__
  508. /** @} */