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.
33 #if defined(__cplusplus)
37 uint8_t serial2TracesEnabled();
40 typedef void (*traceCallbackFunc
)(const char * text
);
41 extern traceCallbackFunc traceCallback
;
42 void debugPrintf(const char * format
, ...);
43 #elif defined(SEMIHOSTING)
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)
51 #define debugPrintf(...)
54 #if defined(__cplusplus)
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 { \
74 if (lua_getstack(L, 1, &ar)) { \
75 lua_getinfo(L, ">Sl", &ar); \
76 debugPrintf("%s:%d: ", ar.short_src, ar.currentline); \
78 debugPrintf(("[LUA INT] " f_ "\r\n"), ##__VA_ARGS__); \
81 #define TRACE_LUA_INTERNALS(...)
82 #define TRACE_LUA_INTERNALS_WITH_LINEINFO(L, f_, ...)
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)
89 #define TIME_MEASURE_START(id)
90 #define TIME_MEASURE_STOP(id)
93 #if defined(DEBUG_TRACE_BUFFER)
95 #define TRACE_BUFFER_LEN 50
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,
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
,
124 ff_f_write_validate
= 40,
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
{
140 enum TraceEvent event
;
144 #if defined(__cplusplus)
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)
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)
168 #define TRACE_SD_CARD_EVENT(condition, event, data)
170 #if defined(TRACE_FATFS)
171 #define TRACE_FATFS_EVENT(condition, event, data) TRACE_EVENT(condition, event, data)
173 #define TRACE_FATFS_EVENT(condition, event, data)
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)
179 #define TRACE_AUDIO_EVENT(condition, event, data)
180 #define TRACEI_AUDIO_EVENT(condition, event, data)
184 #if defined(JITTER_MEASURE) && defined(__cplusplus)
186 template<class T
> class JitterMeter
{
192 JitterMeter() : min(~(T
)0), max(0), measured(0) {};
196 measured
= max
- min
;
197 //reset - begin new measurement
202 void measure(T value
) {
203 if (value
> max
) max
= value
;
204 if (value
< min
) min
= value
;
212 #endif // defined(JITTER_MEASURE)
215 #if defined(DEBUG_INTERRUPTS) && !defined(BOOT)
217 #if defined(PCBHORUS)
218 enum InterruptNames
{
232 #if defined(DEBUG_USB_INTERRUPTS)
236 INT_OTG_FS_MODEMISMATCH
,
243 INT_OTG_FS_INCOMPLETE_IN
,
244 INT_OTG_FS_INCOMPLETE_OUT
,
247 INT_OTG_FS_RX_NOT_DEVICE
,
248 #endif // #if defined(DEBUG_USB_INTERRUPTS)
251 #elif defined(PCBTARANIS)
252 enum InterruptNames
{
268 #if defined(DEBUG_USB_INTERRUPTS)
272 INT_OTG_FS_MODEMISMATCH
,
279 INT_OTG_FS_INCOMPLETE_IN
,
280 INT_OTG_FS_INCOMPLETE_OUT
,
283 INT_OTG_FS_RX_NOT_DEVICE
,
284 #endif // #if defined(DEBUG_USB_INTERRUPTS)
289 struct InterruptCounters
291 uint32_t cnt
[INT_LAST
];
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)
303 #define DEBUG_USB_INTERRUPT(int)
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)
326 extern void CoTaskSwitchHook(uint8_t taskID
);
327 #if defined(__cplusplus)
331 #endif // #if defined(DEBUG_TASKS)
334 #if defined(DEBUG_TIMERS)
336 #if defined(__cplusplus)
337 typedef uint32_t debug_timer_t
;
344 // debug_timer_t avg;
345 debug_timer_t last
; //unit 1us
347 uint16_t _start_hiprec
;
348 uint32_t _start_loprec
;
351 if (min
> last
) min
= last
;
352 if (max
< last
) max
= last
;
357 DebugTimer(): min(-1), max(0), /*avg(0),*/ last(0), _start_hiprec(0), _start_loprec(0) {};
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
; }
372 debugTimerIntPulsesDuration
,
374 debugTimerPer10msPeriod
,
378 debugTimerTelemetryWakeup
,
383 debugTimerLcdRefreshWait
,
385 debugTimerLcdRefresh
,
387 debugTimerMenuHandlers
,
389 debugTimerSimpleMenu
,
394 debugTimerGetSwitches
,
400 debugTimerMixerCalcToUsage
,
401 debugTimerMixerIterval
,
403 debugTimerAudioIterval
,
404 debugTimerAudioDuration
,
405 debugTimerAudioConsume
,
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)