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.

157 lines
5.6 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 bytes
  28. * ...
  29. */
  30. #define SAVE_CRASH_CRASH_TIME 0x00 // 4 bytes
  31. #define SAVE_CRASH_RESTART_REASON 0x04 // 1 byte
  32. #define SAVE_CRASH_EXCEPTION_CAUSE 0x05 // 1 byte
  33. #define SAVE_CRASH_EPC1 0x06 // 4 bytes
  34. #define SAVE_CRASH_EPC2 0x0A // 4 bytes
  35. #define SAVE_CRASH_EPC3 0x0E // 4 bytes
  36. #define SAVE_CRASH_EXCVADDR 0x12 // 4 bytes
  37. #define SAVE_CRASH_DEPC 0x16 // 4 bytes
  38. #define SAVE_CRASH_STACK_START 0x1A // 4 bytes
  39. #define SAVE_CRASH_STACK_END 0x1E // 4 bytes
  40. #define SAVE_CRASH_STACK_TRACE 0x22 // variable
  41. /**
  42. * Save crash information in EEPROM
  43. * This function is called automatically if ESP8266 suffers an exception
  44. * It should be kept quick / consise to be able to execute before hardware wdt may kick in
  45. */
  46. extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack_start, uint32_t stack_end ) {
  47. // Do not record crash data when resetting the board
  48. if (checkNeedsReset()) {
  49. return;
  50. }
  51. // This method assumes EEPROM has already been initialized
  52. // which is the first thing ESPurna does
  53. // write crash time to EEPROM
  54. uint32_t crash_time = millis();
  55. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  56. // write reset info to EEPROM
  57. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON, rst_info->reason);
  58. EEPROMr.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE, rst_info->exccause);
  59. // write epc1, epc2, epc3, excvaddr and depc to EEPROM
  60. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, rst_info->epc1);
  61. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, rst_info->epc2);
  62. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, rst_info->epc3);
  63. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, rst_info->excvaddr);
  64. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, rst_info->depc);
  65. // write stack start and end address to EEPROM
  66. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  67. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  68. // starting address of Embedis data plus reserve
  69. const uint16_t settings_start = SPI_FLASH_SEC_SIZE - settingsSize() - 0x10;
  70. // write stack trace to EEPROM and avoid overwriting settings
  71. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  72. for (uint32_t i = stack_start; i < stack_end; i++) {
  73. if (current_address >= settings_start) break;
  74. byte* byteValue = (byte*) i;
  75. EEPROMr.write(current_address++, *byteValue);
  76. }
  77. EEPROMr.commit();
  78. }
  79. /**
  80. * Clears crash info
  81. */
  82. void crashClear() {
  83. uint32_t crash_time = 0xFFFFFFFF;
  84. EEPROMr.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  85. EEPROMr.commit();
  86. }
  87. /**
  88. * Print out crash information that has been previusly saved in EEPROM
  89. */
  90. void crashDump() {
  91. uint32_t crash_time;
  92. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  93. if ((crash_time == 0) || (crash_time == 0xFFFFFFFF)) {
  94. DEBUG_MSG_P(PSTR("[DEBUG] No crash info\n"));
  95. return;
  96. }
  97. DEBUG_MSG_P(PSTR("[DEBUG] Latest crash was at %lu ms after boot\n"), crash_time);
  98. DEBUG_MSG_P(PSTR("[DEBUG] Reason of restart: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON));
  99. DEBUG_MSG_P(PSTR("[DEBUG] Exception cause: %u\n"), EEPROMr.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE));
  100. uint32_t epc1, epc2, epc3, excvaddr, depc;
  101. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, epc1);
  102. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, epc2);
  103. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3);
  104. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr);
  105. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc);
  106. DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3);
  107. DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc);
  108. uint32_t stack_start, stack_end;
  109. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  110. EEPROMr.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  111. DEBUG_MSG_P(PSTR("[DEBUG] sp=0x%08x end=0x%08x\n"), stack_start, stack_end);
  112. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  113. int16_t stack_len = stack_end - stack_start;
  114. uint32_t stack_trace;
  115. DEBUG_MSG_P(PSTR("[DEBUG] >>>stack>>>\n[DEBUG] "));
  116. for (int16_t i = 0; i < stack_len; i += 0x10) {
  117. DEBUG_MSG_P(PSTR("%08x: "), stack_start + i);
  118. for (byte j = 0; j < 4; j++) {
  119. EEPROMr.get(current_address, stack_trace);
  120. DEBUG_MSG_P(PSTR("%08x "), stack_trace);
  121. current_address += 4;
  122. }
  123. DEBUG_MSG_P(PSTR("\n[DEBUG] "));
  124. }
  125. DEBUG_MSG_P(PSTR("<<<stack<<<\n"));
  126. }
  127. #endif // DEBUG_SUPPORT