uint32_t app_simple_timer_start(app_simple_timer_mode_t mode, app_simple_timer_timeout_handler_t timeout_handler, uint16_t timeout_ticks, void * p_context) { uint32_t err_code = NRF_SUCCESS; nrf_timer_short_mask_t timer_short; VERIFY_PARAM_NOT_NULL(timeout_handler); if (APP_SIMPLE_TIMER_MODE_REPEATED == mode) { timer_short = NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK; } else if (APP_SIMPLE_TIMER_MODE_SINGLE_SHOT == mode) { timer_short = NRF_TIMER_SHORT_COMPARE0_STOP_MASK; } else { return NRF_ERROR_INVALID_PARAM; } if (SIMPLE_TIMER_STATE_IDLE == m_simple_timer_state) { return NRF_ERROR_INVALID_STATE; } if (SIMPLE_TIMER_STATE_STARTED == m_simple_timer_state) { err_code = app_simple_timer_stop(); APP_ERROR_CHECK(err_code); } if (SIMPLE_TIMER_STATE_STOPPED == m_simple_timer_state) { nrf_drv_timer_clear(&SIMPLE_TIMER); } m_mode = mode; m_timeout_handler = timeout_handler; mp_timeout_handler_context = p_context; nrf_drv_timer_extended_compare( &SIMPLE_TIMER, NRF_TIMER_CC_CHANNEL0, (uint32_t)timeout_ticks, timer_short, true); if (m_simple_timer_state == SIMPLE_TIMER_STATE_STOPPED) { nrf_drv_timer_resume(&SIMPLE_TIMER); } else { nrf_drv_timer_enable(&SIMPLE_TIMER); } m_simple_timer_state = SIMPLE_TIMER_STATE_STARTED; return NRF_SUCCESS; }
/** * @brief Timer0 interrupt handler. * * @param[in] event_type Timer event. * @param[in] p_context General purpose parameter set during initialization of * the timer. This parameter can be used to pass * additional information to the handler function, for * example, the timer ID. */ static void counter_compare_handler(nrf_timer_event_t event_type, void* p_context) { if(event_type == NRF_TIMER_EVENT_COMPARE0) { uint16_t val = nrf_drv_timer_capture_get(&m_timer1, NRF_TIMER_CC_CHANNEL1); nrf_drv_timer_pause(&m_timer1); nrf_drv_timer_clear(&m_timer1); /* Handle finished measurement. */ conversion_handler(val); } }