-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdebug.c
More file actions
349 lines (288 loc) · 8.26 KB
/
Copy pathdebug.c
File metadata and controls
349 lines (288 loc) · 8.26 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#include <string.h>
#include "debug.h"
#include "trace.h"
#include "logger.h"
#include "macros/status_buffer.h"
#ifdef __ZEPHYR__
#include "logger.h"
#include "keyboard/oled/screens/screen_manager.h"
#include <zephyr/kernel.h>
#else
#include "segment_display.h"
#endif
#ifndef __ZEPHYR__
// SOFT_ASSERT reporter (see shared/atomicity.h): prints only the first failure,
// as soft asserts may fire from hot ISR paths.
void SoftAssertFailed(const char* file, int line)
{
LogS("!SOFT_ASSERT:%s:%d!\n", file, line);
}
// Main/MSP stack canary. main + every ISR share the 1 KB MSP (__StackLimit ..
// __StackTop). At boot we fill it - and the unused gap below it, down to
// __HeapLimit - with a pattern; the lowest word the stack ever clobbered is
// then its all-time low-water mark. Filling below __StackLimit means an
// overflow is measured, not just detected (it lands in the gap, which is
// otherwise unused; only past __HeapLimit does it corrupt the heap).
extern uint32_t __StackLimit[];
extern uint32_t __StackTop[];
extern uint32_t __HeapLimit[];
#define STACK_CANARY_PATTERN 0xC0DEBA5Eu
void Debug_InitStackCanary(void) {
uint32_t sp;
__asm volatile ("mov %0, sp" : "=r" (sp));
uint32_t* end = (uint32_t*)((sp - 32) & ~3u);
for (uint32_t* p = __HeapLimit; p < end && p < __StackTop; p++) {
*p = STACK_CANARY_PATTERN;
}
}
static uint32_t* stackLowWaterMark(void) {
uint32_t* p = __HeapLimit;
while (p < __StackTop && *p == STACK_CANARY_PATTERN) {
p++;
}
return p;
}
uint32_t Debug_StackSize(void) {
return (uint8_t*)__StackTop - (uint8_t*)__StackLimit;
}
uint32_t Debug_StackUsed(void) {
return (uint8_t*)__StackTop - (uint8_t*)stackLowWaterMark();
}
// Bytes still unused below the deepest the stack ever went. Negative means the
// stack grew past __StackLimit by that many bytes (into the unused gap; if it
// reaches -HEADROOM_GAP it has eaten into the heap).
int32_t Debug_StackHeadroom(void) {
return (uint8_t*)stackLowWaterMark() - (uint8_t*)__StackLimit;
}
#endif
#ifdef WATCHES
#include "timer.h"
#include "key_states.h"
#include <limits.h>
#include "macros/status_buffer.h"
#include "hid/transport.h"
#include "segment_display.h"
#include "logger.h"
uint8_t CurrentWatch = 0;
static uint16_t tickCount = 0;
static uint32_t lastWatch = 0;
static void showInt(int32_t n) {
#ifdef __ZEPHYR__
Log("W%i: %i\n", CurrentWatch, n);
#else
SegmentDisplay_SetInt(n, SegmentDisplaySlot_Debug);
#endif
}
static void showString(const char* str) {
#ifdef __ZEPHYR__
Log("W%i: %s\n", CurrentWatch, str);
#else
SegmentDisplay_SetText(strlen(str), str, SegmentDisplaySlot_Debug);
#endif
}
static void showFloat(float f) {
#ifdef __ZEPHYR__
uint16_t intPart = (uint16_t)f;
uint16_t fracPart = (uint16_t)((f - intPart) * 100); // Show two decimal places
Log("W%i: %u.%02u\n", CurrentWatch, intPart, fracPart);
#else
SegmentDisplay_SetFloat(f, SegmentDisplaySlot_Debug);
#endif
}
static void writeScancode(uint8_t b)
{
Macros_SetStatusChar(' ');
Macros_SetStatusNum(b);
}
void AddReportToStatusBuffer(char* dbgTag, hid_keyboard_report_t *report)
{
if (dbgTag != NULL && *dbgTag != '\0') {
Macros_SetStatusString(dbgTag, NULL);
Macros_SetStatusChar(' ');
}
Macros_SetStatusNum(report->modifiers);
UsbBasicKeyboard_ForeachScancode(report, &writeScancode);
Macros_SetStatusChar('\n');
}
void TriggerWatch(key_state_t *keyState)
{
int16_t key = (keyState - &KeyStates[SlotId_LeftKeyboardHalf][0]);
if (0 <= key && key <= 6) {
// Set the LED value to --- until next update occurs.
#ifdef __ZEPHYR__
if (DEBUG_CONSOLE) {
ScreenManager_ActivateScreen(ScreenId_Debug);
}
#endif
showString("---");
CurrentWatch = key;
tickCount = 0;
}
}
void WatchTime(uint8_t n)
{
static uint32_t lastUpdate = 0;
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showInt(Timer_GetCurrentTime() - lastUpdate);
lastWatch = Timer_GetCurrentTime();
}
lastUpdate = Timer_GetCurrentTime();
}
bool WatchCondition(uint8_t n)
{
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
lastWatch = Timer_GetCurrentTime();
return true;
}
return false;
}
void WatchTimeMicros(uint8_t n)
{
static uint32_t lastUpdate = 0;
static uint16_t i = 0;
i++;
if (i == 1000) {
showInt(Timer_GetCurrentTime() - lastUpdate);
lastUpdate = Timer_GetCurrentTime();
i = 0;
}
}
void WatchCallCount(uint8_t n)
{
tickCount++;
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showInt(tickCount);
lastWatch = Timer_GetCurrentTime();
}
}
void WatchValue(int v, uint8_t n)
{
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showInt(v);
lastWatch = Timer_GetCurrentTime();
}
}
void WatchString(char const *v, uint8_t n)
{
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showString(v);
lastWatch = Timer_GetCurrentTime();
}
}
void ShowString(char const *v, uint8_t n)
{
showString(v);
}
void ShowValue(int v, uint8_t n)
{
showInt(v);
}
void WatchValueMin(int v, uint8_t n)
{
static int m = 0;
if (v < m) {
m = v;
}
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showInt(m);
lastWatch = Timer_GetCurrentTime();
m = INT_MAX;
}
}
void WatchValueMax(int v, uint8_t n)
{
static int m = 0;
if (v > m) {
m = v;
}
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showInt(m);
lastWatch = Timer_GetCurrentTime();
m = INT_MIN;
}
}
void WatchFloatValue(float v, uint8_t n)
{
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showFloat(v);
lastWatch = Timer_GetCurrentTime();
}
}
void WatchFloatValueMin(float v, uint8_t n)
{
static float m = 0;
if (v < m) {
m = v;
}
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showFloat(m);
lastWatch = Timer_GetCurrentTime();
m = (float)INT_MAX;
}
}
void WatchFloatValueMax(float v, uint8_t n)
{
static float m = 0;
if (v > m) {
m = v;
}
if (Timer_GetCurrentTime() - lastWatch > WATCH_INTERVAL) {
showFloat(m);
lastWatch = Timer_GetCurrentTime();
m = (float)INT_MIN;
}
}
#ifdef __ZEPHYR__
static const char* getJustFilename(const char* filename) {
const char* p = filename;
const char* lastSlash = filename;
while (*p != '\0') {
if (*p == '/') {
lastSlash = p;
}
p++;
}
return ++lastSlash;
}
void WatchSemaforeTake(struct k_sem* sem, char const * label, uint8_t n) {
if (k_sem_take(sem, K_NO_WAIT) != 0) {
uint64_t startTimeUs = k_cyc_to_us_near64(k_cycle_get_32());
k_sem_take(sem, K_FOREVER);
uint64_t endTimeUs = k_cyc_to_us_near64(k_cycle_get_32());
const char* threadName = k_thread_name_get(k_current_get());
printk("Waited %lld us for semaphore %s in thread %s\n", endTimeUs - startTimeUs, getJustFilename(label), threadName);
}
}
#endif // __ZEPHYR__
#else
#endif
/**
* We observe whether a HID send was successful or not.
*
* If we compute reports too early we will see fails because of busy transports.
* If we compute reports too late, we will see no fails, but we will see lower number of successes (/attempts) and higher latency.
*
* Latency is measured per transport as an average of the time between the report is dispatched and its "sent state" callback is called.
* */
void Debug_RecordBleSendResult(int ret)
{
if (DEBUG_BLE_LATENCY_STATS) {
static uint32_t thisMs = 0;
static uint32_t succ = 0;
static uint32_t fail = 0;
uint32_t now = Timer_GetCurrentTime();
uint16_t latInt = (uint16_t)HidReportBleLatencyAvgMs;
uint16_t latFra = (uint16_t)((HidReportBleLatencyAvgMs - latInt) * 100); // Show two decimal places
if (now / 1024 != thisMs) {
LogU("BLE report send: succ=%u, fail=%u, latency=%d.%d\n", succ, fail, latInt, latFra);
thisMs = now / 1024;
succ = 0;
fail = 0;
}
if (ret == 0) {
succ++;
} else {
fail++;
}
}
}