gps time sync improvements

This commit is contained in:
Klaus K Wilting 2020-10-05 13:40:22 +02:00
parent eece72cef0
commit 69235b94d7
2 changed files with 28 additions and 23 deletions

View File

@ -99,8 +99,6 @@ bool gps_hasfix() {
// function to poll current time from GPS data; note: this is costly
time_t get_gpstime(uint16_t *msec) {
time_t time_sec = 0;
// poll NMEA ZDA sentence
#ifdef GPS_SERIAL
GPS_Serial.print(ZDA_Request);
@ -111,28 +109,36 @@ time_t get_gpstime(uint16_t *msec) {
#endif
// did we get a current date & time?
if (gpstime.isUpdated() && gpstime.isValid() && gpsday.isValid()) {
if (gpstime.isValid() && gpsday.isValid()) {
time_t t = 0;
tmElements_t tm;
uint32_t time_bcd = atof(gpstime.value()) * 100;
uint32_t delay_ms =
gpstime.age() + nmea_txDelay_ms + NMEA_COMPENSATION_FACTOR;
uint32_t zdatime = atof(gpstime.value());
tm.Second = (time_bcd / 100) % 100; // second
tm.Minute = (time_bcd / 10000) % 100; // minute
tm.Hour = time_bcd / 1000000; // hour
// convert time to maketime format and make time
tm.Second = zdatime % 100; // second
tm.Minute = (zdatime / 100) % 100; // minute
tm.Hour = zdatime / 10000; // hour
tm.Day = atoi(gpsday.value()); // day
tm.Month = atoi(gpsmonth.value()); // month
tm.Year = CalendarYrToTm(
atoi(gpsyear.value())); // year offset from 1970 in microTime.h
tm.Year = CalendarYrToTm(atoi(gpsyear.value())); // year offset from 1970
t = makeTime(tm);
// add protocol delay to time with millisecond precision
time_sec = makeTime(tm) + delay_ms / 1000 - 1;
*msec = (delay_ms % 1000) ? delay_ms % 1000 : 0;
ESP_LOGD(TAG, "GPS time/date = %2d:%2d:%2d / %2d.%2d.%2d", tm.Hour,
tm.Minute, tm.Second, tm.Day, tm.Month, tm.Year + 1970);
// add protocol delay with millisecond precision
t += delay_ms / 1000 - 1; // whole seconds
*msec = delay_ms % 1000; // fractional seconds
return t;
}
return timeIsValid(time_sec);
ESP_LOGD(TAG, "no valid GPS time");
return 0;
} // get_gpstime()
@ -162,10 +168,9 @@ void gps_loop(void *pvParameters) {
}
#endif
// (only) while device time is not set or unsynched, and we have a valid GPS
// time, we trigger a device time update to poll time from GPS
if (timeSource == _unsynced && gpstime.isUpdated() && gpstime.isValid() &&
gpsday.isValid())
// (only) while device time is not set or unsynched, and we have a valid
// GPS time, we trigger a device time update to poll time from GPS
if (timeSource == _unsynced && gpstime.isUpdated())
calibrateTime();
} // if

View File

@ -85,7 +85,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec % 1000));
}
ESP_LOGD(TAG, "[%0.3f] UTC time: %d.%03d sec", millis() / 1000.0,
ESP_LOGI(TAG, "[%0.3f] UTC time: %d.%03d sec", millis() / 1000.0,
time_to_set, t_msec % 1000);
// if we have got an external timesource, set RTC time and shift RTC_INT pulse
@ -105,11 +105,11 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
timeSource = mytimesource; // set global variable
timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);
ESP_LOGI(TAG, "[%0.3f] Timesync finished, time was set | source: %c",
ESP_LOGD(TAG, "[%0.3f] Timesync finished, time was set | source: %c",
millis() / 1000.0, timeSetSymbols[mytimesource]);
} else {
timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, setTimeSyncIRQ);
ESP_LOGI(TAG, "[%0.3f] Timesync failed, invalid time fetched | source: %c",
ESP_LOGD(TAG, "[%0.3f] Timesync failed, invalid time fetched | source: %c",
millis() / 1000.0, timeSetSymbols[mytimesource]);
}
}