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.

225 lines
6.9 KiB

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