Skip to main content

Cortex-M Development Using VSCode

Visual Studio Code can be used to edit the source code, build the application and debug it. This guide will provide the necessary configuration files and instructions to set up your system.

Prerequisites

Follow the how-to guide Setting Up MCUXpresso SDK and Toolcahin for Cortex-M development and Cortex-M JTAG Debugging before continuing with this article.

Software Requirements

Visual Studio Code with Cortex-M Debug extension is required for this guide. C/C++ extension is optional, but highly recommend. It will help to developt new code for the Cortex-M.

info

For this guide, we used the Linux 64-bit DEB Installer version. Other HOST OSs were not tested.

Hardware Requirements

On the hardware side, this guide uses:

  • Toradex Evaluation Board (Colibri or Apalis), Development Board or Dahlia Board (Verdin).
  • J-Link hardware from Segger. This guide uses hardware version v10.1.

VS Code Development Workflow

Open your terminal and go to the folder where your SDK is located. For example, using Apalis iMX8 and SDK v2.9.0, we can go to the SDK_2_9_0_MIMX8QX5xxxFZ folder.

info

If you are using Ubuntu 22.04+ or a distro based on that, export the Python3.6 lib directory (export LD_LIBRARY_PATH=/home/user/python3.6/lib), as explained at Cortex-M JTAG Debugging.

Now we can open our VSCode inside this folder by running:

$ cd SDK_2_9_0_MIMX8QX5xxxFZ/
$ code .

Compiling Applications with VS Code

To compile our code automatically, we need to set up some tasks for VSCode run the toolchain automatically. This task will run the toolchain as described in Setting Up MCUXpresso SDK and Toolchain for Cortex-M development.

Create a folder called .vscode inside SDK_2_9_0_MIMX8QX5xxxFZ.

$ mkdir SDK_2_9_0_MIMX8QX5xxxFZ/.vscode

Create file called tasks.json inside SDK_2_9_0_MIMX8QX5xxxFZ/.vscode/, set ARMGCC_DIR and cwd variables path correctly, and paste the code below.

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format

// THIS CODE USES APALIS IMX8 AS AN EXAMPLE. PLEASE, SELECT THE RIGHT PATHS FOR YOUR BOARD.

"version": "2.0.0",
"options":
{
"env":
{
"ARMGCC_DIR": "/<PATH_TO_YOUR_TOOLCHAIN>/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi" //Put here the path to your toolchain
}
},
"tasks":
[
{
"label": "Build hello_world debug",
"type": "shell",
"options":
{
"cwd": "${workspaceRoot}/boards/mekmimx8qm/demo_apps/hello_world/cm4_core0/armgcc" // Put here the path for your build_debug.sh script
},
"command":
[
"./build_debug.sh"
],
"problemMatcher":
[
"$gcc"
]
},
{
"label": "Build hello_world release",
"type": "shell",
"options":
{
"cwd": "${workspaceRoot}/boards/mekmimx8qm/demo_apps/hello_world/cm4_core0/armgcc" // Put here the path for your build_release.sh script
},
"command":
[
"./build_release.sh"
],
"problemMatcher":
[
"$gcc"
]
}
]
}

Instead of exporting the ARMGCC_DIR inside the terminal, we can use the env parameter and set the GCC toolchain. After that, we created two tasks: one to compile the hello world code for debugging and another one for release. You can create as many tasks as you want, with different codes and scripts.

Now go back to your main code (hello_world.c for example), press F1 and execute Tasks: Run Task. Select the task that you have created and the compile process will begin automatically.

Loading and Debugging the Application Code

Press F1 inside VSCode and select “Open launch.json“ or create a file called launch.json inside SDK_2_9_0_MIMX8QX5xxxFZ/.vscode/. Place the configurations needed for Cortex-Debug inside of it (select the right configuration for your device):

info

If you're using Linux as the host OS, VSCode should find the path for JLinkGDBServer automatically with the option "servertype": "jlink". However, if VSCode doesn't find the server automatically, add the option "serverpath": "/path/to/JLinkGDBServer with the right path to the code below.

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

// THIS CODE USES APALIS IMX8 AS AN EXAMPLE. PLEASE, SELECT THE RIGHT PATHS FOR YOUR BOARD.

"version": "0.2.0",
"configurations": [
{
"type": "cortex-debug",
"request": "attach",
"name": "Debug J-Link",
"cwd": "${workspaceRoot}/boards/mekmimx8qm/demo_apps/hello_world/cm4_core0", // Put here the path for your armgcc folder
"executable": "armgcc/debug/hello_world_m40.elf", // Select the path for your .elf executable
"servertype": "jlink",
"device": "MIMX8QM6_M4_0", //Select Your Device here
"interface": "jtag",
"postAttachCommands": [
"monitor reset",
"monitor halt",
"load"
],
"armToolchainPath": "/<PATH_TO_GCC_TOOLCHAIN>/gcc-arm-11.2-2022.02-x86_64-arm-none-eabi/bin", // Select here the path for your toolchain
}
]
}

You will need to tell where the armToolchainPath is and set the right path for it.

Now save the file, open hello_world.c, and press F5. The debug will start!

vscode_cortexm_debugging

You can place breakpoints and run the code step by step using the VSCode debugger.



Send Feedback!