void us_ticker_set_interrupt(timestamp_t timestamp) { core_util_critical_section_enter(); const uint32_t counter_mask = ((1ULL << US_TICKER_COUNTER_BITS) - 1); nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, timestamp & counter_mask); if (!nrf_timer_int_enable_check(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0))) { nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0); nrf_timer_int_enable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0)); } core_util_critical_section_exit(); }
void us_ticker_free(void) { nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP); nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0)); us_ticker_initialized = false; }
void nrf_drv_timer_compare_int_disable(nrf_drv_timer_t const * const p_instance, uint32_t channel) { ASSERT(m_cb[p_instance->instance_id].state != NRF_DRV_STATE_UNINITIALIZED); ASSERT(channel < p_instance->cc_channel_count); nrf_timer_int_disable(p_instance->p_reg, nrf_timer_compare_int_get(channel)); }
void us_ticker_init(void) { if (us_ticker_initialized) { nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0); nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0)); return; } nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_STOP); nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0)); /* Configure timer as follows: * - timer mode, * - timer width 16 bits for NRF51 and 32 bits for NRF52, * - timer freq 1 MHz. */ nrf_timer_mode_set(NRF_TIMER1, NRF_TIMER_MODE_TIMER); nrf_timer_frequency_set(NRF_TIMER1, NRF_TIMER_FREQ_1MHz); #ifdef NRF52 nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_32); #else nrf_timer_bit_width_set(NRF_TIMER1, NRF_TIMER_BIT_WIDTH_16); #endif nrf_timer_cc_write(NRF_TIMER1, NRF_TIMER_CC_CHANNEL0, 0); nrf_timer_event_clear(NRF_TIMER1, NRF_TIMER_EVENT_COMPARE0); NVIC_SetVector(TIMER1_IRQn, (uint32_t)us_ticker_irq_handler); nrf_drv_common_irq_enable(TIMER1_IRQn, APP_IRQ_PRIORITY_HIGH); nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_START); /* Bug fix. First value can't be trusted. */ nrf_timer_task_trigger(NRF_TIMER1, NRF_TIMER_TASK_CAPTURE1); us_ticker_initialized = true; }
static void irq_handler(NRF_TIMER_Type * p_reg, timer_control_block_t * p_cb, uint8_t channel_count) { uint8_t i; for (i = 0; i < channel_count; ++i) { nrf_timer_event_t event = nrf_timer_compare_event_get(i); nrf_timer_int_mask_t int_mask = nrf_timer_compare_int_get(i); if (nrf_timer_event_check(p_reg, event) && nrf_timer_int_enable_check(p_reg, int_mask)) { nrf_timer_event_clear(p_reg, event); p_cb->handler(event, p_cb->context); } } }
void nrf_drv_timer_compare(nrf_drv_timer_t const * const p_instance, nrf_timer_cc_channel_t cc_channel, uint32_t cc_value, bool enable_int) { nrf_timer_int_mask_t timer_int = nrf_timer_compare_int_get(cc_channel); if (enable_int) { nrf_timer_int_enable(p_instance->p_reg, timer_int); } else { nrf_timer_int_disable(p_instance->p_reg, timer_int); } nrf_timer_cc_write(p_instance->p_reg, cc_channel, cc_value); }
void nrf_drv_timer_compare(nrf_drv_timer_t const * const p_instance, nrf_timer_cc_channel_t cc_channel, uint32_t cc_value, bool enable_int) { nrf_timer_int_mask_t timer_int = nrf_timer_compare_int_get(cc_channel); if (enable_int) { nrf_timer_int_enable(p_instance->p_reg, timer_int); } else { nrf_timer_int_disable(p_instance->p_reg, timer_int); } nrf_timer_cc_write(p_instance->p_reg, cc_channel, cc_value); NRF_LOG_INFO("Timer id: %d, capture value set: %d, channel: %d.\r\n", p_instance->instance_id, cc_value, cc_channel); }
void us_ticker_disable_interrupt(void) { nrf_timer_int_disable(NRF_TIMER1, nrf_timer_compare_int_get(NRF_TIMER_CC_CHANNEL0)); }