How to setup VSCode debugging for STM32 platforms

We will show you how to build and debug using Visual Studio Code (VSCode) using ST Micro's Nucleo and Discovery board.

Installation of required software

Downloading and installing VSCode

Download the installer according to the host machine to be used for installation. https://code.visualstudio.com/Download

Installing C / C ++ extension

Start VSCode. Select "Extension" from menu "Display" and install C / C ++ function extension.

Installing OpenOCD

For Mac

$ brew install openocd --enable_stlink

For Windows
http://gnutoolchains.com/arm-eabi/openocd/

Installing build tools for Mbed OS


https://github.com/armmbed/mbed-cli#installing-mbed-cli
https://github.com/armmbed/mbed-cli#quickstart-video

In the case of Windows, installation of the make utility is necessary in addition to this.
http://gnuwin32.sourceforge.net/packages/make.htm

Project setting

Create a project. Let's try importing Mbed OS sample code here.

$ mbed import mbed-os-example-blinky

Export the project for VSCode.

$ mbed export -m DISCO_L475VG_IOT01A -i vscode_gcc_arm

The above operation creates files necessary for build and debugging, so open VSCode from the same directory.

$ code.

Open .vscode / launch.json in the project file and change the following 4 places.


1. debugServerArgs
Windows:

"debugServerArgs": "-f \"C:\\Program Files (x86)\\OpenOCD-20170821\\share\\openocd\\scripts\\board\\stm32l4discovery.cfg\" -f \"C:\\Program Files (x86)\\OpenOCD-20170821\\share\\openocd\\scripts\\interface\\stlink-v2-1.cfg\" -c init -c \"reset init\"",

Mac:

"debugServerArgs": "-f /usr/local/Cellar/open-ocd/0.10.0/share/openocd/scripts/board/stm32l4discovery.cfg -f /usr/local/Cellar/open-ocd/0.10.0/share/openocd/scripts/interface/stlink-v2-1.cfg -c init -c \"reset init\"",

Information

Please select the MCU or the same series installed on the board to be used for the board configuration file specified by debugServerArgs. For Windows, put the path in double quotes.


2. serverStarted

"serverStarted": "target halted due to debug-request, current mode: Thread”,


3. MIDebuggerPath
Windows:

"MIDebuggerPath": "C:\\Program Files (x86)\\GNU Tools ARM Embedded\\5.4 2016q3\\bin\\arm-none-eabi-gdb.exe",

Mac:

"MIDebuggerPath": "/usr/local/bin/gcc-arm-none-eabi-6-2017-q1-update/bin/arm-none-eabi-gdb",


4. debugServerPath
Windows:

"debugServerPath": "C:\\Program Files (x86)\\OpenOCD-20170821\\bin\\openocd.exe",

Mac:

"debugServerPath": "/usr/local/bin/openocd"

Build and debug

From the menu "Debug", select "Start Debugging" to start the debugging session.

/media/uploads/MACRUM/vscode.png

It is not possible to display general purpose registers, but it can be displayed by entering the following command from the debug console.

-exec info registers

/media/uploads/MACRUM/vscode_reg.png


3 comments on How to setup VSCode debugging for STM32 platforms :

19 Dec 2017

Hi - I've imported a working blinky project for F746G-disco into VS code and set it up as per your instructions. (I've also tried various other instructions!) My problem is that whatever I do I end up with a vs code error 'Unable to start debugging. No process is associated with this object.' I'm still learning, but I have found that VScode performs MAKE ok and gives a working bin file, but wont go into debug- Hope you can give me some guidlines. Thanks. Chris Launch json: "windows": { "preLaunchTask": "make.exe", "MIMode": "gdb", "MIDebuggerPath": "C:/Program Files (x86)/GNU Tools ARM Embedded/6 2017-q2-update/bin/arm-none-eabi-gdb.exe", "debugServerPath": "C:/Program Files/GNU MCU Eclipse/OpenOCD/0.10.0-5-20171110-1117/bin/openocd.exe", "setupCommands": [ { "text": "-environment-cd ${workspaceRoot}/BUILD" }, { "text": "-target-select remote localhost:3333", "description": "connect to target", "ignoreFailures": false }, { "text": "-file-exec-and-symbols ${workspaceRoot}/build/DISCO-F746NG_blink_led.elf", "description": "load file", "ignoreFailures": false}, { "text": "-interpreter-exec console \"monitor endian little\"", "ignoreFailures": false }, { "text": "-interpreter-exec console \"monitor reset\"", "ignoreFailures": false }, { "text": "-interpreter-exec console \"monitor halt\"", "ignoreFailures": false }, { "text": "-interpreter-exec console \"monitor arm semihosting enable\"", "ignoreFailures": false }, { "text": "-target-download", "description": "flash target", "ignoreFailures": false } ] } Debug OP : (513) Starting: "C:/Program Files/GNU MCU Eclipse/OpenOCD/0.10.0-5-20171110-1117/bin/openocd.exe" -f "C:\Program Files (x86)\OpenOCD-20170821\share\openocd\scripts\board\stm32l4discovery.cfg" -f "C:\Program Files (x86)\OpenOCD-20170821\share\openocd\scripts\interface\stlink-v2-1.cfg" -c init -c "reset init" 1: (567) "C:/Program Files/GNU MCU Eclipse/OpenOCD/0.10.0-5-20171110-1117/bin/openocd.exe" exited with code 1 (0x1). 1: (576) <-logout

19 Dec 2017

I've made a bit more progress by adding "debugServerArgs": "-f \"C:/Program Files/GNU MCU Eclipse/OpenOCD/0.10.0-5-20171110-1117/scripts/board/stm32f7discovery.cfg", the debugserverpath is C:/Program Files/GNU MCU Eclipse/OpenOCD/0.10.0-5-20171110-1117/bin/openocd.exe but I get VScode error Unable to start debugging. Debug server process failed to initialize. Any ideas? Thanks

debug console: 1: (551) STDERR: For bug reports, read 1: (551) STDERR: http://openocd.org/doc/doxygen/bugs.html 1: (557) STDERR: Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD 1: (558) STDERR: adapter speed: 2000 kHz 1: (558) STDERR: adapter_nsrst_delay: 100 1: (558) STDERR: srst_only separate srst_nogate srst_open_drain connect_deassert_srst 1: (559) STDERR: Info : Listening on port 6666 for tcl connections 1: (560) STDERR: Info : Listening on port 4444 for telnet connections 1: (561) STDERR: Info : Unable to match requested speed 2000 kHz, using 1800 kHz 1: (561) STDERR: Info : Unable to match requested speed 2000 kHz, using 1800 kHz 1: (561) STDERR: Info : clock speed 1800 kHz 1: (741) STDERR: Info : STLINK v2 JTAG v29 API v2 SWIM v18 VID 0x0483 PID 0x374B 1: (742) STDERR: Info : using stlink api v2 1: (748) STDERR: Info : Target voltage: 3.234782 1: (764) STDERR: Warn : Silicon bug: single stepping will enter pending exception handler! 1: (834) STDERR: Info : stm32f7x.cpu: hardware has 8 breakpoints, 4 watchpoints 1: (863) STDERR: Info : Listening on port 3333 for gdb connections 1: (10531) <-logout

21 Dec 2017

I am - I think - on the same problem. The DebugServerArgs parameter looks wrong. I could fix it by setting the following value (with my board/interface settings):

"debugServerArgs": "-s \"C:\ \Program Files (x86)\ \OpenOCD\ \share\ \openocd\ \scripts\" -f \"board\ \st_nucleo_f7.cfg\" -f \"interface\ \stlink-v2-1.cfg\" -c init -c \"reset init\"",

(warning: remove blanks between \ \ in paths)

Where C:\Program Files (x86)\OpenOCD is the actual installation directory of OpenOCD in your system. The -s option I added tells OpenOCD where to search for .cfg scripts linked within other scripts. It was the reason for which it failed with the values suggested.

Probably the author had an openocd.cfg file correctly set in his OpenOCD/bin directory.

Please log in to post comments.