-
Notifications
You must be signed in to change notification settings - Fork 7.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
smooth sntp timechanges (IDFGH-10641) #11873
Comments
Just to make sure, have you tried the esp-idf/components/lwip/apps/sntp/sntp.c Lines 45 to 51 in 28167ea
Note that the callback is declared as so you can also implement your own method of adjusting time. |
Thank you David for your reply! There are three different improvements possible: The ESP32 itself, after rtc calibration, has a drift of ±100ppm (8 sec/day) and will vary about 1400ppm per 1°C the temperature.
Changing the clock rate is better than (smoothly) adjusting the The rate the
|
ad 2: in esp-lwip/sntp.c:sntp_process is some code that does use the other times to compute the Clock offset calculation according to RFC 4330. It's enabled with |
I will +1 this. I am currently looking to see what is involved in porting a Chrony client. So many of the things we need to fix are outside the scope of that weak linked function. |
Where did you find this information?
Wouldn't that interfere with the ULP co-processor?
Seconded. |
It took some analysing to see what's going on in all these layers. The callback is called with the network latency corrected local time. Thats ok, but it would be better if it just passed the adjustment. Now the adjustment is added to local time, later on a (later) localtime is subtracted to calculate the boottime offset. Changing the boottime offset is bad.
|
Hi @morgana2313 we will not support the proposed update methods in the near future, but we'd like to allow users to implement various timechange algorithms. If I understand correctly, It is not possible at this moment and these are the points currently missing in IDF:
Is there anything else? @GaryHeaven you mentioned that some functionality that you'd like to modify is not customizable, could you be more specific? |
Ideally the SNTP stack should produce a system time that is consistent with the limitations of the network jitter and the stability of the internal clock source. The current implementation does not do this. I don't think exposing the internal logic is a productive option - the internal stack should just work or, as you have done, allow an external implementation. I have been able to use the loose bound |
I've set up a proper ntp synced disciplined clock for the esp32s3. The key concepts are:
|
Thanks for sharing your views @morgana2313 @GaryHeaven |
Yes, on top of IDF, but I had to implement my own sntp (to get the other parameters and stop sntp for actually changing the time). |
You can probably also use linker wrapping to replace the target_link_libraries(${COMPONENT_LIB} INTERFACE "-Wl,--wrap=_gettimeofday_r") to your project main component makefile and define int IRAM_ATTR __wrap__gettimeofday_r(struct _reent *r, struct timeval *tv, void *tz)
{
// My gettod
return 0;
} (Note that this is a system calls from esp-idf/components/newlib/time.c Line 160 in 4f3cd0d
which we're wrapping) this sounds hacky, but should work. I'll ask around if someone knows of a better way to reroute these syscalls... |
I wrote this in the arduino framework so I only had access to IDF 1.4 but the logic is there. I also have a local time server so I am prepared to ignore the long network latency but it does need to be considered so exposing t1-t4 would be useful as an interim. In all this we must remember the ESP is a microcontroller and thus every cpu cycle should be a prisoner. I think my implementation below is too code heavy for most situations and I have only used it where I need consistent timestamps to less than 0.5 seconds. The current library is good for a minute resolution in most cases and in most cases that is good enough. Hopefully my code comments make sense. `// this implements the weak linked function in ESP IDF // We need a long run delta between SNTP and the underlying ESP Clock
} |
Thanks @GaryHeaven for sharing your implementation. This is very helpful! @morgana2313 Any news from your side? |
Closing as the customization points have been implemented. Please reopen if more details can be provided. |
Is your feature request related to a problem?
The esp-idf lwip sntp code adjusts the boot_time (via
newlib/time.c:adjtime
) for every ntp reply it receives. This is somewhat crude, and makes theboot_time
jump up and down with the (network) latency jitter on the latest ntp reply.To make things worse,
newlib/time.c:adjtime
tends to adjust theboot_time
with the maximum 15_625 usec step when a time function (gettimeofday
) is called.Describe the solution you'd like.
The RTC clock would be much smoother if:
boot_time
. The hardware manual mentions that the RTC_SLOW_CLK is frequency is adjustable?SNTP in nodemcu appears to use an adjustable clock rate on ESP/ESP32: [1] and [2]:
Additional context.
The text was updated successfully, but these errors were encountered: