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

From SEGGER Wiki
Jump to: navigation, search
(Microsoft Windows Setup)
(Microsoft Windows Setup)
Line 33: Line 33:
   
 
[[Image:vs_code_debug_1.png|600px]]
 
[[Image:vs_code_debug_1.png|600px]]
 
   
 
Then press F1(to show all commands), type “launch” and select “Debug:Open launch.json” from the drop-down list.
 
Then press F1(to show all commands), type “launch” and select “Debug:Open launch.json” from the drop-down list.
 
   
 
[[Image:vs_code_debug_2.png||600px]]
 
[[Image:vs_code_debug_2.png||600px]]
   
Afterwards you will be prompted to select the environment. Chose “Cortex Debug”
+
Afterward you will be prompted to select the environment. Chose “Cortex Debug”
 
   
 
[[Image:vs_code_debug_3.png||600px]]
 
[[Image:vs_code_debug_3.png||600px]]
   
 
The launch.json file will be created under .vscode folder within your project folder.
 
The launch.json file will be created under .vscode folder within your project folder.
 
   
 
[[Image:vs_code_debug_4.png||600px]]
 
[[Image:vs_code_debug_4.png||600px]]

Revision as of 12:21, 10 July 2019

Visual Studio Code Debugging with J-Link

Visual Studio Code is a free of charge source code editor from Microsoft that is available for Windows, Linux and macOS. 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 bellow configurations will re-flash your target, reset and attach to debug. If you which to add the option 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

Lets 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. Chose “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 bellow 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/Release/Start_emPower.elf",
	"serverpath": "C:/Program Files (x86)/SEGGER/JLink_V647d/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 debug one target you can just comment this entry out.


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 compilerpath and the intelliSenseMode. Like bellow.

{
  "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

You can achieve the same result through the UI. Press F1 write “config” and select „C/C++:Edit Configurations (UI)“

vs code debug 8.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 bellow output.

vs code debug 9.png

On the Left panel is possible to view the debug Variables(Locals, Global and Static) Watchs , Call Stack, Breakpoints, MCU Peripherals and the Core registers. All the basic and relevant information for debugging your project.

Thats it. Now you can debug your application within Visual Studio Code.

Linux setup

The bellow setup was tested with Linux Ubuntu 16.04.01 LTS.


ARM GCC installation

$ sudo apt-get -y install lib32z1 lib32ncurses5 lib32bz2-1.0
$ sudo apt-get -y install lib32ncurses5
$ sudo apt-get -y install ia32-libs
$ wget -O ${HOME}/Downloads/gcc-arm-none-eabi-8-2018-q4-major-linux.tar.bz2 https://developer.arm.com/-/media/Files/downloads/gnu-rm/8-2018q4/gcc-arm-none-eabi-8-2018-q4-major-linux.tar.bz2?revision=d830f9dd-cd4f-406d-8672-cca9210dd220?product=GNU%20Arm%20Embedded%20Toolchain,64-bit,,Linux,8-2018-q4-major
$ mkdir -p "${HOME}/opt"
$ cd "${HOME}/opt"
$ tar xjf ~/Downloads/gcc-arm-none-eabi-8-2018-q4-major-linux.tar.bz2
$ chmod -R -w "${HOME}/opt/gcc-arm-none-eabi-8-2018-q4-major"

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 bellow by changing the user and directory where you installed ARMs toolchain

  {
  "configurations": [
  {
  "name": "Linux",
  "includePath": [
  "${workspaceFolder}/**"
  ],
  "defines": [
  "\"_DEBUG\",",
  "\"UNICODE\",",
  "\"_UNICODE\""
  ],
  "compilerPath": "/home/anybody/opt/gcc-arm-none-eabi-8-2018-q4-major/bin/arm-none-eabi-gcc",
  "cStandard": "c11",
  "cppStandard": "c++17",
  "intelliSenseMode": "gcc-x64"
  }
  ],
  "version": 4
  }

The end result should look like this.

vs code debug 16.png

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

Under the generated .json file add the bellow 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/Release/Start_emPower.elf",
      "serverpath": "/opt/SEGGER/JLink/JLinkGDBServerCLExe", 
      "servertype": "jlink",
      "device": "MK66FX1M0xxx18",
      "interface": "jtag",
      "serialNumber": "679000005", //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": "/home/anybody/opt/gcc-arm-none-eabi-8-2018-q4-major/bin/",
      "svdFile": "${workspaceRoot}/SVD/MK66F18.svd" 
    }
    ]
  }

vs code debug 17.png

The Cortex-Debug launch.json should look like this in the end.

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 Mac 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 Downloads folder). Just unzip the folder to a directory of your choice. I recommend under /opt.

vs code debug 19.png

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

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

vs code debug 22.png

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

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 bellow 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.

vs code debug 25.png

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, brerakpoints, 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.