From 0f3619238fc3fbba8d76178a7e487fd872aa77da Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 19 Apr 2020 13:08:04 +0200 Subject: [PATCH] Add ESP32 bootloop control --- .../src/esp8266toEsp32.cpp | 30 +++++++++++++++++-- tasmota/settings.h | 10 ++++--- tasmota/settings.ino | 16 +++++----- tasmota/support.ino | 2 -- 4 files changed, 42 insertions(+), 16 deletions(-) diff --git a/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp b/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp index 6635f6d4d..971cb2ec4 100644 --- a/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp +++ b/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.cpp @@ -65,14 +65,40 @@ uint32_t ESP_getBootVersion(void) return 1; } +// ESP32 RTC memory is kept ONLY on deep_sleep wake. Any other restart will erase RTC memory +// Current solution is using NVS hopig it survives many writes ;-) bool ESP_rtcUserMemoryWrite(uint32_t offset, uint32_t *data, size_t size) { - return false; + char sName[16]; + snprintf_P(sName, sizeof(sName), PSTR("rtc%d"), offset); + + nvs_handle handle; + noInterrupts(); + nvs_open("tasrtc", NVS_READWRITE, &handle); + nvs_set_blob(handle, sName, data, size); + nvs_commit(handle); + nvs_close(handle); + interrupts(); + + return true; } +// ESP32 RTC memory is kept ONLY on deep_sleep wake. Any other restart will erase RTC memory +// Current solution is using NVS hopig it survives many writes ;-) bool ESP_rtcUserMemoryRead(uint32_t offset, uint32_t *data, size_t size) { - return false; + char sName[16]; + snprintf_P(sName, sizeof(sName), PSTR("rtc%d"), offset); + + nvs_handle handle; + noInterrupts(); + nvs_open("tasrtc", NVS_READONLY, &handle); + size_t tsize = size; + nvs_get_blob(handle, sName, data, &tsize); + nvs_close(handle); + interrupts(); + + return true; } void ESP_reset() diff --git a/tasmota/settings.h b/tasmota/settings.h index e1b63beff..e5b1c9f7a 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -545,13 +545,14 @@ struct SYSCFG { uint32_t cfg_crc32; // FFC } Settings; -struct RTCRBT { +typedef struct { uint16_t valid; // 280 (RTC memory offset 100 - sizeof(RTCRBT)) uint8_t fast_reboot_count; // 282 uint8_t free_003[1]; // 283 -} RtcReboot; +} TRtcReboot; +TRtcReboot RtcReboot; -struct RTCMEM { +typedef struct { uint16_t valid; // 290 (RTC memory offset 100) uint8_t oswatch_blocked_loop; // 292 uint8_t ota_loader; // 293 @@ -567,7 +568,8 @@ struct RTCMEM { uint8_t free_022[22]; // 2D6 // 2EC - 2FF free locations -} RtcSettings; +} TRtcSettings; +TRtcSettings RtcSettings; struct TIME_T { uint8_t second; diff --git a/tasmota/settings.ino b/tasmota/settings.ino index 1769b676d..62fda63d8 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -30,7 +30,7 @@ uint32_t GetRtcSettingsCrc(void) uint32_t crc = 0; uint8_t *bytes = (uint8_t*)&RtcSettings; - for (uint32_t i = 0; i < sizeof(RTCMEM); i++) { + for (uint32_t i = 0; i < sizeof(RtcSettings); i++) { crc += bytes[i]*(i+1); } return crc; @@ -40,16 +40,16 @@ void RtcSettingsSave(void) { if (GetRtcSettingsCrc() != rtc_settings_crc) { RtcSettings.valid = RTC_MEM_VALID; - ESP_rtcUserMemoryWrite(100, (uint32_t*)&RtcSettings, sizeof(RTCMEM)); + ESP_rtcUserMemoryWrite(100, (uint32_t*)&RtcSettings, sizeof(RtcSettings)); rtc_settings_crc = GetRtcSettingsCrc(); } } void RtcSettingsLoad(void) { - ESP_rtcUserMemoryRead(100, (uint32_t*)&RtcSettings, sizeof(RTCMEM)); // 0x290 + ESP_rtcUserMemoryRead(100, (uint32_t*)&RtcSettings, sizeof(RtcSettings)); // 0x290 if (RtcSettings.valid != RTC_MEM_VALID) { - memset(&RtcSettings, 0, sizeof(RTCMEM)); + memset(&RtcSettings, 0, sizeof(RtcSettings)); RtcSettings.valid = RTC_MEM_VALID; RtcSettings.energy_kWhtoday = Settings.energy_kWhtoday; RtcSettings.energy_kWhtotal = Settings.energy_kWhtotal; @@ -77,7 +77,7 @@ uint32_t GetRtcRebootCrc(void) uint32_t crc = 0; uint8_t *bytes = (uint8_t*)&RtcReboot; - for (uint32_t i = 0; i < sizeof(RTCRBT); i++) { + for (uint32_t i = 0; i < sizeof(RtcReboot); i++) { crc += bytes[i]*(i+1); } return crc; @@ -87,7 +87,7 @@ void RtcRebootSave(void) { if (GetRtcRebootCrc() != rtc_reboot_crc) { RtcReboot.valid = RTC_MEM_VALID; - ESP_rtcUserMemoryWrite(100 - sizeof(RTCRBT), (uint32_t*)&RtcReboot, sizeof(RTCRBT)); + ESP_rtcUserMemoryWrite(100 - sizeof(RtcReboot), (uint32_t*)&RtcReboot, sizeof(RtcReboot)); rtc_reboot_crc = GetRtcRebootCrc(); } } @@ -100,9 +100,9 @@ void RtcRebootReset(void) void RtcRebootLoad(void) { - ESP_rtcUserMemoryRead(100 - sizeof(RTCRBT), (uint32_t*)&RtcReboot, sizeof(RTCRBT)); // 0x280 + ESP_rtcUserMemoryRead(100 - sizeof(RtcReboot), (uint32_t*)&RtcReboot, sizeof(RtcReboot)); // 0x280 if (RtcReboot.valid != RTC_MEM_VALID) { - memset(&RtcReboot, 0, sizeof(RTCRBT)); + memset(&RtcReboot, 0, sizeof(RtcReboot)); RtcReboot.valid = RTC_MEM_VALID; // RtcReboot.fast_reboot_count = 0; // Explicit by memset RtcRebootSave(); diff --git a/tasmota/support.ino b/tasmota/support.ino index 4b9961e94..5e7764bf9 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1208,14 +1208,12 @@ uint8_t ValidPin(uint32_t pin, uint32_t gpio) return GPIO_NONE; // Disable flash pins GPIO6, GPIO7, GPIO8 and GPIO11 } -#ifdef ESP8266 // if (!is_8285 && !Settings.flag3.user_esp8285_enable) { // SetOption51 - Enable ESP8285 user GPIO's if ((WEMOS == Settings.module) && !Settings.flag3.user_esp8285_enable) { // SetOption51 - Enable ESP8285 user GPIO's if ((pin == 9) || (pin == 10)) { return GPIO_NONE; // Disable possible flash GPIO9 and GPIO10 } } -#endif // ESP8266 return gpio; }