Browse Source

Update API, backup EEPROM to last sector before OTA upgrades

fastled^2
Xose Pérez 6 years ago
parent
commit
a83115cb1b
5 changed files with 27 additions and 5 deletions
  1. +8
    -3
      code/espurna/eeprom.ino
  2. +9
    -1
      code/espurna/ota.ino
  3. +1
    -0
      code/espurna/settings.ino
  4. +1
    -1
      code/espurna/utils.ino
  5. +8
    -0
      code/espurna/web.ino

+ 8
- 3
code/espurna/eeprom.ino View File

@ -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 eepromSectors() {
String response; 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(", "); if (i > 0) response = response + String(", ");
response = response + String(EEPROMr.base() - i); response = response + String(EEPROMr.base() - i);
} }
@ -35,7 +40,7 @@ void _eepromInitCommands() {
void eepromSetup() { void eepromSetup() {
#ifdef EEPROM_ROTATE_SECTORS #ifdef EEPROM_ROTATE_SECTORS
EEPROMr.sectors(EEPROM_ROTATE_SECTORS);
EEPROMr.pool(EEPROM_ROTATE_SECTORS);
#else #else
uint8_t sectors = 0; uint8_t sectors = 0;
if (EEPROMr.last() > 1000) { // 4Mb boards if (EEPROMr.last() > 1000) { // 4Mb boards
@ -45,7 +50,7 @@ void eepromSetup() {
} else { } else {
sectors = 1; sectors = 1;
} }
EEPROMr.sectors(sectors);
EEPROMr.pool(sectors);
#endif #endif
EEPROMr.offset(EEPROM_ROTATE_DATA); EEPROMr.offset(EEPROM_ROTATE_DATA);


+ 9
- 1
code/espurna/ota.ino View File

@ -133,6 +133,9 @@ void _otaFrom(const char * host, unsigned int port, const char * url) {
} }
#endif #endif
// Backup EEPROM data to last sector
eepromBackup();
DEBUG_MSG_P(PSTR("[OTA] Downloading %s\n"), _ota_url); DEBUG_MSG_P(PSTR("[OTA] Downloading %s\n"), _ota_url);
char buffer[strlen_P(OTA_REQUEST_TEMPLATE) + strlen(_ota_url) + strlen(_ota_host)]; 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); 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); }, NULL);
#if ASYNC_TCP_SSL_ENABLED #if ASYNC_TCP_SSL_ENABLED
bool connected = _ota_client->connect(host, port, 443 == port); bool connected = _ota_client->connect(host, port, 443 == port);
#else #else
@ -214,10 +216,16 @@ void otaSetup() {
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
ArduinoOTA.onStart([]() { ArduinoOTA.onStart([]() {
// Backup EEPROM data to last sector
eepromBackup();
DEBUG_MSG_P(PSTR("[OTA] Start\n")); DEBUG_MSG_P(PSTR("[OTA] Start\n"));
#if WEB_SUPPORT #if WEB_SUPPORT
wsSend_P(PSTR("{\"message\": 2}")); wsSend_P(PSTR("{\"message\": 2}"));
#endif #endif
}); });
ArduinoOTA.onEnd([]() { ArduinoOTA.onEnd([]() {


+ 1
- 0
code/espurna/settings.ino View File

@ -156,6 +156,7 @@ void _settingsKeysCommand() {
unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize(); unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize();
DEBUG_MSG_P(PSTR("Number of keys: %d\n"), keys.size()); 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); DEBUG_MSG_P(PSTR("Free EEPROM: %d bytes (%d%%)\n"), freeEEPROM, 100 * freeEEPROM / SPI_FLASH_SEC_SIZE);
} }


+ 1
- 1
code/espurna/utils.ino View File

@ -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] 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] 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] 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("[INIT] Reserved space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE);
DEBUG_MSG_P(PSTR("\n")); DEBUG_MSG_P(PSTR("\n"));


+ 8
- 0
code/espurna/web.ino View File

@ -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) { void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
if (!index) { if (!index) {
// Backup EEPROM data to last sector
eepromBackup();
DEBUG_MSG_P(PSTR("[UPGRADE] Start: %s\n"), filename.c_str()); DEBUG_MSG_P(PSTR("[UPGRADE] Start: %s\n"), filename.c_str());
Update.runAsync(true); Update.runAsync(true);
if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) { if (!Update.begin((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000)) {
@ -239,7 +244,9 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde
Update.printError(DEBUG_PORT); Update.printError(DEBUG_PORT);
#endif #endif
} }
} }
if (!Update.hasError()) { if (!Update.hasError()) {
if (Update.write(data, len) != len) { if (Update.write(data, len) != len) {
#ifdef DEBUG_PORT #ifdef DEBUG_PORT
@ -247,6 +254,7 @@ void _onUpgradeData(AsyncWebServerRequest *request, String filename, size_t inde
#endif #endif
} }
} }
if (final) { if (final) {
if (Update.end(true)){ if (Update.end(true)){
DEBUG_MSG_P(PSTR("[UPGRADE] Success: %u bytes\n"), index + len); DEBUG_MSG_P(PSTR("[UPGRADE] Success: %u bytes\n"), index + len);


Loading…
Cancel
Save