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.
rtc time doesn't seem to get updated from system time / ntp?
-
- Posts: 40
- Joined: Mon Mar 18, 2019 12:34 pm
Re: rtc time doesn't seem to get updated from system time / ntp?
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.
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.
-
- Posts: 40
- Joined: Mon Mar 18, 2019 12:34 pm
Re: rtc time doesn't seem to get updated from system time / ntp?
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.
Re: rtc time doesn't seem to get updated from system time / ntp?
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 211 guests