External module on X10 now tested OK
[opentx.git] / radio / src / debug.h
blobce4e9c9f8d1777d0afdf9d35274007c63dd9250f
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_TIM1CC,
259 INT_TIM1,
260 INT_TIM8,
261 INT_SER2,
262 INT_TELEM_DMA,
263 INT_TELEM_USART,
264 INT_TRAINER,
265 INT_OTG_FS,
266 #if defined(DEBUG_USB_INTERRUPTS)
267 INT_OTG_FS_SPURIOUS,
268 INT_OTG_FS_OUT_EP,
269 INT_OTG_FS_IN_EP,
270 INT_OTG_FS_MODEMISMATCH,
271 INT_OTG_FS_WAKEUP,
272 INT_OTG_FS_SUSPEND,
273 INT_OTG_FS_SOF,
274 INT_OTG_FS_RX_STAT,
275 INT_OTG_FS_RESET,
276 INT_OTG_FS_ENUM,
277 INT_OTG_FS_INCOMPLETE_IN,
278 INT_OTG_FS_INCOMPLETE_OUT,
279 INT_OTG_FS_SESSION,
280 INT_OTG_FS_OTG,
281 INT_OTG_FS_RX_NOT_DEVICE,
282 #endif // #if defined(DEBUG_USB_INTERRUPTS)
283 INT_LAST
285 #endif
287 struct InterruptCounters
289 uint32_t cnt[INT_LAST];
290 uint32_t resetTime;
293 extern const char * const interruptNames[INT_LAST];
294 extern struct InterruptCounters interruptCounters;
296 #define DEBUG_INTERRUPT(int) (++interruptCounters.cnt[int])
298 #if defined(DEBUG_USB_INTERRUPTS)
299 #define DEBUG_USB_INTERRUPT(int) DEBUG_INTERRUPT(int)
300 #else
301 #define DEBUG_USB_INTERRUPT(int)
302 #endif
304 #else //#if defined(DEBUG_INTERRUPTS)
306 #define DEBUG_INTERRUPT(int)
307 #define DEBUG_USB_INTERRUPT(int)
309 #endif //#if defined(DEBUG_INTERRUPTS)
311 #if defined(DEBUG_TASKS)
313 #define DEBUG_TASKS_LOG_SIZE 512
315 // each 32bit is used as:
316 // top 8 bits: task id
317 // botom 24 bits: system tick counter
318 extern uint32_t taskSwitchLog[DEBUG_TASKS_LOG_SIZE];
319 extern uint16_t taskSwitchLogPos;
321 #if defined(__cplusplus)
322 extern "C" {
323 #endif
324 extern void CoTaskSwitchHook(uint8_t taskID);
325 #if defined(__cplusplus)
327 #endif
329 #endif // #if defined(DEBUG_TASKS)
332 #if defined(DEBUG_TIMERS)
334 #if defined(__cplusplus)
335 typedef uint32_t debug_timer_t;
337 class DebugTimer
339 private:
340 debug_timer_t min;
341 debug_timer_t max;
342 // debug_timer_t avg;
343 debug_timer_t last; //unit 1us
345 uint16_t _start_hiprec;
346 uint32_t _start_loprec;
348 void evalStats() {
349 if (min > last) min = last;
350 if (max < last) max = last;
351 //todo avg
354 public:
355 DebugTimer(): min(-1), max(0), /*avg(0),*/ last(0), _start_hiprec(0), _start_loprec(0) {};
357 void start();
358 void stop();
359 void sample() { stop(); start(); }
361 void reset() { min = -1; max = last = 0; }
363 debug_timer_t getMin() const { return min; }
364 debug_timer_t getMax() const { return max; }
365 debug_timer_t getLast() const { return last; }
368 enum DebugTimers {
369 debugTimerIntPulses,
370 debugTimerIntPulsesDuration,
371 debugTimerPer10ms,
372 debugTimerRotEnc,
373 debugTimerHaptic,
374 debugTimerMixer,
375 debugTimerTelemetryWakeup,
376 debugTimerPerMain,
377 debugTimerPerMainPeriod,
378 debugTimerPerMain1,
379 debugTimerGuiMain,
380 debugTimerLuaBg,
381 debugTimerLcdRefreshWait,
382 debugTimerLuaFg,
383 debugTimerLcdRefresh,
384 debugTimerMenus,
385 debugTimerMenuHandlers,
386 debugTimerVersion,
387 debugTimerSimpleMenu,
388 debugTimerDrawText,
389 debugTimerDrawText1,
391 debugTimerGetAdc,
392 debugTimerGetSwitches,
393 debugTimerEvalMixes,
394 debugTimerMixes10ms,
396 debugTimerAdcRead,
398 debugTimerMixerCalcToUsage,
399 debugTimerMixerIterval,
401 debugTimerAudioIterval,
402 debugTimerAudioDuration,
403 debugTimerAudioConsume,
405 DEBUG_TIMERS_COUNT
408 extern DebugTimer debugTimers[DEBUG_TIMERS_COUNT];
409 extern const char * const debugTimerNames[DEBUG_TIMERS_COUNT];
411 #endif // #if defined(__cplusplus)
413 #define DEBUG_TIMER_START(timer) debugTimers[timer].start()
414 #define DEBUG_TIMER_STOP(timer) debugTimers[timer].stop()
415 #define DEBUG_TIMER_SAMPLE(timer) debugTimers[timer].sample()
418 #else //#if defined(DEBUG_TIMERS)
420 #define DEBUG_TIMER_START(timer)
421 #define DEBUG_TIMER_STOP(timer)
422 #define DEBUG_TIMER_SAMPLE(timer)
424 #endif //#if defined(DEBUG_TIMERS)
426 #endif // _DEBUG_H_