J-Link Visual Studio Code
Contents
Debugging with J-Link
There are 3rd party plugins available that enable Visual Studio Code debugging on embedded targets via GDB + J-Link + GDBServer. is a free of charge source code editor from Microsoft that is available for Windows, Linux and OS-X. Besides allowing to do code refactoring and version control by installing extensions it is possible to extend this “simple” code editor to a multi-platform development environment. In this tutorial, we will cover only how to add Debug capability to Cortex cores Microcontrollers over debug probe J-Link. For this example purpose, we will be using the SEGGER‘s emPower v2.0 evaluation board. Our target MCU will be the NXP MK66FX1M0xxx18, you can acquire it in our online store(link). Please notice that the below configurations will re-flash your target, reset and attach to debug. If you wish to add the option to attach to a running target, just change the launch.json "request": "launch" to "request": "attach".
Requirements
- Visual Studio Code (link)
- GNU ARM Embedded tool-chain
- Note: under windows use version 7-2018 (link)
- J-Link (link)
- Visual Studio Code Plugins
- SVD for the NXP MK66F device (link)
- SEGGER Evaluation Software for emPower (link)
Note: from the above list download the packages according to your OS.
Microsoft Windows Setup
Let's get started. Once you installed Visual Studio Code and the above plugins, start by opening Visual Studio Code.
Open the project folder
Under File menu select Open Folder and select the EMPower project folder that you previously downloaded and extracted.
Then press F1(to show all commands), type “launch” and select “Debug:Open launch.json” from the drop-down list.
Afterward, you will be prompted to select the environment. Choose “Cortex Debug”
The launch.json file will be created under .vscode folder within your project folder.
Now we have to adapt the .json file with the below settings.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "cortex-debug",
"request": "launch",
"name": "Debug J-Link",
"cwd": "${workspaceRoot}",
"executable": "${workspaceRoot}/BSP/SEGGER/K66FN2M0_emPower/Output/Debug/Start_emPower.elf",
"serverpath": "C:/Program Files (x86)/SEGGER/JLink_V687b/JLinkGDBServerCL.exe",
"servertype": "jlink",
"device": "MK66FX1M0xxx18",
"interface": "jtag",
"serialNumber": "", //if you have more than one J-Link probe add the serial number here
"jlinkscript":"${workspaceRoot}/BSP/SEGGER/K66FN2M0_emPower/Setup/Kinetis_K66_Target.js",
"runToMain": true,
"svdFile": "${workspaceRoot}/SVD/MK66F18.svd"
}
]
}
The “serverpath” should be your J-Link GDB server installation directory.
If you have more than one J-Link probe connected, add your probe serial number. If you are only debugging one target, you can just comment this entry out.
To get the Start_emPower.elf go to BSP/SEGGER/K66FN2M0_emPower and build the Start_SEGGER_emPower.emProject using SEGGER Embedded Studio.
Note:
The MK66F18.svd file can be found under Keil.Kinetis_K60_DFP.1.5.0/SVD from the above provided link from Keil. Copy this folder to your EMPower folder.
The last step is to setup the ARMs GDB toolchain. Press F1 and write "config". From the drop-down menu chose C/C++:Edit Configurations (JSON)
In this JSON configuration file you just add the compiler path and the intelliSenseMode. Like below.
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/GUI/Inc"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "gcc-x64",
"compilerPath": "C:\\Tool\\C\\Arm\\7_2018-q2-update\\bin\\arm-none-eabi-gcc.exe"
}
],
"version": 4
}
Final result:
In the settings-file we have to specify the armToolchainPath. To do this, press F1 and type "settings" and select "Open Settings (JSON)":
The following line should lead to the folder where your arm-none-eabi-gdb.exe is located:
"cortex-debug.armToolchainPath": "C:\\Tool\\C\\Arm\\7_2018-q2-update\\bin"
The setup is now all done. You can start debugging by simply pressing F5 or from the Debug Menu → Start Debugging.
If you did the above steps you should expect the below output.
On the Left panel is possible to view the debug Variables(Locals, Global and Static) Watch, Call Stack, Breakpoints, MCU Peripherals and the Core registers. All the basic and relevant information for debugging your project.
That's it. Now you can debug your target application within Visual Studio Code.
Linux Ubuntu setup
The below setup was tested with Linux Ubuntu 16.04.01 LTS.
ARM GCC installation
$ sudo apt-get -y install gcc-arm-none-eabi binutils-arm-none-eabi gdb-arm-none-eabi openocd
Microsoft Visual Studio Code installation
$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg
$ sudo install -o root -g root -m 644 microsoft.gpg /etc/apt/trusted.gpg.d/
$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main" > /etc/apt/sources.list.d/vscode.list'
$ sudo apt-get install apt-transport-https
$ sudo apt-get update
$ sudo apt-get install code
J-Link installation
In the directory where you downloaded the j-link installer, you use the installer to perform the installation.
The J-Link installation directory can be found under /opt/SEGGER/.
In the terminal just type code to launch Visual Studio Code and install the following extensions under Visual Studio Code Extension Manager(Left panel Extensions symbol) Cortex Debug and C/C++ IntelliSense.
Open the project folder that you Select Open folder from the File menu the emPower BSP.
Press F1 and write config and select C/C++:Edit Configurations(UI)
Add the compiler path to
/home/user/opt/gcc-arm-none-eabi-8-2018-q4-major/bin/arm-none-eabi-gcc
the Defines
"_DEBUG",
"UNICODE",
"_UNICODE"
You can perform the same through JSON config file
F1 → type config→ and Select „C/C++:Edit Configurations(JSON)
Adapt your configuration like below.
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"\"_DEBUG\",",
"\"UNICODE\",",
"\"_UNICODE\""
],
"compilerPath": "/usr/bin/arm-none-eabi-gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "gcc-x64"
}
],
"version": 4
}
Now we need to generate and configure the launch Debug file for the Cortex-Debug extension. Press F1 and type launch. From the drop-down list select "Debug:Open launch.json"
Then select Cortex-Debug...
The end result should look like this.
Under the generated .json file add the below setup.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "cortex-debug",
"request": "launch",
"name": "Debug J-Link",
"cwd": "${workspaceRoot}",
"executable": "${workspaceRoot}/BSP/SEGGER/K66FN2M0_emPower/Output/Debug/Start_emPower.elf",
"serverpath": "/opt/SEGGER/JLink/JLinkGDBServerCLExe",
"servertype": "jlink",
"device": "MK66FX1M0xxx18",
"interface": "jtag",
"serialNumber": "", //if you have more than one J-Link probe add the serial number here
"jlinkscript":"${workspaceRoot}/BSP/SEGGER/K66FN2M0_emPower/Setup/Kinetis_K66_Target.js",
"runToMain": true,
"armToolchainPath": "/usr/bin/",
"svdFile": "${workspaceRoot}/SVD/MK66F18.svd"
}
]
}
The Cortex-Debug launch.json should look like this in the end.
To get the .svd files, extract the Keil.Kinetis_K60_DFP.1.5.0.pack and copy the SVD folder into the main project folder.
To get the Start_emPower.elf go to BSP/SEGGER/K66FN2M0_emPower and build the Start_SEGGER_emPower.emProject using SEGGER Embedded Studio.
Now you are ready to start debugging. Just press F5 or from the Menu Debug select "Start Debugging".#
On the left Debug panel, you may find the debug variables, watch, call stack, breakpoints, peripherals registers and the core registers. All you need to start debugging your target.
Apple Mac OS X Setup
The setup for Apple OS-X is quite straightforward with one exception in the launch.json config file.
Visual Studio Code
Download the Visual Studio for Mac. Unzip the App to your Applications folder or any other folder of your choice.
ARM GCC installation
Open a terminal window and go to the directory where you download the ARM GCC toolchain(most likely in the Downloads folder). Just unzip the folder to a directory of your choice. I recommend under /opt.
$ sudo tar -xjvf gcc-arm-none-eabi-9-2020-q2-update-mac.tar.bz2 -C /opt/
Open Visual Studio Code, and install the Extensions Cortex-Debug and C/C++ Intelisense.
Under File menu select Open and select the EMPower project folder that you previously downloaded and extracted.
Then Press F1 and write “config”. From the drop-down list select “C/C++ Edit configurations...”
In the default generated json file change to the below settings.
Lastly, press again F1 and write launch and select “Debug: Open launch.json” followed by Cortex Debug option.
Change the default .json file to the below settings with the extra line for the compiler. In the current version from Cortex-Debug plugin/extension (v0.3.0) is not possible to run a Debug session without previously specifying the “armToolchainPath”.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"request": "launch",
"type": "cortex-debug",
"name": "Debug J-Link",
"cwd": "${workspaceRoot}",
"executable": "${workspaceRoot}/BSP/SEGGER/K66FN2M0_emPower/Output/Release/Start_emPower.elf",
"serverpath": "/Applications/SEGGER/JLink_V647e/JLinkGDBServerCLExe",
"servertype": "jlink",
"armToolchainPath": "/opt/gcc-arm-none-eabi-8-2018-q4-major/bin/",
"device": "MK66FX1M0xxx18",
"interface": "jtag",
"jlinkscript":"${workspaceRoot}/BSP/SEGGER/K66FN2M0_emPower/Setup/Kinetis_K66_Target.js",
"runToMain": true,
"svdFile":"${workspaceRoot}/SVD/MK66F18.svd"
}
]
}
Your launch .json file should look like this.
You can now run a debug session by Pressing F5 or from the Debug Menu → Start debugging.
On the left panel(Debug Panel) you will find all the relevant information for debugging your project(Variables, Watch,.call stack, breakpoints, peripherals registers and cortex registers).
Final Remarks
Visual Studio Code is a good freeware alternative to Eclipse based development environments when you add and set the extensions for debugging and compiling.
In the above configurations we only add the option "request": "launch", but if you wish to attach to a running target you can simply change this by set it to "request": "attach". Alternatively you can add an extra .json file for attach to target and assign another short-cut key to it. Leaving you this way with the two options.
If you wish to compile within Visual Studio Code there are plenty of tutorials online that can help you, setting up your environment by use of ARM GCC toolchain.
That's it. Happy debugging with J-Link under Visual Studio Code.