From 37ee4e6d8eefac4ccc69e7f807b57c576773fa6c Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 12 Jan 2021 17:18:56 +0100 Subject: [PATCH] Add optional file to most recent settings check --- tasmota/settings.ino | 45 ++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/tasmota/settings.ino b/tasmota/settings.ino index 2f9bad000..d61f66743 100644 --- a/tasmota/settings.ino +++ b/tasmota/settings.ino @@ -566,39 +566,48 @@ void SettingsSave(uint8_t rotate) void SettingsLoad(void) { #ifdef ESP8266 -#ifdef USE_UFILESYS - if (TfsLoadFile(TASM_FILE_SETTINGS, (uint8_t*)&Settings, sizeof(Settings))) { - settings_location = 1; - AddLog_P(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG "Loaded from File, " D_COUNT " %lu"), Settings.save_flag); - } else { -#endif - // Load configuration from eeprom or one of 7 slots below if first valid load does not stop_flash_rotate + // Load configuration from optional file and flash (eeprom and 7 additonal slots) if first valid load does not stop_flash_rotate // Activated with version 8.4.0.2 - Fails to read any config before version 6.6.0.11 settings_location = 0; uint32_t save_flag = 0; - uint32_t flash_location = FLASH_EEPROM_START; - for (uint32_t i = 0; i <= CFG_ROTATES; i++) { // Read all config pages in search of valid and latest - if (1 == i) { flash_location = SETTINGS_LOCATION; } - ESP.flashRead(flash_location * SPI_FLASH_SEC_SIZE, (uint32*)&Settings, sizeof(Settings)); + uint32_t max_slots = CFG_ROTATES +1; + uint32_t flash_location; + uint32_t slot = 1; +#ifdef USE_UFILESYS + if (TfsLoadFile(TASM_FILE_SETTINGS, (uint8_t*)&Settings, sizeof(Settings))) { + flash_location = 1; + slot = 0; + } +#endif + while (slot <= max_slots) { // Read all config pages in search of valid and latest + if (slot > 0) { + flash_location = (1 == slot) ? FLASH_EEPROM_START : (2 == slot) ? SETTINGS_LOCATION : flash_location -1; + ESP.flashRead(flash_location * SPI_FLASH_SEC_SIZE, (uint32*)&Settings, sizeof(Settings)); + } if ((Settings.cfg_crc32 != 0xFFFFFFFF) && (Settings.cfg_crc32 != 0x00000000) && (Settings.cfg_crc32 == GetSettingsCrc32())) { - if (Settings.save_flag > save_flag) { // Find latest page based on incrementing save_flag + if (Settings.save_flag > save_flag) { // Find latest page based on incrementing save_flag save_flag = Settings.save_flag; settings_location = flash_location; - if (Settings.flag.stop_flash_rotate && (0 == i)) { // Stop if only eeprom area should be used and it is valid + if (Settings.flag.stop_flash_rotate && (1 == slot)) { // Stop if only eeprom area should be used and it is valid break; } } } - flash_location--; + slot++; delay(1); } if (settings_location > 0) { - ESP.flashRead(settings_location * SPI_FLASH_SEC_SIZE, (uint32*)&Settings, sizeof(Settings)); - AddLog_P(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG D_LOADED_FROM_FLASH_AT " %X, " D_COUNT " %lu"), settings_location, Settings.save_flag); - } #ifdef USE_UFILESYS - } + if (1 == settings_location) { + TfsLoadFile(TASM_FILE_SETTINGS, (uint8_t*)&Settings, sizeof(Settings)); + AddLog_P(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG "Loaded from File, " D_COUNT " %lu"), Settings.save_flag); + } else #endif + { + ESP.flashRead(settings_location * SPI_FLASH_SEC_SIZE, (uint32*)&Settings, sizeof(Settings)); + AddLog_P(LOG_LEVEL_NONE, PSTR(D_LOG_CONFIG D_LOADED_FROM_FLASH_AT " %X, " D_COUNT " %lu"), settings_location, Settings.save_flag); + } + } #endif // ESP8266 #ifdef ESP32 uint32_t source = SettingsRead(&Settings, sizeof(Settings));