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

Improving nrf_drv_timer accuracy

Hi guys,

I'm trying to use the HFCLK to generate high resolution timing. Ideally at the millisecond resolution. I've written a layer around the nrf_drv_timer.c module. I'm using PPI to reset the Timer as it generates Compare interrupts. However I see the accuracy drifting over time when BLE activity is high. I've tried compensated but grab freeRTOS ticks since the last interrupt but no luck.

Timer Setup: nrf_drv_timer_config_t hfclk_timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG; hfclk_timer_cfg.mode = NRF_TIMER_MODE_TIMER; hfclk_timer_cfg.bit_width = NRF_TIMER_BIT_WIDTH_16; hfclk_timer_cfg.frequency = NRF_TIMER_FREQ_4MHz; err_code = nrf_drv_timer_init( &TIMER_0, &hfclk_timer_cfg, hfclk_timer_event_handler ); if (NRF_SUCCESS == err_code)

PPI Setup: err_code = nrf_drv_ppi_init(); APP_ERROR_CHECK(err_code);

// Configure 1st available PPI channel to stop TIMER0 counter on TIMER1 COMPARE[0] match, which is every even number of seconds.
err_code = nrf_drv_ppi_channel_alloc(&ppi_channel1);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_ppi_channel_assign(ppi_channel1,
                                      nrf_drv_timer_event_address_get(&TIMER_0, NRF_TIMER_EVENT_COMPARE0),
                                      nrf_drv_timer_task_address_get(&TIMER_0, NRF_TIMER_TASK_CLEAR));
APP_ERROR_CHECK(err_code);

// Enable both configured PPI channels
err_code = nrf_drv_ppi_channel_enable(ppi_channel1);
APP_ERROR_CHECK(err_code);

The handler function just increments a millisecond/second counter I have setup. But the drift is just too much. Is there a way I can compensate for the BLE hog time? I also use SAADC sampling at 100hz which I know is also blocking. Any help/examples?

Related