Correct way to read RTC registers?

Status
Not open for further replies.

Rena

Active member
The register `RTC_TSR` contains the counter in seconds, and `RTC_TPR` contains fractional seconds, but what's the proper way to read both? If I read one and then the other, it's possible the TSR increments between reads, so the resulting value will be off by almost a whole second. Is there some "atomic 64-bit read" I can use? Should I stop the counter before every read?

Also, the datasheet claims that the TSR increments when bit 14 of TPR transitions to zero. Is this correct? So the TPR counts in increments of 1/16384 second, not 1/32768? Is this intended to provide a solution to the race condition? Or is this a typo? From my tests it does seem to be counting 1/32768 second.
 
The register `RTC_TSR` contains the counter in seconds, and `RTC_TPR` contains fractional seconds, but what's the proper way to read both? If I read one and then the other, it's possible the TSR increments between reads, so the resulting value will be off by almost a whole second. Is there some "atomic 64-bit read" I can use? Should I stop the counter before every read?

Also, the datasheet claims that the TSR increments when bit 14 of TPR transitions to zero. Is this correct? So the TPR counts in increments of 1/16384 second, not 1/32768? Is this intended to provide a solution to the race condition? Or is this a typo? From my tests it does seem to be counting 1/32768 second.

In other processors that have split registers like this, you read the seconds value twice, and if the versions are different, you loop back, and re-read all three. I.e. something like:

Code:
    unsigned long seconds, seconds2, fraction;

    // ...

    do {
        seconds = RTC_TSR;
        fraction = RTC_TPR;
        seconds2 = RTC_TSR;
    } while (seconds != seconds2);
 
Status
Not open for further replies.
Back
Top