Skip to content

Commit 5cfdb28

Browse files
committed
fix deubg.cpp issue #176
1 parent 3532d6b commit 5cfdb28

File tree

1 file changed

+114
-46
lines changed

1 file changed

+114
-46
lines changed

cores/nRF5/debug.cpp

Lines changed: 114 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/**************************************************************************/
22
/*!
33
@file debug.cpp
4-
@author hathach
4+
@author hathach (tinyusb.org)
55
66
@section LICENSE
77
88
Software License Agreement (BSD License)
99
10-
Copyright (c) 2017, Adafruit Industries (adafruit.com)
10+
Copyright (c) 2018, Adafruit Industries (adafruit.com)
1111
All rights reserved.
1212
1313
Redistribution and use in source and binary forms, with or without
@@ -38,6 +38,7 @@
3838
#include <stdarg.h>
3939
#include <malloc.h>
4040
#include <Arduino.h>
41+
#include <ctype.h>
4142

4243
// defined in linker script
4344
extern uint32_t __data_start__[];
@@ -54,30 +55,30 @@ extern uint32_t __StackLimit[];
5455

5556
extern "C"
5657
{
57-
int cprintf(const char * format, ...)
58-
{
59-
char buf[256];
60-
int len;
6158

62-
va_list ap;
63-
va_start(ap, format);
59+
int cprintf(const char * format, ...)
60+
{
61+
char buf[256];
62+
int len;
6463

65-
len = vsnprintf(buf, 256, format, ap);
66-
Serial.write(buf, len);
64+
va_list ap;
65+
va_start(ap, format);
6766

68-
va_end(ap);
69-
return len;
70-
}
67+
len = vsnprintf(buf, 256, format, ap);
68+
Serial.write(buf, len);
7169

72-
void vApplicationMallocFailedHook(void)
73-
{
74-
Serial.println("Failed to Malloc");
75-
}
70+
va_end(ap);
71+
return len;
72+
}
7673

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);
8182
}
8283

8384
int dbgHeapTotal(void)
@@ -124,14 +125,14 @@ static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint
124125
sprintf(buffer, "%lu", top-bottom);
125126
}
126127

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);
128129
}
129130

130131
void dbgMemInfo(void)
131132
{
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");
135136

136137
// Pritn SRAM used for Stack executed by S132 and ISR
137138
printMemRegion("Stack", ((uint32_t) __StackTop), ((uint32_t) __StackLimit), dbgStackUsed() );
@@ -145,18 +146,19 @@ void dbgMemInfo(void)
145146
// Print SRAM Used by SoftDevice
146147
printMemRegion("S132", (uint32_t) __data_start__, 0x20000000, 0);
147148

148-
Serial.printf("|______________________________________________|\n");
149-
Serial.println();
149+
cprintf("|______________________________________________|\n");
150+
cprintf("\n");
150151

151152
// Print Task list
152153
uint32_t tasknum = uxTaskGetNumberOfTasks();
153154
char* buf = (char*) rtos_malloc(tasknum*40); // 40 bytes per task
154155

155156
vTaskList(buf);
156157

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");
160162
rtos_free(buf);
161163
}
162164

@@ -174,50 +176,116 @@ void dbgPrintVersion(void)
174176
@brief Helper function to display memory contents in a friendly format
175177
*/
176178
/******************************************************************************/
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+
177189
void dbgDumpMemory(void const *buf, uint8_t size, uint16_t count, bool printOffset)
178190
{
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+
}
184196

185-
char format[] = "%02lX";
197+
uint8_t const *buf8 = (uint8_t const *) buf;
186198

187-
char offset_fmt[] = "%00lX: ";
188-
offset_fmt[2] += offset_fmt_size;
199+
char format[] = "%00lX";
200+
format[2] += 2*size;
189201

190-
const uint8_t item_per_line = 16 / size;
202+
const uint8_t item_per_line = 16 / size;
191203

192204
for(int i=0; i<count; i++)
193205
{
194206
uint32_t value=0;
195207

196-
// Print address
197208
if ( i%item_per_line == 0 )
198209
{
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+
}
200217

201218
// print offset or absolute address
202219
if (printOffset)
203220
{
204-
Serial.printf(offset_fmt, 16*i/item_per_line);
221+
cprintf("%03lX: ", 16*i/item_per_line);
205222
}else
206223
{
207-
Serial.printf("%08lX:", (uint32_t) buf8);
224+
cprintf("%08lX:", (uint32_t) buf8);
208225
}
209226
}
210227

211228
memcpy(&value, buf8, size);
212229
buf8 += size;
213230

214-
Serial.print(' ');
215-
Serial.printf(format, value);
231+
cprintf(" ");
232+
cprintf(format, value);
216233
}
217234

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");
219253
}
220254

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+
221289
#if CFG_DEBUG
222290

223291
#include "ble.h"

0 commit comments

Comments
 (0)