ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

User avatar
Site Admin
Posts: 13125
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 769 times
Been thanked: 653 times

Re: LPTIMER & LPUART support?

Postby Giovanni » Wed Aug 09, 2017 6:49 pm

st_lld_get_counter() must be able to work both inside and outside critical zones, there is the risk that the soft counter could be incremented by both the function and the ISR.


Posts: 251
Joined: Mon Sep 24, 2012 3:52 pm
Location: Donetsk
Been thanked: 29 times

Re: LPTIMER & LPUART support?

Postby alexblack » Wed Aug 09, 2017 10:20 pm

Yes it is. But I calculate counter several times. "Wrong" situation can be once and will be detected by comparing two consecutive readings and will be recalculated once more giving right result. I checked last code and there are no more "race condition" detected.

Posts: 129
Joined: Sun Sep 30, 2012 8:32 pm

Re: LPTIMER & LPUART support?

Postby iggarpe » Tue Jan 21, 2020 11:48 am

What do you think about this strategy:

1- Use a 64 bit soft counter coupled with the timer 16 bit counter.

2- Generate two timer interrupts, one at timer counter 0x0000 crossing and another at 0x8000 crossing.

3- On 0x0000 crossing increase soft counter.

4- On 0x8000 crossing save soft counter to shadow variable.

Now when reading the system time we need to account for the possibility that the timer counter has crossed 0x0000 but the interrupt has not been serviced yet and thus the soft counter has not been incremented yet:

1- Look at timer counter, if greater than 0x4000 we assume all is fine, return (uint32_t)((soft counter << 16) | timer counter)

2- Otherwise, if soft counter < shadow variable, then all is fine because soft counter has already been incremented, return (uint32_t)((soft counter << 16) | timer counter).

3- Otherwire soft counter has not been incremented yet, return (uint32_t)(((soft counter + 1) << 16) | timer counter).

This strategy works as long as the soft counter never overflows (hence the use of a 64 bit soft counter) and the timer interrupt is never unserviced for longer that 0x4000 timer ticks (and if it is, you're in trouble anyway).

Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 10 guests