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.

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