What is your view on adding some sort of automatic synchronization that
would ensure system clock stays in sync with RTC (or at least error is less
than some value) even when i drifts away from "real time"? Quite simple
solution is to let clock_gettime() to update base time after N reads however
this would add some computation complexity to gettime (I2C communication
delay for external RTC etc.) and I am not sure if we really want this inside
clock_gettime().
I didn't respond to this.
I don't know if a system solution makes sense. This is a user function
that is supported by all common OS via adjtime(). We should implement
the solution in the same way that other systems do.
I suspect that the properties of RTCs are too variable and that any
solution has to be hardware specific, at least in some aspects.
clock_synchronize() or clock_resynchronize() are totally inappropriate
for this and would clearly break system timing. But adjtime() as
specified (but not as currently implemented ) would be a very good
solution to do this and is already exposed to applications. See
https://github.com/apache/nuttx/issues/8858
Waiting for N reads is arbitrary. It would be better to get an event
from the RTC, like when the second timer rolls over or an alarm
expires. An application should be able to get that from
drivers/timer/rtc.c. Sampling an RTC asynchronous does not give you
good times due to the limited resolution of most RTC time. But sampling
the RTC at well known points in time (like incrementing the second)
would be synchronous and accurate.
I'm sure the solution does not belong in clock_gettime(). That would be
mixing functionality and not in the spirit of good modular design. And
would not support the most common use case for adjtime():
The most common way of adjusting time via adjtime() is as part of the
use of NTP (I see PTP also uses adjtime). Certainly any system solution
cannot prohibit that most common use of adjtime(). Certainly
clock_gettime() cannot get involved with NTP which is an application
space protocol.
There are lots of examples of adjtime implementations. And examples of
NTP clients on the internet using adjtime().
https://github.com/openntpd-portable/openntpd-portable,
https://github.com/ntp-project/ntp.
Here is an NTP adjtime() call:
https://github.com/ntp-project/ntp/blob/master-no-authorname/ntpdate/ntpdate.c#L2039