Debugging code on your MBED with pyOCD and CMSIS-DAP

Added by jaap about 10 years ago

Using pyOCD, you can get realtime debugging access to programs running on your mbed.

Since I had a hard time getting all the bits and pieces together, I decided to document the process here.
If you have no idea what a (on-chip) debugger is, watch this video first:

Compiler environment

My normal toolset consists of:

I'm not using an IDE. When I write a new program, I just add it as one of the "tests" within the mbed libraries and compile it like any other test.

Setting up debugging

Upgrade your mbed-firmware

Most likely, your firmware is too old to support debugging. You can upgrade it by downloading the latest firmware from
Upgrading is just a matter of dropping that file on your MBED and power-cycling.

Set up pyOCD

Install python, libusb and libncursus (i386 to be compatible with arm-none-eabi-gdb)

sudo apt-get install python libusb-1.0-0-dev libncurses5:i386

It might be necessary to update your USB settings to get non-root access to DAP:
sudo sh -c 'echo SUBSYSTEM==\"usb\", ATTR{idVendor}==\"0d28\", ATTR{idProduct}==\"0204\", MODE:=\"666\" > /etc/udev/rules.d/mbed.rules'
sudo /etc/init.d/udev restart

Download pyusb and pyOCD from github:
git clone
cd pyusb
sudo python install
cd ..
git clone
sudo python install
cd ..

Now, you should have a working pyOCD. Test it with:

python tests/

This should flash a test (blinking light) firmware on your MBED. Read the python file to see what more it does.

Now pyOCD is ready to use.

Compiling a test program and debugging it

Compile program

Compile your program in the mbed script. Add -o debug-info to create extra information for the debugger.
This will compiles the SD File system example test (A12):

cd mbed
python workspace_tools/ -m LPC1768 -t GCC_ARM -n MBED_A12 -o debug-info

The last line of the compilation will tell you where the image is stored:
Image: /home/jaap/mbed/build/test/LPC1768/GCC_ARM/MBED_A12/sd.bin

Copy that file to the mbed and remember that line, you need that directory later!

Start pyOCD

Make a connection with pyOCD for use with gdb:

cd pyOCD
python test/

Start debugging

When is running, you can start gdb from the directory which contains the *.elf file for your code.

export PATH=$PATH:<where-ever you have your gcc-arm-none-eabi../bin>
cd  /home/jaap/mbed/build/test/LPC1768/GCC_ARM/MBED_A12/
arm-none-eabi-gdb sd.elf

Now that gdb is running, you can connect to pyOCD with:

<gdb> target remote localhost:3333
<gdb> load
<gdb> b main
<gdb> continue

Usefull links:

Useful GDB commands

b breakpoint            set a breakpoint. You can use a function name or a line number
  clear breakpoints     remove all breakpoints
  clear 3               remove breakpoint at line 3
  info breakpoints      list existing breakpoints
c continue              continue running the program until the next breakpoint
s step                  one line forward, jump into functions
n next                  one line forward, not jumping into functions
  where                 tells you where you are in the program  
p print                 print value of variable
  display               print value of variable at each breakpoint
q quit                  stop gdb