Visual Studio Code (VS Code for short) is a very popular source code editor that can also be used for embedded development. In embedded applications, how do you configure VS Code to build and debug projects? This article will show you how to set up VS Code as the development environment for FreeRTOS projects, introduce the extensions that need to be installed, and the options for environment configuration.
If you haven't used VS Code yet, you can visit the VS Code website (https://code.visualstudio.com/) to learn about its basic features and download the installer for your system. VS Code is a lightweight editor that can be tailored to your needs by adding extensions to get additional language support or other features. It supports debugging and integrates Git for source code control. In VS Code, you can open the folder where the source code is located through the File>Open Folder menu to work without creating additional project files. VS Code will recommend installing related extensions based on the open code. VS Code also has many advanced editing features, such as multiple pointers. You can open the "Interative playground" through the "Help" help menu to learn more about the features.
Basic settings and extensions
The FreeRTOS kernel is written in C, so you need to install the C/C++ extension in VS Code. If the FreeRTOS project uses CMake, you also need to install the CMake tool extension. If you want to deploy and debug the target device in VS Code, you can select the Cortex Debug extension. The following will show you how to configure the FreeRTOS project with these extensions.
You also need to set up a development environment for embedded development and install a cross-compiler for the target device, such as the GNU ARM Embedded tool-chain and burning/debugging tools. For specific work, refer to the target device development guide.
Creating a FreeRTOS Project
FreeRTOS provides example projects and getting started guides for many development boards, integrated development environments (IDEs), and compilers. Find a GCC project based on makefiles and try using VS Code.
Many chip manufacturers provide configuration tools that can generate FreeRTOS projects corresponding to the evaluation board. Use the configuration tool to generate a make or CMake-based project, select GCC as the target compiler, and the generated project can be easily used in VS Code. For information on how to use ST STM32CubeIDE or STM32CubeMX tools to generate FreeRTOS projects, please refer to ST official documents. Note that Makefile must be selected as the Toolchain/IDE option in the Project Manager tab.
NXP’s MCUXpresso configuration tools and Espressif’s IDF tools can also generate CMake-based FreeRTOS projects for their devices, and you can also use the development board demos included in the FreeRTOS AWS project.
After generating the FreeRTOS project, open the project directory in VS Code through the menu (File->Open Folder), or switch to the project directory through the command line and enter: code. to start VS Code.
The C++ extension provides IntelliSense for C and C++ files. In addition to simple syntax highlighting, it also provides automatic code completion based on variable types, function definitions, and more. To configure IntelliSense for the C++ extension, we need to let it know where the header files are, what the compile options are, and so on. Makefiles don't have an automatic way to handle this information, but it's relatively easy to add. Open the .vscode/c_cpp_properties.json file (press F1, select C/C++: Edit configurations (JSON), and generate the c_cpp_properties.json file). First, update the intelliSenseMode variable to gcc-arm and set the cross compiler path used in the compiler path variable CompilerPath. Open the project makefile to find the file include list and copy it to the includePath array in the c_cpp_properties.json file. Use the Multiline cursor feature of VS Code to add ${workspaceFolder} in front of each folder location to indicate its relative path. You can also add compiler options to the CompilerArgs array and flag definitions to the defines array.
The following is a sample c_cpp_properties.json file for a FreeRTOS project generated based on STM32CubeMX:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/Core/Inc",
"${workspaceFolder}/Drivers/STM32F7xx_HAL_Driver/Inc",
"${workspaceFolder}/Drivers/STM32F7xx_HAL_Driver/Inc/Legacy",
"${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/include",
"${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS_V2",
"${workspaceFolder}/Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM7/r0p1",
"${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32F7xx/Include",
"${workspaceFolder}/Drivers/CMSIS/Include"
],
"defines": [
"USE_HAL_DRIVER",
"STM32F767xx"
],
"compilerPath": "opt/GNU Arm Embedded Toolchain/10 2020-q4-major/bin/arm-none-eabi-gcc.exe",
"compilerArgs": [
"-mcpu=cortex-m7",
"-mthumb",
"-mfpu=fpv5-d16",
"-mfloat-abi=hard"
],
"cStandard": "gnu11",
"cppStandard": "gnu++14",
"intelliSenseMode": "gcc-arm"
}
],
"version": 4
}
In the VS Code terminal window (the Terminal window can be opened via View->Terminal), run make to build the application.
Compile errors are displayed in the Problems window, with direct navigation to the line of code that caused the error.
If you already have a FreeRTOS project using CMake, the CMake Tools extension in VS Code will automatically query the CMake cache for information that you had to configure manually before, but the extension will configure your project using the compiler it finds on your system as a "kit". If the extension prompts you to use a kit, you can select "unspecified" so that you can configure some additional options.
One problem you'll run into when using CMake in an embedded application is that it first tries to compile a simple C program to verify that there is a working C compiler. This doesn't work with a cross-compiler, since the compiled program is for a specific target system. There is an option to tell CMake not to initiate this. Open .vscode/setings.json and add the cmake.configSettings entry. In that structure, add CMAKE_C_COMPILER_WORKS TRUE to tell CMake not to perform this check. You can also pass additional arguments to CMake through this structure, for example, any arguments prefixed with -D in the build instructions.
Below is an example of a settings.json file for compiling a FreeRTOS AWS project. Note that the compiler, chip vendor, and development board options are all relevant to the actual running example. Add the relevant options to the cmake.configureSettings array as part of the command line for building the project using CMake, and you can build the project in VS Code.
Settings.json
{
"cmake.configureOnOpen": true,
"cmake.configureSettings": {
"COMPILER": "xtensa-esp32",
"CMAKE_C_COMPILER_WORKS": "TRUE",
"VENDOR": "espressif",
"BOARD": "esp32_wrover_kit"
},
"C_Cpp.default.configurationProvider": "ms-vscode.cmake-tools"
}
After updating the settings, you can right-click the CMakeLists.txt file or use the F1 shortcut key and select the CMake Delete cache and reconfigure option to rebuild the project. Compilation errors will be displayed in the "Problems" window.
The Cortex-Debug extension enables deployment and debugging of FreeRTOS applications on a device. This extension supports different hardware debuggers and corresponding software. You need to configure the environment according to the debugging tool, and then configure the extension to use the tool. See the wiki for instructions on enabling the extension for a specific configuration.
First, in VS Code, press F1 to display all commands, enter "launch", select open launch.json file, generate a basic launch.json file, and then configure the file according to the hardware debugger and target device.
Below is a sample launch.json configuration using the Nucleo-F746ZG development board and J-Link. The content that needs to be configured is to point to the executable file of the build output as well as the debug hardware and target board. These settings need to be updated according to the environment.
{
"version": "0.2.0",
"configurations": [
{
"cwd": "${workspaceRoot}",
"executable": "${workspaceRoot}/build/Nucleo_F746ZG_FreeRTOS_GCC.elf",
"name": "Debug J-Link",
"request": "launch",
"type": "cortex-debug",
"serverpath": "C:/Program Files (x86)/SEGGER/JLink/JLinkGDBServerCL.exe",
"servertype": "jlink",
"device": "STM32F746ZG",
"interface": "swd",
"jlinkscript": "",
"runToMain": true,
"svdFile": "",
},
]
}
You also need to update the settings.json file to tell the Cortex-Debug cross compiler installation path.
{
"cortex-debug.armToolchainPath": "opt/GNU Arm Embedded Toolchain/10 2020-q4-major/bin "
}
Press F5 or Run->Start Debugging to enter the debugging interface and debug the application.
In addition to allowing code refactoring and version control by installing extensions, VS Code can also extend this "simple" code editor to a multi-platform development environment. VS code can be used as a working environment for embedded development using FreeRTOS. The key is to use the appropriate extension functions to meet development needs and learn how to configure them for specific environments. Once set up, it is easy to use CMake or make FreeRTOS projects. I hope that what this article shows can help you try your own FreeRTOS project.
Related settings and installation URL: (WeChat does not support external links, please copy the link to your browser)
C/C++ extensions:
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cpptools
CMake Tools:
https://marketplace.visualstudio.com/items?itemName=ms-vscode.cmake-tools
Cortex Debug:
https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug
GNU ARM Embedded tool-chain:
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads
Wiki:
https://github.com/Marus/cortex-debug/wiki