This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Timer interval not as expected

Hi Community,

I am trying to program a timer to get a clean timebase of 50ms. But for some reason

A) the timer is little slower as expected. I have to add few counts to get close to my 50ms

B) I have a jitter of about 70us.

Do you have any idea what is wrong?

I measured the timer interval with an oscilloscope on a led pin.

Regards,

Arne

void TIMER1_IRQHandler(void) {
    BaseType_t pxHigherPriorityTaskWoken = pdFALSE;
    NRF_TIMER1->EVENTS_COMPARE[0] = 0;
    xSemaphoreGiveFromISR(counter_timebase_semaphore, &pxHigherPriorityTaskWoken);
    {
        static uint8_t led_state = 0;
        set_led_state(led_state & 1);
        led_state++;
    }
    portEND_SWITCHING_ISR(pxHigherPriorityTaskWoken);
}

void timer_init() {

    NRF_TIMER1->TASKS_STOP = 1;
    NRF_TIMER1->TASKS_CLEAR = 1;
    NRF_TIMER1->INTENCLR = 0xffffffff;
    NRF_TIMER1->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
    NRF_TIMER1->BITMODE = (TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos);
    NRF_TIMER1->SHORTS = (1 << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
    NRF_TIMER1->PRESCALER = 5; // Ensures 2 us resolution @ 16MHz

    NRF_TIMER1->CC[0] = 250 + 1000 * 25; //the added 250 are due to Trial and error to get 50ms

    NVIC_ClearPendingIRQ(TIMER1_IRQn);
    NVIC_EnableIRQ(TIMER1_IRQn);
    NRFX_IRQ_PRIORITY_SET(TIMER1_IRQn, 2);
    NRF_TIMER1->TASKS_START = 1;
}

Parents
  • Hello,

    What HW are you running this test on?

    Have you tested the PPI example? It may be that your application is handling some other interrupts when the timer interrupt should be fired. If you use PPI you can toggle the pin without the CPU, so it should be more accurate. 

    If it is still unaccurate, then it is the HFXTAL that is inaccurate. Do you use any other peripherals, or the softdevice?  If so, does it help using the PPI? You can see how it can be used using the examples\peripheral\ppi example.

    Best regards,

    Edvin

Reply
  • Hello,

    What HW are you running this test on?

    Have you tested the PPI example? It may be that your application is handling some other interrupts when the timer interrupt should be fired. If you use PPI you can toggle the pin without the CPU, so it should be more accurate. 

    If it is still unaccurate, then it is the HFXTAL that is inaccurate. Do you use any other peripherals, or the softdevice?  If so, does it help using the PPI? You can see how it can be used using the examples\peripheral\ppi example.

    Best regards,

    Edvin

Children
  • Hi Edvin,

    thank you for your answer.

    I am using the chip built in the ublox' NINA-B112 module. As you suggested I just tried the ppi example and toggled the led using the interrupt routine of the timer. It seems I am still 0.97ms off 100ms. And also the jitter is still there.

    Do you still have any idea what I could try?

    Best Regards,

    Arne

  • Hello Arne,

    The accuracy of the timer is no better than the accuracy of the HFCLOCK. It may be that you are only using the HF RC Oscillator. Try to request the HFCLK with the XTAL as the source.

    Do you use the softdevice? If so, you can use sd_clock_hfclk_request() to leave the HFXO running at all times. If you don't use the softdevice, you can start it manually:

        NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
        NRF_CLOCK->TASKS_HFCLKSTART = 1;
        while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
        {
            // Wait.
        }

    But I guess it is already running, if you are using the timer, and it responds. 

    Is it possible to share the project that you are testing on, so that I can check if it behaves the same way on a Nordic nRF52832 DK?

    Do you happen to know what HFXTAL that is present on the NINA-B112 module?

  • Thank you very much! it seems the XTAL was not activated. Now my timer interval is pretty stable.

Related