DEV Community

Hedy
Hedy

Posted on

A minimal FreeRTOS example for an STM32 microcontroller

Here’s a minimal FreeRTOS example for an STM32 microcontroller (works with STM32CubeIDE or PlatformIO). This example creates two tasks (LED blink and serial print) to demonstrate multitasking.

1. Hardware Setup

  • MCU: STM32F103C8T6 (Blue Pill) or any STM32/ESP32.
  • Peripherals:

    • LED connected to PC13 (STM32) or any GPIO.
    • Serial port (UART) for debugging.

2. Code Example (STM32CubeIDE)
Step 1: Include FreeRTOS
In main.c, ensure FreeRTOS is enabled in STM32CubeMX:

  • Enable FREERTOS under "Middleware".
  • Set CMSIS_V1 or CMSIS_V2 API.

Step 2: Define Tasks

c
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"

// Task prototypes
void vBlinkTask(void *pvParameters);
void vSerialTask(void *pvParameters);

// Main function
void MX_FREERTOS_Init(void) {
  // Create Task 1: Blink LED every 500ms
  xTaskCreate(vBlinkTask, "Blink", 128, NULL, 1, NULL);

  // Create Task 2: Print to serial every 1s
  xTaskCreate(vSerialTask, "Serial", 128, NULL, 1, NULL);

  // Start scheduler (never returns)
  vTaskStartScheduler();
}

// Task 1: Blink LED
void vBlinkTask(void *pvParameters) {
  for (;;) {
    HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_13); // Toggle LED
    vTaskDelay(500 / portTICK_PERIOD_MS);    // Non-blocking delay
  }
}

// Task 2: Print to serial
void vSerialTask(void *pvParameters) {
  for (;;) {
    printf("Hello from FreeRTOS!\r\n");
    vTaskDelay(1000 / portTICK_PERIOD_MS);
  }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure Hardware (STM32CubeMX)

  1. Enable USART2 (or any UART) for serial output.
  2. Set PC13 as GPIO_Output for the LED.
  3. Generate code and flash the MCU.

3. Expected Output
LED: Blinks every 500ms.

Serial Monitor (e.g., PuTTY or Arduino IDE):

text
Hello from FreeRTOS!
Hello from FreeRTOS!
...
Enter fullscreen mode Exit fullscreen mode

Multitasking: Both tasks run concurrently without blocking each other.

4. Key FreeRTOS Functions Used

5. Customizing for Your Hardware

  • ESP32: Replace HAL_GPIO with ESP-IDF’s gpio_set_level().
  • Arduino: Use Serial.print() instead of printf.
  • Change Priorities: Adjust the uxPriority parameter in xTaskCreate (higher number = higher priority).

6. Debugging Tips

  • Stack Overflow: Increase stack size (e.g., 256 instead of 128 in xTaskCreate).
  • Task Monitoring: Use uxTaskGetNumberOfTasks() to list active tasks.
  • Serial Debug: Enable configUSE_TRACE_FACILITY in FreeRTOSConfig.h.

Next Steps

  1. Add a Mutex: Protect shared UART access.
  2. Use Queues: Pass data between tasks (e.g., sensor readings → serial).
  3. Enable Tickless Mode: For low-power applications.

Top comments (0)