From bc30b9e2f6b3507f7acba75df48e2b8a95d53f6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Mon, 28 May 2018 02:02:26 +0200 Subject: [PATCH] Specific EEPROM module --- code/espurna/config/general.h | 4 +++ code/espurna/config/prototypes.h | 1 + code/espurna/eeprom.ino | 58 ++++++++++++++++++++++++++++++++ code/espurna/espurna.ino | 5 ++- code/espurna/settings.ino | 13 +++---- code/espurna/system.ino | 12 ------- code/espurna/utils.ino | 7 ++-- 7 files changed, 76 insertions(+), 24 deletions(-) create mode 100644 code/espurna/eeprom.ino diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index 8a8723fc..3f4ef21a 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -142,6 +142,10 @@ //------------------------------------------------------------------------------ #define EEPROM_SIZE 4096 // EEPROM size in bytes +//#define EEPROM_RORATE_SECTORS 2 // Number of sectors to use for EEPROM rotation + // If not defined the firmware will use a number based + // on the number of available sectors + #define EEPROM_RELAY_STATUS 0 // Address for the relay status (1 byte) #define EEPROM_ENERGY_COUNT 1 // Address for the energy counter (4 bytes) #define EEPROM_CUSTOM_RESET 5 // Address for the reset reason (1 byte) diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 317d46f5..6db0a23b 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -73,6 +73,7 @@ void settingsGetJson(JsonObject& data); bool settingsRestoreJson(JsonObject& data); void settingsRegisterCommand(const String& name, void (*call)(Embedis*)); void settingsInject(void *data, size_t len); +Stream & settingsSerial(); // ----------------------------------------------------------------------------- // I2C diff --git a/code/espurna/eeprom.ino b/code/espurna/eeprom.ino new file mode 100644 index 00000000..2ba9ab9c --- /dev/null +++ b/code/espurna/eeprom.ino @@ -0,0 +1,58 @@ +/* + +EEPROM MODULE + +*/ + +#include + +// ----------------------------------------------------------------------------- + +String eepromSectors() { + String response; + for (uint32_t i = 0; i < EEPROMr.sectors(); i++) { + if (i > 0) response = response + String(", "); + response = response + String(EEPROMr.base() - i); + } + return response; +} + +#if TERMINAL_SUPPORT + +void _eepromInitCommands() { + + settingsRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) { + EEPROMr.dump(settingsSerial()); + DEBUG_MSG_P(PSTR("\n+OK\n")); + }); + +} + +#endif + +// ----------------------------------------------------------------------------- + +void eepromSetup() { + + #ifdef EEPROM_ROTATE_SECTORS + EEPROMr.sectors(EEPROM_ROTATE_SECTORS); + #else + uint8_t sectors = 0; + if (EEPROMr.last() > 1000) { // 4Mb boards + sectors = 4; + } else if (EEPROMr.last() > 250) { // 1Mb boards + sectors = 2; + } else { + sectors = 1; + } + EEPROMr.sectors(sectors); + #endif + + EEPROMr.offset(EEPROM_ROTATE_DATA); + EEPROMr.begin(EEPROM_SIZE); + + #if TERMINAL_SUPPORT + _eepromInitCommands(); + #endif + +} diff --git a/code/espurna/espurna.ino b/code/espurna/espurna.ino index ac2f217f..def70f6b 100644 --- a/code/espurna/espurna.ino +++ b/code/espurna/espurna.ino @@ -47,7 +47,10 @@ void setup() { debugSetup(); #endif - // Init EEPROM, Serial, SPIFFS and system check + // Init EEPROM + eepromSetup(); + + // Init Serial, SPIFFS and system check systemSetup(); // Init persistance and terminal features diff --git a/code/espurna/settings.ino b/code/espurna/settings.ino index 29576a50..6107fb59 100644 --- a/code/espurna/settings.ino +++ b/code/espurna/settings.ino @@ -167,10 +167,6 @@ void _settingsFactoryResetCommand() { EEPROMr.commit(); } -void _settingsDumpCommand() { - EEPROMr.dump(_serial); -} - void _settingsInitCommands() { #if DEBUG_SUPPORT @@ -186,11 +182,6 @@ void _settingsInitCommands() { DEBUG_MSG_P(PSTR("+OK\n")); }); - settingsRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) { - _settingsDumpCommand(); - DEBUG_MSG_P(PSTR("\n+OK\n")); - }); - settingsRegisterCommand(F("ERASE.CONFIG"), [](Embedis* e) { DEBUG_MSG_P(PSTR("+OK\n")); resetReason(CUSTOM_RESET_TERMINAL); @@ -343,6 +334,10 @@ void settingsInject(void *data, size_t len) { _serial.inject((char *) data, len); } +Stream & settingsSerial() { + return (Stream &) _serial; +} + size_t settingsMaxSize() { size_t size = EEPROM_SIZE; if (size > SPI_FLASH_SEC_SIZE) size = SPI_FLASH_SEC_SIZE; diff --git a/code/espurna/system.ino b/code/espurna/system.ino index 4c019254..e437bccc 100644 --- a/code/espurna/system.ino +++ b/code/espurna/system.ino @@ -148,18 +148,6 @@ void _systemSetupSpecificHardware() { void systemSetup() { - uint8_t sectors = 0; - if (EEPROMr.last() > 1000) { // 4Mb boards - sectors = 4; - } else if (EEPROMr.last() > 250) { // 1Mb boards - sectors = 2; - } else { - sectors = 1; - } - EEPROMr.offset(EEPROM_ROTATE_DATA); - EEPROMr.rotate(sectors); - EEPROMr.begin(EEPROM_SIZE); - #if SPIFFS_SUPPORT SPIFFS.begin(); #endif diff --git a/code/espurna/utils.ino b/code/espurna/utils.ino index 5bc955b2..7d849954 100644 --- a/code/espurna/utils.ino +++ b/code/espurna/utils.ino @@ -240,8 +240,11 @@ 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"), settingsMaxSize(), sectors(settingsMaxSize())); - DEBUG_MSG_P(PSTR("[INIT] Empty space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE); + 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] Reserved space: %8u bytes / 4 sectors\n"), 4 * SPI_FLASH_SEC_SIZE); + DEBUG_MSG_P(PSTR("\n")); + + DEBUG_MSG_P(PSTR("[INIT] EEPROM sectors: %s\n"), (char *) eepromSectors().c_str()); DEBUG_MSG_P(PSTR("\n")); // -------------------------------------------------------------------------