diff --git a/code/espurna/eeprom.ino b/code/espurna/eeprom.ino index ceee1868..29e07427 100644 --- a/code/espurna/eeprom.ino +++ b/code/espurna/eeprom.ino @@ -8,6 +8,11 @@ EEPROM MODULE // ----------------------------------------------------------------------------- +bool _eeprom_commit = false; + +uint32_t _eeprom_commit_count = 0; +bool _eeprom_last_commit_result = false; + void eepromRotate(bool value) { // Enable/disable EEPROM rotation only if we are using more sectors than the // reserved by the memory layout @@ -34,15 +39,38 @@ String eepromSectors() { return response; } +bool _eepromCommit() { + _eeprom_commit_count++; + _eeprom_last_commit_result = EEPROMr.commit(); + return _eeprom_last_commit_result; +} + +void eepromCommit() { + _eeprom_commit = true; +} + #if TERMINAL_SUPPORT void _eepromInitCommands() { settingsRegisterCommand(F("EEPROM"), [](Embedis* e) { infoMemory("EEPROM", SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE - settingsSize()); + if (_eeprom_commit_count > 0) { + DEBUG_MSG_P(PSTR("[MAIN] Commits done: %lu\n"), _eeprom_commit_count); + DEBUG_MSG_P(PSTR("[MAIN] Last result: %s\n"), _eeprom_last_commit_result ? "OK" : "ERROR"); + } DEBUG_MSG_P(PSTR("+OK\n")); }); + settingsRegisterCommand(F("EEPROM.COMMIT"), [](Embedis* e) { + const bool res = _eepromCommit(); + if (res) { + DEBUG_MSG_P(PSTR("+OK\n")); + } else { + DEBUG_MSG_P(PSTR("-ERROR\n")); + } + }); + settingsRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) { EEPROMr.dump(settingsSerial()); DEBUG_MSG_P(PSTR("\n+OK\n")); @@ -69,6 +97,13 @@ void _eepromInitCommands() { // ----------------------------------------------------------------------------- +void eepromLoop() { + if (_eeprom_commit) { + _eepromCommit(); + _eeprom_commit = false; + } +} + void eepromSetup() { #ifdef EEPROM_ROTATE_SECTORS @@ -92,4 +127,6 @@ void eepromSetup() { _eepromInitCommands(); #endif + espurnaRegisterLoop(eepromLoop); + } diff --git a/code/espurna/mqtt.ino b/code/espurna/mqtt.ino index bbd0e3d9..eef83bac 100644 --- a/code/espurna/mqtt.ino +++ b/code/espurna/mqtt.ino @@ -291,7 +291,7 @@ unsigned long _mqttNextMessageId() { EEPROMr.write(EEPROM_MESSAGE_ID + 1, (id >> 16) & 0xFF); EEPROMr.write(EEPROM_MESSAGE_ID + 2, (id >> 8) & 0xFF); EEPROMr.write(EEPROM_MESSAGE_ID + 3, (id >> 0) & 0xFF); - saveSettings(); + eepromCommit(); } id++; diff --git a/code/espurna/relay.ino b/code/espurna/relay.ino index df01fbc4..acd3f36c 100644 --- a/code/espurna/relay.ino +++ b/code/espurna/relay.ino @@ -418,7 +418,7 @@ void relaySave(bool do_commit) { // We are actually enqueuing the commit so it will be // executed on the main loop, in case this is called from a callback - saveSettings(); + eepromCommit(); } @@ -548,7 +548,7 @@ void _relayBoot() { // Save if there is any relay in the RELAY_BOOT_TOGGLE mode if (trigger_save) { EEPROMr.write(EEPROM_RELAY_STATUS, mask); - saveSettings(); + eepromCommit(); } _relayRecursive = false; diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 53657cab..db65a52c 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -22,8 +22,6 @@ EmbedisWrap embedis(_serial, TERMINAL_BUFFER_SIZE); #endif // SERIAL_RX_ENABLED #endif // TERMINAL_SUPPORT -bool _settings_save = false; - // ----------------------------------------------------------------------------- // Reverse engineering EEPROM storage format // ----------------------------------------------------------------------------- @@ -310,7 +308,7 @@ void _settingsInitCommands() { #if not SETTINGS_AUTOSAVE settingsRegisterCommand(F("SAVE"), [](Embedis* e) { - _settings_save = true; + eepromCommit(); DEBUG_MSG_P(PSTR("\n+OK\n")); }); #endif @@ -367,7 +365,7 @@ bool hasSetting(const String& key, unsigned int index) { void saveSettings() { #if not SETTINGS_AUTOSAVE - _settings_save = true; + eepromCommit(); #endif } @@ -464,7 +462,7 @@ void settingsSetup() { [](size_t pos) -> char { return EEPROMr.read(pos); }, [](size_t pos, char value) { EEPROMr.write(pos, value); }, #if SETTINGS_AUTOSAVE - []() { _settings_save = true; } + []() { eepromCommit(); } #else []() {} #endif @@ -485,12 +483,6 @@ void settingsSetup() { void settingsLoop() { - if (_settings_save) { - EEPROMr.commit(); - _settings_save = false; - } - - #if TERMINAL_SUPPORT #if DEBUG_SERIAL_SUPPORT diff --git a/code/espurna/system.ino b/code/espurna/system.ino index c988c6cd..560cb84c 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -42,7 +42,7 @@ void systemCheck(bool stable) { } } EEPROMr.write(EEPROM_CRASH_COUNTER, value); - EEPROMr.commit(); + eepromCommit(); } bool systemCheck() {