Skip to main content
Visitor II
March 4, 2025
Solved

Back to back reads of RTC registers sometimes show increasing SubSeconds

  • March 4, 2025
  • 3 replies
  • 1067 views

I think I may have found a bug with the STM32G4 RTC that I am not seeing in the errata.

I am using the RTC on STM32G4 with a 32.768 KHz LSE oscillator. I am having a issue where I would read the RTC twice fairly close together in code. I observed that occasionally on the 2nd read of the RTC the SSR value would increase without a change to either the TR or DR. I tested with shadow bypass both enabled and disable and I observed the same behavior.

I tried to profile the SSR values that I would get which triggered this condition and I noticed that the 2nd read was often 3 more than the 1st read (ex. 22492 and 22495) but sometimes the 2nd read was even more. It also seemed that multiple least significate bits of the 1st read were 0; I suspect that this could be explained by the fact the RTC is not on the same clock domain and I happen to be reading the SSR in the middle of a decrement before the bits are stable.

I seem to be able to work around this by reading the RTC registers 2 times until they match. I saw this is needed on older processors with a RTC shadow register bug.

Is this a known issue, a new issue, or am I doing something wrong?

    This topic has been closed for replies.

    3 replies

    matkSENSAuthor
    Visitor II
    March 4, 2025

    Thanks! Those do seem to be the same issue. It almost seems like shadow RTC registers just don't work. I was surprised that "bits within registers were inconsistent" but I could not find that documented in the G4 RM. It makes sense why as they are in different clock domains. Reading the registers twice until the values are consistent seems to be the best way to go.

    Super User
    March 5, 2025

    waclawekjan_0-1741165755932.png

    As I wrote in the article @Andrew Neil linked to above, I've observed this only when BYPSHAD=1. I would be surprised to see it when BYPSHAD=0 as one of the purposes of the shadow registers readout mechanism is exactly to eliminate problems stemming from reading asynchronously clocked counters (the other is to provide consistent readout of multiple registers i.e. SSR+TR+DR through the locking mechanism).

    Btw. 'G4 has RTCv3, which does not suffer from the "sometimes shadow registers won't lock" erratum.

    JW

    matkSENSAuthor
    Visitor II
    March 5, 2025

    Thank you for pointing that out!

    I guess my biggest issue is with the SSR. My best understanding is that it can be read in an unstable state no matter if BYPSHAD is 1 or 0. So for example, if someone is relying on the RTC with sub seconds as a monotonically increasing timer, a single read to the SSR can be inconstant. Also from your article, the code to get a constant RTC reading with BYPSHAD=0 is more complex and takes longer than the double read solution when BYPSHAD=1.

    Again I am still new to this microcontroller and my expectation was that using BYPSHAD=0 would be less complicated to ensure consistent clock readings, but it seems to come with more caveats and complexities.

    Super User
    March 7, 2025

    > My best understanding is that it can be read in an unstable state no matter if BYPSHAD is 1 or 0.

    This shouldn't be the case. If BYPSHAD=0, hardware should work so that SSR is read into its shadow in a way that reading from shadow is always consistent. Can you please confirm that your finding is with BYPHSHAD=0?

    > Also from your article, the code to get a constant RTC reading with BYPSHAD=0 is more complex and takes longer than the double read solution when BYPSHAD=1.

    This is because of the "RTC shadow lock sometimes fails" erratum (the exact name of erratum is different, I am lazy to look it up at the moment). That erratum is for RTCv2, whereas in 'G4 there is RTCv3, which should not have this problem (btw. I say so in the small print at the beginning of the article, easy to overlook I admit).

    JW