From 2d486e5edef2d3d61a458bb3a80ab2ab75a75767 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sat, 19 Dec 2020 12:27:07 +0100 Subject: [PATCH] persist uptime during deep sleep --- include/cyclic.h | 1 - include/reset.h | 2 ++ src/cyclic.cpp | 10 +--------- src/reset.cpp | 6 ++++-- 4 files changed, 7 insertions(+), 12 deletions(-) diff --git a/include/cyclic.h b/include/cyclic.h index 10509f1b..6bd9b2a7 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -15,7 +15,6 @@ extern Ticker cyclicTimer; void setCyclicIRQ(void); void doHousekeeping(void); -uint64_t _uptime(uint64_t diff); uint64_t uptime(void); void reset_counters(void); uint32_t getFreeRAM(); diff --git a/include/reset.h b/include/reset.h index 69735a77..f540ff62 100644 --- a/include/reset.h +++ b/include/reset.h @@ -13,4 +13,6 @@ void do_reset(bool warmstart); void do_after_reset(void); void enter_deepsleep(const uint64_t wakeup_sec, const gpio_num_t wakeup_gpio); +extern RTC_DATA_ATTR unsigned long RTC_millis; + #endif // _RESET_H \ No newline at end of file diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 7e12db65..b6dd1120 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -20,8 +20,6 @@ void setCyclicIRQ() { // do all housekeeping void doHousekeeping() { - // update uptime counter - uptime(); // check if update mode trigger switch was set if (RTC_runmode == RUNMODE_UPDATE) { // check battery status if we can before doing ota @@ -136,13 +134,7 @@ void doHousekeeping() { } // doHousekeeping() -uint64_t _uptime(uint64_t diff) { - static uint64_t offset = 0; - offset += diff; - return millis() + offset; -} - -uint64_t uptime(void){return _uptime(0);}; +uint64_t uptime() { return (RTC_millis + millis()); } uint32_t getFreeRAM() { #ifndef BOARD_HAS_PSRAM diff --git a/src/reset.cpp b/src/reset.cpp index 40fd9772..ade00195 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -11,6 +11,7 @@ static const char TAG[] = __FILE__; // variables keep its values after a wakeup from sleep RTC_DATA_ATTR runmode_t RTC_runmode = RUNMODE_POWERCYCLE; RTC_DATA_ATTR struct timeval RTC_sleep_start_time; +RTC_DATA_ATTR unsigned long RTC_millis = 0; timeval sleep_stop_time; const char *runmode[5] = {"powercycle", "normal", "wakeup", "update", "sleep"}; @@ -49,7 +50,7 @@ void do_after_reset(void) { (sleep_stop_time.tv_sec - RTC_sleep_start_time.tv_sec) * 1000 + (sleep_stop_time.tv_usec - RTC_sleep_start_time.tv_usec) / 1000; ESP_LOGI(TAG, "Time spent in deep sleep: %d ms", sleep_time_ms); - _uptime(sleep_time_ms); // increment uptime + RTC_millis += sleep_time_ms; // increment system monotonic time RTC_runmode = RUNMODE_WAKEUP; break; @@ -180,8 +181,9 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, esp_sleep_enable_ext1_wakeup(1ULL << wakeup_gpio, ESP_EXT1_WAKEUP_ALL_LOW); } - // time stamp sleep start time. Deep sleep. + // time stamp sleep start time and save system monotonic time. Deep sleep. gettimeofday(&RTC_sleep_start_time, NULL); + RTC_millis = millis(); ESP_LOGI(TAG, "Going to sleep, good bye."); esp_deep_sleep_start();