Page 2 of 2

Re: LPTIMER & LPUART support?

Posted: Wed Aug 09, 2017 6:49 pm
by Giovanni
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.

Giovanni

Re: LPTIMER & LPUART support?

Posted: Wed Aug 09, 2017 10:20 pm
by alexblack
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.

Re: LPTIMER & LPUART support?

Posted: Tue Jan 21, 2020 11:48 am
by iggarpe
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).