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.

204 lines
7.5 KiB

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