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.

168 lines
6.4 KiB

  1. // -----------------------------------------------------------------------------
  2. // Save crash info
  3. // Taken from krzychb EspSaveCrash
  4. // https://github.com/krzychb/EspSaveCrash
  5. // -----------------------------------------------------------------------------
  6. #include "crash.h"
  7. #if DEBUG_SUPPORT
  8. #include <stdio.h>
  9. #include <stdarg.h>
  10. #include "system.h"
  11. #include "storage_eeprom.h"
  12. uint16_t _save_crash_stack_trace_max = SAVE_CRASH_STACK_TRACE_MAX;
  13. bool _save_crash_enabled = true;
  14. /**
  15. * Save crash information in EEPROM
  16. * This function is called automatically if ESP8266 suffers an exception
  17. * It should be kept quick / consise to be able to execute before hardware wdt may kick in
  18. * This method assumes EEPROM has already been initialized, which is the first thing ESPurna does
  19. */
  20. extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack_start, uint32_t stack_end ) {
  21. // Do not record crash data when resetting the board
  22. if (checkNeedsReset()) {
  23. return;
  24. }
  25. // Check if runtime setting disabled this callback
  26. if (!_save_crash_enabled) {
  27. return;
  28. }
  29. // write crash time to EEPROM, which we will later use as a marker that there was a crash
  30. uint32_t crash_time = millis();
  31. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  32. // rst_info::reason and ::exccause are uint32_t, but are holding small values
  33. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON, rst_info->reason);
  34. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE, rst_info->exccause);
  35. // write epc1, epc2, epc3, excvaddr and depc to EEPROM as uint32_t
  36. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, rst_info->epc1);
  37. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, rst_info->epc2);
  38. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, rst_info->epc3);
  39. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, rst_info->excvaddr);
  40. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, rst_info->depc);
  41. // EEPROM size is limited, write as little as possible.
  42. // we sometimes want to avoid big stack traces, e.g. if stack_end == 0x3fffffb0, we are in SYS context.
  43. // but still should get enough relevant info and it is possible to set needed size at build/runtime
  44. const uint16_t stack_size = constrain((stack_end - stack_start), 0, _save_crash_stack_trace_max);
  45. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  46. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  47. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_SIZE, stack_size);
  48. // starting EEPROM address of Embedis data plus reserve
  49. const uint16_t settings_start = (
  50. ((SPI_FLASH_SEC_SIZE - settingsSize() + 31) & -32) - 0x20);
  51. // write stack trace to EEPROM and avoid overwriting settings
  52. int16_t eeprom_addr = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  53. for (uint32_t* addr = (uint32_t*)stack_start; addr < (uint32_t*)(stack_start + stack_size); addr++) {
  54. if (eeprom_addr >= settings_start) break;
  55. EEPROMr.put(eeprom_addr, *addr);
  56. eeprom_addr += sizeof(uint32_t);
  57. }
  58. EEPROMr.commit();
  59. }
  60. /**
  61. * Clears crash info
  62. */
  63. void crashClear() {
  64. uint32_t crash_time = 0xFFFFFFFF;
  65. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  66. EEPROMr.commit();
  67. }
  68. /**
  69. * Print out crash information that has been previusly saved in EEPROM
  70. */
  71. void crashDump() {
  72. uint32_t crash_time;
  73. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  74. if ((crash_time == 0) || (crash_time == 0xFFFFFFFF)) {
  75. DEBUG_MSG_P(PSTR("[DEBUG] No crash info\n"));
  76. return;
  77. }
  78. DEBUG_MSG_P(PSTR("[DEBUG] Latest crash was at %lu ms after boot\n"), crash_time);
  79. DEBUG_MSG_P(PSTR("[DEBUG] Reason of restart: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON));
  80. DEBUG_MSG_P(PSTR("[DEBUG] Exception cause: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE));
  81. uint32_t epc1, epc2, epc3, excvaddr, depc;
  82. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, epc1);
  83. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, epc2);
  84. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3);
  85. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr);
  86. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc);
  87. DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3);
  88. DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc);
  89. uint32_t stack_start, stack_end;
  90. uint16_t stack_size;
  91. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  92. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  93. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_SIZE, stack_size);
  94. DEBUG_MSG_P(PSTR("sp=0x%08x end=0x%08x saved=0x%04x\n\n"), stack_start, stack_end, stack_size);
  95. if (0xFFFF == stack_size) return;
  96. stack_size = constrain(stack_size, 0, _save_crash_stack_trace_max);
  97. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  98. uint32_t stack_trace;
  99. DEBUG_MSG_P(PSTR("[DEBUG] >>>stack>>>\n[DEBUG] "));
  100. uint16_t offset = 0;
  101. do {
  102. DEBUG_MSG_P(PSTR("%08x: "), stack_start + offset);
  103. for (byte b = 0; b < 4; b++) {
  104. EEPROMr.get(current_address, stack_trace);
  105. DEBUG_MSG_P(PSTR("%08x "), stack_trace);
  106. current_address += 4;
  107. }
  108. DEBUG_MSG_P(PSTR("\n[DEBUG] "));
  109. } while ((offset < stack_size) && (offset += 0x10));
  110. DEBUG_MSG_P(PSTR("<<<stack<<<\n"));
  111. }
  112. void crashSetup() {
  113. #if TERMINAL_SUPPORT
  114. terminalRegisterCommand(F("CRASH"), [](Embedis* e) {
  115. crashDump();
  116. crashClear();
  117. terminalOK();
  118. });
  119. #endif
  120. // Minumum of 16 and align for column formatter in crashDump()
  121. // Maximum of flash sector size minus reserved space at the beginning
  122. const uint16_t trace_max = constrain(
  123. abs((getSetting("sysTraceMax", SAVE_CRASH_STACK_TRACE_MAX) + 15) & -16),
  124. 0, (SPI_FLASH_SEC_SIZE - crashUsedSpace())
  125. );
  126. if (trace_max != _save_crash_stack_trace_max) {
  127. setSetting("sysTraceMax", trace_max);
  128. }
  129. _save_crash_stack_trace_max = trace_max;
  130. _save_crash_enabled = getSetting("sysCrashSave", 1 == SAVE_CRASH_ENABLED);
  131. }
  132. #endif // DEBUG_SUPPORT