Seeed-Studio / Seeed_Arduino_RTC

When your project needs to be unplugged, how to keep the time counting? This library can help you set up the current time.
MIT License
7 stars 7 forks source link

Use XOSC32K for RTC to improve RTC clock accuracy. #2

Closed ciniml closed 3 years ago

ciniml commented 3 years ago

Hi. Currently I'm facing a problem of RTC clock accuracy.

After running around 5 hours, the RTC clock is 5 minutes ahead of actual time, which is too low accuracy to make a clock application.

Then I've checked the RTC peripheral initialization code and I've found that the RTC peripheral uses internal 32[kHz] oscillator instead of external 32[kHz] crystal oscillator, which is less precise and accurate than crystal oscillator.

So this PR modifies RTC initialization code to use 32[kHz] crystal oscillator if available.

According to the ArduinoCore-samd source code (startup.c), the 32k crystal is enabled by the Arduino core if CRYSTALLESS is not defined.

https://github.com/Seeed-Studio/ArduinoCore-samd/blob/master/cores/arduino/startup.c#L66

Thus, we can use safely without initializing XOSC32K if CRYSTALLES is not defined.

If CRYSTALLES is defined, we can just use the original RTC clock initilalization code.

Pillar1989 commented 3 years ago

Thanks