From dba2d93c21aacad1ba2ff6cbf360b146455ce2ba Mon Sep 17 00:00:00 2001 From: Jeff Kletsky Date: Tue, 6 Feb 2018 19:02:03 -0800 Subject: [PATCH] extras/sntp: Capture RTC counts immediately on update Get the "best value" rather than waiting for logging --- extras/sntp/sntp_fun.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/extras/sntp/sntp_fun.c b/extras/sntp/sntp_fun.c index 093e7d7..c9d0206 100644 --- a/extras/sntp/sntp_fun.c +++ b/extras/sntp/sntp_fun.c @@ -102,18 +102,22 @@ int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) { // Update RTC timer. Called by SNTP module each time it receives an update. void sntp_update_rtc(time_t t, uint32_t us) { + + uint32_t rtc_now = TIMER_COUNT; + // Apply daylight and timezone correction t += (stz.tz_minuteswest + stz.tz_dsttime * 60) * 60; + // DEBUG: Compute and print drift - int64_t sntp_current = sntp_base + TIMER_COUNT - tim_ref; + int64_t sntp_current = sntp_base + rtc_now - tim_ref; int64_t sntp_correct = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / cal; // esp-open-rtos printf does not supply %lld or PRiu64; use long double SNTP_LOGD("RTC Adjust: drift = %.0Lf ticks, cal = %d", (long double)(sntp_correct - sntp_current), (uint32_t)cal); - tim_ref = TIMER_COUNT; - cal = sdk_system_rtc_clock_cali_proc(); + cal = sdk_system_rtc_clock_cali_proc(); + tim_ref = rtc_now; sntp_base = (((uint64_t)us + (uint64_t)t * 1000000U)<<12) / cal; }