Comparison between bare-metal programming and using an RTOS on STM32 microcontrollers



This content originally appeared on DEV Community and was authored by Hedy

Comparison: Bare-Metal vs. RTOS on STM32

Image description

Example – STM32 without RTOS (Bare-Metal):

c

int main(void) {
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

    while (1) {
        if (HAL_GPIO_ReadPin(B1_GPIO_Port, B1_Pin) == GPIO_PIN_SET) {
            HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
            HAL_Delay(500);
        }
    }
}

Example – STM32 with FreeRTOS:

c

void LEDTask(void *argument) {
    for (;;) {
        HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
        vTaskDelay(pdMS_TO_TICKS(500));
    }
}

int main(void) {
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();

    xTaskCreate(LEDTask, "LED", 128, NULL, 1, NULL);
    vTaskStartScheduler();

    while (1) {} // Never reached
}

When Should You Use Bare-Metal or RTOS on STM32?
Use Bare-Metal if:

  • Your project is simple (e.g., LED, basic sensors)
  • You need maximum control and minimum latency
  • Flash and RAM are limited (STM32F0, L0)
  • You’re developing a low-power device and want to manage power manually

Use RTOS if:

  • You need to handle multiple tasks simultaneously (e.g., sensors + communication + display)
  • Your project is growing and requires modularity
  • You depend on timing precision, synchronization, and task priorities
  • You’re working with middleware like USB, TCP/IP, BLE (many ST libraries are RTOS-friendly)

STM32CubeMX Advantage:
ST’s STM32CubeMX allows you to generate projects with or without an RTOS:

  • Choose “RTOS: CMSIS-RTOS2 (FreeRTOS)” in the middleware section
  • Tasks can be configured conveniently (stack size, priority, etc.)


This content originally appeared on DEV Community and was authored by Hedy