2012-06-05 11:48 PM
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 readSo the fetched values of RTC->CNTH and RTC->CNTL are now wrongWhat is the preferred method to avoid this problem ?I currently useu32 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 pleaseThanks, Mark2012-06-06 04:49 AM
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