Uneven/Increasing/Decreasing elapsed time by STM32 timer

Hello everyone,

I am working with the board STM32F446RE with SX1261 on mbed os.

SITUATION: I want to measure the time difference between the send moment and the receive moment of a packet (transmitted by LoRa). Basically, when I send packet A (send time) from 1st board, the second board receives the packet and immediately responds back. 1st board receives (receive time) the response. I measured this period 30 times to get the time difference but this value kept increasing/decreasing until it reached a point before dropping/rocketing.

SET UP: I used timer 2 with 90MHz clock frequency. Two boards are at fixed distance.

WHAT I HAVE DONE: Considering that changing measurement, I also measured the time difference between the receive time and the response time on the 2nd board with timer 2. The same dropping/rocketing still occurred.

QUESTION: What might be the cause of this change? I suspect it might be the timer that has a rollover.

The pattern is something as below. “Dedicated” means the 1st board sends a packet and receives a response. The 2nd board receives the packet and sends a response. “Reverse” means the opposite.

Kind regards,
Huy Nguyen.

Well yes, hardware timers do roll over. On STM32, there are usually some 32-bit and some 16-bit timers to work with. You can usually check out the datasheet to see which are which. If the rollover is causing a problem, and there isn’t a wider timer available, a common solution is to use a prescaler to slow down the timer’s incrementing and make it take longer to count up.

P.S. I think me and you are working on very similar projects. The Lightspeed Rangefinder | Engineering Archive

Hello @MultipleMonomials ,

Thank you for your response. The timer (TIM2) I am using can return 32-bit counting values. The reason Why I set it to run at 90 MHz bcuz the signal travels at the speed of light. So high frequency can give me better precision for a short distance. In other words, the slower the timer gets, the less precision I get. Using Prescaler is also a possible option. I will try and update you.

P.S. I just looked at the description of your Lightspeed Rangefinder and, indeed, it is very similar to my project. Your range (around dozens/hundred of kilometers) can tolerate less precision. And the LOS is an advantage in your project since you work with flight vehicles.

Hello @MultipleMonomials

I just tried the Prescaler and the drop/rocket is still there, although the amount of drop/rocket changes (of course due to the division). I am more curious about why the difference is not stable as expectation.

Do you happen to know why?

My friend just found out the cause. It is because of the HAL_delay(). The resolution of that delay function is 1ms. So with the 90MHz frequency, 1ms is equal to 90000 ticks which is A LOT. That is why there is a periodic drop/rocket.

Once my friend removes the delay function, the large fluctuation does not happen anymore.

Thank you,

Huy Nguyen.