1 /* 2 * Copyright (c) 2015 - 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 NRFX_TIMER_H__ 33 #define NRFX_TIMER_H__ 34 35 #include <nrfx.h> 36 #include <hal/nrf_timer.h> 37 38 #ifdef __cplusplus 39 extern "C" { 40 #endif 41 42 /** 43 * @defgroup nrfx_timer Timer driver 44 * @{ 45 * @ingroup nrf_timer 46 * @brief TIMER peripheral driver. 47 */ 48 49 /** 50 * @brief Timer driver instance data structure. 51 */ 52 typedef struct 53 { 54 NRF_TIMER_Type * p_reg; ///< Pointer to the structure with TIMER peripheral instance registers. 55 uint8_t instance_id; ///< Driver instance index. 56 uint8_t cc_channel_count; ///< Number of capture/compare channels. 57 } nrfx_timer_t; 58 59 /** 60 * @brief Macro for creating a timer driver instance. 61 */ 62 #define NRFX_TIMER_INSTANCE(id) \ 63 { \ 64 .p_reg = NRFX_CONCAT_2(NRF_TIMER, id), \ 65 .instance_id = NRFX_CONCAT_3(NRFX_TIMER, id, _INST_IDX), \ 66 .cc_channel_count = NRF_TIMER_CC_CHANNEL_COUNT(id), \ 67 } 68 69 enum { 70 #if NRFX_CHECK(NRFX_TIMER0_ENABLED) 71 NRFX_TIMER0_INST_IDX, 72 #endif 73 #if NRFX_CHECK(NRFX_TIMER1_ENABLED) 74 NRFX_TIMER1_INST_IDX, 75 #endif 76 #if NRFX_CHECK(NRFX_TIMER2_ENABLED) 77 NRFX_TIMER2_INST_IDX, 78 #endif 79 #if NRFX_CHECK(NRFX_TIMER3_ENABLED) 80 NRFX_TIMER3_INST_IDX, 81 #endif 82 #if NRFX_CHECK(NRFX_TIMER4_ENABLED) 83 NRFX_TIMER4_INST_IDX, 84 #endif 85 NRFX_TIMER_ENABLED_COUNT 86 }; 87 88 /** 89 * @brief Timer driver instance configuration structure. 90 */ 91 typedef struct 92 { 93 nrf_timer_frequency_t frequency; ///< Frequency. 94 nrf_timer_mode_t mode; ///< Mode of operation. 95 nrf_timer_bit_width_t bit_width; ///< Bit width. 96 uint8_t interrupt_priority; ///< Interrupt priority. 97 void * p_context; ///< Context passed to interrupt handler. 98 } nrfx_timer_config_t; 99 100 /** 101 * @brief Timer driver instance default configuration. 102 */ 103 #define NRFX_TIMER_DEFAULT_CONFIG \ 104 { \ 105 .frequency = (nrf_timer_frequency_t)NRFX_TIMER_DEFAULT_CONFIG_FREQUENCY,\ 106 .mode = (nrf_timer_mode_t)NRFX_TIMER_DEFAULT_CONFIG_MODE, \ 107 .bit_width = (nrf_timer_bit_width_t)NRFX_TIMER_DEFAULT_CONFIG_BIT_WIDTH,\ 108 .interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY, \ 109 .p_context = NULL \ 110 } 111 112 /** 113 * @brief Timer driver event handler type. 114 * 115 * @param[in] event_type Timer event. 116 * @param[in] p_context General purpose parameter set during initialization of 117 * the timer. This parameter can be used to pass 118 * additional information to the handler function, for 119 * example, the timer ID. 120 */ 121 typedef void (* nrfx_timer_event_handler_t)(nrf_timer_event_t event_type, 122 void * p_context); 123 124 /** 125 * @brief Function for initializing the timer. 126 * 127 * @param[in] p_instance Pointer to the driver instance structure. 128 * @param[in] p_config Pointer to the structure with initial configuration. 129 * @param[in] timer_event_handler Event handler provided by the user. 130 * Must not be NULL. 131 * 132 * @retval NRFX_SUCCESS If initialization was successful. 133 * @retval NRFX_ERROR_INVALID_STATE If the instance is already initialized. 134 */ 135 nrfx_err_t nrfx_timer_init(nrfx_timer_t const * const p_instance, 136 nrfx_timer_config_t const * p_config, 137 nrfx_timer_event_handler_t timer_event_handler); 138 139 /** 140 * @brief Function for uninitializing the timer. 141 * 142 * @param[in] p_instance Pointer to the driver instance structure. 143 */ 144 void nrfx_timer_uninit(nrfx_timer_t const * const p_instance); 145 146 /** 147 * @brief Function for turning on the timer. 148 * 149 * @param[in] p_instance Pointer to the driver instance structure. 150 */ 151 void nrfx_timer_enable(nrfx_timer_t const * const p_instance); 152 153 /** 154 * @brief Function for turning off the timer. 155 * 156 * Note that the timer will allow to enter the lowest possible SYSTEM_ON state 157 * only after this function is called. 158 * 159 * @param[in] p_instance Pointer to the driver instance structure. 160 */ 161 void nrfx_timer_disable(nrfx_timer_t const * const p_instance); 162 163 /** 164 * @brief Function for checking the timer state. 165 * 166 * @param[in] p_instance Pointer to the driver instance structure. 167 * 168 * @return True if timer is enabled, false otherwise. 169 */ 170 bool nrfx_timer_is_enabled(nrfx_timer_t const * const p_instance); 171 172 /** 173 * @brief Function for pausing the timer. 174 * 175 * @param[in] p_instance Pointer to the driver instance structure. 176 */ 177 void nrfx_timer_pause(nrfx_timer_t const * const p_instance); 178 179 /** 180 * @brief Function for resuming the timer. 181 * 182 * @param[in] p_instance Pointer to the driver instance structure. 183 */ 184 void nrfx_timer_resume(nrfx_timer_t const * const p_instance); 185 186 /** 187 * @brief Function for clearing the timer. 188 * 189 * @param[in] p_instance Pointer to the driver instance structure. 190 */ 191 void nrfx_timer_clear(nrfx_timer_t const * const p_instance); 192 193 /** 194 * @brief Function for incrementing the timer. 195 * 196 * @param[in] p_instance Pointer to the driver instance structure. 197 */ 198 void nrfx_timer_increment(nrfx_timer_t const * const p_instance); 199 200 /** 201 * @brief Function for returning the address of a specific timer task. 202 * 203 * @param[in] p_instance Pointer to the driver instance structure. 204 * @param[in] timer_task Timer task. 205 * 206 * @return Task address. 207 */ 208 __STATIC_INLINE uint32_t nrfx_timer_task_address_get(nrfx_timer_t const * const p_instance, 209 nrf_timer_task_t timer_task); 210 211 /** 212 * @brief Function for returning the address of a specific timer capture task. 213 * 214 * @param[in] p_instance Pointer to the driver instance structure. 215 * @param[in] channel Capture channel number. 216 * 217 * @return Task address. 218 */ 219 __STATIC_INLINE uint32_t nrfx_timer_capture_task_address_get(nrfx_timer_t const * const p_instance, 220 uint32_t channel); 221 222 /** 223 * @brief Function for returning the address of a specific timer event. 224 * 225 * @param[in] p_instance Pointer to the driver instance structure. 226 * @param[in] timer_event Timer event. 227 * 228 * @return Event address. 229 */ 230 __STATIC_INLINE uint32_t nrfx_timer_event_address_get(nrfx_timer_t const * const p_instance, 231 nrf_timer_event_t timer_event); 232 233 /** 234 * @brief Function for returning the address of a specific timer compare event. 235 * 236 * @param[in] p_instance Pointer to the driver instance structure. 237 * @param[in] channel Compare channel number. 238 * 239 * @return Event address. 240 */ 241 __STATIC_INLINE uint32_t nrfx_timer_compare_event_address_get(nrfx_timer_t const * const p_instance, 242 uint32_t channel); 243 244 /** 245 * @brief Function for capturing the timer value. 246 * 247 * @param[in] p_instance Pointer to the driver instance structure. 248 * @param[in] cc_channel Capture channel number. 249 * 250 * @return Captured value. 251 */ 252 uint32_t nrfx_timer_capture(nrfx_timer_t const * const p_instance, 253 nrf_timer_cc_channel_t cc_channel); 254 255 /** 256 * @brief Function for returning the capture value from a specific channel. 257 * 258 * Use this function to read channel values when PPI is used for capturing. 259 * 260 * @param[in] p_instance Pointer to the driver instance structure. 261 * @param[in] cc_channel Capture channel number. 262 * 263 * @return Captured value. 264 */ 265 __STATIC_INLINE uint32_t nrfx_timer_capture_get(nrfx_timer_t const * const p_instance, 266 nrf_timer_cc_channel_t cc_channel); 267 268 /** 269 * @brief Function for setting the timer channel in compare mode. 270 * 271 * @param[in] p_instance Pointer to the driver instance structure. 272 * @param[in] cc_channel Compare channel number. 273 * @param[in] cc_value Compare value. 274 * @param[in] enable_int Enable or disable the interrupt for the compare channel. 275 */ 276 void nrfx_timer_compare(nrfx_timer_t const * const p_instance, 277 nrf_timer_cc_channel_t cc_channel, 278 uint32_t cc_value, 279 bool enable_int); 280 281 /** 282 * @brief Function for setting the timer channel in extended compare mode. 283 * 284 * @param[in] p_instance Pointer to the driver instance structure. 285 * @param[in] cc_channel Compare channel number. 286 * @param[in] cc_value Compare value. 287 * @param[in] timer_short_mask Shortcut between the compare event on the channel 288 * and the timer task (STOP or CLEAR). 289 * @param[in] enable_int Enable or disable the interrupt for the compare 290 * channel. 291 */ 292 void nrfx_timer_extended_compare(nrfx_timer_t const * const p_instance, 293 nrf_timer_cc_channel_t cc_channel, 294 uint32_t cc_value, 295 nrf_timer_short_mask_t timer_short_mask, 296 bool enable_int); 297 298 /** 299 * @brief Function for converting time in microseconds to timer ticks. 300 * 301 * @param[in] p_instance Pointer to the driver instance structure. 302 * @param[in] time_us Time in microseconds. 303 * 304 * @return Number of ticks. 305 */ 306 __STATIC_INLINE uint32_t nrfx_timer_us_to_ticks(nrfx_timer_t const * const p_instance, 307 uint32_t time_us); 308 309 /** 310 * @brief Function for converting time in milliseconds to timer ticks. 311 * 312 * @param[in] p_instance Pointer to the driver instance structure. 313 * @param[in] time_ms Time in milliseconds. 314 * 315 * @return Number of ticks. 316 */ 317 __STATIC_INLINE uint32_t nrfx_timer_ms_to_ticks(nrfx_timer_t const * const p_instance, 318 uint32_t time_ms); 319 320 /** 321 * @brief Function for enabling timer compare interrupt. 322 * 323 * @param[in] p_instance Pointer to the driver instance structure. 324 * @param[in] channel Compare channel. 325 */ 326 void nrfx_timer_compare_int_enable(nrfx_timer_t const * const p_instance, 327 uint32_t channel); 328 329 /** 330 * @brief Function for disabling timer compare interrupt. 331 * 332 * @param[in] p_instance Pointer to the driver instance structure. 333 * @param[in] channel Compare channel. 334 */ 335 void nrfx_timer_compare_int_disable(nrfx_timer_t const * const p_instance, 336 uint32_t channel); 337 338 339 #ifndef SUPPRESS_INLINE_IMPLEMENTATION 340 341 __STATIC_INLINE uint32_t nrfx_timer_task_address_get(nrfx_timer_t const * const p_instance, 342 nrf_timer_task_t timer_task) 343 { 344 return (uint32_t)nrf_timer_task_address_get(p_instance->p_reg, timer_task); 345 } 346 347 __STATIC_INLINE uint32_t nrfx_timer_capture_task_address_get(nrfx_timer_t const * const p_instance, 348 uint32_t channel) 349 { 350 NRFX_ASSERT(channel < p_instance->cc_channel_count); 351 return (uint32_t)nrf_timer_task_address_get(p_instance->p_reg, 352 nrf_timer_capture_task_get(channel)); 353 } 354 355 __STATIC_INLINE uint32_t nrfx_timer_event_address_get(nrfx_timer_t const * const p_instance, 356 nrf_timer_event_t timer_event) 357 { 358 return (uint32_t)nrf_timer_event_address_get(p_instance->p_reg, timer_event); 359 } 360 361 __STATIC_INLINE uint32_t nrfx_timer_compare_event_address_get(nrfx_timer_t const * const p_instance, 362 uint32_t channel) 363 { 364 NRFX_ASSERT(channel < p_instance->cc_channel_count); 365 return (uint32_t)nrf_timer_event_address_get(p_instance->p_reg, 366 nrf_timer_compare_event_get(channel)); 367 } 368 369 __STATIC_INLINE uint32_t nrfx_timer_capture_get(nrfx_timer_t const * const p_instance, 370 nrf_timer_cc_channel_t cc_channel) 371 { 372 return nrf_timer_cc_read(p_instance->p_reg, cc_channel); 373 } 374 375 __STATIC_INLINE uint32_t nrfx_timer_us_to_ticks(nrfx_timer_t const * const p_instance, 376 uint32_t timer_us) 377 { 378 return nrf_timer_us_to_ticks(timer_us, nrf_timer_frequency_get(p_instance->p_reg)); 379 } 380 381 __STATIC_INLINE uint32_t nrfx_timer_ms_to_ticks(nrfx_timer_t const * const p_instance, 382 uint32_t timer_ms) 383 { 384 return nrf_timer_ms_to_ticks(timer_ms, nrf_timer_frequency_get(p_instance->p_reg)); 385 } 386 387 #endif // SUPPRESS_INLINE_IMPLEMENTATION 388 389 390 void nrfx_timer_0_irq_handler(void); 391 void nrfx_timer_1_irq_handler(void); 392 void nrfx_timer_2_irq_handler(void); 393 void nrfx_timer_3_irq_handler(void); 394 void nrfx_timer_4_irq_handler(void); 395 396 397 /** @} */ 398 399 #ifdef __cplusplus 400 } 401 #endif 402 403 #endif // NRFX_TIMER_H__ 404 405