VS Code

This is a new guide and may contain inaccuracies, if you find any incorrect information or have ideas for a more streamlined process, you are encouraged to share them with the TAs so we can update these instructions accordingly

VSCode is a much lighter IDE than CLion and is an excellent choice for those looking for a lightweight IDE which allows a large degree of freedom when it comes to customizations. VSCode also has a lot of useful extensions that can make it easier to use and write better quality code. Like with other IDEs, it is possible to build and flash with a single click, without having to activate the virtual environment every time you wish to use west and debugging is much simpler, as breakpoints can be set through the IDe and the values of variables can also be viewed through the GUI. This guide is similar to this Zephyr VS Code example, which can be used as reference to modify and/or add additional functionality.

Note: A lot of commands and paths are OS and PC specific, so it is best you understand what each task does. Simply copy pasting the templates will not work!

Required Extensions

Make sure the following VSCode extensions are installed prior before going to the next steps.

  • C/C++
  • CMake Tools
  • Cortex-Debug

Setting up compile comands and configuring C/C++ extensions

You can follow Zephyr's guide to set up linting and navigation. Once you do this, you should be able to navigate to header files and syntax highlighting should no longer give incorrect errors and/or warnings.

Flashing using OpenOCD

We can configure a task in task.json (should be created inside the .vscode directory) that builds the application code and then flashes the board using OpenOCD. To do this, first create a new tasks.json file and then use the template below (use the one corresponding to your OS!) to configure a task that can build and flash the application onto the board.

Linux

{
    "label": "Build & Flash using West", // Feel free to give it any label you like
    "type": "shell",
    // Remember to update paths so they match your zephyr installation and your assignment
    "command": "cd ~/zephyrproject && source ./.venv/bin/activate && west build -p always -b stm32f4_disco <<replace with path to assignment repo>> && west flash -r openocd && deactivate",
    "group": {
        "kind": "build",
        "isDefault": true
      },
},

Windows

{
    "label": "Build Zephyr Project with West and Flash with OpenOCD",// Feel free to give it any label you like
    "type": "shell",
    "command": "cmd.exe",
    "args": [
    "/c",
    // Remember to update paths so they match your zephyr installation and your assignment
    "cd zephyrproject && .\\.venv\\Scripts\\activate && west build -p always -b stm32f4_disco <<replace with path to assignment repo>> && west flash -r openocd"
    ],
    "problemMatcher": [],
    "group": {
    "kind": "build",
    "isDefault": true
    },
},

Once configured, you can run it using the shortcut Ctrl+Shift+B.

Debug using OpenOCD and Cortex Debug

VS Code can also be used for debugging with OpenOCD and GDB.

In order to debug using the IDE, a new launch configuration has to be defined that launches the gdb debugger and indicates to it the OpenOCD server to connect to. At the same time a new task has to be created that launches OpenOCD debug server for GDB to connect to.

The first step is the configuration for gdb. We use cortex-debug for this and a template to configure can be seen below (the configuration should be placed in .vscode/launch.json):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug",
            "type": "cortex-debug",
            "cwd": "<<if you used the script, then your home directory>>/zephyrproject", //We will launch GDB from the Zephyr project directory - path to your Zephyr installation here
            "executable":  "./zephyr/build/zephyr/zephyr.elf", //Path to executable
            "request": "launch",
            "servertype": "external", //External server - we launch OpenOCD separately
            /// On Windows, look for `arm-none-eabi-gdb.exe`
            "gdbPath": "/usr/bin/arm-none-eabi-gdb", //Replace with the path to GDB on your PC!, if you don't have GDB, you will have to install it!
            "gdbTarget": "localhost:3333",
            "preLaunchTask": "openocd" //We ask VSCode to launch OpenOCD (the launch configured as a task) before GDB
        },
    ]
}

The template for the task (to be placed as a separate task inside .vscode/tasks.json) to launch OpenOCD:

{
  "tasks": [
    // Other tasks
    {
        "name": "openocd",
        "type": "shell",
        "command": "/usr/bin/openocd", //Remember to use the correct path to the openocd executable! On Windows, look for `openocd.exe`
        "args": [
            "-f"
            "board/stm32f4discovery.cfg" //Link to the board config file goes here, this is the one in your OpenOCD installation, not from Zephyr!
        ],
        "problemMatcher": [""],
        "isBackground" : true, // We run this in the background, so we will have to remember to close it once we are done using it!
    }
  ]
}

You can also choose to manually launch OpenOCD using the terminal. To do so, start debugging through the GUI or using F5 and then in a new VSCode terminal instance execute the commands corresponding to your OS. Note that you may see a pop-up saying that openocd doesn't exit, click on debug anyway and continue. This is just VSCode reminding us that this task is will continue running in the background and has to be closed manually.

Windows Users

    "REPLACE with OpenOCD path" -f "Path to board file here"

Linux Users

    /usr/bin/openocd -f "Path to board file here" #Your OpenOCD path may be different!