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

inner and outer application timers, is it a problem?

Hi everyone,

I have one problem with the application timer. When I change the state of characteristic to the value of 1, the following code is executed:

   err_code = app_timer_stop(m_mass_timer_id);        					   
   p_mass->load_cell_calibration.flag_offset = 1; 				
   err_code = app_timer_start(m_mass_timer_id, MASS_MEAS_100MS_INTERVAL ,NULL);
   APP_ERROR_CHECK(err_code);        			   			   					
    			

The m_mass_timer_id handler is defined as:

    static void mass_timer_handler(void * p_context)
    { 

  uint8_t          err_code; 
  	
	  
		if(m_mass.mass_flag == 1) 
		{											
				
				m_mass.mass_flag = 0;

				err_code = app_timer_start(m_mass_one_cycle_timer_id, MASS_MEAS_30MS_INTERVAL, NULL);
				APP_ERROR_CHECK(err_code);
				SEGGER_RTT_WriteString(0, "MASS_TIMER\n");
				err_code = app_timer_start(m_mass_timer_id, MASS_MEAS_INTERVAL, NULL);
				APP_ERROR_CHECK(err_code);							
							
					
		}		
		else
		{
			SEGGER_RTT_WriteString(0, "MISTAKE\n");
		}	
	
}              			
    				

where m_mass_one_cyler_timer is executed by the following code:

static void mass_one_cycle_timer_handler(void * p_context)
{ 
	

	uint8_t err_code; 	

			if(m_mass.load_cell_calibration.flag_offset == 1)
			{
				
				err_code = ble_mgs_offset_load_cell_update(&m_mass,0); 			
        APP_ERROR_CHECK(err_code); 
								

        }

			m_mass.mass_flag = 1;			



}

If I change the state of characteristic a few times in a row, then the mass_timer_handler does not instantly execute but it holds on for a few seconds. I do not get any error and I do not know why this latency occurs. I am using SDK 10. The initialization of both timers are defined by the following code:

static void timers_init(void)
{
   
	  APP_TIMER_APPSH_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, true);
    uint8_t err_code;	

		
	  err_code = app_timer_create(&m_mass_timer_id, APP_TIMER_MODE_SINGLE_SHOT, mass_timer_handler);
	  APP_ERROR_CHECK(err_code);
//		
		err_code = app_timer_create(&m_mass_one_cycle_timer_id, APP_TIMER_MODE_SINGLE_SHOT, mass_one_cycle_timer_handler);
	  APP_ERROR_CHECK(err_code);
      	 
}

I would kindly appreciate your assistance on this issue.

Edit: A few minutes ago, I tested the program only with timer m_mass_timer_id I removed timer m_mass_one_cycle_timer_id, but I got the same problem as was described.

 static void mass_timer_handler(void * p_context)
        { 
    
               uint8_t          err_code;       	
    	  
    		if(m_mass.mass_flag == 1) 
    		{											
    				
    				if(m_mass.load_cell_calibration.flag_offset == 1)
    			{
    				
    				err_code = ble_mgs_offset_load_cell_update(&m_mass,0); 			
                               APP_ERROR_CHECK(err_code);    								
    
                       }      
    				
    				SEGGER_RTT_WriteString(0, "MASS_TIMER\n");
    				err_code = app_timer_start(m_mass_timer_id, MASS_MEAS_INTERVAL, NULL);
    				APP_ERROR_CHECK(err_code);							
    							
    					
    		}		
    		else
    		{
    			SEGGER_RTT_WriteString(0, "MISTAKE\n");
    		}	
    	
    }              	

	

I hope that I will get reply to my problem as fast as possible.

Thanks in advance.

Best regards Mike

  • I was researching a little bit more why the timer is not instantly executed and I came across that if I put the function timer start into schedule then the timer is executed immediately otherwise timer has a pause a few seconds even though the timer has to be executed in a few ms. The test was done by using only one timer which works all the time. If the user changes the state of the characteristic, the timer is stopped and started again. I really do not understand why the timer is not executed immediately after state of the characteristic is changed. Thanks for help in advance

  • I noticed that you are using some flags in your handlers. Have you declared these volatile? It is not completely clear to me how all your timers are connected, but since you are starting and stopping timers in your handlers based on the status of these flags maybe the timings get skewed if the flags aren't handled properly by the compiler.

Related