From 529059edf4466f79440c09a771537a8d9aadee2a Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 23 Jan 2022 21:12:28 +0100 Subject: [PATCH] integrate esp32 sntp --- include/if482.h | 1 + include/rcommand.h | 1 + include/timekeeper.h | 1 + src/if482.cpp | 18 +++++------------- src/timekeeper.cpp | 18 +++++++----------- 5 files changed, 15 insertions(+), 24 deletions(-) diff --git a/include/if482.h b/include/if482.h index faf2a01f..9a3fe92f 100644 --- a/include/if482.h +++ b/include/if482.h @@ -3,6 +3,7 @@ #include "globals.h" #include "timekeeper.h" +#include "esp_sntp.h" #define IF482_FRAME_SIZE (17) #define IF482_SYNC_FIXUP (10) // calibration to fixup processing time [milliseconds] diff --git a/include/rcommand.h b/include/rcommand.h index 75689597..0cdb23c6 100644 --- a/include/rcommand.h +++ b/include/rcommand.h @@ -10,6 +10,7 @@ #include "sensor.h" #include "cyclic.h" #include "timekeeper.h" +#include "esp_sntp.h" #include "timesync.h" #include "power.h" #include "antenna.h" diff --git a/include/timekeeper.h b/include/timekeeper.h index c95d41d0..8eb184b4 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -8,6 +8,7 @@ #include "gpsread.h" #include "if482.h" #include "dcf77.h" +#include "esp_sntp.h" #define SECS_YR_2000 (946684800UL) // the time at the start of y2k #define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch diff --git a/src/if482.cpp b/src/if482.cpp index b4fb6250..37c12c38 100644 --- a/src/if482.cpp +++ b/src/if482.cpp @@ -88,24 +88,16 @@ String IF482_Frame(time_t t) { char mon, out[IF482_FRAME_SIZE + 1]; - switch (sntp_get_sync_status()) { // indicates if time has been set and recently synced - case SNTP_SYNC_STATUS_COMPLETED: // time is set and is synced - mon = 'A'; - break; - case SNTP_SYNC_STATUS_IN_PROGRESS: // time had been set but sync attempt did not succeed - case SNTP_SYNC_STATUS_RESET: - mon = 'M'; - break; - default: // unknown time status (should never be reached) - mon = '?'; - break; - } // switch + if (sntp_get_sync_status() == SNTP_SYNC_STATUS_IN_PROGRESS) + mon = 'M'; // time had been set but sync not completed + else + mon = 'A'; // time has been set and was recently synced // generate IF482 telegram // snprintf(out, sizeof(out), "O%cL%s\r", mon, myTZ.dateTime(t, UTC_TIME, // "ymdwHis").c_str()); - //ESP_LOGD(TAG, "[%s] IF482 date/time: %s", ctime(time(NULL), out); + // ESP_LOGD(TAG, "[%s] IF482 date/time: %s", ctime(time(NULL), out); return out; } diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index a1ebda4b..876bff85 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -103,7 +103,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, tv.tv_sec = time_to_set; tv.tv_usec = 0; - settimeofday(&tv, NULL); + sntp_sync_time(&tv); ESP_LOGI(TAG, "[%0.3f] UTC time: %d.000 sec", _seconds(), time_to_set); @@ -122,7 +122,6 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, #endif timeSource = mytimesource; // set global variable - sntp_set_sync_status(SNTP_SYNC_STATUS_COMPLETED); timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ); ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | timesource=%d", @@ -140,6 +139,10 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, // helper function to setup a pulse per second for time synchronisation uint8_t timepulse_init() { + // set esp-idf API sntp sync mode + //sntp_init(); + sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); + // use time pulse from GPS as time base with fixed 1Hz frequency #ifdef GPS_INT @@ -273,15 +276,8 @@ void clock_loop(void *taskparameter) { // ClockTask // wait for timepulse and store UTC time xTaskNotifyWait(0x00, ULONG_MAX, ¤t_time, portMAX_DELAY); - /* - // no confident or no recent time -> suppress clock output - if ((sntp_get_sync_status() != SNTP_SYNC_STATUS_COMPLETED) || - !(timeIsValid(current_time)) || (current_time == previous_time)) - continue; - */ - - // no confident or no recent time -> suppress clock output - if (!(timeIsValid(current_time)) || (current_time == previous_time)) + if ((sntp_get_sync_status() == SNTP_SYNC_STATUS_IN_PROGRESS) || + !(timeIsValid(current_time)) || (current_time == previous_time)) continue; // set calendar time for next second of clock output