nrfx timer multiple compare value unexpected behavior

I am new to Nordic (coming from STM32), and I am trying to set up a basic timer with multiple compare channels.  For this simple example, I want a timer that triggers a compare event at 250 ms and at 1000 ms.  These compare events will turn on/off an LED, resulting in an effective frequency of 1 Hz with a duty cycle of 25%.

To do this, I want to use the nrfx libraries.  I have already configured the LED output pin, and confirmed that I can turn it on/off without issue.

Following other examples I've seen on the forums (there doesn't seem to be any documentation about nrfx besides the function calls on the API reference pages), I've configured my timer as follows:

/* Timer config */
nrfx_timer_config_t timer0_config = NRFX_TIMER_DEFAULT_CONFIG(125000);
nrfx_timer_init(&timer0, &timer0_config, timer0_callback);
nrfx_timer_compare(&timer0, NRF_TIMER_CC_CHANNEL0, nrfx_timer_ms_to_ticks(&timer0, 100), true);
nrfx_timer_extended_compare(&timer0, NRF_TIMER_CC_CHANNEL1, nrfx_timer_ms_to_ticks(&timer0, 1000), NRF_TIMER_SHORT_COMPARE1_CLEAR_MASK, true);
IRQ_CONNECT(TIMER0_IRQn, IRQ_PRIO_LOWEST, nrfx_timer_0_irq_handler, NULL, 0);
nrfx_timer_enable(&timer0);

I set the compare values at 100ms and 1000ms in order to trigger events at those intervals.  I configured compare channel 1 to reset the timer value, which should make this whole system period with a frequency of 1 Hz.

My timer callback handles each compare channel event and sets the LED accordingly:

static void timer0_callback(nrf_timer_event_t event_type, void * p_context)
{
    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
        {
            nrf_gpio_pin_set(LED_RESET_GPIO_PIN);
            break;
        }

        case NRF_TIMER_EVENT_COMPARE1:
        {
            nrf_gpio_pin_clear(LED_RESET_GPIO_PIN);
            break;
        }

        default:
        {
            break;
        }
    }
}

However, upon running this code, I measure a frequency of 2.1 Hz and a duty cycle of 79%.  I'm not sure where these numbers are coming from.

I feel I am missing something with the way Nordic handles timer counter/compare values, and I can't find any references from Nordic about how to implement this correctly.

Parents Reply Children
No Data
Related