Difference between revisions of "J-Link Visual Studio Code"

From SEGGER Wiki
Jump to: navigation, search
(Microsoft Windows Setup)
(Linux Ubuntu setup)
Line 177: Line 177:
 
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.
 
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.
   
[[Image:vs_code_debug_10.png||600px]]
+
[[Image:vs_code_debug_10.png||800px]]
   
 
Open the project folder that you Select Open folder from the File menu the emPower BSP.
 
Open the project folder that you Select Open folder from the File menu the emPower BSP.
   
[[Image:vs_code_debug_11.png||600px]]
+
[[Image:vs_code_debug_11.png||800px]]
   
 
Press F1 and write config and select C/C++:Edit Configurations(UI)
 
Press F1 and write config and select C/C++:Edit Configurations(UI)
   
   
[[Image:vs_code_debug_12.png||600px]]
+
[[Image:vs_code_debug_12.png||800px]]
   
 
Add the compiler path to
 
Add the compiler path to
Line 206: Line 206:
   
   
[[Image:vs_code_debug_13.png||600px]]
+
[[Image:vs_code_debug_13.png||800px]]
   
   
Line 236: Line 236:
 
Press F1 and type launch. From the drop-down list select "Debug:Open launch.json"
 
Press F1 and type launch. From the drop-down list select "Debug:Open launch.json"
   
[[Image:vs_code_debug_14.png||600px]]
+
[[Image:vs_code_debug_14.png||800px]]
   
 
Then select Cortex-Debug...
 
Then select Cortex-Debug...
   
[[Image:vs_code_debug_15.png||600px]]
+
[[Image:vs_code_debug_15.png||800px]]
   
 
The end result should look like this.
 
The end result should look like this.
   
[[Image:vs_code_debug_16.png||600px]]
+
[[Image:vs_code_debug_16.png||800px]]
   
 
Under the generated .json file add the below setup.
 
Under the generated .json file add the below setup.
Line 274: Line 274:
 
</source>
 
</source>
   
[[Image:vs_code_debug_17.png||600px]]
+
[[Image:vs_code_debug_17.png||800px]]
   
 
The Cortex-Debug launch.json should look like this in the end.
 
The Cortex-Debug launch.json should look like this in the end.
Line 282: Line 282:
 
To get the Start_emPower.elf go to BSP/SEGGER/K66FN2M0_emPower and build the Start_SEGGER_emPower.emProject using SEGGER Embedded Studio.
 
To get the Start_emPower.elf go to BSP/SEGGER/K66FN2M0_emPower and build the Start_SEGGER_emPower.emProject using SEGGER Embedded Studio.
   
[[Image:vs_code_debug_17_1.png||600px]]
+
[[Image:vs_code_debug_17_1.png||800px]]
   
 
Now you are ready to start debugging. Just press F5 or from the Menu Debug select "Start Debugging".#
 
Now you are ready to start debugging. Just press F5 or from the Menu Debug select "Start Debugging".#
   
[[Image:vs_code_debug_18.png||600px]]
+
[[Image:vs_code_debug_18.png||800px]]
   
 
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.
 
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.

Revision as of 15:02, 5 October 2020

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

  1. Visual Studio Code (link)
  2. GNU ARM Embedded tool-chain
    1. Note: under windows use version 7-2018 (link)
  3. J-Link (link)
  4. Visual Studio Code Plugins
    1. C/C++ for Visual Studio Code (link)
    2. Cortex-Debug (link)
    3. C/C++ Intellisense(link) optional
  5. SVD for the NXP MK66F device (link)
  6. 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.

vs code debug 1.png

Then press F1(to show all commands), type “launch” and select “Debug:Open launch.json” from the drop-down list.

vs code debug 2.png

Afterward, you will be prompted to select the environment. Choose “Cortex Debug”

vs code debug 3.png

The launch.json file will be created under .vscode folder within your project folder.

vs code debug 4.png

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.

vs code debug 4 1.png


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.


vs code debug 5.png


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)


vs code debug 6.png

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:

vs code debug 7.png

In the settings-file we have to specify the armToolchainPath. To do this, press F1 and type "settings" and select "Open Settings (JSON)":

vs code debug 8.png

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"

vs code debug 8 1.png

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.

vs code debug 9.png

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.

vs code debug 10.png

Open the project folder that you Select Open folder from the File menu the emPower BSP.

vs code debug 11.png

Press F1 and write config and select C/C++:Edit Configurations(UI)


vs code debug 12.png

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)


vs code debug 13.png


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"

vs code debug 14.png

Then select Cortex-Debug...

vs code debug 15.png

The end result should look like this.

vs code debug 16.png

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" 
    }
    ]
}

vs code debug 17.png

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.

vs code debug 17 1.png

Now you are ready to start debugging. Just press F5 or from the Menu Debug select "Start Debugging".#

vs code debug 18.png

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-8-2018-q4-major-mac.tar.bz2 -C /opt/

vs code debug 19.png

Open Visual Studio Code, and install the Extensions Cortex-Debug and C/C++ Intelisense.

Click on "Open Folder" and then select the EMPower project folder that you previously downloaded and extracted.

vs code debug 20.png

Then Press F1 and write “config”. From the drop-down list select “C/C++ Edit configurations...”

vs code debug 21.png

In the default generated json file change to the below settings.

{
  "configurations": [
  {
  "name": "Mac",
  "includePath": [
  "${workspaceFolder}/**"
  ],
  "defines": [],
  "macFrameworkPath": [
    "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/System/Library/Frameworks"
  ],
  "compilerPath": "/opt/gcc-arm-none-eabi-8-2018-q4-major",
  "cStandard": "c11",
  "cppStandard": "c++17",
  "intelliSenseMode": "gcc-x64"
  }
  ],
  "version": 4
}

vs code debug 23.png

Lastly, press again F1 and write launch and select “Debug: Open launch.json” followed by Cortex Debug option.

vs code debug 24.png

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_V684a/JLinkGDBServerCLExe",
      "servertype": "jlink",
      "armToolchainPath": "/opt/gcc-arm-none-eabi-8-2018-q4-major/bin/",
      "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"
    }
    ]
  }

Your launch .json file should look like this.

vs code debug 25.png


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.

You can now run a debug session by Pressing F5 or from the Debug Menu → Start debugging.

vs code debug 26.png

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.