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

timer deviation nrf52840 sdk15.0 s140

Hello, i have a central card nrf52840 and several peripheral cards nrf52840.

I have a program that allows me to launch the timer on the central and peripherals at the same time. The peripherals send their timers to the central station and the central station compares its timer with the one received.

I have a connection interval of 62.5 ms and a connection time of 7.8ms.

When I have a central connection and 1 peripheral: I have a drift only 3 second on 16h00.

But when I have a central 2 peripheral connection: I have a drift of 1 second after a few minutes.  and i don't why  if if anyone can help me.

Here is the central code: 

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);

uint32_t compteurTimer;
uint32_t compteur;
uint32_t compteur2;
uint32_t compteur3;
uint32_t compteur4;

void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context){


switch (event_type)
{
case NRF_TIMER_EVENT_COMPARE0:

compteur2++;
compteur3++;
compteur++;
compteur4++;
if(compteur==1000){

//NRF_LOG_INFO("dif_Cent_Perip = %d",dif_Cent_Perip);
compteur = 0;
}

if(compteur3==6250){
compteurTimer=compteur2;
compteur3=0;
}
if(compteur4==100000){
tmp=2;
compteur4=0;
}

break;

default:
//Do nothing.
break;
}
}

void test(){

NRF_LOG_INFO("test");
uint32_t time_us = 10; //Time(in microsecond) between consecutive compare events.
uint32_t time_ticks;
uint32_t err_code = NRF_SUCCESS;

//Configure all leds on board.
bsp_board_init(BSP_INIT_LEDS);
//Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);
NRF_LOG_INFO("err_code %d",err_code);
APP_ERROR_CHECK(err_code);
time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_LED, time_us);
nrf_drv_timer_extended_compare(
&TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
nrf_drv_timer_enable(&TIMER_LED);
}

Here is the peripheral code: 

const nrf_drv_timer_t TIMER_LED = NRF_DRV_TIMER_INSTANCE(1);
uint32_t compteur;
uint32_t compteur2;
uint32_t compteur3;
uint32_t tab[20] ={0};
int d = 0;
int f;
int x ;
void timer_led_event_handler(nrf_timer_event_t event_type, void* p_context){


switch (event_type)
{
case NRF_TIMER_EVENT_COMPARE0:
d++;
compteur++;

break;

default:
//Do nothing.
break;
}
}

void test(){

NRF_LOG_INFO("test");
uint32_t time_us =10; //Time(in miliseconds) between consecutive compare events.
uint32_t time_ticks;
uint32_t err_code = NRF_SUCCESS;

//Configure all leds on board.
bsp_board_init(BSP_INIT_LEDS);
//Configure TIMER_LED for generating simple light effect - leds on board will invert his state one after the other.
nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
err_code = nrf_drv_timer_init(&TIMER_LED, &timer_cfg, timer_led_event_handler);
NRF_LOG_INFO("err_code %d",err_code);
APP_ERROR_CHECK(err_code);
time_ticks = nrf_drv_timer_us_to_ticks(&TIMER_LED, time_us);
nrf_drv_timer_extended_compare(
&TIMER_LED, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
nrf_drv_timer_enable(&TIMER_LED);
}

thank's

Parents
  • Not sure if I quite understand the resolution of your timer ticks here, but I recommend to have them to minimum 1ms. If you have very small ticks (e.g. in the tens of us), then the interrupts will be blocked during BLE events, this will work as long as the communication is 1:1 (since both are blocked equally much), but will not work in a 1:2 (since then the central will be blocked twize as much.Can that be the case?

    Possible easier would be to use an app_timer (RTC) with 1ms interval that increment an 1ms counter value. You can always have finer resolution by using timer when needed.

     

  • Now  i can send mi counter but it's the same, there are a big diference into conter central and conteur peripheral

  • Hello,

    A few things to look into:

    - An RTC tick is (1/32768=) 30.51757 us. This means that when you configure an app timer with 1ms, the timer_sd_handler() will run at every 32 RTC periods, since that is the closest: 0.9765625 ms. 

    - The accuracy of the RTC tick depends on the LFCLK source, for instance if you are using the internal LFRC then the accuracy is <500ppm, which means a potential drift of 500us after 1second have elapsed. However the <500ppm only apply if you have calibrated the LFRC, else it may be much larger (1% or more). A more accurate resolution can be achieved by using external LFXO,then the accuracy is typical <50ppm and there is no need to calibrate.

    Best regards,
    Kenneth

Reply
  • Hello,

    A few things to look into:

    - An RTC tick is (1/32768=) 30.51757 us. This means that when you configure an app timer with 1ms, the timer_sd_handler() will run at every 32 RTC periods, since that is the closest: 0.9765625 ms. 

    - The accuracy of the RTC tick depends on the LFCLK source, for instance if you are using the internal LFRC then the accuracy is <500ppm, which means a potential drift of 500us after 1second have elapsed. However the <500ppm only apply if you have calibrated the LFRC, else it may be much larger (1% or more). A more accurate resolution can be achieved by using external LFXO,then the accuracy is typical <50ppm and there is no need to calibrate.

    Best regards,
    Kenneth

Children
  • ok now i have this in central main :

     

    static void timer_sd_handler(void * p_context){
    			  
    				compteur++; // affichage chaque seconde
    				compteur2++; // recupere la valeur
    				compteur3++; //recuperer la valeur tout les 62.5ms
    				compteur4++; // affichage pour 2 valeur
    	
    				if(compteur==1000){
    					
    					//NRF_LOG_INFO("dif_Cent_Perip = %d",dif_Cent_Perip);	
    					compteur = 0; 
    				}
    				
    				if(compteur3==62){
    					compteurTimer=compteur2;
    					compteur3=0;
    				}
    				if(compteur4==1000){
    					NRF_LOG_INFO("------------------>1sec<-------------------------------");
    				tmp=2;
    					compteur4=0;
    				}
    				//NRF_LOG_INFO("compteur2 = %d",compteur2);
    	
    }
    
    static void lfclk_request(void)
    {
        ret_code_t err_code;// = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        nrf_drv_clock_lfclk_request(NULL);
    		NRF_LOG_INFO("LFCLK: OFF");
        if(nrf_drv_clock_lfclk_is_running()){
    		NRF_LOG_INFO("LFCLK: ON");
    	}
    }
    
    void test(){
    	
    	ret_code_t err_code;
    	
    	NRF_LOG_INFO("1");
    
    	
    	NRF_LOG_INFO("3");
    	//Create timers
    	
    	err_code = app_timer_create(&sd_timer_id,APP_TIMER_MODE_REPEATED,timer_sd_handler);
    	NRF_LOG_INFO("4");
    		lfclk_request();
    	APP_ERROR_CHECK(err_code);
    	NRF_LOG_INFO("Timer_sd_start");
    	err_code = app_timer_start(sd_timer_id, COMPTEUR_INTERVAL, NULL);
    	NRF_LOG_INFO("%d",err_code);
    	APP_ERROR_CHECK(err_code);
    }

    in central sdk_config.h : 

    //==========================================================
    // <e> CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver - legacy layer
    //==========================================================
    #ifndef CLOCK_ENABLED
    #define CLOCK_ENABLED 1
    #endif
    // <o> CLOCK_CONFIG_LF_SRC  - LF Clock Source
     
    // <0=> RC 
    // <1=> XTAL 
    // <2=> Synth 
    
    #ifndef CLOCK_CONFIG_LF_SRC
    #define CLOCK_CONFIG_LF_SRC 0
    #endif
    
    // <o> CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    
    // <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef CLOCK_CONFIG_IRQ_PRIORITY
    #define CLOCK_CONFIG_IRQ_PRIORITY 2
    #endif

    in peripheral main : 

    static void lfclk_request(void)
    {
        ret_code_t err_code;// = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        nrf_drv_clock_lfclk_request(NULL);
    		NRF_LOG_INFO("LFCLK: OFF");
        if(nrf_drv_clock_lfclk_is_running()){
    		NRF_LOG_INFO("LFCLK: ON");
    	}
    }
    
    static void timer_sd_handler(void * p_context){
    			
    	compteur++;
    	
    }
    
    void test(){
    	
    	
    
    	ret_code_t err_code;
    	lfclk_request();
    	// Create timers
    	err_code = app_timer_create(&sd_timer_id,APP_TIMER_MODE_REPEATED,timer_sd_handler);
    	NRF_LOG_INFO("");
    	APP_ERROR_CHECK(err_code);
    	
    	NRF_LOG_INFO("Timer_sd_start");
    	err_code = app_timer_start(sd_timer_id, COMPTEUR_INTERVAL, NULL);
    	NRF_LOG_INFO("%d",err_code);
    	APP_ERROR_CHECK(err_code);
    }
    
    

    in peripheral config_sdk.h : 

    //==========================================================
    // <e> CLOCK_ENABLED - nrf_drv_clock - CLOCK peripheral driver - legacy layer
    //==========================================================
    #ifndef CLOCK_ENABLED
    #define CLOCK_ENABLED 1
    #endif
    // <o> CLOCK_CONFIG_LF_SRC  - LF Clock Source
     
    // <0=> RC 
    // <1=> XTAL 
    // <2=> Synth 
    
    #ifndef CLOCK_CONFIG_LF_SRC
    #define CLOCK_CONFIG_LF_SRC 0
    #endif
    
    // <o> CLOCK_CONFIG_IRQ_PRIORITY  - Interrupt priority
     
    
    // <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
    // <0=> 0 (highest) 
    // <1=> 1 
    // <2=> 2 
    // <3=> 3 
    // <4=> 4 
    // <5=> 5 
    // <6=> 6 
    // <7=> 7 
    
    #ifndef CLOCK_CONFIG_IRQ_PRIORITY
    #define CLOCK_CONFIG_IRQ_PRIORITY 2
    #endif
    
    // </e>

    So when a send the peripheral's counter to the central card, a few minute i have a diviation between the two timers (central / peripheral ) surrounding 500 ms or more than 1 second.

    what part of code should I add to calibrate the CRLF or then what should I do to use an external LFXO.

    I want a maximum drift of 3 seconds after 16 hours or 0.003125 per second   

  • Can you try to use:
    #define CLOCK_CONFIG_LF_SRC 1
    or
    #define CLOCK_CONFIG_LF_SRC 2

    instead of
    #define CLOCK_CONFIG_LF_SRC 0

Related