bugfixing in time handling

This commit is contained in:
Klaus K Wilting 2019-02-02 09:15:31 +01:00
parent a2b0cc7315
commit ab5cd0b0a2
7 changed files with 30 additions and 21 deletions

View File

@ -12,8 +12,8 @@
extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available
int rtc_init(void); int rtc_init(void);
int set_rtctime(RtcDateTime t);
int set_rtctime(uint32_t t); int set_rtctime(uint32_t t);
int set_rtctime(time_t t);
void sync_rtctime(void); void sync_rtctime(void);
time_t get_rtctime(void); time_t get_rtctime(void);
float get_rtctemp(void); float get_rtctemp(void);

View File

@ -38,10 +38,10 @@ void doHousekeeping() {
// do cyclic write back system time to RTC if we have an external time source // do cyclic write back system time to RTC if we have an external time source
#if (defined TIME_SYNC_INTERVAL_LORA || defined TIME_SYNC_INTERVAL_GPS) && \ #if (defined TIME_SYNC_INTERVAL_LORA || defined TIME_SYNC_INTERVAL_GPS) && \
defined HAS_RTC defined HAS_RTC
if ((millis() >= nextRTCTimeSync) && timeSet) { if ((millis() >= nextRTCTimeSync) && (timeStatus() == timeSet)) {
nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC *
60000; // set up next time sync period 60000; // set up next time sync period
if (!set_rtctime(now())) if (!set_rtctime(now())) // epoch time
ESP_LOGE(TAG, "RTC set time failure"); ESP_LOGE(TAG, "RTC set time failure");
else else
ESP_LOGI(TAG, "RTC time updated"); ESP_LOGI(TAG, "RTC time updated");

View File

@ -70,9 +70,12 @@ time_t tmConvert_t(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh,
time_t get_gpstime(void) { time_t get_gpstime(void) {
// never call now() in this function, this would cause a recursion! // never call now() in this function, this would cause a recursion!
time_t t = 0; time_t t = 0;
if (gps.time.age() < 1500) { if ((gps.time.age() < 1500) && (gps.time.isValid())) {
t = tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(), t = tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second()); gps.time.hour(), gps.time.minute(), gps.time.second());
ESP_LOGD(TAG, "GPS time: %d/%d/%d %d:%d:%d", gps.date.year(),
gps.date.month(), gps.date.day(), gps.time.hour(),
gps.time.minute(), gps.time.second());
} else { } else {
ESP_LOGW(TAG, "GPS has no confident time"); ESP_LOGW(TAG, "GPS has no confident time");
} }

View File

@ -105,7 +105,9 @@ int if482_init(void) {
} // if482_init } // if482_init
String if482Telegram(time_t t) { String if482Telegram(time_t tt) {
time_t t = myTZ.toLocal(tt);
char mon; char mon;
char buf[14] = "000000F000000"; char buf[14] = "000000F000000";
@ -123,7 +125,8 @@ String if482Telegram(time_t t) {
break; break;
} // switch } // switch
if (!timeNotSet) // do we have valid time? if ((timeStatus() == timeSet) ||
(timeStatus() == timeNeedsSync)) // do we have valid time?
snprintf(buf, sizeof buf, "%02u%02u%02u%1u%02u%02u%02u", year(t) - 2000, snprintf(buf, sizeof buf, "%02u%02u%02u%1u%02u%02u%02u", year(t) - 2000,
month(t), day(t), weekday(t), hour(t), minute(t), second(t)); month(t), day(t), weekday(t), hour(t), minute(t), second(t));
@ -137,15 +140,18 @@ void if482_loop(void *pvParameters) {
TickType_t wakeTime; TickType_t wakeTime;
time_t t, tt; time_t t, tt;
const TickType_t shotTime = pdMS_TO_TICKS(IF482_OFFSET); const TickType_t timeOffset =
pdMS_TO_TICKS(IF482_OFFSET); // duration of telegram transmit
const TickType_t startTime = xTaskGetTickCount(); // now
// wait until begin of a new second to sync clock signal and absolute time // wait until begin of a new second
t = tt = now(); t = tt = now();
do { do {
tt = now(); tt = now();
} while (t == tt); } while (t == tt);
const TickType_t startOffset = xTaskGetTickCount(); // take timestamp at moment of start of new second
const TickType_t shotTime = xTaskGetTickCount() - startTime - timeOffset;
// task remains in blocked state until it is notified by isr // task remains in blocked state until it is notified by isr
for (;;) { for (;;) {
@ -155,13 +161,10 @@ void if482_loop(void *pvParameters) {
&wakeTime, // receives moment of call from isr &wakeTime, // receives moment of call from isr
portMAX_DELAY); // wait forever (missing error handling here...) portMAX_DELAY); // wait forever (missing error handling here...)
t = myTZ.toLocal(now()); // now we're synced to start of second tt and wait
wakeTime -= startOffset; // until it's time to start transmit telegram for tt+1
vTaskDelayUntil(&wakeTime, shotTime); // sets waketime to moment of shot
// now we're synced to start of second t and wait IF482.print(if482Telegram(now() + 1));
// until it's time to start transmit telegram for t+1
vTaskDelayUntil(&wakeTime, shotTime);
IF482.print(if482Telegram(t + 1));
} }
vTaskDelete(IF482Task); // shoud never be reached vTaskDelete(IF482Task); // shoud never be reached
} // if482_loop() } // if482_loop()

View File

@ -457,7 +457,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
setTime(*pUserUTCTime); setTime(*pUserUTCTime);
ESP_LOGI(TAG, "LoRaWAN network has set the system time"); ESP_LOGI(TAG, "LoRaWAN network has set the system time");
#ifdef HAS_RTC #ifdef HAS_RTC
if (!set_rtctime(*pUserUTCTime)) if (!set_rtctime(*pUserUTCTime)) // epoch time
ESP_LOGE(TAG, "RTC set time failure"); ESP_LOGE(TAG, "RTC set time failure");
#endif #endif
} }

View File

@ -429,7 +429,7 @@ void setup() {
else { else {
ESP_LOGI(TAG, "GPS has set the system time"); ESP_LOGI(TAG, "GPS has set the system time");
#ifdef HAS_RTC #ifdef HAS_RTC
if (!set_rtctime(now())) if (!set_rtctime(now())) // epoch time
ESP_LOGE(TAG, "RTC set time failure"); ESP_LOGE(TAG, "RTC set time failure");
#endif #endif
} }

View File

@ -57,16 +57,19 @@ error:
} // rtc_init() } // rtc_init()
int set_rtctime(RtcDateTime t) { int set_rtctime(time_t t) { // t is epoch time starting 1.1.1970
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
Rtc.SetDateTime(t); Rtc.SetDateTime(RtcDateTime(t));
I2C_MUTEX_UNLOCK(); // release i2c bus access I2C_MUTEX_UNLOCK(); // release i2c bus access
return 1; // success return 1; // success
} }
return 0; // failure return 0; // failure
} // set_rtctime() } // set_rtctime()
int set_rtctime(uint32_t t) { return set_rtctime(RtcDateTime(t)); }; int set_rtctime(uint32_t t) { // t is epoch seconds starting 1.1.1970
return set_rtctime(static_cast<time_t>(t));
// set_rtctime()
}
time_t get_rtctime(void) { time_t get_rtctime(void) {
// never call now() in this function, this would cause a recursion! // never call now() in this function, this would cause a recursion!