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.

86 lines
2.2 KiB

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