cancel
Showing results for 
Search instead for 
Did you mean: 

issue of reading RTC

markaren1
Associate II
Posted on June 06, 2012 at 08:48

Hi All,

I am wondering if there is an internal interlock that prevents the RTC from being updated while the registers (RTC->CNTH and RTC->CNTL) are being read.

There is a situation where

RTC->CNTH has been read

RTC then updates rolling RTC->CNTH and RTC->CNTL over

RTC->CNTL is then read

So the fetched values of RTC->CNTH and RTC->CNTL are now wrong

What is the preferred method to avoid this problem ?

I currently use

u32 rtc0, rtc1;

do

{

        rtc0 = 0; rtc0 = RTC->CNTH << 16; rtc0 |= RTC->CNTL;// get two, same RTC counts

        rtc1 = 0; rtc1 = RTC->CNTH << 16; rtc1 |= RTC->CNTL;

}

while(rtc0 != rtc1);

which is a bit bone headed...

Thoughts please

Thanks, Mark

1 REPLY 1
Posted on June 06, 2012 at 13:49

Short of stopping the counter, you're not going to be read it atomically, change the settings would take longer than just reading the low register twice.

This would be more computationally efficient, and have a smaller window of uncertainty.

u16 h, l0, l1;
u32 rtc;
do
{
l0 = RTC->CNTL;
h = RTC->CNTH;
l1 = RTC->CNTL;
}
while(l0 != l1);
rtc = ((u32)h << 16) | (u32)l1;

Still you have the potential of being off by 0.99999 seconds
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..