|
15 | 15 | * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>. |
16 | 16 | */ |
17 | 17 |
|
18 | | -#include <stdbool.h> |
19 | | -#include <stdint.h> |
20 | | -#include <stdarg.h> |
21 | | -#include <ctype.h> |
22 | | - |
23 | 18 | #include "build/debug.h" |
24 | | -#include "build/version.h" |
25 | | - |
26 | | -#include "drivers/serial.h" |
27 | | -#include "drivers/time.h" |
28 | | - |
29 | | -#include "common/printf.h" |
30 | | -#include "common/utils.h" |
31 | | - |
32 | | -#include "config/feature.h" |
33 | | - |
34 | | -#include "io/serial.h" |
35 | | - |
36 | | -#include "fc/config.h" |
37 | | - |
38 | | -#include "msp/msp.h" |
39 | | -#include "msp/msp_serial.h" |
40 | | -#include "msp/msp_protocol.h" |
41 | 19 |
|
42 | 20 | #ifdef DEBUG_SECTION_TIMES |
43 | 21 | timeUs_t sectionTimes[2][4]; |
44 | 22 | #endif |
45 | 23 |
|
46 | 24 | int32_t debug[DEBUG32_VALUE_COUNT]; |
47 | 25 | uint8_t debugMode; |
48 | | - |
49 | | -#if defined(USE_DEBUG_TRACE) |
50 | | - |
51 | | -#define DEBUG_TRACE_PREFIX "[%6d.%03d] " |
52 | | -#define DEBUG_TRACE_PREFIX_FORMATTED_SIZE 13 |
53 | | - |
54 | | -static serialPort_t * tracePort = NULL; |
55 | | -static mspPort_t * mspTracePort = NULL; |
56 | | - |
57 | | -void debugTraceInit(void) |
58 | | -{ |
59 | | - if (!feature(FEATURE_DEBUG_TRACE)) { |
60 | | - return; |
61 | | - } |
62 | | - |
63 | | - const serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_DEBUG_TRACE); |
64 | | - if (!portConfig) { |
65 | | - return; |
66 | | - } |
67 | | - |
68 | | - bool tracePortIsSharedWithMSP = false; |
69 | | - |
70 | | - if (determinePortSharing(portConfig, FUNCTION_DEBUG_TRACE) == PORTSHARING_SHARED) { |
71 | | - // We support sharing a DEBUG_TRACE port only with MSP |
72 | | - if (portConfig->functionMask != (FUNCTION_DEBUG_TRACE | FUNCTION_MSP)) { |
73 | | - return; |
74 | | - } |
75 | | - tracePortIsSharedWithMSP = true; |
76 | | - } |
77 | | - |
78 | | - // If the port is shared with MSP, reuse the port |
79 | | - if (tracePortIsSharedWithMSP) { |
80 | | - const serialPort_t *traceAndMspPort = findSharedSerialPort(FUNCTION_DEBUG_TRACE, FUNCTION_MSP); |
81 | | - if (!traceAndMspPort) { |
82 | | - return; |
83 | | - } |
84 | | - |
85 | | - mspTracePort = mspSerialPortFind(traceAndMspPort); |
86 | | - if (!mspTracePort) { |
87 | | - return; |
88 | | - } |
89 | | - |
90 | | - } else { |
91 | | - tracePort = openSerialPort(portConfig->identifier, FUNCTION_DEBUG_TRACE, NULL, NULL, baudRates[BAUD_921600], MODE_TX, SERIAL_NOT_INVERTED); |
92 | | - if (!tracePort) { |
93 | | - return; |
94 | | - } |
95 | | - } |
96 | | - // Initialization done |
97 | | - DEBUG_TRACE_SYNC("%s/%s %s %s / %s (%s)", |
98 | | - FC_FIRMWARE_NAME, |
99 | | - targetName, |
100 | | - FC_VERSION_STRING, |
101 | | - buildDate, |
102 | | - buildTime, |
103 | | - shortGitRevision |
104 | | - ); |
105 | | -} |
106 | | - |
107 | | -static void debugTracePutcp(void *p, char ch) |
108 | | -{ |
109 | | - *(*((char **) p))++ = ch; |
110 | | -} |
111 | | - |
112 | | -static void debugTracePrint(bool synchronous, const char *buf, size_t size) |
113 | | -{ |
114 | | - if (tracePort) { |
115 | | - // Send data via trace UART (if configured & connected - a safeguard against zombie VCP) |
116 | | - if (serialIsConnected(tracePort)) { |
117 | | - serialPrint(tracePort, buf); |
118 | | - if (synchronous) { |
119 | | - waitForSerialPortToFinishTransmitting(tracePort); |
120 | | - } |
121 | | - } |
122 | | - } else if (mspTracePort) { |
123 | | - mspSerialPushPort(MSP_DEBUGMSG, (uint8_t*)buf, size, mspTracePort, MSP_V2_NATIVE); |
124 | | - } |
125 | | -} |
126 | | - |
127 | | -static size_t debugTraceFormatPrefix(char *buf, const timeMs_t timeMs) |
128 | | -{ |
129 | | - // Write timestamp |
130 | | - return tfp_sprintf(buf, DEBUG_TRACE_PREFIX, timeMs / 1000, timeMs % 1000); |
131 | | -} |
132 | | - |
133 | | -void debugTracePrintf(bool synchronous, const char *format, ...) |
134 | | -{ |
135 | | - char buf[128]; |
136 | | - char *bufPtr; |
137 | | - int charCount; |
138 | | - |
139 | | - STATIC_ASSERT(MSP_PORT_OUTBUF_SIZE >= sizeof(buf), MSP_PORT_OUTBUF_SIZE_not_big_enough_for_debug_trace); |
140 | | - |
141 | | - if (!feature(FEATURE_DEBUG_TRACE)) |
142 | | - return; |
143 | | - |
144 | | - charCount = debugTraceFormatPrefix(buf, millis()); |
145 | | - bufPtr = &buf[charCount]; |
146 | | - |
147 | | - // Write message |
148 | | - va_list va; |
149 | | - va_start(va, format); |
150 | | - charCount += tfp_format(&bufPtr, debugTracePutcp, format, va); |
151 | | - debugTracePutcp(&bufPtr, '\n'); |
152 | | - debugTracePutcp(&bufPtr, 0); |
153 | | - charCount += 2; |
154 | | - va_end(va); |
155 | | - |
156 | | - debugTracePrint(synchronous, buf, charCount); |
157 | | -} |
158 | | - |
159 | | -void debugTracePrintBufferHex(bool synchronous, const void *buffer, size_t size) |
160 | | -{ |
161 | | - // Print lines of up to maxBytes bytes. We need 5 characters per byte |
162 | | - // 0xAB[space|\n] |
163 | | - const size_t charsPerByte = 5; |
164 | | - const size_t maxBytes = 8; |
165 | | - char buf[DEBUG_TRACE_PREFIX_FORMATTED_SIZE + charsPerByte * maxBytes + 1]; // +1 for the null terminator |
166 | | - size_t bufPos = DEBUG_TRACE_PREFIX_FORMATTED_SIZE; |
167 | | - const uint8_t *inputPtr = buffer; |
168 | | - |
169 | | - debugTraceFormatPrefix(buf, millis()); |
170 | | - |
171 | | - for (size_t ii = 0; ii < size; ii++) { |
172 | | - tfp_sprintf(buf + bufPos, "0x%02x ", inputPtr[ii]); |
173 | | - bufPos += charsPerByte; |
174 | | - if (bufPos == sizeof(buf)-1) { |
175 | | - buf[bufPos-1] = '\n'; |
176 | | - buf[bufPos] = '\0'; |
177 | | - debugTracePrint(synchronous, buf, bufPos + 1); |
178 | | - bufPos = DEBUG_TRACE_PREFIX_FORMATTED_SIZE; |
179 | | - } |
180 | | - } |
181 | | - |
182 | | - if (bufPos > DEBUG_TRACE_PREFIX_FORMATTED_SIZE) { |
183 | | - buf[bufPos-1] = '\n'; |
184 | | - buf[bufPos] = '\0'; |
185 | | - debugTracePrint(synchronous, buf, bufPos + 1); |
186 | | - } |
187 | | -} |
188 | | - |
189 | | - |
190 | | -#endif |
0 commit comments