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.

103 lines
2.2 KiB

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