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.

101 lines
2.1 KiB

  1. /*
  2. RTMEM MODULE
  3. */
  4. bool _rtcmem_status = false;
  5. void _rtcmemErase() {
  6. auto ptr = reinterpret_cast<volatile uint32_t*>(RTCMEM_ADDR);
  7. const auto end = ptr + RTCMEM_BLOCKS;
  8. DEBUG_MSG_P(PSTR("[RTCMEM] Erasing start=%p end=%p\n"), ptr, end);
  9. do {
  10. *ptr = 0;
  11. } while (++ptr != end);
  12. }
  13. void _rtcmemInit() {
  14. _rtcmemErase();
  15. Rtcmem->magic = RTCMEM_MAGIC;
  16. }
  17. // Treat memory as dirty on cold boot, hardware wdt reset and rst pin
  18. bool _rtcmemStatus() {
  19. bool readable;
  20. switch (systemResetReason()) {
  21. case REASON_EXT_SYS_RST:
  22. case REASON_WDT_RST:
  23. case REASON_DEFAULT_RST:
  24. readable = false;
  25. break;
  26. default:
  27. readable = true;
  28. }
  29. readable = readable and (RTCMEM_MAGIC == Rtcmem->magic);
  30. return readable;
  31. }
  32. #if TERMINAL_SUPPORT
  33. void _rtcmemInitCommands() {
  34. terminalRegisterCommand(F("RTCMEM.REINIT"), [](Embedis* e) {
  35. _rtcmemInit();
  36. });
  37. #if DEBUG_SUPPORT
  38. terminalRegisterCommand(F("RTCMEM.DUMP"), [](Embedis* e) {
  39. DEBUG_MSG_P(PSTR("[RTCMEM] boot_status=%u status=%u blocks_used=%u\n"),
  40. _rtcmem_status, _rtcmemStatus(), RtcmemSize);
  41. String line;
  42. line.reserve(96);
  43. char buffer[16] = {0};
  44. auto addr = reinterpret_cast<volatile uint32_t*>(RTCMEM_ADDR);
  45. uint8_t block = 1;
  46. uint8_t offset = 0;
  47. uint8_t start = 0;
  48. do {
  49. offset = block - 1;
  50. snprintf(buffer, sizeof(buffer), "%08x ", *(addr + offset));
  51. line += buffer;
  52. if ((block % 8) == 0) {
  53. DEBUG_MSG_P(PSTR("%02u %p: %s\n"), start, addr+start, line.c_str());
  54. start = block;
  55. line = "";
  56. }
  57. ++block;
  58. } while (block<(RTCMEM_BLOCKS+1));
  59. });
  60. #endif
  61. }
  62. #endif
  63. bool rtcmemStatus() {
  64. return _rtcmem_status;
  65. }
  66. void rtcmemSetup() {
  67. _rtcmem_status = _rtcmemStatus();
  68. if (!_rtcmem_status) {
  69. _rtcmemInit();
  70. }
  71. #if TERMINAL_SUPPORT
  72. _rtcmemInitCommands();
  73. #endif
  74. }