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.

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