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.

229 lines
7.1 KiB

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