Timer callback issue?
Hi,
I have timer with period 1 sec (80 MHz clock):
htim2.Init.Prescaler = 80-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1000000-1;
Global var (volatile uint32_t) increased every time counter wraps around
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM2) {
globalSeconds++;
}
}
In the code, timestamp (timer counter and var) collected and send later over UART (speed 921600)
do {
sec = globalSeconds;
us = TIM2->CNT;
} while (sec != globalSeconds);
When I get values on pc, I noticed that seconds are not always correct. It happens when usec timer counter wraps around:
34.995010
34.999530
34.100404
34.100858
I'm a bit puzzled about it, coz globalSeconds changed only in one place in the code by TIM2 interrupt.
The only explanation I could imagine, that HAL_TIM_PeriodElapsedCallback() is not called (INT disabled?)...
Even if handling of INT was delayed by other INT, eventually it shall increase globalSeconds counter.
Could other interrupts (two UARTs, SPI) or high MCU load cause trouble with HAL_TIM_PeriodElapsedCallback()?
Any ideas are welcome :)
Regards,
Lex
