cancel
Showing results for 
Search instead for 
Did you mean: 

Question about chained timers (again) and how to avoid wraparound glitches

BCano.1
Associate III

Hello, I ask this same question a month ago, but i want to reformulate the question to better understanding.

I need to implement a lifetime counter using the nucleo-stm32L432kc timers to send the number periodically to an AFBR-S50 sensor. I think the ASIC inside the sensor uses this LTC timer value to regulate laser power and to stop it when it reaches its estimated end of life working time.

The ASIC does its own checks to verify the LTC timer is working "more or less" ok.

To do this LTC timer i have to chain 2 16bit times and one 32bit timer (htim15 (16) -> htim1 (16) -> htim2 (32)). To avoid the wraparound glitch i added my own condition inspired in the condition of the AFBR-S50 docs, but i've always suspected something is wrong.

This is my timers read function:

double us;
double ms;
do{
	us = __HAL_TIM_GET_COUNTER(&htim15);
	ms = __HAL_TIM_GET_COUNTER(&htim1);
	*hct = __HAL_TIM_GET_COUNTER(&htim2);
 
}while ( (us > __HAL_TIM_GET_COUNTER(&htim15)) | (ms > __HAL_TIM_GET_COUNTER(&htim1)));
 
	*lct = ms * 1000 + us;

Here, some people proposed my this implementation:

do{
	us = __HAL_TIM_GET_COUNTER(&htim15);
	ms = __HAL_TIM_GET_COUNTER(&htim1);
	s = __HAL_TIM_GET_COUNTER(&htim2);
} while (us != __HAL_TIM_GET_COUNTER(&htim15));
 
*lct = ms * 1000 + us;
*hct = s;*/

which is not working because it has to read the 3 timers in the same microsecond and never leaves to loop.

I think both implementations are wrong and its causing me laser power issues.

Thanks

1 REPLY 1
BCano.1
Associate III

This is my last implementation:

do{
	s = __HAL_TIM_GET_COUNTER(&htim2);
	ms = __HAL_TIM_GET_COUNTER(&htim1);
	us = __HAL_TIM_GET_COUNTER(&htim15);
	ms2 = __HAL_TIM_GET_COUNTER(&htim1);
	s2 = __HAL_TIM_GET_COUNTER(&htim2);
} while (ms != ms2 || s != s2);
 
*lct = ms * 1000 + us;
*hct = s;

seems to be working fine. let me know if you think can be improved.