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.

194 lines
7.2 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. #define SAVE_CRASH_STACK_TRACE_MAX 0x80 // limit at 128 bytes (increment/decrement by 16)
  44. uint16_t _save_crash_stack_trace_max = SAVE_CRASH_STACK_TRACE_MAX;
  45. bool _save_crash_enabled = true;
  46. /**
  47. * Save crash information in EEPROM
  48. * This function is called automatically if ESP8266 suffers an exception
  49. * It should be kept quick / consise to be able to execute before hardware wdt may kick in
  50. * This method assumes EEPROM has already been initialized, which is the first thing ESPurna does
  51. */
  52. extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack_start, uint32_t stack_end ) {
  53. // Do not record crash data when resetting the board
  54. if (checkNeedsReset()) {
  55. return;
  56. }
  57. // Check if runtime setting disabled this callback
  58. if (!_save_crash_enabled) {
  59. return;
  60. }
  61. // write crash time to EEPROM, which we will later use as a marker that there was a crash
  62. uint32_t crash_time = millis();
  63. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  64. // rst_info::reason and ::exccause are uint32_t, but are holding small values
  65. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON, rst_info->reason);
  66. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE, rst_info->exccause);
  67. // write epc1, epc2, epc3, excvaddr and depc to EEPROM as uint32_t
  68. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, rst_info->epc1);
  69. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, rst_info->epc2);
  70. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, rst_info->epc3);
  71. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, rst_info->excvaddr);
  72. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, rst_info->depc);
  73. // EEPROM size is limited, write as little as possible.
  74. // we sometimes want to avoid big stack traces, e.g. if stack_end == 0x3fffffb0, we are in SYS context.
  75. // but still should get enough relevant info and it is possible to set needed size at build/runtime
  76. const uint16_t stack_size = constrain((stack_end - stack_start), 0, _save_crash_stack_trace_max);
  77. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  78. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  79. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_SIZE, stack_size);
  80. // starting EEPROM address of Embedis data plus reserve
  81. const uint16_t settings_start = (
  82. ((SPI_FLASH_SEC_SIZE - settingsSize() + 31) & -32) - 0x20);
  83. // write stack trace to EEPROM and avoid overwriting settings
  84. int16_t eeprom_addr = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  85. for (uint32_t* addr = (uint32_t*)stack_start; addr < (uint32_t*)(stack_start + stack_size); addr++) {
  86. if (eeprom_addr >= settings_start) break;
  87. EEPROMr.put(eeprom_addr, *addr);
  88. eeprom_addr += sizeof(uint32_t);
  89. }
  90. EEPROMr.commit();
  91. }
  92. /**
  93. * Clears crash info
  94. */
  95. void crashClear() {
  96. uint32_t crash_time = 0xFFFFFFFF;
  97. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  98. EEPROMr.commit();
  99. }
  100. /**
  101. * Print out crash information that has been previusly saved in EEPROM
  102. */
  103. void crashDump() {
  104. uint32_t crash_time;
  105. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  106. if ((crash_time == 0) || (crash_time == 0xFFFFFFFF)) {
  107. DEBUG_MSG_P(PSTR("[DEBUG] No crash info\n"));
  108. return;
  109. }
  110. DEBUG_MSG_P(PSTR("[DEBUG] Latest crash was at %lu ms after boot\n"), crash_time);
  111. DEBUG_MSG_P(PSTR("[DEBUG] Reason of restart: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON));
  112. DEBUG_MSG_P(PSTR("[DEBUG] Exception cause: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE));
  113. uint32_t epc1, epc2, epc3, excvaddr, depc;
  114. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, epc1);
  115. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, epc2);
  116. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3);
  117. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr);
  118. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc);
  119. DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3);
  120. DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc);
  121. uint32_t stack_start, stack_end;
  122. uint16_t stack_size;
  123. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  124. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  125. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_SIZE, stack_size);
  126. DEBUG_MSG_P(PSTR("sp=0x%08x end=0x%08x saved=0x%04x\n\n"), stack_start, stack_end, stack_size);
  127. if (0xFFFF == stack_size) return;
  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. for (int16_t i = 0; i < stack_size; i += 0x10) {
  132. DEBUG_MSG_P(PSTR("%08x: "), stack_start + i);
  133. for (byte j = 0; j < 4; j++) {
  134. EEPROMr.get(current_address, stack_trace);
  135. DEBUG_MSG_P(PSTR("%08x "), stack_trace);
  136. current_address += 4;
  137. }
  138. DEBUG_MSG_P(PSTR("\n[DEBUG] "));
  139. }
  140. DEBUG_MSG_P(PSTR("<<<stack<<<\n"));
  141. }
  142. void crashSetup() {
  143. #if TERMINAL_SUPPORT
  144. terminalRegisterCommand(F("CRASH"), [](Embedis* e) {
  145. crashDump();
  146. crashClear();
  147. terminalOK();
  148. });
  149. #endif
  150. // Minumum of 16 and align for column formatter in crashDump()
  151. _save_crash_stack_trace_max = getSetting("sysTraceMax", SAVE_CRASH_STACK_TRACE_MAX).toInt();
  152. _save_crash_stack_trace_max = (_save_crash_stack_trace_max + 15) & -16;
  153. setSetting("sysScTraceMax", _save_crash_stack_trace_max);
  154. _save_crash_enabled = getSetting("sysCrashSave", 1).toInt() == 1;
  155. }
  156. #endif // DEBUG_SUPPORT