Addressing review comments
This commit is contained in:
+10
-5
@@ -10,8 +10,10 @@
|
|||||||
// ---- RTC-memory state (survives deep sleep, not cold boot) ----------------
|
// ---- RTC-memory state (survives deep sleep, not cold boot) ----------------
|
||||||
|
|
||||||
static constexpr uint32_t CLOCK_RTC_MAGIC = 0xC10C4B1D;
|
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 rtcClockMagic;
|
||||||
|
RTC_NOINIT_ATTR static uint32_t rtcClockFlags;
|
||||||
RTC_NOINIT_ATTR static time_t rtcEpoch; // last-known unix epoch
|
RTC_NOINIT_ATTR static time_t rtcEpoch; // last-known unix epoch
|
||||||
RTC_NOINIT_ATTR static uint64_t rtcLpTimeUs; // esp_clk_rtc_time() at capture
|
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; }
|
static bool rtcValid() { return rtcClockMagic == CLOCK_RTC_MAGIC && rtcEpoch > 0; }
|
||||||
|
|
||||||
/// Capture current time + LP timer into RTC memory, and epoch into NVS.
|
/// Capture current time + LP timer into RTC memory, and epoch into NVS.
|
||||||
static void capture() {
|
static void capture(bool lpValid) {
|
||||||
rtcEpoch = time(nullptr);
|
rtcEpoch = time(nullptr);
|
||||||
rtcLpTimeUs = esp_clk_rtc_time();
|
rtcLpTimeUs = esp_clk_rtc_time();
|
||||||
rtcClockMagic = CLOCK_RTC_MAGIC;
|
rtcClockMagic = CLOCK_RTC_MAGIC;
|
||||||
|
rtcClockFlags = lpValid ? CLOCK_RTC_FLAG_LP_VALID : 0;
|
||||||
nvsWrite(rtcEpoch);
|
nvsWrite(rtcEpoch);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,22 +86,23 @@ bool syncNtp() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
capture();
|
capture(false);
|
||||||
clockApproximate = false;
|
clockApproximate = false;
|
||||||
LOG_INF("CLK", "NTP synced, epoch %lld", (long long)rtcEpoch);
|
LOG_INF("CLK", "NTP synced, epoch %lld", (long long)rtcEpoch);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveBeforeSleep() {
|
void saveBeforeSleep(bool keepLpAlive) {
|
||||||
if (!isSynced()) {
|
if (!isSynced()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
capture();
|
capture(keepLpAlive);
|
||||||
LOG_DBG("CLK", "Saved epoch %lld before sleep", (long long)rtcEpoch);
|
LOG_DBG("CLK", "Saved epoch %lld before sleep", (long long)rtcEpoch);
|
||||||
}
|
}
|
||||||
|
|
||||||
void restore() {
|
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.
|
// RTC memory survived — we woke from deep sleep.
|
||||||
// Use the LP timer to compute how much time elapsed during sleep.
|
// Use the LP timer to compute how much time elapsed during sleep.
|
||||||
uint64_t lpNow = esp_clk_rtc_time();
|
uint64_t lpNow = esp_clk_rtc_time();
|
||||||
@@ -122,6 +126,7 @@ void restore() {
|
|||||||
rtcEpoch = epoch;
|
rtcEpoch = epoch;
|
||||||
rtcLpTimeUs = esp_clk_rtc_time();
|
rtcLpTimeUs = esp_clk_rtc_time();
|
||||||
rtcClockMagic = CLOCK_RTC_MAGIC;
|
rtcClockMagic = CLOCK_RTC_MAGIC;
|
||||||
|
rtcClockFlags = 0;
|
||||||
clockApproximate = true;
|
clockApproximate = true;
|
||||||
LOG_INF("CLK", "Restored from NVS, epoch %lld (no elapsed correction)", (long long)epoch);
|
LOG_INF("CLK", "Restored from NVS, epoch %lld (no elapsed correction)", (long long)epoch);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-2
@@ -31,8 +31,9 @@ namespace HalClock {
|
|||||||
bool syncNtp();
|
bool syncNtp();
|
||||||
|
|
||||||
/// Call just before deep sleep. Snapshots the current system time to RTC
|
/// Call just before deep sleep. Snapshots the current system time to RTC
|
||||||
/// memory and NVS so it can be restored on wake / cold boot.
|
/// memory and NVS so it can be restored on wake / cold boot. Pass true when
|
||||||
void saveBeforeSleep();
|
/// 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
|
/// 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
|
/// value. When RTC memory is valid (deep-sleep wake) and the LP timer was
|
||||||
|
|||||||
@@ -34,7 +34,13 @@ void SyncTimeActivity::onEnter() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
startActivityForResult(std::make_unique<WifiSelectionActivity>(renderer, mappedInput),
|
||||||
[this](const ActivityResult& result) { onWifiSelectionComplete(!result.isCancelled); });
|
[this](const ActivityResult& result) {
|
||||||
|
if (result.isCancelled) {
|
||||||
|
onWifiSelectionCancelled();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
onWifiSelectionComplete(true);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyncTimeActivity::onExit() {
|
void SyncTimeActivity::onExit() {
|
||||||
@@ -58,6 +64,8 @@ void SyncTimeActivity::onWifiSelectionComplete(bool success) {
|
|||||||
performSync();
|
performSync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SyncTimeActivity::onWifiSelectionCancelled() { finish(); }
|
||||||
|
|
||||||
void SyncTimeActivity::performSync() {
|
void SyncTimeActivity::performSync() {
|
||||||
bool ok = HalClock::syncNtp();
|
bool ok = HalClock::syncNtp();
|
||||||
wifiOff();
|
wifiOff();
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ class SyncTimeActivity final : public Activity {
|
|||||||
private:
|
private:
|
||||||
enum State { CONNECTING, SYNCING, SUCCESS, FAILED };
|
enum State { CONNECTING, SYNCING, SUCCESS, FAILED };
|
||||||
State state = CONNECTING;
|
State state = CONNECTING;
|
||||||
|
|
||||||
void onWifiSelectionComplete(bool success);
|
void onWifiSelectionComplete(bool success);
|
||||||
|
void onWifiSelectionCancelled();
|
||||||
void performSync();
|
void performSync();
|
||||||
};
|
};
|
||||||
|
|||||||
+1
-1
@@ -185,7 +185,7 @@ void waitForPowerRelease() {
|
|||||||
void enterDeepSleep() {
|
void enterDeepSleep() {
|
||||||
HalPowerManager::Lock powerLock; // Ensure we are at normal CPU frequency for sleep preparation
|
HalPowerManager::Lock powerLock; // Ensure we are at normal CPU frequency for sleep preparation
|
||||||
APP_STATE.lastSleepFromReader = activityManager.isReaderActivity();
|
APP_STATE.lastSleepFromReader = activityManager.isReaderActivity();
|
||||||
HalClock::saveBeforeSleep();
|
HalClock::saveBeforeSleep(SETTINGS.keepClockAlive);
|
||||||
APP_STATE.saveToFile();
|
APP_STATE.saveToFile();
|
||||||
|
|
||||||
activityManager.goToSleep();
|
activityManager.goToSleep();
|
||||||
|
|||||||
Reference in New Issue
Block a user