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.

227 lines
7.2 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. DEBUG MODULE
  3. Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #if DEBUG_SUPPORT
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #if DEBUG_UDP_SUPPORT
  9. #include <WiFiUdp.h>
  10. WiFiUDP udpDebug;
  11. #endif
  12. void debugSend(const char * format, ...) {
  13. char buffer[DEBUG_MESSAGE_MAX_LENGTH+1];
  14. va_list args;
  15. va_start(args, format);
  16. int len = ets_vsnprintf(buffer, DEBUG_MESSAGE_MAX_LENGTH, format, args);
  17. va_end(args);
  18. #if DEBUG_SERIAL_SUPPORT
  19. DEBUG_PORT.printf(buffer);
  20. if (len > DEBUG_MESSAGE_MAX_LENGTH) {
  21. DEBUG_PORT.printf(" (...)\n");
  22. }
  23. #endif
  24. #if DEBUG_UDP_SUPPORT
  25. #if SYSTEM_CHECK_ENABLED
  26. if (systemCheck()) {
  27. #endif
  28. udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
  29. udpDebug.write(buffer);
  30. if (len > DEBUG_MESSAGE_MAX_LENGTH) {
  31. udpDebug.write(" (...)\n");
  32. }
  33. udpDebug.endPacket();
  34. delay(1);
  35. #if SYSTEM_CHECK_ENABLED
  36. }
  37. #endif
  38. #endif
  39. #if DEBUG_TELNET_SUPPORT
  40. _telnetWrite(buffer, strlen(buffer));
  41. #endif
  42. }
  43. void debugSend_P(PGM_P format, ...) {
  44. char f[DEBUG_MESSAGE_MAX_LENGTH+1];
  45. memcpy_P(f, format, DEBUG_MESSAGE_MAX_LENGTH);
  46. char buffer[DEBUG_MESSAGE_MAX_LENGTH+1];
  47. va_list args;
  48. va_start(args, format);
  49. int len = ets_vsnprintf(buffer, DEBUG_MESSAGE_MAX_LENGTH, f, args);
  50. va_end(args);
  51. #if DEBUG_SERIAL_SUPPORT
  52. DEBUG_PORT.printf(buffer);
  53. if (len > DEBUG_MESSAGE_MAX_LENGTH) {
  54. DEBUG_PORT.printf(" (...)\n");
  55. }
  56. #endif
  57. #if DEBUG_UDP_SUPPORT
  58. #if SYSTEM_CHECK_ENABLED
  59. if (systemCheck()) {
  60. #endif
  61. udpDebug.beginPacket(DEBUG_UDP_IP, DEBUG_UDP_PORT);
  62. udpDebug.write(buffer);
  63. if (len > DEBUG_MESSAGE_MAX_LENGTH) {
  64. udpDebug.write(" (...)\n");
  65. }
  66. udpDebug.endPacket();
  67. delay(1);
  68. #if SYSTEM_CHECK_ENABLED
  69. }
  70. #endif
  71. #endif
  72. #if DEBUG_TELNET_SUPPORT
  73. _telnetWrite(buffer, strlen(buffer));
  74. #endif
  75. }
  76. // -----------------------------------------------------------------------------
  77. // Save crash info
  78. // Taken from krzychb EspSaveCrash
  79. // https://github.com/krzychb/EspSaveCrash
  80. // -----------------------------------------------------------------------------
  81. #define SAVE_CRASH_EEPROM_OFFSET 0x0100 // initial address for crash data
  82. /**
  83. * Structure of the single crash data set
  84. *
  85. * 1. Crash time
  86. * 2. Restart reason
  87. * 3. Exception cause
  88. * 4. epc1
  89. * 5. epc2
  90. * 6. epc3
  91. * 7. excvaddr
  92. * 8. depc
  93. * 9. adress of stack start
  94. * 10. adress of stack end
  95. * 11. stack trace bytes
  96. * ...
  97. */
  98. #define SAVE_CRASH_CRASH_TIME 0x00 // 4 bytes
  99. #define SAVE_CRASH_RESTART_REASON 0x04 // 1 byte
  100. #define SAVE_CRASH_EXCEPTION_CAUSE 0x05 // 1 byte
  101. #define SAVE_CRASH_EPC1 0x06 // 4 bytes
  102. #define SAVE_CRASH_EPC2 0x0A // 4 bytes
  103. #define SAVE_CRASH_EPC3 0x0E // 4 bytes
  104. #define SAVE_CRASH_EXCVADDR 0x12 // 4 bytes
  105. #define SAVE_CRASH_DEPC 0x16 // 4 bytes
  106. #define SAVE_CRASH_STACK_START 0x1A // 4 bytes
  107. #define SAVE_CRASH_STACK_END 0x1E // 4 bytes
  108. #define SAVE_CRASH_STACK_TRACE 0x22 // variable
  109. /**
  110. * Save crash information in EEPROM
  111. * This function is called automatically if ESP8266 suffers an exception
  112. * It should be kept quick / consise to be able to execute before hardware wdt may kick in
  113. */
  114. extern "C" void custom_crash_callback(struct rst_info * rst_info, uint32_t stack_start, uint32_t stack_end ) {
  115. // This method assumes EEPROM has already been initialized
  116. // which is the first thing ESPurna does
  117. // write crash time to EEPROM
  118. uint32_t crash_time = millis();
  119. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  120. // write reset info to EEPROM
  121. EEPROM.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON, rst_info->reason);
  122. EEPROM.write(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE, rst_info->exccause);
  123. // write epc1, epc2, epc3, excvaddr and depc to EEPROM
  124. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, rst_info->epc1);
  125. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, rst_info->epc2);
  126. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, rst_info->epc3);
  127. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, rst_info->excvaddr);
  128. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, rst_info->depc);
  129. // write stack start and end address to EEPROM
  130. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  131. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  132. // write stack trace to EEPROM
  133. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  134. for (uint32_t i = stack_start; i < stack_end; i++) {
  135. byte* byteValue = (byte*) i;
  136. EEPROM.write(current_address++, *byteValue);
  137. }
  138. EEPROM.commit();
  139. }
  140. /**
  141. * Clears crash info
  142. */
  143. void debugClearCrashInfo() {
  144. uint32_t crash_time = 0xFFFFFFFF;
  145. EEPROM.put(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  146. EEPROM.commit();
  147. }
  148. /**
  149. * Print out crash information that has been previusly saved in EEPROM
  150. */
  151. void debugDumpCrashInfo() {
  152. uint32_t crash_time;
  153. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_CRASH_TIME, crash_time);
  154. if ((crash_time == 0) || (crash_time == 0xFFFFFFFF)) {
  155. DEBUG_MSG_P(PSTR("[DEBUG] No crash info\n"));
  156. return;
  157. }
  158. DEBUG_MSG_P(PSTR("[DEBUG] Crash at %ld ms\n"), crash_time);
  159. DEBUG_MSG_P(PSTR("[DEBUG] Reason of restart: %d\n"), EEPROM.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_RESTART_REASON));
  160. DEBUG_MSG_P(PSTR("[DEBUG] Exception cause: %d\n"), EEPROM.read(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCEPTION_CAUSE));
  161. uint32_t epc1, epc2, epc3, excvaddr, depc;
  162. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC1, epc1);
  163. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC2, epc2);
  164. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EPC3, epc3);
  165. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_EXCVADDR, excvaddr);
  166. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_DEPC, depc);
  167. DEBUG_MSG_P(PSTR("[DEBUG] epc1=0x%08x epc2=0x%08x epc3=0x%08x\n"), epc1, epc2, epc3);
  168. DEBUG_MSG_P(PSTR("[DEBUG] excvaddr=0x%08x depc=0x%08x\n"), excvaddr, depc);
  169. uint32_t stack_start, stack_end;
  170. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_START, stack_start);
  171. EEPROM.get(SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_END, stack_end);
  172. DEBUG_MSG_P(PSTR("[DEBUG] >>>stack>>>\n[DEBUG] "));
  173. int16_t current_address = SAVE_CRASH_EEPROM_OFFSET + SAVE_CRASH_STACK_TRACE;
  174. int16_t stack_len = stack_end - stack_start;
  175. uint32_t stack_trace;
  176. for (int16_t i = 0; i < stack_len; i += 0x10) {
  177. DEBUG_MSG_P(PSTR("%08x: "), stack_start + i);
  178. for (byte j = 0; j < 4; j++) {
  179. EEPROM.get(current_address, stack_trace);
  180. DEBUG_MSG_P(PSTR("%08x "), stack_trace);
  181. current_address += 4;
  182. }
  183. DEBUG_MSG_P(PSTR("\n[DEBUG] "));
  184. }
  185. DEBUG_MSG_P(PSTR("<<<stack<<<\n"));
  186. }
  187. #endif // DEBUG_SUPPORT