Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.1 KiB

  1. /*
  2. EEPROM MODULE
  3. */
  4. #include <EEPROM_Rotate.h>
  5. // -----------------------------------------------------------------------------
  6. bool eepromBackup() {
  7. // Backup data to last sector if we are using more sectors than the
  8. // reserved by the memory layout
  9. if (EEPROMr.size() > EEPROMr.reserved()) {
  10. DEBUG_MSG_P(PSTR("[EEPROM] Backing up data to last sector\n"));
  11. return EEPROMr.backup();
  12. }
  13. }
  14. String eepromSectors() {
  15. String response;
  16. for (uint32_t i = 0; i < EEPROMr.size(); i++) {
  17. if (i > 0) response = response + String(", ");
  18. response = response + String(EEPROMr.base() - i);
  19. }
  20. return response;
  21. }
  22. #if TERMINAL_SUPPORT
  23. void _eepromInitCommands() {
  24. settingsRegisterCommand(F("EEPROM.DUMP"), [](Embedis* e) {
  25. EEPROMr.dump(settingsSerial());
  26. DEBUG_MSG_P(PSTR("\n+OK\n"));
  27. });
  28. settingsRegisterCommand(F("FLASH.DUMP"), [](Embedis* e) {
  29. if (e->argc < 2) {
  30. DEBUG_MSG_P(PSTR("-ERROR: Wrong arguments\n"));
  31. return;
  32. }
  33. uint32_t sector = String(e->argv[1]).toInt();
  34. uint32_t max = ESP.getFlashChipSize() / SPI_FLASH_SEC_SIZE;
  35. if (sector >= max) {
  36. DEBUG_MSG_P(PSTR("-ERROR: Sector out of range\n"));
  37. return;
  38. }
  39. EEPROMr.dump(settingsSerial(), sector);
  40. DEBUG_MSG_P(PSTR("\n+OK\n"));
  41. });
  42. }
  43. #endif
  44. // -----------------------------------------------------------------------------
  45. void eepromSetup() {
  46. #ifdef EEPROM_ROTATE_SECTORS
  47. EEPROMr.size(EEPROM_ROTATE_SECTORS);
  48. #else
  49. // If the memory layout has more than one sector reserved use those,
  50. // otherwise calculate pool size based on memory size.
  51. if (EEPROMr.size() == 1) {
  52. if (EEPROMr.last() > 1000) { // 4Mb boards
  53. EEPROMr.size(4);
  54. } else if (EEPROMr.last() > 250) { // 1Mb boards
  55. EEPROMr.size(2);
  56. }
  57. }
  58. #endif
  59. EEPROMr.offset(EEPROM_ROTATE_DATA);
  60. EEPROMr.begin(EEPROM_SIZE);
  61. #if TERMINAL_SUPPORT
  62. _eepromInitCommands();
  63. #endif
  64. }