Xlite PPM/Heartbeat trainer input (#5881)
[opentx.git] / radio / src / debug.h
blobeb5520bb704b85f6372a11e0bf043b6250e68870
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef _DEBUG_H_
22 #define _DEBUG_H_
24 #include <inttypes.h>
25 #include "rtc.h"
26 #include "dump.h"
27 #if defined(CLI)
28 #include "cli.h"
29 #elif defined(CPUARM)
30 #include "serial.h"
31 #endif
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
37 uint8_t serial2TracesEnabled();
39 #if defined(SIMU)
40 typedef void (*traceCallbackFunc)(const char * text);
41 extern traceCallbackFunc traceCallback;
42 void debugPrintf(const char * format, ...);
43 #elif defined(SEMIHOSTING)
44 #include <stdio.h>
45 #define debugPrintf(...) printf(__VA_ARGS__)
46 #elif defined(DEBUG) && defined(CLI)
47 #define debugPrintf(...) do { if (cliTracesEnabled) serialPrintf(__VA_ARGS__); } while(0)
48 #elif defined(DEBUG) && defined(CPUARM) && defined(SERIAL2)
49 #define debugPrintf(...) do { serialPrintf(__VA_ARGS__); } while(0)
50 #else
51 #define debugPrintf(...)
52 #endif
54 #if defined(__cplusplus)
56 #endif
58 #define TRACE_NOCRLF(...) debugPrintf(__VA_ARGS__)
59 #define TRACE(f_, ...) debugPrintf((f_ "\r\n"), ##__VA_ARGS__)
60 #define DUMP(data, size) dump(data, size)
61 #define TRACE_DEBUG(...) debugPrintf("-D- " __VA_ARGS__)
62 #define TRACE_DEBUG_WP(...) debugPrintf(__VA_ARGS__)
63 #define TRACE_INFO(...) debugPrintf("-I- " __VA_ARGS__)
64 #define TRACE_INFO_WP(...) debugPrintf(__VA_ARGS__)
65 #define TRACE_WARNING(...) debugPrintf("-W- " __VA_ARGS__)
66 #define TRACE_WARNING_WP(...) debugPrintf(__VA_ARGS__)
67 #define TRACE_ERROR(...) debugPrintf("-E- " __VA_ARGS__)
69 #if defined(TRACE_LUA_INTERNALS_ENABLED)
70 #define TRACE_LUA_INTERNALS(f_, ...) debugPrintf(("[LUA INT] " f_ "\r\n"), ##__VA_ARGS__)
72 #define TRACE_LUA_INTERNALS_WITH_LINEINFO(L, f_, ...) do { \
73 lua_Debug ar; \
74 if (lua_getstack(L, 1, &ar)) { \
75 lua_getinfo(L, ">Sl", &ar); \
76 debugPrintf("%s:%d: ", ar.short_src, ar.currentline); \
77 } \
78 debugPrintf(("[LUA INT] " f_ "\r\n"), ##__VA_ARGS__); \
79 } while(0)
80 #else
81 #define TRACE_LUA_INTERNALS(...)
82 #define TRACE_LUA_INTERNALS_WITH_LINEINFO(L, f_, ...)
83 #endif
85 #if defined(DEBUG) && !defined(SIMU)
86 #define TIME_MEASURE_START(id) uint16_t t0 ## id = getTmr2MHz()
87 #define TIME_MEASURE_STOP(id) TRACE("Measure(" # id ") = %.1fus", float((uint16_t)(getTmr2MHz() - t0 ## id))/2)
88 #else
89 #define TIME_MEASURE_START(id)
90 #define TIME_MEASURE_STOP(id)
91 #endif
93 #if defined(DEBUG_TRACE_BUFFER)
95 #define TRACE_BUFFER_LEN 50
97 enum TraceEvent {
98 trace_start = 1,
100 sd_wait_ready = 10,
101 sd_rcvr_datablock,
102 sd_xmit_datablock_wait_ready,
103 sd_xmit_datablock_rcvr_spi,
104 sd_send_cmd_wait_ready,
105 sd_send_cmd_rcvr_spi,
107 sd_SD_ReadSectors = 16,
108 sd_disk_read,
109 sd_SD_WriteSectors,
110 sd_disk_write,
112 sd_disk_ioctl_CTRL_SYNC = 20,
113 sd_disk_ioctl_GET_SECTOR_COUNT,
114 sd_disk_ioctl_MMC_GET_CSD,
115 sd_disk_ioctl_MMC_GET_CID,
116 sd_disk_ioctl_MMC_GET_OCR,
117 sd_disk_ioctl_MMC_GET_SDSTAT_1,
118 sd_disk_ioctl_MMC_GET_SDSTAT_2,
119 sd_spi_reset,
120 sd_wait_read,
121 sd_wait_write,
122 sd_irq,
124 ff_f_write_validate = 40,
125 ff_f_write_flag,
126 ff_f_write_clst,
127 ff_f_write_sync_window,
128 ff_f_write_disk_write_dirty,
129 ff_f_write_clust2sect,
130 ff_f_write_disk_write,
131 ff_f_write_disk_read,
132 ff_f_write_move_window,
134 audio_getNextFilledBuffer_skip = 60,
137 struct TraceElement {
138 gtime_t time;
139 uint8_t time_ms;
140 enum TraceEvent event;
141 uint32_t data;
144 #if defined(__cplusplus)
145 extern "C" {
146 #endif
147 void trace_event(enum TraceEvent event, uint32_t data);
148 void trace_event_i(enum TraceEvent event, uint32_t data);
149 const struct TraceElement * getTraceElement(uint16_t idx);
150 void dumpTraceBuffer();
151 #if defined(__cplusplus)
153 #endif
155 #define TRACE_EVENT(condition, event, data) if (condition) { trace_event(event, data); }
156 #define TRACEI_EVENT(condition, event, data) if (condition) { trace_event_i(event, data); }
158 #else // #if defined(DEBUG_TRACE_BUFFER)
160 #define TRACE_EVENT(condition, event, data)
161 #define TRACEI_EVENT(condition, event, data)
163 #endif // #if defined(DEBUG_TRACE_BUFFER)
165 #if defined(TRACE_SD_CARD)
166 #define TRACE_SD_CARD_EVENT(condition, event, data) TRACE_EVENT(condition, event, data)
167 #else
168 #define TRACE_SD_CARD_EVENT(condition, event, data)
169 #endif
170 #if defined(TRACE_FATFS)
171 #define TRACE_FATFS_EVENT(condition, event, data) TRACE_EVENT(condition, event, data)
172 #else
173 #define TRACE_FATFS_EVENT(condition, event, data)
174 #endif
175 #if defined(TRACE_AUDIO)
176 #define TRACE_AUDIO_EVENT(condition, event, data) TRACE_EVENT(condition, event, data)
177 #define TRACEI_AUDIO_EVENT(condition, event, data) TRACEI_EVENT(condition, event, data)
178 #else
179 #define TRACE_AUDIO_EVENT(condition, event, data)
180 #define TRACEI_AUDIO_EVENT(condition, event, data)
181 #endif
184 #if defined(JITTER_MEASURE) && defined(__cplusplus)
186 template<class T> class JitterMeter {
187 public:
188 T min;
189 T max;
190 T measured;
192 JitterMeter() : min(~(T)0), max(0), measured(0) {};
194 void reset() {
195 // store mesaurement
196 measured = max - min;
197 //reset - begin new measurement
198 min = ~(T)0;
199 max = 0;
202 void measure(T value) {
203 if (value > max) max = value;
204 if (value < min) min = value;
207 T get() const {
208 return measured;
212 #endif // defined(JITTER_MEASURE)
215 #if defined(DEBUG_INTERRUPTS) && !defined(BOOT)
217 #if defined(PCBHORUS)
218 enum InterruptNames {
219 INT_TICK,
220 INT_1MS,
221 INT_SER2,
222 INT_TELEM_DMA,
223 INT_TELEM_USART,
224 INT_SDIO,
225 INT_SDIO_DMA,
226 INT_DMA2S7,
227 INT_TIM1CC,
228 INT_TIM2,
229 INT_TRAINER,
230 INT_BLUETOOTH,
231 INT_OTG_FS,
232 #if defined(DEBUG_USB_INTERRUPTS)
233 INT_OTG_FS_SPURIOUS,
234 INT_OTG_FS_OUT_EP,
235 INT_OTG_FS_IN_EP,
236 INT_OTG_FS_MODEMISMATCH,
237 INT_OTG_FS_WAKEUP,
238 INT_OTG_FS_SUSPEND,
239 INT_OTG_FS_SOF,
240 INT_OTG_FS_RX_STAT,
241 INT_OTG_FS_RESET,
242 INT_OTG_FS_ENUM,
243 INT_OTG_FS_INCOMPLETE_IN,
244 INT_OTG_FS_INCOMPLETE_OUT,
245 INT_OTG_FS_SESSION,
246 INT_OTG_FS_OTG,
247 INT_OTG_FS_RX_NOT_DEVICE,
248 #endif // #if defined(DEBUG_USB_INTERRUPTS)
249 INT_LAST
251 #elif defined(PCBTARANIS)
252 enum InterruptNames {
253 INT_TICK,
254 INT_5MS,
255 INT_AUDIO,
256 INT_BLUETOOTH,
257 INT_LCD,
258 INT_DMA2S7,
259 INT_TIM1CC,
260 INT_TIM1,
261 INT_TIM8,
262 INT_SER2,
263 INT_TELEM_DMA,
264 INT_TELEM_USART,
265 INT_TRAINER,
266 INT_HEARTBEAT,
267 INT_OTG_FS,
268 #if defined(DEBUG_USB_INTERRUPTS)
269 INT_OTG_FS_SPURIOUS,
270 INT_OTG_FS_OUT_EP,
271 INT_OTG_FS_IN_EP,
272 INT_OTG_FS_MODEMISMATCH,
273 INT_OTG_FS_WAKEUP,
274 INT_OTG_FS_SUSPEND,
275 INT_OTG_FS_SOF,
276 INT_OTG_FS_RX_STAT,
277 INT_OTG_FS_RESET,
278 INT_OTG_FS_ENUM,
279 INT_OTG_FS_INCOMPLETE_IN,
280 INT_OTG_FS_INCOMPLETE_OUT,
281 INT_OTG_FS_SESSION,
282 INT_OTG_FS_OTG,
283 INT_OTG_FS_RX_NOT_DEVICE,
284 #endif // #if defined(DEBUG_USB_INTERRUPTS)
285 INT_LAST
287 #endif
289 struct InterruptCounters
291 uint32_t cnt[INT_LAST];
292 uint32_t resetTime;
295 extern const char * const interruptNames[INT_LAST];
296 extern struct InterruptCounters interruptCounters;
298 #define DEBUG_INTERRUPT(int) (++interruptCounters.cnt[int])
300 #if defined(DEBUG_USB_INTERRUPTS)
301 #define DEBUG_USB_INTERRUPT(int) DEBUG_INTERRUPT(int)
302 #else
303 #define DEBUG_USB_INTERRUPT(int)
304 #endif
306 #else //#if defined(DEBUG_INTERRUPTS)
308 #define DEBUG_INTERRUPT(int)
309 #define DEBUG_USB_INTERRUPT(int)
311 #endif //#if defined(DEBUG_INTERRUPTS)
313 #if defined(DEBUG_TASKS)
315 #define DEBUG_TASKS_LOG_SIZE 512
317 // each 32bit is used as:
318 // top 8 bits: task id
319 // botom 24 bits: system tick counter
320 extern uint32_t taskSwitchLog[DEBUG_TASKS_LOG_SIZE];
321 extern uint16_t taskSwitchLogPos;
323 #if defined(__cplusplus)
324 extern "C" {
325 #endif
326 extern void CoTaskSwitchHook(uint8_t taskID);
327 #if defined(__cplusplus)
329 #endif
331 #endif // #if defined(DEBUG_TASKS)
334 #if defined(DEBUG_TIMERS)
336 #if defined(__cplusplus)
337 typedef uint32_t debug_timer_t;
339 class DebugTimer
341 private:
342 debug_timer_t min;
343 debug_timer_t max;
344 // debug_timer_t avg;
345 debug_timer_t last; //unit 1us
347 uint16_t _start_hiprec;
348 uint32_t _start_loprec;
350 void evalStats() {
351 if (min > last) min = last;
352 if (max < last) max = last;
353 //todo avg
356 public:
357 DebugTimer(): min(-1), max(0), /*avg(0),*/ last(0), _start_hiprec(0), _start_loprec(0) {};
359 void start();
360 void stop();
361 void sample() { stop(); start(); }
363 void reset() { min = -1; max = last = 0; }
365 debug_timer_t getMin() const { return min; }
366 debug_timer_t getMax() const { return max; }
367 debug_timer_t getLast() const { return last; }
370 enum DebugTimers {
371 debugTimerIntPulses,
372 debugTimerIntPulsesDuration,
373 debugTimerPer10ms,
374 debugTimerPer10msPeriod,
375 debugTimerRotEnc,
376 debugTimerHaptic,
377 debugTimerMixer,
378 debugTimerTelemetryWakeup,
379 debugTimerPerMain,
380 debugTimerPerMain1,
381 debugTimerGuiMain,
382 debugTimerLuaBg,
383 debugTimerLcdRefreshWait,
384 debugTimerLuaFg,
385 debugTimerLcdRefresh,
386 debugTimerMenus,
387 debugTimerMenuHandlers,
388 debugTimerVersion,
389 debugTimerSimpleMenu,
390 debugTimerDrawText,
391 debugTimerDrawText1,
393 debugTimerGetAdc,
394 debugTimerGetSwitches,
395 debugTimerEvalMixes,
396 debugTimerMixes10ms,
398 debugTimerAdcRead,
400 debugTimerMixerCalcToUsage,
401 debugTimerMixerIterval,
403 debugTimerAudioIterval,
404 debugTimerAudioDuration,
405 debugTimerAudioConsume,
407 DEBUG_TIMERS_COUNT
410 extern DebugTimer debugTimers[DEBUG_TIMERS_COUNT];
411 extern const char * const debugTimerNames[DEBUG_TIMERS_COUNT];
413 #endif // #if defined(__cplusplus)
415 #define DEBUG_TIMER_START(timer) debugTimers[timer].start()
416 #define DEBUG_TIMER_STOP(timer) debugTimers[timer].stop()
417 #define DEBUG_TIMER_SAMPLE(timer) debugTimers[timer].sample()
420 #else //#if defined(DEBUG_TIMERS)
422 #define DEBUG_TIMER_START(timer)
423 #define DEBUG_TIMER_STOP(timer)
424 #define DEBUG_TIMER_SAMPLE(timer)
426 #endif //#if defined(DEBUG_TIMERS)
428 #endif // _DEBUG_H_