diff --git a/code/espurna/eeprom.ino b/code/espurna/eeprom.ino index 2ba9ab9c..3088f3fa 100644 --- a/code/espurna/eeprom.ino +++ b/code/espurna/eeprom.ino @@ -8,9 +8,14 @@ EEPROM MODULE // ----------------------------------------------------------------------------- +bool eepromBackup() { + DEBUG_MSG_P(PSTR("[EEPROM] Backing up data to last sector\n")); + return EEPROMr.backup(); +} + String eepromSectors() { String response; - for (uint32_t i = 0; i < EEPROMr.sectors(); i++) { + for (uint32_t i = 0; i < EEPROMr.pool(); i++) { if (i > 0) response = response + String(", "); response = response + String(EEPROMr.base() - i); } @@ -35,7 +40,7 @@ void _eepromInitCommands() { void eepromSetup() { #ifdef EEPROM_ROTATE_SECTORS - EEPROMr.sectors(EEPROM_ROTATE_SECTORS); + EEPROMr.pool(EEPROM_ROTATE_SECTORS); #else uint8_t sectors = 0; if (EEPROMr.last() > 1000) { // 4Mb boards @@ -45,7 +50,7 @@ void eepromSetup() { } else { sectors = 1; } - EEPROMr.sectors(sectors); + EEPROMr.pool(sectors); #endif EEPROMr.offset(EEPROM_ROTATE_DATA); diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index 2d730f43..172becd5 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -133,6 +133,9 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { } #endif + // Backup EEPROM data to last sector + eepromBackup(); + DEBUG_MSG_P(PSTR("[OTA] Downloading %s\n"), _ota_url); char buffer[strlen_P(OTA_REQUEST_TEMPLATE) + strlen(_ota_url) + strlen(_ota_host)]; snprintf_P(buffer, sizeof(buffer), OTA_REQUEST_TEMPLATE, _ota_url, _ota_host); @@ -140,7 +143,6 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { }, NULL); - #if ASYNC_TCP_SSL_ENABLED bool connected = _ota_client->connect(host, port, 443 == port); #else @@ -214,10 +216,16 @@ void otaSetup() { // ------------------------------------------------------------------------- ArduinoOTA.onStart([]() { + + // Backup EEPROM data to last sector + eepromBackup(); + DEBUG_MSG_P(PSTR("[OTA] Start\n")); + #if WEB_SUPPORT wsSend_P(PSTR("{\"message\": 2}")); #endif + }); ArduinoOTA.onEnd([]() { diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 6107fb59..2854c322 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -156,6 +156,7 @@ void _settingsKeysCommand() { unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize(); DEBUG_MSG_P(PSTR("Number of keys: %d\n"), keys.size()); + DEBUG_MSG_P(PSTR("Current EEPROM sector: %u\n"), EEPROMr.current()); DEBUG_MSG_P(PSTR("Free EEPROM: %d bytes (%d%%)\n"), freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE); } diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 7d849954..1213ae7d 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -240,7 +240,7 @@ void info() { DEBUG_MSG_P(PSTR("[INIT] Flash size (SDK): %8u bytes / %4d sectors\n"), ESP.getFlashChipSize(), sectors(ESP.getFlashChipSize())); DEBUG_MSG_P(PSTR("[INIT] Firmware size: %8u bytes / %4d sectors\n"), ESP.getSketchSize(), sectors(ESP.getSketchSize())); DEBUG_MSG_P(PSTR("[INIT] Max OTA size: %8u bytes / %4d sectors\n"), maxSketchSpace(), sectors(maxSketchSpace())); - DEBUG_MSG_P(PSTR("[INIT] EEPROM size: %8u bytes / %4d sectors\n"), EEPROMr.sectors() * SPI_FLASH_SEC_SIZE, EEPROMr.sectors()); + DEBUG_MSG_P(PSTR("[INIT] EEPROM size: %8u bytes / %4d sectors*\n"), EEPROMr.pool() * SPI_FLASH_SEC_SIZE, EEPROMr.pool()); DEBUG_MSG_P(PSTR("[INIT] Reserved space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE); DEBUG_MSG_P(PSTR("\n")); diff --git a/code/espurna/web.ino b/code/espurna/web.ino index 8deb4c4d..d840d604 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -231,7 +231,12 @@ void _onUpgrade(AsyncWebServerRequest *request) { } void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { + if (!index) { + + // Backup EEPROM data to last sector + eepromBackup(); + DEBUG_MSG_P(PSTR("[UPGRADE] Start: %s\n"), filename.c_str()); Update.runAsync(true); if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) { @@ -239,7 +244,9 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde Update.printError(DEBUG_PORT); #endif } + } + if (!Update.hasError()) { if (Update.write(data, len) != len) { #ifdef DEBUG_PORT @@ -247,6 +254,7 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde #endif } } + if (final) { if (Update.end(true)){ DEBUG_MSG_P(PSTR("[UPGRADE] Success: %u bytes\n"), index + len);