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.

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