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.

99 lines
2.0 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. terminalRegisterCommand(F("RTCMEM.DUMP"), [](Embedis* e) {
  38. DEBUG_MSG_P(PSTR("[RTCMEM] boot_status=%u status=%u blocks_used=%u\n"),
  39. _rtcmem_status, _rtcmemStatus(), RtcmemSize);
  40. String line;
  41. line.reserve(96);
  42. char buffer[16] = {0};
  43. auto addr = reinterpret_cast<volatile uint32_t*>(RTCMEM_ADDR);
  44. uint8_t block = 1;
  45. uint8_t offset = 0;
  46. uint8_t start = 0;
  47. do {
  48. offset = block - 1;
  49. snprintf(buffer, sizeof(buffer), "%08x ", *(addr + offset));
  50. line += buffer;
  51. if ((block % 8) == 0) {
  52. DEBUG_MSG_P(PSTR("%02u %p: %s\n"), start, addr+start, line.c_str());
  53. start = block;
  54. line = "";
  55. }
  56. ++block;
  57. } while (block<(RTCMEM_BLOCKS+1));
  58. });
  59. }
  60. #endif
  61. bool rtcmemStatus() {
  62. return _rtcmem_status;
  63. }
  64. void rtcmemSetup() {
  65. _rtcmem_status = _rtcmemStatus();
  66. if (!_rtcmem_status) {
  67. _rtcmemInit();
  68. }
  69. #if TERMINAL_SUPPORT
  70. _rtcmemInitCommands();
  71. #endif
  72. }