arduino-libraries / RTCZero

RTC Library for SAMD21 based boards
http://arduino.cc/en/Reference/RTC
GNU Lesser General Public License v2.1
77 stars 78 forks source link

Add function that uses the register "FREQCORR" for clock correction #89

Open Abravius opened 2 years ago

Abravius commented 2 years ago

The RTC in my chip isn't really accurate. I think I'm not the only one

I looked in the library source code, but didn't find any existing function that allows correcting the clock.

I think it would be very important to add a function to correct the clock. This can be done using the FREQCORR" register.

Datasheet:

https://cdn.sparkfun.com/datasheets/Dev/Arduino/Boards/Atmel-42181-SAM-D21_Datasheet.pdf

Register in the page 262.

Abravius commented 2 years ago

I've this solution :

//Function used to correct the RTC clock

//<_Sign> :   If the correction must be positive (false) or negative (true)
//<_Value> :  Value to put in the register from 1 to 127.
void SetClockCorrection(bool _Sign, byte _Value)
{
         RTC->MODE2.FREQCORR.reg = (byte)_Sign << 8 || _Value;
}

I'm testing it right now.

Abravius commented 2 years ago

After 24 hours, same problem. I don't know why...

pe-jot commented 11 months ago

After 24 hours, same problem. I don't know why...

Your code shifts _Sign one bit too far. Furthermore, as far as I read the datasheet, there's need for write synchronization, thus you'll have to place something like while(RTC->MODE2.STATUS.bit.SYNCBUSY); before writing the register.