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

accuracy of application timer with schedular on nRF52

I use the application timer with a schedular on the nRF52 with SoftDevice S132 1.0.0.3 alpha and SDK v0.9.2.

The application timer is running in repeated mode and should have a timeout of 1 ms.

Defines:

#define APP_TIMER_PRESCALER             0                            
#define APP_TIMER_MAX_TIMERS            (3 + BSP_APP_TIMERS_NUMBER)  
#define APP_TIMER_OP_QUEUE_SIZE         4    

Initialize of the timer:

// Initialize timer module, making it use the scheduler
APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, true);

err_code = app_timer_create(&m_data_timer_id, APP_TIMER_MODE_REPEATED, timer_data_timeout_handler);
APP_ERROR_CHECK(err_code);

The timer is started with this code:

err_code = app_timer_start(m_data_timer_id, APP_TIMER_TICKS(1, APP_TIMER_PRESCALER), NULL);
APP_ERROR_CHECK(err_code);

Every time the timout handler is called a GPIO PIN is toggeled..

// Timeout handler for the repeated timer
static void timer_data_timeout_handler(void * p_context)
{
	nrf_drv_gpiote_out_set(PIN_OUT);
	nrf_drv_gpiote_out_clear(PIN_OUT);
	ms_counter++;
	
}

When I measure the time between the peaks with a osciloscope the time between each peak is between 1 ms and 6 ms and not as expacted always 1 ms.

Have you any idea which causes that timeout handler is not called excatly every millisecond?

Parents
  • I am not sure of the exact accuracy of the app_timer library but I can for sure say that it does not add delay of 5ms. The interrupt handler for the app_timer is called from lower priority than the softdevice activity, so if there is some high priority activity going on in the softdevice, then your RTC handler will be delayed until that is finished.

    If you want to quickly verify this, then run your app_timer code with softdevice disabled and you will see that the accuracy is acceptable.

  • There are two ways which will have better performance than normal but softdevice scheduling will still have the preference:

    1. using the app_timer with higher priority. You need to change this define in app_timer.c
      #define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOW

    The side-effect of this is that you cannot make SVC calls (calls to sd_xxx) from this high priority context.

    2 . Using timeslots and implement your own app_timer. Then your app timer will become part of softdevice activity.

Reply
  • There are two ways which will have better performance than normal but softdevice scheduling will still have the preference:

    1. using the app_timer with higher priority. You need to change this define in app_timer.c
      #define RTC1_IRQ_PRI APP_IRQ_PRIORITY_LOW

    The side-effect of this is that you cannot make SVC calls (calls to sd_xxx) from this high priority context.

    2 . Using timeslots and implement your own app_timer. Then your app timer will become part of softdevice activity.

Children
No Data
Related