Panel For Example Panel For Example Panel For Example

Porting μC/OS-III to STM32: Step-by-Step

Author : Adrian May 12, 2026

 

Overview

μC/OS-III is a real-time operating system commonly used in embedded systems development. Porting μC/OS-III to STM32 series microcontrollers enables real-time task scheduling and multitasking. This article describes the detailed steps to port μC/OS-III to an STM32 platform.

 

1. Preparation

Before starting the port, prepare the following tools and environment:

  • STM32 development board
  • STM32CubeMX (for generating peripheral initialization code)
  • Keil MDK (for compiling and flashing)

 

2. Create the STM32 Project

Use STM32CubeMX to create a new STM32 project. Select the target STM32 microcontroller model and configure the clock source and pin assignments.

When configuring peripherals, choose a USART or UART to act as the μC/OS-III debug output interface. This interface will be used to print μC/OS-III debug information.

After generating the code, open the generated project in Keil MDK.

 

3. Add μC/OS-III Source Code

Download the latest μC/OS-III source package from the official distribution. Extract the files and copy the source files into appropriate folders inside the Keil MDK project.

 

4. Configure μC/OS-III

Open the generated project in Keil MDK and edit the ucos_cfg.h file to modify μC/OS-III configuration options.

Main configuration items include:

  • OS_TICKS_PER_SEC: Set the tick frequency, preferably matching the system tick configuration
  • OS_MAX_TASKS: Set the maximum number of tasks
  • OS_MAX_EVENTS: Set the maximum number of events
  • OS_SCHED_LOCK_EN: Enable/disable scheduler lock (for critical section protection)
  • OS_CPU_HOOKS_EN: Enable/disable CPU hook functions

Adjust these options according to project requirements and save the file.

 

5. Add Startup Code

μC/OS-III requires startup code. In the Keil MDK project, open the startup file (for example, startup_stm32xxxx.s) and add the following startup sequence:

; μC/OS-III Startup Code ; ---------------------------------- EXTERN __iar_program_start LDR R0, =__iar_program_start LDR R1, =main BX R1

 

6. Create μC/OS-III Tasks

In main(), initialize μC/OS-III and create tasks. First call OSInit() to initialize the kernel, then create tasks using OSTaskCreate().

Example:

#include "includes.h" void task1(void *p_arg) { while (1) { // Code for task1 OSTimeDly(1); // Delay 1 tick } } void task2(void *p_arg) { while (1) { // Code for task2 OSTimeDly(1); // Delay 1 tick } } int main(void) { OS_ERR err; OSInit(&err); // Create tasks OSTaskCreate(task1, NULL, &err); OSTaskCreate(task2, NULL, &err); OSStart(&err); while (1) { } }

 

7. Build and Flash

Build the project in Keil MDK and resolve any compilation errors. Then flash the generated hex or bin file to the STM32 development board.

 

8. Debug and Test

Connect the development board to a computer and open a serial terminal to the μC/OS-III debug output interface. Observe debug output such as task switches and delays to verify the RTOS behavior.

 

Conclusion

This article outlined the main steps to port μC/OS-III to an STM32 microcontroller: preparing the toolchain, creating a CubeMX project, integrating μC/OS-III source code, configuring the kernel, adding startup code, creating tasks, building and flashing, and performing debug and validation.