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.

     

Reply
  • 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.

     

Children
  • ok thank's you but how i how to do  app_timer (RTC) with a 1 ms interval that increments a counter reading by 1 ms? 

  • I have changed my timer, my counter increments every ms but now I have a problem with my send I can no longer store the counter reading in the table.

     

    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++;
    //	NRF_LOG_INFO("%d",compteur);
    	
    }
    
    void test(){
    	
    
    	ret_code_t err_code;
    	// Initialize timer module.
    	err_code = app_timer_init();
    	APP_ERROR_CHECK(err_code);
    
    	// 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, SD_SAVE_DATA_INTERVAL, NULL);
    	APP_ERROR_CHECK(err_code);
    }
    static void data_send_timeout_handler(void * p_context)
    {	
    	
    	
    	bsp_board_led_on(BSP_BOARD_LED_3);
    	ret_code_t err_code=20;
    	uint32_t data_send=DATA_TO_SEND_IN_INTERVAL;
    	static const uint8_t data_length=243;
    	static uint8_t data_cid[data_length];
    	static bool all_data_send=1;
    	static uint32_t num_data=0;
    	static int i =0;
    	uint32_t tmp;
    	
    	
    	tmp = compteur ; 
    	
    	if(data_send<DATA_TO_SEND_IN_INTERVAL){
    		all_data_send=0;
    	}
    	
    	
    	if(param_conn.periph_to_central && all_data_send && test_1_16){
    //		NRF_LOG_INFO("1/16second");
    //		if(data_cid[30]!=29){
    //			for(uint8_t i=0;i<data_length;i++){
    //				data_cid[i]=i;
    //			}
    //		}
    		
    		if(i == 0){
    			test();
    			i=1;
    		}
    			
    		
    
    		
    		
    	//!!!!!!!!!!!!!!!!!!!here i have a problem !!!!!!!!!!!!!!!!!//////////////////	
    		//NRF_LOG_INFO("num_data=%d",num_data);
    		data_cid[0]=(tmp >> 0)& 0xFF;
    		data_cid[1]=(tmp >> 8)& 0xFF;
    		data_cid[2]=(tmp >> 16)& 0xFF;
    		data_cid[3]=(tmp >> 24)& 0xFF;
    		data_send=0;
    		
    		
    		for(int x = 0; x<=3;x++){
    		NRF_LOG_INFO("data_cid %d = %d",x,data_cid[x]);
    		}
    //!!!!!!!!!!!!!!!!!!!here i have a problem !!!!!!!!!!!!!!!!!//////////////////	
    		//send start
    		while(err_code!=NRF_SUCCESS){
    			err_code=ble_cid_measurement_send(&m_cid,&data_cid[0],1,DATA_1_16_START_FLAG);
    			if ((err_code != NRF_SUCCESS) &&
    							(err_code != NRF_ERROR_INVALID_STATE) &&
    							(err_code != NRF_ERROR_RESOURCES) &&
    							(err_code != NRF_ERROR_BUSY) &&
    							(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    					 )
    				{
    
    					APP_ERROR_HANDLER(err_code);
    				}
    				
    				if(m_cid.conn_handle==BLE_CONN_HANDLE_INVALID || !param_conn.periph_to_central){
    					return;
    				}
    		}
    		err_code=20;
    		//NRF_LOG_INFO("send start");
    		
    		
    		//send data
    		while(data_send<DATA_TO_SEND_IN_INTERVAL){
    			while(err_code!=NRF_SUCCESS){
    				bsp_board_led_on(BSP_BOARD_LED_2);
    				if(data_send+data_length<DATA_TO_SEND_IN_INTERVAL){
    
    					err_code = ble_cid_measurement_send(&m_cid, data_cid, data_length,SIMPLE_DATA_SEND_FLAG);
    				
    				}else{
    
    					err_code = ble_cid_measurement_send(&m_cid, data_cid, DATA_TO_SEND_IN_INTERVAL-data_send,SIMPLE_DATA_SEND_FLAG);
    
    				}
    				if ((err_code != NRF_SUCCESS) &&
    							(err_code != NRF_ERROR_INVALID_STATE) &&
    							(err_code != NRF_ERROR_RESOURCES) &&
    							(err_code != NRF_ERROR_BUSY) &&
    							(err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
    					 )
    				{
    
    					APP_ERROR_HANDLER(err_code);
    
    
    				}
    				
    				bsp_board_led_off(BSP_BOARD_LED_2);
    				
    				if(m_cid.conn_handle==BLE_CONN_HANDLE_INVALID || !param_conn.periph_to_central){
    					return;
    
    				}
    			}
    			err_code=20;
    			data_send+=data_length;
    			
    			
    		}
    		all_data_send=1;

  • 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

Related