Debug using SWD

Introduction

Cortex-Debug is an extension for Visual Studio Code to streamline the debug process when working with ARM Cortex-M microcontrollers.

Requirements

If you did not install these previously, you need to install now.

  • Cortex-Debug

    Install Cortex-Debug extension by marus25 in VSCODE.

  • gdb-multiarch

    Install gdb-multiarch by executing command sudo apt install gdb-multiarch.

Setup for OpenOCD

OpenOCD server is better than ST-Link server. We also prefer to use OpenOCD instead of ST-Link but it is not neccessary.

Install OpenOCD by executing command sudo apt install openocd. Update .vscode/launch.json.

launch.json
 1{
 2    "version": "0.2.0",
 3    "configurations": [
 4        {
 5            "name": "OpenOCD Debug",
 6            "cwd": "${workspaceFolder}",
 7            "executable": "${workspaceFolder}/build/BasicSetup.elf",
 8            "request": "launch",
 9            "type": "cortex-debug",
10            "runToEntryPoint": "main",
11            "device": "STM32F407VG",
12            "interface": "swd",
13            "servertype": "openocd",
14            "configFiles": [
15                "interface/stlink-v2-1.cfg",
16                "target/stm32f4x.cfg"
17            ],
18            "gdbPath": "/usr/bin/gdb-multiarch"
19        }
20    ]
21}

Change .elf filename for executable and device name for device. Also you need to chage configFiles (e.g. target/stm32f1x.cfg for bluepill or interface/jlink.cfg for jlink interface). For more about OpenOCD, watch :this.

Run Debugger

  • Connet your ST-Link or JLink to your microcontroller and your PC. If you use external debugger like JLink or ST-Link adapter, connect it as:

    • SWCLK of microcontroller to SWCLK of debugger.

    • SWDIO of microcontroller to SWDIO of debugger.

    • SWO of microcontroller to SWO of debugger. (Optional, only if you want to see ITM messages)

    • NRST of microcontroller to NRST of debugger. (Optional, only if you want to hard reset microcontroller using debugger)

    • GND of microcontroller to GND of debugger.

    • 3V3 of microcontroller to 3V3 of debugger.

  • Go to Run and Debug section in VSCODE.

  • Click Run button.

You will see a floating toolbar which you can use for continue, step over, step into, step out, restart and stop.

cortex_debug.webp