From cc4daa095433d90df00ae8f4405f338693525aac Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 21 Mar 2021 20:20:39 +0100 Subject: [PATCH] reset logic fixed (initialize RTC vars after OTA) --- include/configmanager.h | 1 + include/reset.h | 2 ++ src/configmanager.cpp | 7 +++++-- src/main.cpp | 4 +--- src/rcommand.cpp | 2 +- src/reset.cpp | 11 +++++++++-- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/include/configmanager.h b/include/configmanager.h index d196e6ab..95864994 100644 --- a/include/configmanager.h +++ b/include/configmanager.h @@ -2,6 +2,7 @@ #define _CONFIGMANAGER_H #include "globals.h" +#include "reset.h" #include void saveConfig(bool erase = false); diff --git a/include/reset.h b/include/reset.h index 653164c0..d55163b2 100644 --- a/include/reset.h +++ b/include/reset.h @@ -9,6 +9,7 @@ #include "display.h" #include "power.h" +void reset_rtc_vars(void); void do_reset(bool warmstart); void do_after_reset(void); void enter_deepsleep(const uint64_t wakeup_sec = 60, @@ -16,5 +17,6 @@ void enter_deepsleep(const uint64_t wakeup_sec = 60, unsigned long long uptime(void); extern RTC_NOINIT_ATTR runmode_t RTC_runmode; +extern RTC_NOINIT_ATTR uint32_t RTC_restarts; #endif // _RESET_H \ No newline at end of file diff --git a/src/configmanager.cpp b/src/configmanager.cpp index f115c484..5ef3c41f 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -48,7 +48,7 @@ static void defaultConfig(configData_t *myconfig) { myconfig->blescantime = BLESCANINTERVAL / 10; // BT channel scan cycle [seconds/100], default 1 (= 10ms) - myconfig->blescan = BLECOUNTER; // 0=disabled, 1=enabled + myconfig->blescan = BLECOUNTER; // 0=disabled, 1=enabled myconfig->wifiscan = WIFICOUNTER; // 0=disabled, 1=enabled myconfig->wifiant = 0; // 0=internal, 1=external (for LoPy/LoPy4) myconfig->macfilter = MACFILTER; // 0=disabled, 1=enabled @@ -159,4 +159,7 @@ int version_compare(const String v1, const String v2) { return 1; } -void eraseConfig(void) { saveConfig(true); } \ No newline at end of file +void eraseConfig(void) { + reset_rtc_vars(); + saveConfig(true); +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8f48c926..1decf901 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -140,6 +140,7 @@ void setup() { esp_log_level_set("*", ESP_LOG_NONE); #endif + // load device configuration from NVRAM and set runmode do_after_reset(); // print chip information on startup if in verbose mode after coldstart @@ -206,9 +207,6 @@ void setup() { strcat_P(features, " PMU"); #endif - // read (and initialize on first run) runtime settings from NVRAM - _ASSERT(loadConfig()); // includes initialize if necessary - // now that we are powered, we scan i2c bus for devices if (RTC_runmode == RUNMODE_POWERCYCLE) i2c_scan(); diff --git a/src/rcommand.cpp b/src/rcommand.cpp index c22ad0b7..7758ec5d 100644 --- a/src/rcommand.cpp +++ b/src/rcommand.cpp @@ -306,7 +306,7 @@ void get_status(uint8_t val[]) { payload.reset(); payload.addStatus(read_voltage(), (uint64_t)(uptime() / 1000ULL), temperatureRead(), getFreeRAM(), rtc_get_reset_reason(0), - rtc_get_reset_reason(1)); + (uint8_t)RTC_restarts); SendPayload(STATUSPORT); }; diff --git a/src/reset.cpp b/src/reset.cpp index 5e6165f7..350e33cb 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -18,6 +18,11 @@ RTC_DATA_ATTR unsigned long long RTC_millis = 0; timeval sleep_stop_time; +void reset_rtc_vars(void) { + RTC_runmode = RUNMODE_POWERCYCLE; + RTC_restarts = 0; +} + void do_reset(bool warmstart) { if (warmstart) { ESP_LOGI(TAG, "restarting device (warmstart)"); @@ -38,13 +43,15 @@ void do_after_reset(void) { struct timeval sleep_stop_time; uint64_t sleep_time_ms; + // read (and initialize on first run) runtime settings from NVRAM + loadConfig(); + switch (rtc_get_reset_reason(0)) { case POWERON_RESET: // 0x01 Vbat power on reset case RTCWDT_BROWN_OUT_RESET: // 0x0f Reset when the vdd voltage is not // stable - RTC_runmode = RUNMODE_POWERCYCLE; - RTC_restarts = 0; + reset_rtc_vars(); break; case SW_CPU_RESET: // 0x0c Software reset CPU