diff --git a/include/globals.h b/include/globals.h index 8755b1f0..e11fb67e 100644 --- a/include/globals.h +++ b/include/globals.h @@ -101,7 +101,7 @@ extern uint8_t volatile channel; // wifi channel rotation counter extern uint16_t volatile macs_total, macs_wifi, macs_ble, batt_voltage; // display values extern hw_timer_t *sendCycle, *displaytimer; -extern SemaphoreHandle_t I2Caccess; +extern SemaphoreHandle_t I2Caccess, TimePulse; extern bool volatile BitsPending; extern std::set, Mallocator> macs; diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 4b921e5a..e20d2948 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -41,7 +41,7 @@ void doHousekeeping() { if ((millis() >= nextRTCTimeSync) && (timeStatus() == timeSet)) { nextRTCTimeSync = millis() + TIME_WRITE_INTERVAL_RTC * 60000; // set up next time sync period - if (!set_rtctime(now())) // epoch time + if (!set_rtctime(now())) // epoch time ESP_LOGE(TAG, "RTC set time failure"); else ESP_LOGI(TAG, "RTC time updated"); diff --git a/src/gpsread.cpp b/src/gpsread.cpp index 639ad725..4a46a482 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -90,16 +90,26 @@ time_t tmConvert_t(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, time_t get_gpstime(void) { // never call now() in this function, this would break this function // to use as SyncProvider due to recursive call to now() - time_t t = 0; + + time_t t; if ((gps.time.age() < 1500) && (gps.time.isValid())) { - t = 1 + tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(), - gps.time.hour(), gps.time.minute(), gps.time.second()); + t = tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(), + gps.time.hour(), gps.time.minute(), gps.time.second()); ESP_LOGD(TAG, "GPS time: %4d/%02d/%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t)); } else { ESP_LOGW(TAG, "GPS has no confident time"); + return 0; } - return t; + + // sync on top of next second bv timepulse + if (xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)) == pdTRUE) + return t; + else { + ESP_LOGW(TAG, "No GPS timepulse, thus time can't be synced by GPS"); + return 0; + } // failure + } // get_gpstime() // GPS serial feed FreeRTos Task diff --git a/src/main.cpp b/src/main.cpp index b2ce3336..64ec184f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,7 +73,7 @@ hw_timer_t *displaytimer = NULL; #endif TaskHandle_t irqHandlerTask; -SemaphoreHandle_t I2Caccess; +SemaphoreHandle_t I2Caccess, TimePulse; // container holding unique MAC address hashes with Memory Alloctor using PSRAM, // if present @@ -99,7 +99,12 @@ void setup() { I2Caccess = xSemaphoreCreateMutex(); // for access management of i2c bus if (I2Caccess) - xSemaphoreGive((I2Caccess)); // Flag the i2c bus available for use + xSemaphoreGive(I2Caccess); // Flag the i2c bus available for use + + TimePulse = xSemaphoreCreateMutex(); // for time pulse flip + if (TimePulse) + xSemaphoreTake(TimePulse, (TickType_t)10); + // Block TimePulse since we have no pulse yet // disable brownout detection #ifdef DISABLE_BROWNOUT diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 1f2c13da..8beb3b4c 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -191,5 +191,8 @@ time_t sync_clock(time_t t) { // timer void IRAM_ATTR CLOCKIRQ() { xTaskNotifyFromISR(ClockTask, xTaskGetTickCountFromISR(), eSetBits, NULL); +#ifdef GPS_INT + xSemaphoreGiveFromISR(TimePulse, NULL); +#endif portYIELD_FROM_ISR(); } \ No newline at end of file