1 /* 2 * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its 16 * contributors may be used to endorse or promote products derived from this 17 * software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef NRF_TIMER_H__ 33 #define NRF_TIMER_H__ 34 35 #include <nrfx.h> 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 /** 42 * @defgroup nrf_timer_hal TIMER HAL 43 * @{ 44 * @ingroup nrf_timer 45 * @brief Hardware access layer for managing the TIMER peripheral. 46 */ 47 48 /** 49 * @brief Macro for validating the correctness of the BIT_WIDTH setting. 50 */ 51 52 #define TIMER_MAX_SIZE(id) NRFX_CONCAT_3(TIMER, id, _MAX_SIZE) 53 54 #define TIMER_BIT_WIDTH_MAX(id, bit_width) \ 55 (TIMER_MAX_SIZE(id) == 8 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) : \ 56 (TIMER_MAX_SIZE(id) == 16 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) || \ 57 (bit_width == NRF_TIMER_BIT_WIDTH_16) : \ 58 (TIMER_MAX_SIZE(id) == 24 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) || \ 59 (bit_width == NRF_TIMER_BIT_WIDTH_16) || \ 60 (bit_width == NRF_TIMER_BIT_WIDTH_24) : \ 61 (TIMER_MAX_SIZE(id) == 32 ? (bit_width == NRF_TIMER_BIT_WIDTH_8) || \ 62 (bit_width == NRF_TIMER_BIT_WIDTH_16) || \ 63 (bit_width == NRF_TIMER_BIT_WIDTH_24) || \ 64 (bit_width == NRF_TIMER_BIT_WIDTH_32) : \ 65 false)))) 66 67 #if TIMER_COUNT > 3 68 #define NRF_TIMER_IS_BIT_WIDTH_VALID(p_reg, bit_width) ( \ 69 ((p_reg == NRF_TIMER0) && (TIMER_BIT_WIDTH_MAX(0, bit_width))) \ 70 || ((p_reg == NRF_TIMER1) && (TIMER_BIT_WIDTH_MAX(1, bit_width))) \ 71 || ((p_reg == NRF_TIMER2) && (TIMER_BIT_WIDTH_MAX(2, bit_width))) \ 72 || ((p_reg == NRF_TIMER3) && (TIMER_BIT_WIDTH_MAX(3, bit_width))) \ 73 || ((p_reg == NRF_TIMER4) && (TIMER_BIT_WIDTH_MAX(4, bit_width))) ) 74 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 81 #endif 82 83 /** 84 * @brief Macro for getting the number of capture/compare channels available 85 * in a given timer instance. 86 */ 87 #define NRF_TIMER_CC_CHANNEL_COUNT(id) NRFX_CONCAT_3(TIMER, id, _CC_NUM) 88 89 /** 90 * @brief Timer tasks. 91 */ 92 typedef enum 93 { 94 /*lint -save -e30 -esym(628,__INTADDR__)*/ 95 NRF_TIMER_TASK_START = offsetof(NRF_TIMER_Type, TASKS_START), ///< Task for starting the timer. 96 NRF_TIMER_TASK_STOP = offsetof(NRF_TIMER_Type, TASKS_STOP), ///< Task for stopping the timer. 97 NRF_TIMER_TASK_COUNT = offsetof(NRF_TIMER_Type, TASKS_COUNT), ///< Task for incrementing the timer (in counter mode). 98 NRF_TIMER_TASK_CLEAR = offsetof(NRF_TIMER_Type, TASKS_CLEAR), ///< Task for resetting the timer value. 99 NRF_TIMER_TASK_SHUTDOWN = offsetof(NRF_TIMER_Type, TASKS_SHUTDOWN), ///< Task for powering off the timer. 100 NRF_TIMER_TASK_CAPTURE0 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[0]), ///< Task for capturing the timer value on channel 0. 101 NRF_TIMER_TASK_CAPTURE1 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[1]), ///< Task for capturing the timer value on channel 1. 102 NRF_TIMER_TASK_CAPTURE2 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[2]), ///< Task for capturing the timer value on channel 2. 103 NRF_TIMER_TASK_CAPTURE3 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[3]), ///< Task for capturing the timer value on channel 3. 104 #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__) 105 NRF_TIMER_TASK_CAPTURE4 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[4]), ///< Task for capturing the timer value on channel 4. 106 #endif 107 #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__) 108 NRF_TIMER_TASK_CAPTURE5 = offsetof(NRF_TIMER_Type, TASKS_CAPTURE[5]), ///< Task for capturing the timer value on channel 5. 109 #endif 110 /*lint -restore*/ 111 } nrf_timer_task_t; 112 113 /** 114 * @brief Timer events. 115 */ 116 typedef enum 117 { 118 /*lint -save -e30*/ 119 NRF_TIMER_EVENT_COMPARE0 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[0]), ///< Event from compare channel 0. 120 NRF_TIMER_EVENT_COMPARE1 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[1]), ///< Event from compare channel 1. 121 NRF_TIMER_EVENT_COMPARE2 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[2]), ///< Event from compare channel 2. 122 NRF_TIMER_EVENT_COMPARE3 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[3]), ///< Event from compare channel 3. 123 #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__) 124 NRF_TIMER_EVENT_COMPARE4 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[4]), ///< Event from compare channel 4. 125 #endif 126 #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__) 127 NRF_TIMER_EVENT_COMPARE5 = offsetof(NRF_TIMER_Type, EVENTS_COMPARE[5]), ///< Event from compare channel 5. 128 #endif 129 /*lint -restore*/ 130 } nrf_timer_event_t; 131 132 /** 133 * @brief Types of timer shortcuts. 134 */ 135 typedef enum 136 { 137 NRF_TIMER_SHORT_COMPARE0_STOP_MASK = TIMER_SHORTS_COMPARE0_STOP_Msk, ///< Shortcut for stopping the timer based on compare 0. 138 NRF_TIMER_SHORT_COMPARE1_STOP_MASK = TIMER_SHORTS_COMPARE1_STOP_Msk, ///< Shortcut for stopping the timer based on compare 1. 139 NRF_TIMER_SHORT_COMPARE2_STOP_MASK = TIMER_SHORTS_COMPARE2_STOP_Msk, ///< Shortcut for stopping the timer based on compare 2. 140 NRF_TIMER_SHORT_COMPARE3_STOP_MASK = TIMER_SHORTS_COMPARE3_STOP_Msk, ///< Shortcut for stopping the timer based on compare 3. 141 #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__) 142 NRF_TIMER_SHORT_COMPARE4_STOP_MASK = TIMER_SHORTS_COMPARE4_STOP_Msk, ///< Shortcut for stopping the timer based on compare 4. 143 #endif 144 #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__) 145 NRF_TIMER_SHORT_COMPARE5_STOP_MASK = TIMER_SHORTS_COMPARE5_STOP_Msk, ///< Shortcut for stopping the timer based on compare 5. 146 #endif 147 NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK = TIMER_SHORTS_COMPARE0_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 0. 148 NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK = TIMER_SHORTS_COMPARE1_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 1. 149 NRF_TIMER_SHORT_COMPARE2_CLEAR_MASK = TIMER_SHORTS_COMPARE2_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 2. 150 NRF_TIMER_SHORT_COMPARE3_CLEAR_MASK = TIMER_SHORTS_COMPARE3_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 3. 151 #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__) 152 NRF_TIMER_SHORT_COMPARE4_CLEAR_MASK = TIMER_SHORTS_COMPARE4_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 4. 153 #endif 154 #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__) 155 NRF_TIMER_SHORT_COMPARE5_CLEAR_MASK = TIMER_SHORTS_COMPARE5_CLEAR_Msk, ///< Shortcut for clearing the timer based on compare 5. 156 #endif 157 } nrf_timer_short_mask_t; 158 159 /** 160 * @brief Timer modes. 161 */ 162 typedef enum 163 { 164 NRF_TIMER_MODE_TIMER = TIMER_MODE_MODE_Timer, ///< Timer mode: timer. 165 NRF_TIMER_MODE_COUNTER = TIMER_MODE_MODE_Counter, ///< Timer mode: counter. 166 #if defined(TIMER_MODE_MODE_LowPowerCounter) || defined(__NRFX_DOXYGEN__) 167 NRF_TIMER_MODE_LOW_POWER_COUNTER = TIMER_MODE_MODE_LowPowerCounter, ///< Timer mode: low-power counter. 168 #endif 169 } nrf_timer_mode_t; 170 171 /** 172 * @brief Timer bit width. 173 */ 174 typedef enum 175 { 176 NRF_TIMER_BIT_WIDTH_8 = TIMER_BITMODE_BITMODE_08Bit, ///< Timer bit width 8 bit. 177 NRF_TIMER_BIT_WIDTH_16 = TIMER_BITMODE_BITMODE_16Bit, ///< Timer bit width 16 bit. 178 NRF_TIMER_BIT_WIDTH_24 = TIMER_BITMODE_BITMODE_24Bit, ///< Timer bit width 24 bit. 179 NRF_TIMER_BIT_WIDTH_32 = TIMER_BITMODE_BITMODE_32Bit ///< Timer bit width 32 bit. 180 } nrf_timer_bit_width_t; 181 182 /** 183 * @brief Timer prescalers. 184 */ 185 typedef enum 186 { 187 NRF_TIMER_FREQ_16MHz = 0, ///< Timer frequency 16 MHz. 188 NRF_TIMER_FREQ_8MHz, ///< Timer frequency 8 MHz. 189 NRF_TIMER_FREQ_4MHz, ///< Timer frequency 4 MHz. 190 NRF_TIMER_FREQ_2MHz, ///< Timer frequency 2 MHz. 191 NRF_TIMER_FREQ_1MHz, ///< Timer frequency 1 MHz. 192 NRF_TIMER_FREQ_500kHz, ///< Timer frequency 500 kHz. 193 NRF_TIMER_FREQ_250kHz, ///< Timer frequency 250 kHz. 194 NRF_TIMER_FREQ_125kHz, ///< Timer frequency 125 kHz. 195 NRF_TIMER_FREQ_62500Hz, ///< Timer frequency 62500 Hz. 196 NRF_TIMER_FREQ_31250Hz ///< Timer frequency 31250 Hz. 197 } nrf_timer_frequency_t; 198 199 /** 200 * @brief Timer capture/compare channels. 201 */ 202 typedef enum 203 { 204 NRF_TIMER_CC_CHANNEL0 = 0, ///< Timer capture/compare channel 0. 205 NRF_TIMER_CC_CHANNEL1, ///< Timer capture/compare channel 1. 206 NRF_TIMER_CC_CHANNEL2, ///< Timer capture/compare channel 2. 207 NRF_TIMER_CC_CHANNEL3, ///< Timer capture/compare channel 3. 208 #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__) 209 NRF_TIMER_CC_CHANNEL4, ///< Timer capture/compare channel 4. 210 #endif 211 #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__) 212 NRF_TIMER_CC_CHANNEL5, ///< Timer capture/compare channel 5. 213 #endif 214 } nrf_timer_cc_channel_t; 215 216 /** 217 * @brief Timer interrupts. 218 */ 219 typedef enum 220 { 221 NRF_TIMER_INT_COMPARE0_MASK = TIMER_INTENSET_COMPARE0_Msk, ///< Timer interrupt from compare event on channel 0. 222 NRF_TIMER_INT_COMPARE1_MASK = TIMER_INTENSET_COMPARE1_Msk, ///< Timer interrupt from compare event on channel 1. 223 NRF_TIMER_INT_COMPARE2_MASK = TIMER_INTENSET_COMPARE2_Msk, ///< Timer interrupt from compare event on channel 2. 224 NRF_TIMER_INT_COMPARE3_MASK = TIMER_INTENSET_COMPARE3_Msk, ///< Timer interrupt from compare event on channel 3. 225 #if defined(TIMER_INTENSET_COMPARE4_Msk) || defined(__NRFX_DOXYGEN__) 226 NRF_TIMER_INT_COMPARE4_MASK = TIMER_INTENSET_COMPARE4_Msk, ///< Timer interrupt from compare event on channel 4. 227 #endif 228 #if defined(TIMER_INTENSET_COMPARE5_Msk) || defined(__NRFX_DOXYGEN__) 229 NRF_TIMER_INT_COMPARE5_MASK = TIMER_INTENSET_COMPARE5_Msk, ///< Timer interrupt from compare event on channel 5. 230 #endif 231 } nrf_timer_int_mask_t; 232 233 234 /** 235 * @brief Function for activating a specific timer task. 236 * 237 * @param[in] p_reg Pointer to the peripheral registers structure. 238 * @param[in] task Task to activate. 239 */ 240 __STATIC_INLINE void nrf_timer_task_trigger(NRF_TIMER_Type * p_reg, 241 nrf_timer_task_t task); 242 243 /** 244 * @brief Function for getting the address of a specific timer task register. 245 * 246 * @param[in] p_reg Pointer to the peripheral registers structure. 247 * @param[in] task Requested task. 248 * 249 * @return Address of the specified task register. 250 */ 251 __STATIC_INLINE uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_reg, 252 nrf_timer_task_t task); 253 254 /** 255 * @brief Function for clearing a specific timer event. 256 * 257 * @param[in] p_reg Pointer to the peripheral registers structure. 258 * @param[in] event Event to clear. 259 */ 260 __STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_reg, 261 nrf_timer_event_t event); 262 263 /** 264 * @brief Function for checking the state of a specific timer event. 265 * 266 * @param[in] p_reg Pointer to the peripheral registers structure. 267 * @param[in] event Event to check. 268 * 269 * @retval true If the event is set. 270 * @retval false If the event is not set. 271 */ 272 __STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type * p_reg, 273 nrf_timer_event_t event); 274 275 /** 276 * @brief Function for getting the address of a specific timer event register. 277 * 278 * @param[in] p_reg Pointer to the peripheral registers structure. 279 * @param[in] event Requested event. 280 * 281 * @return Address of the specified event register. 282 */ 283 __STATIC_INLINE uint32_t * nrf_timer_event_address_get(NRF_TIMER_Type * p_reg, 284 nrf_timer_event_t event); 285 286 /** 287 * @brief Function for enabling specified shortcuts. 288 * 289 * @param[in] p_reg Pointer to the peripheral registers structure. 290 * @param[in] timer_shorts_mask Shortcuts to enable. 291 */ 292 __STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_reg, 293 uint32_t timer_shorts_mask); 294 295 /** 296 * @brief Function for disabling specified shortcuts. 297 * 298 * @param[in] p_reg Pointer to the peripheral registers structure. 299 * @param[in] timer_shorts_mask Shortcuts to disable. 300 */ 301 __STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_reg, 302 uint32_t timer_shorts_mask); 303 304 /** 305 * @brief Function for enabling specified interrupts. 306 * 307 * @param[in] p_reg Pointer to the peripheral registers structure. 308 * @param[in] timer_int_mask Interrupts to enable. 309 */ 310 __STATIC_INLINE void nrf_timer_int_enable(NRF_TIMER_Type * p_reg, 311 uint32_t timer_int_mask); 312 313 /** 314 * @brief Function for disabling specified interrupts. 315 * 316 * @param[in] p_reg Pointer to the peripheral registers structure. 317 * @param[in] timer_int_mask Interrupts to disable. 318 */ 319 __STATIC_INLINE void nrf_timer_int_disable(NRF_TIMER_Type * p_reg, 320 uint32_t timer_int_mask); 321 322 /** 323 * @brief Function for retrieving the state of a given interrupt. 324 * 325 * @param[in] p_reg Pointer to the peripheral registers structure. 326 * @param[in] timer_int Interrupt to check. 327 * 328 * @retval true If the interrupt is enabled. 329 * @retval false If the interrupt is not enabled. 330 */ 331 __STATIC_INLINE bool nrf_timer_int_enable_check(NRF_TIMER_Type * p_reg, 332 uint32_t timer_int); 333 334 #if defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__) 335 /** 336 * @brief Function for setting the subscribe configuration for a given 337 * TIMER task. 338 * 339 * @param[in] p_reg Pointer to the structure of registers of the peripheral. 340 * @param[in] task Task for which to set the configuration. 341 * @param[in] channel Channel through which to subscribe events. 342 */ 343 __STATIC_INLINE void nrf_timer_subscribe_set(NRF_TIMER_Type * p_reg, 344 nrf_timer_task_t task, 345 uint8_t channel); 346 347 /** 348 * @brief Function for clearing the subscribe configuration for a given 349 * TIMER task. 350 * 351 * @param[in] p_reg Pointer to the structure of registers of the peripheral. 352 * @param[in] task Task for which to clear the configuration. 353 */ 354 __STATIC_INLINE void nrf_timer_subscribe_clear(NRF_TIMER_Type * p_reg, 355 nrf_timer_task_t task); 356 357 /** 358 * @brief Function for setting the publish configuration for a given 359 * TIMER event. 360 * 361 * @param[in] p_reg Pointer to the structure of registers of the peripheral. 362 * @param[in] event Event for which to set the configuration. 363 * @param[in] channel Channel through which to publish the event. 364 */ 365 __STATIC_INLINE void nrf_timer_publish_set(NRF_TIMER_Type * p_reg, 366 nrf_timer_event_t event, 367 uint8_t channel); 368 369 /** 370 * @brief Function for clearing the publish configuration for a given 371 * TIMER event. 372 * 373 * @param[in] p_reg Pointer to the structure of registers of the peripheral. 374 * @param[in] event Event for which to clear the configuration. 375 */ 376 __STATIC_INLINE void nrf_timer_publish_clear(NRF_TIMER_Type * p_reg, 377 nrf_timer_event_t event); 378 #endif // defined(DPPI_PRESENT) || defined(__NRFX_DOXYGEN__) 379 380 /** 381 * @brief Function for setting the timer mode. 382 * 383 * @param[in] p_reg Pointer to the peripheral registers structure. 384 * @param[in] mode Timer mode. 385 */ 386 __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_reg, 387 nrf_timer_mode_t mode); 388 389 /** 390 * @brief Function for retrieving the timer mode. 391 * 392 * @param[in] p_reg Pointer to the peripheral registers structure. 393 * 394 * @return Timer mode. 395 */ 396 __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_reg); 397 398 /** 399 * @brief Function for setting the timer bit width. 400 * 401 * @param[in] p_reg Pointer to the peripheral registers structure. 402 * @param[in] bit_width Timer bit width. 403 */ 404 __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg, 405 nrf_timer_bit_width_t bit_width); 406 407 /** 408 * @brief Function for retrieving the timer bit width. 409 * 410 * @param[in] p_reg Pointer to the peripheral registers structure. 411 * 412 * @return Timer bit width. 413 */ 414 __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_reg); 415 416 /** 417 * @brief Function for setting the timer frequency. 418 * 419 * @param[in] p_reg Pointer to the peripheral registers structure. 420 * @param[in] frequency Timer frequency. 421 */ 422 __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg, 423 nrf_timer_frequency_t frequency); 424 425 /** 426 * @brief Function for retrieving the timer frequency. 427 * 428 * @param[in] p_reg Pointer to the peripheral registers structure. 429 * 430 * @return Timer frequency. 431 */ 432 __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_reg); 433 434 /** 435 * @brief Function for writing the capture/compare register for a specified channel. 436 * 437 * @param[in] p_reg Pointer to the peripheral registers structure. 438 * @param[in] cc_channel Requested capture/compare channel. 439 * @param[in] cc_value Value to write to the capture/compare register. 440 */ 441 __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_reg, 442 nrf_timer_cc_channel_t cc_channel, 443 uint32_t cc_value); 444 445 /** 446 * @brief Function for retrieving the capture/compare value for a specified channel. 447 * 448 * @param[in] p_reg Pointer to the peripheral registers structure. 449 * @param[in] cc_channel Requested capture/compare channel. 450 * 451 * @return Value from the requested capture/compare register. 452 */ 453 __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_reg, 454 nrf_timer_cc_channel_t cc_channel); 455 456 /** 457 * @brief Function for getting a specific timer capture task. 458 * 459 * @param[in] channel Capture channel. 460 * 461 * @return Capture task. 462 */ 463 __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel); 464 465 /** 466 * @brief Function for getting a specific timer compare event. 467 * 468 * @param[in] channel Compare channel. 469 * 470 * @return Compare event. 471 */ 472 __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel); 473 474 /** 475 * @brief Function for getting a specific timer compare interrupt. 476 * 477 * @param[in] channel Compare channel. 478 * 479 * @return Compare interrupt. 480 */ 481 __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel); 482 483 /** 484 * @brief Function for calculating the number of timer ticks for a given time 485 * (in microseconds) and timer frequency. 486 * 487 * @param[in] time_us Time in microseconds. 488 * @param[in] frequency Timer frequency. 489 * 490 * @return Number of timer ticks. 491 */ 492 __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us, 493 nrf_timer_frequency_t frequency); 494 495 /** 496 * @brief Function for calculating the number of timer ticks for a given time 497 * (in milliseconds) and timer frequency. 498 * 499 * @param[in] time_ms Time in milliseconds. 500 * @param[in] frequency Timer frequency. 501 * 502 * @return Number of timer ticks. 503 */ 504 __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms, 505 nrf_timer_frequency_t frequency); 506 507 508 #ifndef SUPPRESS_INLINE_IMPLEMENTATION 509 510 __STATIC_INLINE void nrf_timer_task_trigger(NRF_TIMER_Type * p_reg, 511 nrf_timer_task_t task) 512 { 513 *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL; 514 } 515 516 __STATIC_INLINE uint32_t * nrf_timer_task_address_get(NRF_TIMER_Type * p_reg, 517 nrf_timer_task_t task) 518 { 519 return (uint32_t *)((uint8_t *)p_reg + (uint32_t)task); 520 } 521 522 __STATIC_INLINE void nrf_timer_event_clear(NRF_TIMER_Type * p_reg, 523 nrf_timer_event_t event) 524 { 525 *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL; 526 #if __CORTEX_M == 0x04 527 volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)); 528 (void)dummy; 529 #endif 530 } 531 532 __STATIC_INLINE bool nrf_timer_event_check(NRF_TIMER_Type * p_reg, 533 nrf_timer_event_t event) 534 { 535 return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event); 536 } 537 538 __STATIC_INLINE uint32_t * nrf_timer_event_address_get(NRF_TIMER_Type * p_reg, 539 nrf_timer_event_t event) 540 { 541 return (uint32_t *)((uint8_t *)p_reg + (uint32_t)event); 542 } 543 544 __STATIC_INLINE void nrf_timer_shorts_enable(NRF_TIMER_Type * p_reg, 545 uint32_t timer_shorts_mask) 546 { 547 p_reg->SHORTS |= timer_shorts_mask; 548 } 549 550 __STATIC_INLINE void nrf_timer_shorts_disable(NRF_TIMER_Type * p_reg, 551 uint32_t timer_shorts_mask) 552 { 553 p_reg->SHORTS &= ~(timer_shorts_mask); 554 } 555 556 __STATIC_INLINE void nrf_timer_int_enable(NRF_TIMER_Type * p_reg, 557 uint32_t timer_int_mask) 558 { 559 p_reg->INTENSET = timer_int_mask; 560 } 561 562 __STATIC_INLINE void nrf_timer_int_disable(NRF_TIMER_Type * p_reg, 563 uint32_t timer_int_mask) 564 { 565 p_reg->INTENCLR = timer_int_mask; 566 } 567 568 __STATIC_INLINE bool nrf_timer_int_enable_check(NRF_TIMER_Type * p_reg, 569 uint32_t timer_int) 570 { 571 return (bool)(p_reg->INTENSET & timer_int); 572 } 573 574 #if defined(DPPI_PRESENT) 575 __STATIC_INLINE void nrf_timer_subscribe_set(NRF_TIMER_Type * p_reg, 576 nrf_timer_task_t task, 577 uint8_t channel) 578 { 579 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 580 ((uint32_t)channel | TIMER_SUBSCRIBE_START_EN_Msk); 581 } 582 583 __STATIC_INLINE void nrf_timer_subscribe_clear(NRF_TIMER_Type * p_reg, 584 nrf_timer_task_t task) 585 { 586 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) task + 0x80uL)) = 0; 587 } 588 589 __STATIC_INLINE void nrf_timer_publish_set(NRF_TIMER_Type * p_reg, 590 nrf_timer_event_t event, 591 uint8_t channel) 592 { 593 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 594 ((uint32_t)channel | TIMER_PUBLISH_COMPARE_EN_Msk); 595 } 596 597 __STATIC_INLINE void nrf_timer_publish_clear(NRF_TIMER_Type * p_reg, 598 nrf_timer_event_t event) 599 { 600 *((volatile uint32_t *) ((uint8_t *) p_reg + (uint32_t) event + 0x80uL)) = 0; 601 } 602 #endif // defined(DPPI_PRESENT) 603 604 __STATIC_INLINE void nrf_timer_mode_set(NRF_TIMER_Type * p_reg, 605 nrf_timer_mode_t mode) 606 { 607 p_reg->MODE = (p_reg->MODE & ~TIMER_MODE_MODE_Msk) | 608 ((mode << TIMER_MODE_MODE_Pos) & TIMER_MODE_MODE_Msk); 609 } 610 611 __STATIC_INLINE nrf_timer_mode_t nrf_timer_mode_get(NRF_TIMER_Type * p_reg) 612 { 613 return (nrf_timer_mode_t)(p_reg->MODE); 614 } 615 616 __STATIC_INLINE void nrf_timer_bit_width_set(NRF_TIMER_Type * p_reg, 617 nrf_timer_bit_width_t bit_width) 618 { 619 p_reg->BITMODE = (p_reg->BITMODE & ~TIMER_BITMODE_BITMODE_Msk) | 620 ((bit_width << TIMER_BITMODE_BITMODE_Pos) & 621 TIMER_BITMODE_BITMODE_Msk); 622 } 623 624 __STATIC_INLINE nrf_timer_bit_width_t nrf_timer_bit_width_get(NRF_TIMER_Type * p_reg) 625 { 626 return (nrf_timer_bit_width_t)(p_reg->BITMODE); 627 } 628 629 __STATIC_INLINE void nrf_timer_frequency_set(NRF_TIMER_Type * p_reg, 630 nrf_timer_frequency_t frequency) 631 { 632 p_reg->PRESCALER = (p_reg->PRESCALER & ~TIMER_PRESCALER_PRESCALER_Msk) | 633 ((frequency << TIMER_PRESCALER_PRESCALER_Pos) & 634 TIMER_PRESCALER_PRESCALER_Msk); 635 } 636 637 __STATIC_INLINE nrf_timer_frequency_t nrf_timer_frequency_get(NRF_TIMER_Type * p_reg) 638 { 639 return (nrf_timer_frequency_t)(p_reg->PRESCALER); 640 } 641 642 __STATIC_INLINE void nrf_timer_cc_write(NRF_TIMER_Type * p_reg, 643 nrf_timer_cc_channel_t cc_channel, 644 uint32_t cc_value) 645 { 646 p_reg->CC[cc_channel] = cc_value; 647 } 648 649 __STATIC_INLINE uint32_t nrf_timer_cc_read(NRF_TIMER_Type * p_reg, 650 nrf_timer_cc_channel_t cc_channel) 651 { 652 return (uint32_t)p_reg->CC[cc_channel]; 653 } 654 655 __STATIC_INLINE nrf_timer_task_t nrf_timer_capture_task_get(uint32_t channel) 656 { 657 return (nrf_timer_task_t) 658 ((uint32_t)NRF_TIMER_TASK_CAPTURE0 + (channel * sizeof(uint32_t))); 659 } 660 661 __STATIC_INLINE nrf_timer_event_t nrf_timer_compare_event_get(uint32_t channel) 662 { 663 return (nrf_timer_event_t) 664 ((uint32_t)NRF_TIMER_EVENT_COMPARE0 + (channel * sizeof(uint32_t))); 665 } 666 667 __STATIC_INLINE nrf_timer_int_mask_t nrf_timer_compare_int_get(uint32_t channel) 668 { 669 return (nrf_timer_int_mask_t) 670 ((uint32_t)NRF_TIMER_INT_COMPARE0_MASK << channel); 671 } 672 673 __STATIC_INLINE uint32_t nrf_timer_us_to_ticks(uint32_t time_us, 674 nrf_timer_frequency_t frequency) 675 { 676 // The "frequency" parameter here is actually the prescaler value, and the 677 // timer runs at the following frequency: f = 16 MHz / 2^prescaler. 678 uint32_t prescaler = (uint32_t)frequency; 679 uint64_t ticks = ((time_us * 16ULL) >> prescaler); 680 NRFX_ASSERT(ticks <= UINT32_MAX); 681 return (uint32_t)ticks; 682 } 683 684 __STATIC_INLINE uint32_t nrf_timer_ms_to_ticks(uint32_t time_ms, 685 nrf_timer_frequency_t frequency) 686 { 687 // The "frequency" parameter here is actually the prescaler value, and the 688 // timer runs at the following frequency: f = 16000 kHz / 2^prescaler. 689 uint32_t prescaler = (uint32_t)frequency; 690 uint64_t ticks = ((time_ms * 16000ULL) >> prescaler); 691 NRFX_ASSERT(ticks <= UINT32_MAX); 692 return (uint32_t)ticks; 693 } 694 695 #endif // SUPPRESS_INLINE_IMPLEMENTATION 696 697 /** @} */ 698 699 #ifdef __cplusplus 700 } 701 #endif 702 703 #endif // NRF_TIMER_H__ 704