SNTP_fix_rtc_counter_wrapping

This commit is contained in:
Rutger Huijgen 2020-04-18 14:42:14 +02:00
parent bc979883c2
commit f280fa27b5
2 changed files with 72 additions and 117 deletions

View file

@ -60,11 +60,6 @@ int sntp_set_servers(const char *server_url[], int num_servers);
*/ */
void sntp_set_update_delay(uint32_t ms); void sntp_set_update_delay(uint32_t ms);
/*
* Returns the time read from RTC counter, in seconds from Epoch. If
* us is not null, it will be filled with the microseconds.
*/
time_t sntp_get_rtc_time(int32_t *us);
/* /*
* Update RTC timer. This function is called by the SNTP module each time * Update RTC timer. This function is called by the SNTP module each time

View file

@ -8,36 +8,26 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/errno.h> #include <sys/errno.h>
#include <stdio.h> #include <stdio.h>
#include <sys/time.h>
#include <espressif/esp_common.h> #include <espressif/esp_common.h>
#include <esp/timer.h> #include <esp/timer.h>
#include <esp/rtc_regs.h> #include <esp/rtc_regs.h>
#include <sntp.h> #include <FreeRTOS.h>
#include <assert.h>
#include <semphr.h>
#ifdef SNTP_LOGD_WITH_PRINTF #include "sntp.h"
#define SNTP_LOGD(FMT, ...) printf(FMT "\n", ##__VA_ARGS__)
#endif
#ifndef SNTP_LOGD
#define SNTP_LOGD(...)
#define SKIP_DIAGNOSTICS
#endif
#define TIMER_COUNT RTC.COUNTER #define TIMER_COUNT RTC.COUNTER
// daylight settings // daylight settings
// Base calculated with value obtained from NTP server (64 bits) // Base calculated with value obtained from NTP server (64 bits)
#define sntp_base (*((uint64_t*)RTC.SCRATCH)) #define sntp_base (*((uint64_t*)RTC.SCRATCH))
// Timer value when base was obtained // Timer value when sntp_base was obtained
#define tim_ref (RTC.SCRATCH[2]) #define tim_ref (RTC.SCRATCH[2])
// Calibration value -- ( microseconds / RTC tick ) * 2^12 // Calibration value
#define cal (RTC.SCRATCH[3]) #define cal (RTC.SCRATCH[3])
#ifndef SKIP_DIAGNOSTICS // To protect access to the above.
// Keep the last time SNTP updated the time static SemaphoreHandle_t sntp_sem = NULL;
static struct timeval last_update_time = {0, 0};
#endif
// Timezone related data. // Timezone related data.
static struct timezone stz; static struct timezone stz;
@ -48,118 +38,88 @@ void sntp_init(void);
// Sets time zone. // Sets time zone.
// NOTE: Settings do not take effect until SNTP time is updated. // NOTE: Settings do not take effect until SNTP time is updated.
void sntp_set_timezone(const struct timezone *tz) { void sntp_set_timezone(const struct timezone *tz) {
if (tz) { if (tz) {
stz = *tz; stz = *tz;
} else { } else {
stz.tz_minuteswest = 0; stz.tz_minuteswest = 0;
stz.tz_dsttime = 0; stz.tz_dsttime = 0;
} }
} }
// Initialization // Initialization
void sntp_initialize(const struct timezone *tz) { void sntp_initialize(const struct timezone *tz) {
if (tz) { if (tz) {
stz = *tz; stz = *tz;
} else { } else {
stz.tz_minuteswest = 0; stz.tz_minuteswest = 0;
stz.tz_dsttime = 0; stz.tz_dsttime = 0;
} }
sntp_base = 0; sntp_base = 0;
// To avoid div by 0 exceptions if requesting time before SNTP config // To avoid div by 0 exceptions if requesting time before SNTP config
cal = sdk_system_rtc_clock_cali_proc(); cal = sdk_system_rtc_clock_cali_proc();
tim_ref = TIMER_COUNT; tim_ref = TIMER_COUNT;
sntp_init(); sntp_sem = xSemaphoreCreateMutex();
assert(sntp_sem == NULL);
sntp_init();
} }
// Check if a timer wrap has occurred. Compensate sntp_base reference // Return usecs.
// if affirmative. static inline uint64_t sntp_get_rtc_time() {
// TODO: think about multitasking and race conditions xSemaphoreTake(sntp_sem, portMAX_DELAY);
static inline void sntp_check_timer_wrap(uint32_t current_value) { uint32_t tim = TIMER_COUNT;
if (current_value < tim_ref) { // Assume the difference does not overflow in which case
// Timer wrap has occurred, compensate by subtracting 2^32 to ref. // wrapping of the RTC timer still yields a good difference.
sntp_base -= 1LLU<<32; uint32_t diff = tim - tim_ref;
// DEBUG tim_ref = tim;
SNTP_LOGD("SNTP RTC counter wrapped"); uint64_t diff_us = ((uint64_t)diff * cal) >> 12;
} uint64_t base = sntp_base + diff_us;
} sntp_base = base;
xSemaphoreGive(sntp_sem);
// Return secs. If us is not a null pointer, fill it with usecs return base;
inline time_t sntp_get_rtc_time(int32_t *us) {
time_t secs;
uint32_t tim;
uint64_t base;
tim = TIMER_COUNT;
// Check for timer wrap
sntp_check_timer_wrap(tim);
base = sntp_base + tim - tim_ref;
secs = (base * cal) / (1000000U<<12);
if (us) {
*us = ((base * cal) % (1000000U<<12)) >>12;
}
return secs;
} }
// Syscall implementation. doesn't seem to use tzp. // Syscall implementation. doesn't seem to use tzp.
int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) { int _gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) {
(void)r; (void)r;
// Syscall defined by xtensa newlib defines tzp as void* // Syscall defined by xtensa newlib defines tzp as void*
// So it looks like it is not used. Also check tp is not NULL // So it looks like it is not used. Also check tp is not NULL
if (tzp || !tp) return EINVAL; if (tzp || !tp) return EINVAL;
tp->tv_sec = sntp_get_rtc_time((int32_t*)&tp->tv_usec); if (sntp_base == 0) {
return 0; printf("Time not valid yet");
return EINVAL;
}
if (xTaskGetSchedulerState() != taskSCHEDULER_RUNNING) {
printf("Scheduler NOT RUNNING RHURHU");
return EINVAL;
}
uint64_t base = sntp_get_rtc_time();
tp->tv_sec = base / 1000000U;
tp->tv_usec = base % 1000000U;
return 0;
} }
// Update RTC timer. Called by SNTP module each time it receives an update. // Update RTC timer. Called by SNTP module each time it receives an update.
void sntp_update_rtc(time_t t, uint32_t us) { void sntp_update_rtc(time_t t, uint32_t us) {
uint32_t now_rtc = TIMER_COUNT;
// Apply daylight and timezone correction // Apply daylight and timezone correction
t += (stz.tz_minuteswest + stz.tz_dsttime * 60) * 60; t += (stz.tz_minuteswest + stz.tz_dsttime * 60) * 60;
int64_t sntp_correct = (uint64_t)us + (uint64_t)t * 1000000U;
#ifndef SKIP_DIAGNOSTICS xSemaphoreTake(sntp_sem, portMAX_DELAY);
uint32_t tim = TIMER_COUNT;
int64_t sntp_reference_time, local_clock_time, clock_difference; // Assume the difference does not overflow in which case
struct timeval this_update_time, elapsed_since_update; // wrapping of the RTC timer still yields a good difference.
double ppm; uint32_t diff = tim - tim_ref;
tim_ref = tim;
// Calculate in diagnostics in microseconds uint64_t diff_us = ((uint64_t)diff * cal) >> 12;
sntp_reference_time = (uint64_t)us + (uint64_t)t * 1000000U; uint64_t sntp_current = sntp_base + diff_us;
local_clock_time = ((sntp_base + now_rtc - tim_ref) * cal) / (1U<<12); sntp_base = sntp_correct;
clock_difference = sntp_reference_time - local_clock_time; cal = sdk_system_rtc_clock_cali_proc();
xSemaphoreGive(sntp_sem);
this_update_time.tv_sec = t;
this_update_time.tv_usec = us;
timersub(&this_update_time, &last_update_time, &elapsed_since_update);
// If over a day since last update, don't trust the last_update_time
if (elapsed_since_update.tv_sec < 24 * 60 * 60 ) {
ppm = ((double)clock_difference /
((double)elapsed_since_update.tv_sec * 1000000
+ elapsed_since_update.tv_usec))
* 1000000;
SNTP_LOGD("SNTP RTC adjust: %0.3Lf s; %0.3f ppm over %0.3f s; cal: %u\n",
((long double)clock_difference)/1000000,
ppm,
(double)elapsed_since_update.tv_sec
+ ((double)elapsed_since_update.tv_usec)/1000000,
cal);
} else {
SNTP_LOGD("SNTP RTC adjust: %0.3Lf s; cal: %u\n",
((long double)clock_difference)/1000000, cal);
}
last_update_time.tv_sec = t;
last_update_time.tv_usec = us;
#endif // SKIP_DIAGNOSTICS
cal = sdk_system_rtc_clock_cali_proc();
tim_ref = now_rtc;
sntp_base = (((uint64_t)us + (uint64_t)t * 1000000U) <<12) / cal;
printf("\nRTC Adjust: drift = %d usec, cal = %d", (int)(sntp_correct - sntp_current), cal);
} }