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.

63 lines
1.4 KiB

  1. bool _rtcmem_status = false;
  2. void _rtcmemInit() {
  3. memset((uint32_t*)RTCMEM_ADDR, 0, sizeof(uint32_t) * RTCMEM_BLOCKS);
  4. Rtcmem->magic = RTCMEM_MAGIC;
  5. }
  6. // Treat memory as dirty on cold boot, hardware wdt reset and rst pin
  7. bool _rtcmemStatus() {
  8. bool readable;
  9. switch (systemResetReason()) {
  10. case REASON_EXT_SYS_RST:
  11. case REASON_WDT_RST:
  12. case REASON_DEFAULT_RST:
  13. readable = false;
  14. break;
  15. default:
  16. readable = true;
  17. }
  18. readable = readable and (RTCMEM_MAGIC == Rtcmem->magic);
  19. return readable;
  20. }
  21. #if TERMINAL_SUPPORT
  22. void _rtcmemInitCommands() {
  23. terminalRegisterCommand(F("RTCMEM.REINIT"), [](Embedis* e) {
  24. _rtcmemInit();
  25. });
  26. terminalRegisterCommand(F("RTCMEM.TEST"), [](Embedis* e) {
  27. });
  28. terminalRegisterCommand(F("RTCMEM.DUMP"), [](Embedis* e) {
  29. DEBUG_MSG_P(PSTR("[RTCMEM] status:%u blocks:%u addr:0x%p\n"),
  30. _rtcmemStatus(), RtcmemSize, Rtcmem);
  31. for (uint8_t block=0; block<RtcmemSize; ++block) {
  32. DEBUG_MSG_P(PSTR("[RTCMEM] %02u: %u\n"),
  33. block, reinterpret_cast<volatile uint32_t*>(RTCMEM_ADDR)[block]);
  34. }
  35. });
  36. }
  37. #endif
  38. bool rtcmemStatus() {
  39. return _rtcmem_status;
  40. }
  41. void rtcmemSetup() {
  42. _rtcmem_status = _rtcmemStatus();
  43. if (!_rtcmem_status) {
  44. _rtcmemInit();
  45. }
  46. #if TERMINAL_SUPPORT
  47. _rtcmemInitCommands();
  48. #endif
  49. }