rtc time doesn't seem to get updated from system time / ntp?

willemmerson
Posts: 40
Joined: Mon Mar 18, 2019 12:34 pm

rtc time doesn't seem to get updated from system time / ntp?

Postby willemmerson » Wed Feb 17, 2021 10:42 am

I have some devices which were running for several months, and their time was being maintained correctly by sntp. But when I did a soft reboot and just before they got time from ntp, their system time was wrong by a few hours/days. My theory about this, which may be wrong, is that the RTC clock maintains time during a soft reboot and therefore the RTC clock had drifted, which is expected, and the system time was set from this after the soft reboot.
Is this correct and is it possible to keep the RTC clock in sync with system time / NTP?
It would be useful to have this fallback in case NTP isn't available and the device reboots for some reason.

devanl
Posts: 14
Joined: Sat Jan 04, 2020 5:26 pm

Re: rtc time doesn't seem to get updated from system time / ntp?

Postby devanl » Sat Feb 20, 2021 5:04 am

Hi,

I've run into this problem before. As you guessed, the fundamental issue is that by default, the system uses a different timer than the RTC to keep track of system time and those two clocks have very different accuracy specs (ironically, the system timer is more accurate than the RTC, but to be fair, the "C" in RTC means "Control", not "Clock"), so over a long period of time they accumulate drift. Upon reboot, the time is reinitialized from the RTC and suddenly your time jumps by hours or days as a result.

The simplest solution is to use the RTC as the sole time source, but this makes all time lookups slower and reduces the granularity from 1us to something like 1/32768s. Ideally, you would be able to periodically sync the RTC time so that both the system clock and RTC are in agreement. Unfortunately, right now it's only possible to do this when going to sleep because the storage for the offset between the elapsed time and the real-world-time is the same for both the RTC and the system clock. As a result, fixing the time for the RTC will make the time for the system clock wrong, so it's only safe to do it right before going to sleep when you're not going to look at the system time anyways. So going to sleep periodically might help by putting an upper bound on the amount of drift between the two clocks.

I think it would be best if it were possible to store separate offsets for the RTC vs the system clock. Then you could periodically sync the RTC to the system clock.

willemmerson
Posts: 40
Joined: Mon Mar 18, 2019 12:34 pm

Re: rtc time doesn't seem to get updated from system time / ntp?

Postby willemmerson » Mon Feb 22, 2021 9:50 am

Thanks for the detailed answer, I can probably just use the RTC as sole time source as I don't need the higher precision. I agree there should be a manual way of setting the RTC without going into deep sleep, I might open an issue.

devanl
Posts: 14
Joined: Sat Jan 04, 2020 5:26 pm

Re: rtc time doesn't seem to get updated from system time / ntp?

Postby devanl » Mon Feb 22, 2021 8:39 pm

I forgot to add that the other drawback of using the RTC is that now your system time could drift substantially between NTP resyncs (IIRC, the RTC is rated for +/-5% frequency accuracy - the equivalent of 50000 ppm!).

Who is online

Users browsing this forum: No registered users and 161 guests