VSCode stm32-for-vscode, cortex-debug, Segger, STLink, STM32 Debugging, et al
The VSCode cortex-debug extension can debug using JLink, even for STM32, but you have to add a couple of things into the VSCode System to let it know what to do.
Once I had the STM32 being debugged with the STLinkV2 clone, I worked on debugging with my JLink. I connected the wires like I did with the STLink, but nothing happened.
Well, the problem was two fold, with the STM32-FOR-VSCODE extension needing to be setup properly, and the launch.json also needed work applied. That extension drops several files into the stm32 project directory. One of those files is openocd.cfg. It also places the .vscode directory in that folder, and inside that folder are the .json files that are used for various things such as build instructions.
First, you need to edit settings.json and put in the path to the JLinkGDBServer, like the following. You can also set up the STLink path if you wish:
{
"cortex-debug.armToolchainPath": "/Applications/VSCode_OnethinxPack_macOS/gcc-arm-none-eabi-10-2021-q1-update/bin",
"cortex-debug.openocdPath": "/opt/local/bin/openocd"
"cortex-debug.JLinkGDBServerPath": "/usr/local/bin/JLinkGDBServer"
}
Secondly, you need to click on the ST icon and change programmer line, and select jlink in the dropdown. That will change the openocd.cfg file to look like this:
#OpenOCD configuration file, generated by STM32 for VSCode
# Programmer, can be changed to several interfaces
# Standard will be the stlink interface as this is the standard for STM32 dev boards
source [find interface/jlink.cfg]
# The target MCU. This should match your board
source [find target/stm32f1x.cfg]
Note you have to go back through this process to switch back to the STLink.
Next, you have to add into your launch.json all the configurations possible. Note that choosing JLink debug if openocd.cfg points to STLink will give an error, with the current launch.json. However, these things are so simple in this configuration setting, it becomes obvious what is happening. Here is the new launch.json:
"configurations": [
{
"name": "Debug STM32 STLink",
"showDevDebugOutput": "parsed",
"cwd": "${workspaceRoot}",
"executable": "./build/blink2.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "openocd",
"preLaunchTask": "Build STM",
"device": "stm32f103x6.s",
"configFiles": [
"openocd.cfg"
]
},
{
"name": "Attach STM32 STLink",
"showDevDebugOutput": "parsed",
"cwd": "${workspaceRoot}",
"executable": "./build/blink2.elf",
"request": "attach",
"type": "cortex-debug",
"servertype": "openocd",
"preLaunchTask": "Build STM",
"device": "stm32f103x6.s",
"configFiles": [
"openocd.cfg"
]
},
{
"name": "JLink launch",
"cwd": "${workspaceRoot}",
"executable": "./build/blink2.elf",
"request": "launch",
"type": "cortex-debug",
"servertype": "jlink",
"device": "stm32f103x6",
"interface": "swd",
"runToMain": true,
"preLaunchTask": "Build STM", // configured in tasks.json
// "preLaunchCommands": ["Build all"], // you can execute command instead of task
"svdFile": "", // Include svd to watch device peripherals
"swoConfig":
{
"enabled": true,
"cpuFrequency": 160000000,
"swoFrequency": 4000000,
"source": "probe",
"decoders":
[
{
"label": "ITM port 0 output",
"type": "console",
"port": 0,
"showOnStartup": true,
"encoding": "ascii"
}
]
}
},
{
"name": "JLink attach",
"cwd": "${workspaceRoot}",
"executable": "./build/blink2.elf",
"request": "attach",
"type": "cortex-debug",
"servertype": "jlink",
"device": "stm32f103x6",
"interface": "swd",
"runToMain": true,
//"preLaunchTask": "Build all", // configured in tasks.json
// "preLaunchCommands": ["Build all"], // you can execute command instead of task
"svdFile": "", // Include svd to watch device peripherals
"swoConfig":
{
"enabled": true,
"cpuFrequency": 160000000,
"swoFrequency": 4000000,
"source": "probe",
"decoders":
[
{
"label": "ITM port 0 output",
"type": "console",
"port": 0,
"showOnStartup": true,
"encoding": "ascii"
}
]
}
},
Once you change programmer, and then select the proper debugger from the launch section, you debug the board using the debugger of your choice. You can switch between them. However, that is several steps, and in my career, switching between debuggers does not often happen.
I derived enough information from the following video (once I had stm32-for-vscode extension installed and working) to allow me to switch to the JLink and back. What I have done is clunky, but works: https://www.youtube.com/watch?v=g2Kf6RbdrIs
Fini
I hope to work with the XMC JLink once I get time, to see if the manufacturer specific JLink works. So far the various clones I have played with work.
Enjoy!