-
Notifications
You must be signed in to change notification settings - Fork 65
/
debug.c
251 lines (200 loc) · 5.84 KB
/
debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Force Feedback Joystick
Basic debugging utilities.
This code is for Microsoft Sidewinder Force Feedback Pro joystick
with some room for additional extra controls.
Copyright 2012 Tero Loimuneva (tloimu [at] gmail [dot] com)
MIT License.
Permission to use, copy, modify, distribute, and sell this
software and its documentation for any purpose is hereby granted
without fee, provided that the above copyright notice appear in
all copies and that both that the copyright notice and this
permission notice and warranty disclaimer appear in supporting
documentation, and that the name of the author not be used in
advertising or publicity pertaining to distribution of the
software without specific, written prior permission.
The author disclaim all warranties with regard to this
software, including all implied warranties of merchantability
and fitness. In no event shall the author be liable for any
special, indirect or consequential damages or any damages
whatsoever resulting from loss of use, data or profits, whether
in an action of contract, negligence or other tortious action,
arising out of or in connection with the use or performance of
this software.
*/
#include "debug.h"
// Various debug target settings
const uint8_t DEBUG_TO_NONE = 0; // Disable sending debug data
const uint8_t DEBUG_TO_UART = 1; // Debug data sent to UART-out
const uint8_t DEBUG_TO_USB = 2; // Debug data sent to USB COM-port - enables basic level debugging
const uint8_t DEBUG_DETAIL = 4; // Include additional details to debug data
// Controls whether debug data contains data as hexadecimal ascii instead of as raw binary
#define DEBUG_DATA_AS_HEX
volatile uint8_t gDebugMode = 2; // set this higher if debugging e.g. at startup is needed
#ifdef DEBUG_ENABLE_USB
// Internal buffer for sending debug data to USB COM-port
volatile char debug_buffer[DEBUG_BUFFER_SIZE];
volatile uint16_t debug_buffer_used = 0;
#endif
void LogSendData(uint8_t *data, uint16_t len)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
uint16_t i = 0;
for (i = 0; i < len; i++)
{
#ifdef DEBUG_DATA_AS_HEX
static const char *hexmap = "0123456789ABCDEF";
LogSendByte( ' ' );
LogSendByte( hexmap[(data[i] >> 4)]);
LogSendByte( hexmap[(data[i] & 0x0F)]);
#else
LogSendByte(data[i]);
#endif
}
}
void LogText(const char *text)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
while (1)
{
char c = *text++;
if (c == 0)
break;
if (c == '\n')
LogSendByte('\r'); // CR+LF
LogSendByte(c);
}
}
void LogTextLf(const char *text)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
LogText(text);
LogSendByte('\r'); // CR+LF
LogSendByte('\n'); // CR+LF
}
void LogTextP(const char *text)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
while (1)
{
char c = pgm_read_byte(text++);
if (c == 0)
break;
if (c == '\n')
LogSendByte('\r'); // CR+LF
LogSendByte(c);
}
}
void LogTextLfP(const char *text)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
LogTextP(text);
LogSendByte('\r'); // CR+LF
LogSendByte('\n'); // CR+LF
}
void LogBinary(const void *data, uint16_t len)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
uint8_t temp = (uint8_t) (len & 0xFF);
if (temp > 0)
{
LogSendData((uint8_t*) data, temp);
}
}
void LogBinaryLf(const void *data, uint16_t len)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
uint8_t temp = (uint8_t) (len & 0xFF);
if (temp > 0)
{
LogSendData((uint8_t*) data, temp);
}
LogSendByte('\r'); // CR+LF
LogSendByte('\n'); // CR+LF
}
void LogData(const char *text, uint8_t reportId, const void *data, uint16_t len)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
LogText(text);
LogBinary(&reportId, 1);
LogBinary(data, len);
}
void LogDataLf(const char *text, uint8_t reportId, const void *data, uint16_t len)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
LogText(text);
LogBinary(&reportId, 1);
LogBinaryLf(data, len);
}
// Log all reports found in the given data (may have one or more)
void LogReport(const char *text, const uint16_t *reportSizeArray, uint8_t *data, uint16_t len)
{
if (gDebugMode == DEBUG_TO_NONE)
return;
LogTextP(text);
uint8_t *p = data;
while (p < data + len)
{
uint8_t replen = reportSizeArray[p[0]-1];
LogBinary(p, 1);
if (replen > 1)
LogBinary(&p[1], replen-1);
p += replen;
}
LogSendByte('\r'); // CR+LF
LogSendByte('\n'); // CR+LF
}
bool DoDebug(const uint8_t type)
{
return ((gDebugMode & type) == type);
}
// -------------------------------
// Send debug data to/from buffer
// to the chosen bus (USB or MIDI)
void LogSendByte(uint8_t data)
{
#ifdef DEBUG_ENABLE_USB
if (gDebugMode & DEBUG_TO_USB)
{
if (debug_buffer_used >= DEBUG_BUFFER_SIZE)
return; // overflow - discard
debug_buffer[debug_buffer_used++] = data;
}
#endif
#ifdef DEBUG_ENABLE_UART
if (gDebugMode & DEBUG_TO_UART)
{
// Wait if a byte is being transmitted
while((UCSR1A & (1<<UDRE1)) == 0);
UDR1 = data;
}
#endif // DEBUG_ENABLE_UART
}
void FlushDebugBuffer(void)
{
#ifdef DEBUG_ENABLE_USB
uint16_t len = debug_buffer_used; // use this to lessen chance of value changing in the middle of sending it - e.g. in interrupt
if (len == 0)
return;
debug_buffer_used = 0;
// Select the Serial Tx Endpoint
Endpoint_SelectEndpoint(CDC1_TX_EPNUM);
// Write the String to the Endpoint
Endpoint_Write_Stream_LE(&debug_buffer, len, NULL);
// Finalize the stream transfer to send the last packet
Endpoint_ClearIN();
// Wait until the endpoint is ready for another packet
Endpoint_WaitUntilReady();
// Send an empty packet to ensure that the host does not buffer data sent to it
Endpoint_ClearIN();
#endif // DEBUG_ENABLE_USB
}