1
1
/* *************************************************************************/
2
2
/* !
3
3
@file debug.cpp
4
- @author hathach
4
+ @author hathach (tinyusb.org)
5
5
6
6
@section LICENSE
7
7
8
8
Software License Agreement (BSD License)
9
9
10
- Copyright (c) 2017 , Adafruit Industries (adafruit.com)
10
+ Copyright (c) 2018 , Adafruit Industries (adafruit.com)
11
11
All rights reserved.
12
12
13
13
Redistribution and use in source and binary forms, with or without
38
38
#include < stdarg.h>
39
39
#include < malloc.h>
40
40
#include < Arduino.h>
41
+ #include < ctype.h>
41
42
42
43
// defined in linker script
43
44
extern uint32_t __data_start__[];
@@ -54,30 +55,30 @@ extern uint32_t __StackLimit[];
54
55
55
56
extern " C"
56
57
{
57
- int cprintf (const char * format, ...)
58
- {
59
- char buf[256 ];
60
- int len;
61
58
62
- va_list ap;
63
- va_start (ap, format);
59
+ int cprintf (const char * format, ...)
60
+ {
61
+ char buf[256 ];
62
+ int len;
64
63
65
- len = vsnprintf (buf, 256 , format, ap) ;
66
- Serial. write (buf, len );
64
+ va_list ap ;
65
+ va_start (ap, format );
67
66
68
- va_end (ap);
69
- return len;
70
- }
67
+ len = vsnprintf (buf, 256 , format, ap);
68
+ Serial.write (buf, len);
71
69
72
- void vApplicationMallocFailedHook (void )
73
- {
74
- Serial.println (" Failed to Malloc" );
75
- }
70
+ va_end (ap);
71
+ return len;
72
+ }
76
73
77
- void vApplicationStackOverflowHook ( TaskHandle_t xTask, char *pcTaskName )
78
- {
79
- Serial.printf (" %s Stack Overflow !!!" , pcTaskName);
80
- }
74
+ void vApplicationMallocFailedHook (void )
75
+ {
76
+ cprintf (" Failed to Malloc" );
77
+ }
78
+
79
+ void vApplicationStackOverflowHook ( TaskHandle_t xTask, char *pcTaskName )
80
+ {
81
+ cprintf (" %s Stack Overflow !!!" , pcTaskName);
81
82
}
82
83
83
84
int dbgHeapTotal (void )
@@ -124,14 +125,14 @@ static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint
124
125
sprintf (buffer, " %lu" , top-bottom);
125
126
}
126
127
127
- Serial. printf (" | %-5s| 0x%04X - 0x%04X | %-19s |\n " , name, (uint16_t ) bottom, (uint16_t ) (top-1 ), buffer);
128
+ cprintf (" | %-5s| 0x%04X - 0x%04X | %-19s |\n " , name, (uint16_t ) bottom, (uint16_t ) (top-1 ), buffer);
128
129
}
129
130
130
131
void dbgMemInfo (void )
131
132
{
132
- Serial. println (" ______________________________________________" );
133
- Serial. println (" | Name | Addr 0x2000xxxx | Usage |" );
134
- Serial. println (" | ---------------------------------------------|" );
133
+ cprintf (" ______________________________________________\n " );
134
+ cprintf (" | Name | Addr 0x2000xxxx | Usage |\n " );
135
+ cprintf (" | ---------------------------------------------|\n " );
135
136
136
137
// Pritn SRAM used for Stack executed by S132 and ISR
137
138
printMemRegion (" Stack" , ((uint32_t ) __StackTop), ((uint32_t ) __StackLimit), dbgStackUsed () );
@@ -145,18 +146,19 @@ void dbgMemInfo(void)
145
146
// Print SRAM Used by SoftDevice
146
147
printMemRegion (" S132" , (uint32_t ) __data_start__, 0x20000000 , 0 );
147
148
148
- Serial. printf (" |______________________________________________|\n " );
149
- Serial. println ( );
149
+ cprintf (" |______________________________________________|\n " );
150
+ cprintf ( " \n " );
150
151
151
152
// Print Task list
152
153
uint32_t tasknum = uxTaskGetNumberOfTasks ();
153
154
char * buf = (char *) rtos_malloc (tasknum*40 ); // 40 bytes per task
154
155
155
156
vTaskList (buf);
156
157
157
- Serial.println (" Task State Prio StackLeft Num" );
158
- Serial.println (" -----------------------------------" );
159
- Serial.println (buf);
158
+ cprintf (" Task State Prio StackLeft Num\n " );
159
+ cprintf (" -----------------------------------\n " );
160
+ cprintf (buf);
161
+ cprintf (" \n " );
160
162
rtos_free (buf);
161
163
}
162
164
@@ -174,50 +176,116 @@ void dbgPrintVersion(void)
174
176
@brief Helper function to display memory contents in a friendly format
175
177
*/
176
178
/* *****************************************************************************/
179
+ static void dump_str_line (uint8_t const * buf, uint16_t count)
180
+ {
181
+ // each line is 16 bytes
182
+ for (int i=0 ; i<count; i++)
183
+ {
184
+ const char ch = buf[i];
185
+ cprintf (" %c" , isprint (ch) ? ch : ' .' );
186
+ }
187
+ }
188
+
177
189
void dbgDumpMemory (void const *buf, uint8_t size, uint16_t count, bool printOffset)
178
190
{
179
- uint8_t const *buf8 = ( uint8_t const *) buf;
180
-
181
- uint8_t format_size = 2 * size ;
182
- if ( count*size > UINT8_MAX ) offset_fmt_size *= 2 ;
183
- if ( count*size > UINT16_MAX ) offset_fmt_size *= 2 ;
191
+ if ( !buf )
192
+ {
193
+ cprintf ( " NULL \n " ) ;
194
+ return ;
195
+ }
184
196
185
- char format[] = " %02lX " ;
197
+ uint8_t const *buf8 = ( uint8_t const *) buf ;
186
198
187
- char offset_fmt [] = " %00lX: " ;
188
- offset_fmt [2 ] += offset_fmt_size ;
199
+ char format [] = " %00lX" ;
200
+ format [2 ] += 2 *size ;
189
201
190
- const uint8_t item_per_line = 16 / size;
202
+ const uint8_t item_per_line = 16 / size;
191
203
192
204
for (int i=0 ; i<count; i++)
193
205
{
194
206
uint32_t value=0 ;
195
207
196
- // Print address
197
208
if ( i%item_per_line == 0 )
198
209
{
199
- if ( i != 0 ) Serial.println ();
210
+ // Print Ascii
211
+ if ( i != 0 )
212
+ {
213
+ cprintf (" | " );
214
+ dump_str_line (buf8-16 , 16 );
215
+ cprintf (" \n " );
216
+ }
200
217
201
218
// print offset or absolute address
202
219
if (printOffset)
203
220
{
204
- Serial. printf (offset_fmt , 16 *i/item_per_line);
221
+ cprintf ( " %03lX: " , 16 *i/item_per_line);
205
222
}else
206
223
{
207
- Serial. printf (" %08lX:" , (uint32_t ) buf8);
224
+ cprintf (" %08lX:" , (uint32_t ) buf8);
208
225
}
209
226
}
210
227
211
228
memcpy (&value, buf8, size);
212
229
buf8 += size;
213
230
214
- Serial. print ( ' ' );
215
- Serial. printf (format, value);
231
+ cprintf ( " " );
232
+ cprintf (format, value);
216
233
}
217
234
218
- Serial.println ();
235
+ // fill up last row to 16 for printing ascii
236
+ const uint16_t remain = count%16 ;
237
+ uint8_t nback = (remain ? remain : 16 );
238
+
239
+ if ( remain )
240
+ {
241
+ for (int i=0 ; i< 16 -remain; i++)
242
+ {
243
+ cprintf (" " );
244
+ for (int j=0 ; j<2 *size; j++) cprintf (" " );
245
+ }
246
+ }
247
+
248
+ cprintf (" | " );
249
+ dump_str_line (buf8-nback, nback);
250
+ cprintf (" \n " );
251
+
252
+ cprintf (" \n " );
219
253
}
220
254
255
+
256
+ void dbgDumpMemoryCFormat (const char * str, void const *buf, uint16_t count)
257
+ {
258
+ if ( !buf )
259
+ {
260
+ cprintf (" NULL\n " );
261
+ return ;
262
+ }
263
+
264
+ cprintf (" %s = \n {\n " , str);
265
+
266
+ uint8_t const *buf8 = (uint8_t const *) buf;
267
+
268
+ for (int i=0 ; i<count; i++)
269
+ {
270
+ uint32_t value=0 ;
271
+
272
+ if ( i%16 == 0 )
273
+ {
274
+ if ( i != 0 ) cprintf (" ,\n " );
275
+ }else
276
+ {
277
+ if ( i != 0 ) cprintf (" , " );
278
+ }
279
+
280
+ cprintf (" 0x%02lX" , *buf8);
281
+ buf8++;
282
+ }
283
+
284
+ cprintf (" \n \};\n " );
285
+ }
286
+
287
+
288
+
221
289
#if CFG_DEBUG
222
290
223
291
#include " ble.h"
0 commit comments