on 2026-03-19 4:30 AM
This article provides a step-by-step guide to developing FreeRTOS™ applications on STM32 microcontrollers using STM32CubeMX2 and Visual Studio Code. It covers configuring FreeRTOS™ within STM32CubeMX2, generating and building the project in VSCode, and setting up debugging with the STM32CubeIDE extension. The tutorial also explains how to debug FreeRTOS™ tasks and synchronization primitives effectively to facilitate real-time troubleshooting and development.
FreeRTOS™ is a widely used real-time operating system kernel designed for embedded devices, providing multitasking capabilities, task synchronization, and intertask communication. Leveraging FreeRTOS™ on STM32 microcontrollers enables developers to build responsive and efficient applications that can handle multiple concurrent operations with precise timing control.
STM32CubeMX2 simplifies the process of configuring FreeRTOS™ by providing an intuitive graphical interface to set up tasks, queues, semaphores, and other RTOS objects. Combined with Visual Studio Code and the STM32Cube extension, developers can seamlessly generate, build, and debug FreeRTOS™ projects in a modern, lightweight development environment.
This article guides you through the complete workflow of creating a FreeRTOS™-enabled STM32 project using CubeMX2. It covers importing into VS Code, building the application, and configuring the debugger to monitor and troubleshoot FreeRTOS™ tasks in real time. By following this tutorial, you gain practical knowledge to accelerate your embedded development with FreeRTOS™ on STM32 platforms.
Install the following tools:
The hardware used in this tutorial is the NUCLEO-C562RE board.
static void function1(void *pvParameters)
{
for(;;)
{
/* Infinite loop executing HighPriorityTask functionality. */
HAL_GPIO_TogglePin(PA5_PORT, PA5_PIN); // Toggle User LED
vTaskDelay(pdMS_TO_TICKS(500)); // Delay for 500 ms (task blocks here)
}
}
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "mx_freertos_app.h"
#include "mx_gpio_default.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private functions prototype -----------------------------------------------*/
mx_gpio_default_init();
app_synctasks_init ();
vTaskStartScheduler();
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "mx_freertos_app.h"
#include "mx_gpio_default.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private functions prototype -----------------------------------------------*/
/**
* brief: The application entry point.
* retval: none but we specify int to comply with C99 standard
*/
int main(void)
{
/** System Init: this code placed in targets folder initializes your system.
* It calls the initialization (and sets the initial configuration) of the peripherals.
* You can use STM32CubeMX to generate and call this code or not in this project.
* It also contains the HAL initialization and the initial clock configuration.
*/
if (mx_system_init() != SYSTEM_OK)
{
return (-1);
}
else
{
/*
* You can start your application code here
*/
mx_gpio_default_init();
app_synctasks_init ();
vTaskStartScheduler();
while (1) {}
}
} /* end main */
Note: Under normal operation, the scheduler takes control and the while loop is never executed.
Edit the generated launch.json to support debugging FreeRTOS™ with all features enabled.
(Before)
{
"version": "0.2.0",
"configurations": [
{
"type": "stlinkgdbtarget",
"request": "launch",
"name": "STM32Cube: STM32 Launch ST-Link GDB Server",
"origin": "snippet",
"cwd": "${workspaceFolder}",
"preBuild": "${command:st-stm32-ide-debug-launch.build}",
"runEntry": "main",
"imagesAndSymbols": [
{
"imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}"
}
]
}
]
}
(After)
{
"version": "0.2.0",
"configurations": [
{
"type": "stlinkgdbtarget",
"request": "launch",
"name": "STM32C5_FreeRTOS Debug",
"origin": "snippet",
"cwd": "${workspaceFolder}",
"preBuild": "${command:st-stm32-ide-debug-launch.build}",
"runEntry": "main",
"imagesAndSymbols": [
{
"imageFileName": "${command:st-stm32-ide-debug-launch.get-projects-binary-from-context1}"
}
],
"serverRtos": {
"enabled": true,
"port": "60000", // Choose a free local TCP port
"driver": "freertos"
},
}
]
}
Explanation of the changes:
The "name" field was updated from a generic label to "STM32C5_FreeRTOS Debug" to identify the target board and make the configuration easier to recognize in the VS Code interface.
The "serverRtos" block is what enables RTOS‑aware debugging in STM32/VSCode setup and tells the tools that you are using FreeRTOS™.
Without "serverRtos", you still can debug normally (breakpoints, memory, peripherals), but you lose the RTOS‑aware features and see only raw threads/PC, not the FreeRTOS™ task model.
Additionally, with "serverRtos" enabled you get an additional RTOS panel in VS Code that shows richer information about the system at runtime. This makes it much easier to analyze and debug FreeRTOS behavior.
If the RTOS view does not show FreeRTOS tasks or states properly, you may need to add the stlinkgdbtarget debug type to your VS Code extension configurations.
"mcu-debug.rtos-views.trackDebuggers": [
"stlinkgdbtarget",
],
By following the steps outlined in this article, you can successfully create, build, and debug FreeRTOS™ applications on STM32 microcontrollers using STM32CubeMX2 and Visual Studio Code. This tutorial provides a practical approach covering system initialization, task creation, scheduler startup, and real-time debugging with RTOS awareness.
Leveraging the modular initialization function (app_synctasks_init) for task setup and the powerful debugging features available in VS Code, you gain an efficient workflow to develop and troubleshoot FreeRTOS™-based embedded applications. This foundation helps ensure a smooth and effective development experience tailored to your project requirements.