Page 1 of 1

SWO Debugger

Posted: Sun Apr 08, 2012 9:12 am
by DrunkenDonkey
Yo, I'm still a high-level developer, so I'm used to having some easy to use debugging output and while the logic analyzer and the j-link proved useful, they are far from what I like in terms of outputting what the heck is going inside. I did some code using the uart1 as debug, but the ftdi serial-to-usb chip bursted in flames soon after and I lost it. To the rescue, I found a pin called SWO on the cpu, made my day, asked my hw dude to connect it and along with the j-link I hacked a quick program to use it, along with logging code. Worked great, the logic analyzer now stays in the box, I solved tons of communication issues and race conditions with past few days and decided to share if anyone is interested. The viewer is crap, buggy, should be done completely different way, but I just don't have the time, some day...

This is what the viewer looks like:
swo_reader.png (85.03 KiB) Viewed 3032 times

It needs j-link (I guess, dunno how to setup the rest) with j-link gdb server via swd to work. Once the j-link gdb server is started, the "Init SWO" button connects and sends few commands to enable the swo, should be done once per launch of the gdb server, then it just works.

The way to use:
0. Put the correct mcu speed in the debug.c
#define DBG_MCU_SPEED 16000000

1. Define "channels", numeric starting from 1 up to 31, like:

2. (optional) set them a name somewhere at program startup, like:

3a. Log printf text
dbgf(DBG_MAIN,"Compiled on: %s %s",__DATE__,__TIME__);

3b. Log just plain string
dbgStr(DBG_MAIN,"It works!");

3c. Log some data packet
dbgDataW(DBG_MAIN,txBuf,txLen); //will show as "red" hex data on the logger
dbgDataR(DBG_MAIN,rxBuf,rxLen); //will show as "green" hex data on the logger

4. Log "fragmented" packet:
dbgStartW(DBG_MAIN); //starting "red" hex data packet
dbgb(DBG_MAIN,0x55); //adding a byte
dbgb(DBG_MAIN,0xAA); //adding a byte
dbgd(DBG_MAIN,txBuf,txLen); //adding some more data
dbgEnd(DBG_MAIN); //making the data sow up on the logger. Actually starting new packet on that channel instead of calling dbgEnd does the same thing

Thats pretty much it, I have "borrowed" ChibiOS' chprintf function for the purpose and modding it a bit :p
I know it is very early ver and does not look pretty, but it helps me, hope it does help other people too.

Re: SWO Debugger

Posted: Sun Mar 21, 2021 11:12 pm
by jhalfmoon
Hi DD,

I registered to this forum just to say thank you to you, for this post! And to say that I've been screwing around with SWD / SWO on a Black Magic, for the past week, and your swoWrite() is exactly what I was planning to implement; A more cycle-efficient version of ITM_SendChar().

Thanks mate!