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

From SEGGER Wiki
Jump to: navigation, search
(Apple Mac OS X Setup)
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
__TOC__  
 
__TOC__  
 
= Visual Studio Code Debugging with J-Link  =
 
= Visual Studio Code Debugging with J-Link  =
Visual Studio Code is a free to use 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.  
+
Visual Studio Code 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.
+
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 on-line store([https://www.segger.com/evaluate-our-software/segger/empower/ 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([https://www.segger.com/evaluate-our-software/segger/empower/ 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"'''.
 
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"'''.
  
Line 23: Line 23:
  
 
'''Note''': from the above list download the packages according to your OS.
 
'''Note''': from the above list download the packages according to your OS.
 
 
  
 
= Microsoft Windows Setup =
 
= Microsoft Windows Setup =
  
Lets get started. Once you installed Visual Studio Code and the above plugins start by opening Visual Studio Code.
+
Let's get started. Once you installed Visual Studio Code and the above plugins start by opening Visual Studio Code.
 
 
  
 
'''Open the project folder'''   
 
'''Open the project folder'''   
  
 
Under File menu select Open Folder and select the EMPower project folder that you previously downloaded and extracted.
 
Under File menu select Open Folder and select the EMPower project folder that you previously downloaded and extracted.
 
  
 
[[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]]
  
Now we have to adapt the .json file with the bellow settings.
+
Now we have to adapt the .json file with the below settings.
 
   
 
   
 
<source lang="JavaScript">
 
<source lang="JavaScript">
Line 84: Line 76:
 
The “serverpath” should be your J-Link GDB server installation directory.
 
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.
+
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.
  
  
Line 100: Line 92:
 
[[Image:vs_code_debug_6.png||600px]]
 
[[Image:vs_code_debug_6.png||600px]]
  
In this Json configuration file you just add the compilerpath and the intelliSenseMode. Like bellow.  
+
In this JSON configuration file, you just add the compiler path and the intelliSenseMode. Like below.  
  
 
<source lang="JavaScript">
 
<source lang="JavaScript">
Line 125: Line 117:
  
 
Final result:
 
Final result:
 +
 
[[Image:vs_code_debug_7.png||600px]]
 
[[Image:vs_code_debug_7.png||600px]]
  
You can achieve the same result through the UI. Press F1 write “config” and select „C/C++:Edit Configurations (UI)“
+
You can achieve the same result through the UI. Press F1 write “config” and select „C/C++: Edit Configurations (UI)“
  
 
[[Image:vs_code_debug_8.png||600px]]
 
[[Image:vs_code_debug_8.png||600px]]
Line 137: Line 130:
 
[[Image:vs_code_debug_9.png||600px]]
 
[[Image:vs_code_debug_9.png||600px]]
  
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.
+
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.
  
Thats it. Now you can debug your application within Visual Studio Code.  
+
That's it. Now you can debug your target application within Visual Studio Code.
  
 
+
= Linux Ubuntu setup =
= Linux setup =
 
  
 
The bellow setup was tested with Linux Ubuntu 16.04.01 LTS.
 
The bellow setup was tested with Linux Ubuntu 16.04.01 LTS.
Line 172: Line 164:
 
J-Link installation  
 
J-Link installation  
  
In the directory where you downloaded the j-link installer you use the installer to perform the 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/.
 
The J-Link installation directory can be found under /opt/SEGGER/.
Line 203: Line 195:
  
  
You can perform the same through Json config file  
+
You can perform the same through JSON config file  
  
 
F1 → type config→ and Select „C/C++:Edit Configurations(JSON)
 
F1 → type config→ and Select „C/C++:Edit Configurations(JSON)
Line 240: Line 232:
  
 
Now we need to generate and configure the launch Debug file for the Cortex-Debug extension.  
 
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"
+
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||600px]]
Line 288: Line 280:
 
= Apple Mac OS X Setup =
 
= Apple Mac OS X Setup =
  
The setup for Mac OS-X is quite straightforward with one exception in the launch.json config file.  
+
The setup for Apple OS-X is quite straightforward with one exception in the launch.json config file.  
  
 
'''Visual Studio Code'''
 
'''Visual Studio Code'''
Line 296: Line 288:
 
'''ARM GCC installation'''  
 
'''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.''  
+
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.''  
  
 
[[Image:vs_code_debug_19.png||600px]]
 
[[Image:vs_code_debug_19.png||600px]]
Line 304: Line 296:
 
[[Image:vs_code_debug_20.png||600px]]
 
[[Image:vs_code_debug_20.png||600px]]
  
Then Press F1 and write “config”. From the drop down list select “C/C++ Edit configurations...”  
+
Then Press F1 and write “config”. From the drop-down list select “C/C++ Edit configurations...”  
  
 
[[Image:vs_code_debug_21.png||600px]]
 
[[Image:vs_code_debug_21.png||600px]]
Line 314: Line 306:
 
[[Image:vs_code_debug_23.png||600px]]
 
[[Image:vs_code_debug_23.png||600px]]
  
Lastly press again F1 and write launch and select “Debug: Open launch.json” followed by Cortex Debug option.
+
Lastly, press again F1 and write launch and select “Debug: Open launch.json” followed by Cortex Debug option.
  
 
[[Image:vs_code_debug_24.png||600px]]
 
[[Image:vs_code_debug_24.png||600px]]
Line 354: Line 346:
 
[[Image:vs_code_debug_26.png||600px]]
 
[[Image:vs_code_debug_26.png||600px]]
  
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).
+
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 =
 
= Final Remarks =

Revision as of 13:51, 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 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 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

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. 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 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/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 debugging 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 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

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

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