diff --git a/lib/hal/HalClock.cpp b/lib/hal/HalClock.cpp index bd39fcf4..7edad342 100644 --- a/lib/hal/HalClock.cpp +++ b/lib/hal/HalClock.cpp @@ -10,8 +10,10 @@ // ---- RTC-memory state (survives deep sleep, not cold boot) ---------------- static constexpr uint32_t CLOCK_RTC_MAGIC = 0xC10C4B1D; +static constexpr uint32_t CLOCK_RTC_FLAG_LP_VALID = 0x00000001u; RTC_NOINIT_ATTR static uint32_t rtcClockMagic; +RTC_NOINIT_ATTR static uint32_t rtcClockFlags; RTC_NOINIT_ATTR static time_t rtcEpoch; // last-known unix epoch RTC_NOINIT_ATTR static uint64_t rtcLpTimeUs; // esp_clk_rtc_time() at capture @@ -51,10 +53,11 @@ static void setSystemClock(time_t epoch) { static bool rtcValid() { return rtcClockMagic == CLOCK_RTC_MAGIC && rtcEpoch > 0; } /// Capture current time + LP timer into RTC memory, and epoch into NVS. -static void capture() { +static void capture(bool lpValid) { rtcEpoch = time(nullptr); rtcLpTimeUs = esp_clk_rtc_time(); rtcClockMagic = CLOCK_RTC_MAGIC; + rtcClockFlags = lpValid ? CLOCK_RTC_FLAG_LP_VALID : 0; nvsWrite(rtcEpoch); } @@ -83,22 +86,23 @@ bool syncNtp() { return false; } - capture(); + capture(false); clockApproximate = false; LOG_INF("CLK", "NTP synced, epoch %lld", (long long)rtcEpoch); return true; } -void saveBeforeSleep() { +void saveBeforeSleep(bool keepLpAlive) { if (!isSynced()) { return; } - capture(); + capture(keepLpAlive); LOG_DBG("CLK", "Saved epoch %lld before sleep", (long long)rtcEpoch); } void restore() { - if (rtcValid()) { + const bool lpValid = (rtcClockFlags & CLOCK_RTC_FLAG_LP_VALID) != 0; + if (rtcValid() && lpValid) { // RTC memory survived — we woke from deep sleep. // Use the LP timer to compute how much time elapsed during sleep. uint64_t lpNow = esp_clk_rtc_time(); @@ -122,6 +126,7 @@ void restore() { rtcEpoch = epoch; rtcLpTimeUs = esp_clk_rtc_time(); rtcClockMagic = CLOCK_RTC_MAGIC; + rtcClockFlags = 0; clockApproximate = true; LOG_INF("CLK", "Restored from NVS, epoch %lld (no elapsed correction)", (long long)epoch); } diff --git a/lib/hal/HalClock.h b/lib/hal/HalClock.h index 68d4eb38..e5dd5d7a 100644 --- a/lib/hal/HalClock.h +++ b/lib/hal/HalClock.h @@ -31,8 +31,9 @@ namespace HalClock { bool syncNtp(); /// Call just before deep sleep. Snapshots the current system time to RTC -/// memory and NVS so it can be restored on wake / cold boot. -void saveBeforeSleep(); +/// memory and NVS so it can be restored on wake / cold boot. Pass true when +/// the LP timer is kept alive during sleep. +void saveBeforeSleep(bool keepLpAlive); /// Call on boot to seed the system clock from the best available stored /// value. When RTC memory is valid (deep-sleep wake) and the LP timer was diff --git a/src/activities/settings/SyncTimeActivity.cpp b/src/activities/settings/SyncTimeActivity.cpp index 7a99f9a4..66ca2683 100644 --- a/src/activities/settings/SyncTimeActivity.cpp +++ b/src/activities/settings/SyncTimeActivity.cpp @@ -34,7 +34,13 @@ void SyncTimeActivity::onEnter() { } startActivityForResult(std::make_unique(renderer, mappedInput), - [this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); }); + [this](const ActivityResult& result) { + if (result.isCancelled) { + onWifiSelectionCancelled(); + return; + } + onWifiSelectionComplete(true); + }); } void SyncTimeActivity::onExit() { @@ -58,6 +64,8 @@ void SyncTimeActivity::onWifiSelectionComplete(bool success) { performSync(); } +void SyncTimeActivity::onWifiSelectionCancelled() { finish(); } + void SyncTimeActivity::performSync() { bool ok = HalClock::syncNtp(); wifiOff(); diff --git a/src/activities/settings/SyncTimeActivity.h b/src/activities/settings/SyncTimeActivity.h index 5a25b3ec..43a419fb 100644 --- a/src/activities/settings/SyncTimeActivity.h +++ b/src/activities/settings/SyncTimeActivity.h @@ -15,7 +15,7 @@ class SyncTimeActivity final : public Activity { private: enum State { CONNECTING, SYNCING, SUCCESS, FAILED }; State state = CONNECTING; - void onWifiSelectionComplete(bool success); + void onWifiSelectionCancelled(); void performSync(); }; diff --git a/src/main.cpp b/src/main.cpp index 2e8cff70..fa02523a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -185,7 +185,7 @@ void waitForPowerRelease() { void enterDeepSleep() { HalPowerManager::Lock powerLock; // Ensure we are at normal CPU frequency for sleep preparation APP_STATE.lastSleepFromReader = activityManager.isReaderActivity(); - HalClock::saveBeforeSleep(); + HalClock::saveBeforeSleep(SETTINGS.keepClockAlive); APP_STATE.saveToFile(); activityManager.goToSleep();