From a88ea16e3af131bff0e1ce29684147243272da85 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Fri, 15 Mar 2019 22:48:09 +0100 Subject: [PATCH] timesync fixes --- include/timesync.h | 4 ++-- src/timesync.cpp | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/include/timesync.h b/include/timesync.h index e854301e..bbba4a7f 100644 --- a/include/timesync.h +++ b/include/timesync.h @@ -6,8 +6,8 @@ #include "timesync.h" #include "timekeeper.h" -#define TIME_SYNC_SAMPLES 5 // number of time requests for averaging -#define TIME_SYNC_CYCLE 60 // seconds between two time requests +#define TIME_SYNC_SAMPLES 3 // number of time requests for averaging +#define TIME_SYNC_CYCLE 20 // seconds between two time requests #define TIME_SYNC_TIMEOUT 120 // timeout seconds waiting for timeserver answer #define TIME_SYNC_TRIGGER 100 // time deviation in millisec triggering a sync #define TIME_SYNC_FRAME_LENGTH 0x06 // timeserver answer frame length diff --git a/src/timesync.cpp b/src/timesync.cpp index c2b44b4d..8442cdb6 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -140,8 +140,8 @@ void process_timesync_req(void *taskparameter) { // calculate time offset from collected diffs and set time if necessary ESP_LOGD(TAG, "Avg time diff: %lldms", time_offset.count()); time_offset /= TIME_SYNC_SAMPLES; - // 1 sec floor round + 1sec wait for top of second - time_to_set = now() + 2 + time_offset.count() / 1000; + // 1sec wait for top of second + time_to_set = now() + time_offset.count() / 1000 + 1; ESP_LOGD(TAG, "Calculated UTC epoch time: %d", time_to_set); // adjust system time @@ -155,17 +155,17 @@ void process_timesync_req(void *taskparameter) { ESP_LOGD(TAG, "waiting %dms", 1000 - time_offset_msec); vTaskDelay(pdMS_TO_TICKS(1000 - time_offset_msec)); + setTime(time_to_set); // sync timer pps to top of second if (ppsIRQ) - timerRestart(ppsIRQ); + timerWrite(ppsIRQ, 10000); // fire interrupt - setTime(time_to_set); timeSource = _lora; timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // set to regular repeat - ESP_LOGI(TAG, "Timesync finished, time was adjusted"); + ESP_LOGI(TAG, "Timesync finished, time adjusted by %lld ms", time_offset.count()); } else - ESP_LOGI(TAG, "Timesync finished, time is up to date"); + ESP_LOGI(TAG, "Timesync finished, time not adjusted, is up to date"); } else ESP_LOGW(TAG, "Invalid time received from timeserver");