From 8de61598f61c141469f015ef6c8ddab093db9392 Mon Sep 17 00:00:00 2001 From: Max Prokhorov Date: Sat, 9 Mar 2019 16:57:52 +0300 Subject: [PATCH] Fix ESP.eraseConfig() when using Core 2.3.0 (#1595, #1616) --- code/espurna/config/prototypes.h | 1 + code/espurna/terminal.ino | 2 +- code/espurna/utils.ino | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index d9b5d435..e173626e 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -157,6 +157,7 @@ bool settingsRestoreJson(JsonObject& data); // ----------------------------------------------------------------------------- char * ltrim(char * s); void nice_delay(unsigned long ms); +bool inline eraseSDKConfig(); #define ARRAYINIT(type, name, ...) type name[] = {__VA_ARGS__}; diff --git a/code/espurna/terminal.ino b/code/espurna/terminal.ino index b87918eb..7ffa7710 100644 --- a/code/espurna/terminal.ino +++ b/code/espurna/terminal.ino @@ -96,7 +96,7 @@ void _terminalInitCommand() { terminalOK(); resetReason(CUSTOM_RESET_TERMINAL); _eepromCommit(); - ESP.eraseConfig(); + eraseSDKConfig(); *((int*) 0) = 0; // see https://github.com/esp8266/Arduino/issues/1494 }); diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 09e09e1b..d70eb606 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -578,6 +578,25 @@ bool checkNeedsReset() { return _reset_reason > 0; } +// Use fixed method for Core 2.3.0, because it erases only 2 out of 4 SDK-reserved sectors +// Fixed since 2.4.0, see: esp8266/core/esp8266/Esp.cpp: ESP::eraseConfig() +bool eraseSDKConfig() { + #if defined(ARDUINO_ESP8266_RELEASE_2_3_0) + const size_t cfgsize = 0x4000; + size_t cfgaddr = ESP.getFlashChipSize() - cfgsize; + + for (size_t offset = 0; offset < cfgsize; offset += SPI_FLASH_SEC_SIZE) { + if (!ESP.flashEraseSector((cfgaddr + offset) / SPI_FLASH_SEC_SIZE)) { + return false; + } + } + + return true; + #else + return ESP.eraseConfig(); + #endif +} + // ----------------------------------------------------------------------------- char * ltrim(char * s) {