Debug Arduino Zero with ST-Link V2

Update:

ST-Link V2 has compability issue with SAMD21. This is not best way to debug.

More info: https://electronics.stackexchange.com/questions/250664/can-i-use-st-link-programmer-for-non-st-chips

Overview

Arduino Zero has an Atmel’s SAMD21 MCU with SWD debug support. The cheapest SWD debugger I know is a non-official ST-Link V2. It looks like a thumb drive with a 10-pin IDE on its end, and you can get it for around $2 from eBay or AliExpress, etc.

Arduino IDE doesn't support debugging when I write this. But the Boards Manager will download all necessary debug files when you install "Arduino SAMD Boards (32-bit ARM Cortex-M0+)". This tutorial will show you how you can set up debug environment in Visual Studio Code, a cross-platform free IDE with Arduino Plugin.

This tutorial is using Mac OS as an example. But I'll explain every detail to help you understand and use it on other platforms.

Hardware I used

front img

back img

I used an "Adafruit Feather M0 Basic Proto", soldered swdio and swclk pads on the back, 3.3V and GND to a connector. Then I connect these 4 pins to an ST-Link V2 with female wires.

Step 1, get Blink example working

Blink img

I'll skip details on how to install board support, select board and upload examples. There are tons of tutorial you can follow. As long as you can upload a Blink example to Arduino Zero, it shows your Arduino, Boards manager, driver, Arduino Zero, are all working properly.

Step 2, check if all OpenOCD files are ready

In order to debug Arduino Zero, you need 2 tools (comes with Arduino SAMD Boards):

OpenOCD: It connects to debugging hardware and talk to Arduino's debug interface. Then it will open a network port for other debugger software to connect. OpenOCD is doing all low-level interfacing job.

GDB: It is a debugger tool that can load ELF files from GCC compiler and convert human-readable variables and breakpoints into physical memory address on Arduino.

All these tools should be in ~/Library/Arduino15/packages/arduino/tools/

You can open finder to double check if they are installed.

OpenOCD

Depending on your tool version, OpenOCD should be in somewhere like:

~/Library/Arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/bin/openocd

script folder

Depending on your tool version, scripts for OpenOCD should be in somewhere like:

~/Library/Arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/share/openocd/scripts

GDB

Depending on your tool version, GDB should be in somewhere like:

~/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gdb

Step 3, test whether OpenOCD and GDB is functional with terminal

Here is a tricky part. For some reason, the Arduino zero's ID is different from the one in the OpenOCD target. Also, with a bootloader, we don't want to have debugger change the flash content. So I copied at91samdXX.cfg to at91samdXXZero.cfg in this repo. I changed CPU ID and removed flash code to solve the issues.

Open a terminal window and paste following commands. If your files are located in a different folder from mine, edit the path accordingly. Always change the username to yours.

/Users/sundeqing/Library/Arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/bin/openocd -f /Users/sundeqing/Library/Arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/share/openocd/scripts/interface/stlink-v2.cfg -f /Users/sundeqing/Documents/Github/Debug-Arduino-Zero-with-ST-Link-V2/at91samdXXZero.cfg -s /Users/sundeqing/Library/Arduino15/packages/arduino/tools/openocd/0.9.0-arduino6-static/share/openocd/scripts

The command is absurdly long because there is a path with everything. Here I will explain what it means:

The openocd command works in this way:

openocd -f config_file_for_debugger -f config_file_for_board_or_CPU -s dir_to_search_for_config_files_and_scripts

If everything works well, you should see it working:

openocd working

Now we keep it running, open another terminal window and type

/Users/sundeqing/Library/Arduino15/packages/arduino/tools/arm-none-eabi-gcc/4.8.3-2014q1/bin/arm-none-eabi-gdb

Then you type

target remote localhost:3333
monitor reset halt
continue

The first command let GDB should connect to OpenOCD.

The second one resets and halts CPU. Arduino should stop working and LED should be OFF.

The third one resumes Arduino to work. You should see LED blinking again.

gdb working

If everything works so far, close all terminal windows and we are ready to setup VScode.

Step 4, setup VScode

First download VScode from https://code.visualstudio.com/

download VScode

Then you install Arduino extension. Click reload after you finish install.

installArduino

Save your Arduino Sketch somewhere, and open it in VScode with "Open Folder".

installArduino

Then you set Board and Port, just the same ones to your settings in Step 1.

set board and port

Press "Option+Command+U", VScode should compile and upload your code.

set board and port

You can include <Arduino.h> in your arduino sketch to fix error marks under Arduino functions.

Add include

Also, you can use "View" -> "Command Palette" to see all Arduino commands.

Step 5, setup debugging in VScode

Goto "Debug" view and add configuration, add Arduino one.

Add config

You will have a default configuration, no need to add more.

Default config

From here, you want to edit "miDebuggerPath", "debugServerPath" and "debugServerArgs". Use ones you tested in Step 3. OpenOCD is your debugServer, GDB is your debugger.

edited path

Now, you go back your code, add some variable (so you can see it) and upload code again before we debug it. You can also add breakpoints.

edited code

Press F5 and happy debugging.

edited code