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.

90 lines
2.3 KiB

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