Fix doc path
[opentx.git] / radio / src / debug.cpp
blobaf883134ed4b806e8adc02de2f320329542e2c56
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 #include "opentx.h"
22 #include "stamp.h"
23 #include <stdarg.h>
25 #if defined(SIMU)
26 traceCallbackFunc traceCallback = 0;
27 #endif
29 #if defined(SIMU)
30 #define PRINTF_BUFFER_SIZE 1024
31 void debugPrintf(const char * format, ...)
33 va_list arglist;
34 char tmp[PRINTF_BUFFER_SIZE];
36 va_start(arglist, format);
37 vsnprintf(tmp, PRINTF_BUFFER_SIZE, format, arglist);
38 va_end(arglist);
39 fputs(tmp, stdout);
40 fflush(stdout);
41 if (traceCallback) {
42 traceCallback(tmp);
45 #endif
47 #if defined(DEBUG_TRACE_BUFFER)
48 static struct TraceElement traceBuffer[TRACE_BUFFER_LEN];
49 static uint8_t traceBufferPos;
50 extern Fifo<uint8_t, 512> serial2TxFifo;
51 gtime_t filltm(const gtime_t *t, struct gtm *tp);
53 void trace_event(enum TraceEvent event, uint32_t data)
55 if (traceBufferPos >= TRACE_BUFFER_LEN) return;
56 __disable_irq();
57 struct TraceElement * p = &traceBuffer[traceBufferPos++];
58 __enable_irq();
59 p->time = g_rtcTime;
60 p->time_ms = g_ms100;
61 p->event = event;
62 p->data = data;
65 void trace_event_i(enum TraceEvent event, uint32_t data)
67 if (traceBufferPos >= TRACE_BUFFER_LEN) return;
68 struct TraceElement * p = &traceBuffer[traceBufferPos++];
69 p->time = g_rtcTime;
70 p->time_ms = g_ms100;
71 p->event = event;
72 p->data = data;
76 const struct TraceElement * getTraceElement(uint16_t idx)
78 if (idx < TRACE_BUFFER_LEN) return &traceBuffer[idx];
79 return 0;
83 void dumpTraceBuffer()
85 TRACE("Dump of Trace Buffer (%s " DATE " " TIME "):", vers_stamp);
86 TRACE("# Time Event Data");
87 for(int n = 0; n < TRACE_BUFFER_LEN; ++n) {
88 struct gtm tp;
89 filltm(&traceBuffer[n].time, &tp);
90 TRACE_NOCRLF("%02d ", n);
91 TRACE_NOCRLF("%4d-%02d-%02d,%02d:%02d:%02d.%02d0", tp.tm_year+TM_YEAR_BASE, tp.tm_mon+1, tp.tm_mday, tp.tm_hour, tp.tm_min, tp.tm_sec, traceBuffer[n].time_ms);
92 TRACE(" %03d 0x%08x", traceBuffer[n].event, traceBuffer[n].data);
93 if (traceBuffer[n].time == 0 && traceBuffer[n].time_ms == 0) break;
94 #if !defined(SIMU)
95 if ((n % 5) == 0) {
96 while (!serial2TxFifo.isEmpty()) {
97 CoTickDelay(1);
100 #endif
102 TRACE("End of Trace Buffer dump");
104 #endif
106 #if defined(DEBUG_INTERRUPTS)
108 #if defined(PCBHORUS)
109 const char * const interruptNames[INT_LAST] = {
110 "Tick ", // INT_TICK,
111 "1ms ", // INT_1MS,
112 "Ser2 ", // INT_SER2,
113 "TelDm", // INT_TELEM_DMA,
114 "TelUs", // INT_TELEM_USART,
115 "Sdio ", // INT_SDIO,
116 "SdDma", // INT_SDIO_DMA,
117 "D2S7 ", // INT_DMA2S7,
118 "Tim1 ", // INT_TIM1CC,
119 "Tim2 ", // INT_TIM2,
120 "Tim3 ", // INT_TIM3,
121 "BlueT", // INT_BLUETOOTH,
122 "USB ", // INT_OTG_FS,
123 #if defined(DEBUG_USB_INTERRUPTS)
124 " spur", // INT_OTG_FS_SPURIOUS,
125 " out", // INT_OTG_FS_OUT_EP,
126 " in", // INT_OTG_FS_IN_EP,
127 " miss", // INT_OTG_FS_MODEMISMATCH,
128 " wake", // INT_OTG_FS_WAKEUP,
129 " susp", // INT_OTG_FS_SUSPEND,
130 " sof", // INT_OTG_FS_SOF,
131 " rxst", // INT_OTG_FS_RX_STAT,
132 " rst", // INT_OTG_FS_RESET,
133 " enum", // INT_OTG_FS_ENUM,
134 " inci", // INT_OTG_FS_INCOMPLETE_IN,
135 " inco", // INT_OTG_FS_INCOMPLETE_OUT,
136 " sess", // INT_OTG_FS_SESSION,
137 " otg", // INT_OTG_FS_OTG,
138 " notd", // INT_OTG_FS_RX_NOT_DEVICE,
139 #endif // #if defined(DEBUG_USB_INTERRUPTS)
141 #elif defined(PCBTARANIS)
142 const char * const interruptNames[INT_LAST] = {
143 "Tick ", // INT_TICK,
144 "5ms ", // INT_5MS,
145 "Audio", // INT_AUDIO,
146 "BlueT", // INT_BLUETOOTH,
147 "Lcd ", // INT_LCD,
148 "T1CC ", // INT_TIM1CC,
149 "Tim1 ", // INT_TIM1,
150 "Tim8 ", // INT_TIM8,
151 "Ser2 ", // INT_SER2,
152 "TelDm", // INT_TELEM_DMA,
153 "TelUs", // INT_TELEM_USART,
154 "Train", // INT_TRAINER,
155 "Usb ", // INT_OTG_FS,
156 #if defined(DEBUG_USB_INTERRUPTS)
157 " spur", // INT_OTG_FS_SPURIOUS,
158 " out", // INT_OTG_FS_OUT_EP,
159 " in", // INT_OTG_FS_IN_EP,
160 " miss", // INT_OTG_FS_MODEMISMATCH,
161 " wake", // INT_OTG_FS_WAKEUP,
162 " susp", // INT_OTG_FS_SUSPEND,
163 " sof", // INT_OTG_FS_SOF,
164 " rxst", // INT_OTG_FS_RX_STAT,
165 " rst", // INT_OTG_FS_RESET,
166 " enum", // INT_OTG_FS_ENUM,
167 " inci", // INT_OTG_FS_INCOMPLETE_IN,
168 " inco", // INT_OTG_FS_INCOMPLETE_OUT,
169 " sess", // INT_OTG_FS_SESSION,
170 " otg", // INT_OTG_FS_OTG,
171 " notd", // INT_OTG_FS_RX_NOT_DEVICE,
172 #endif // #if defined(DEBUG_USB_INTERRUPTS)
174 #endif
176 struct InterruptCounters interruptCounters;
177 #endif //#if defined(DEBUG_INTERRUPTS)
179 #if defined(DEBUG_TASKS)
181 uint32_t taskSwitchLog[DEBUG_TASKS_LOG_SIZE] __SDRAM;
182 uint16_t taskSwitchLogPos;
185 *******************************************************************************
186 * @brief Hook for task switch logging
187 * @param[in] taskID Task which is now in RUNNING state
188 * @retval None
190 * @par Description
191 * @details This function logs the time when a task entered the RUNNING state.
192 *******************************************************************************
194 void CoTaskSwitchHook(uint8_t taskID)
196 /* Log task switch here */
197 taskSwitchLog[taskSwitchLogPos] = (taskID << 24) + ((uint32_t)CoGetOSTime() & 0xFFFFFF);
198 if(++taskSwitchLogPos >= DEBUG_TASKS_LOG_SIZE) {
199 taskSwitchLogPos = 0;
203 #endif // #if defined(DEBUG_TASKS)
205 #if defined(DEBUG_TIMERS)
207 void DebugTimer::start()
209 _start_hiprec = getTmr2MHz();
210 _start_loprec = get_tmr10ms();
213 void DebugTimer::stop()
215 // getTmr2MHz is 16 bit timer, resolution 0.5us, max measurable value 32.7675 milli seconds
216 // tmr10ms_t tmr10ms = get_tmr10ms(); 32 bit timer, resolution 10ms, max measurable value: 42949672.95 s = 1.3 years
217 // if time difference is bigger than 30ms, then use low resolution timer
218 // otherwise use high resolution
219 if ((_start_hiprec == 0) && (_start_loprec == 0)) return;
221 last = get_tmr10ms() - _start_loprec; //use low precision timer
222 if (last < 3) {
223 //use high precision
224 last = (uint16_t)(getTmr2MHz() - _start_hiprec) / 2;
226 else {
227 last *= 10000ul; //adjust unit to 1us
229 evalStats();
232 DebugTimer debugTimers[DEBUG_TIMERS_COUNT];
234 const char * const debugTimerNames[DEBUG_TIMERS_COUNT] = {
235 "Pulses int." // debugTimerIntPulses,
236 ,"Pulses dur." // debugTimerIntPulsesDuration,
237 ,"10ms dur. " // debugTimerPer10ms,
238 ,"10ms period" // debugTimerPer10msPeriod,
239 ,"Rotary enc." // debugTimerRotEnc,
240 ,"Haptic " // debugTimerHaptic,
241 ,"Mixer calc " // debugTimerMixer,
242 ,"Tel. wakeup" // debugTimerTelemetryWakeup,
243 ,"perMain dur" // debugTimerPerMain,
244 ," perMain s1" // debugTimerPerMain1,
245 ," guiMain " // debugTimerGuiMain,
246 ," LUA bg " // debugTimerLuaBg,
247 ," LCD wait " // debugTimerLcdRefreshWait,
248 ," LUA fg " // debugTimerLuaFg,
249 ," LCD refr." // debugTimerLcdRefresh,
250 ," Menus " // debugTimerMenus,
251 ," Menu hnd" // debugTimerMenuHandlers,
252 ,"Menu Vers. " // debugTimerVersion,
253 ,"Menu simple" // debugTimerSimpleMenu,
254 ,"Menu drawte" // debugTimerDrawText,
255 ,"Menu drawt1" // debugTimerDrawText1,
256 ,"Mix ADC " // debugTimerGetAdc,
257 ,"Mix getsw " // debugTimerGetSwitches,
258 ,"Mix eval " // debugTimerEvalMixes,
259 ,"Mix 10ms " // debugTimerMixes10ms,
260 ,"ADC read " // debugTimerAdcRead,
261 ,"mix-pulses " // debugTimerMixerCalcToUsage
262 ,"mix-int. " // debugTimerMixerIterval
263 ,"Audio int. " // debugTimerAudioIterval
264 ,"Audio dur. " // debugTimerAudioDuration
265 ," A. consume" // debugTimerAudioConsume,
269 #endif