Bare-Metal RTOS on STM32

I built a preemptive real-time operating system from the reset vector up, then used a small vehicle to exercise the scheduler and drivers.

Embedded · 4 months · CMU

STM32 Cortex-M4Rate-monotonic scheduling
In this build

My contribution. For this CMU embedded systems course project, I developed the RTOS kernel, boot code, device drivers, carrier PCB, PID control, and system integration.

Overview

I built a preemptive RTOS on an STM32 Cortex-M4, from the ARM reset vector and exception handlers to the scheduler and device drivers. To exercise it on hardware, I ran a motor-control loop, interrupt-driven UART telemetry, and LCD updates as concurrent tasks on a small vehicle.

Kernel and drivers

I wrote the system bootloader in ARMv7-M assembly to initialize the exception vector table and stack pointers, then built bare-metal C drivers using MMIO for PWM, ADC, I2C, and interrupt-driven UART.

Preemptive multitasking uses the ARM Cortex-M exception model with separate process and kernel stacks for each thread. Thread code runs on PSP; exception handlers use MSP. SysTick tracks thread budgets and pends PendSV. Its assembly wrapper saves PSP and the remaining registers, while the C handler records that kernel frame in the thread control block (TCB) and selects the next runnable thread. The same wrapper then restores the selected context before exception return.

Context switch flow from SysTick through the PendSV handler Thread A runs on the user stack until SysTick pends PendSV. The PendSV wrapper saves Thread A's registers on its kernel stack. Its C handler records the frame in the TCB and calls the scheduler. The scheduler selects Thread B, whose saved frame returns to the same wrapper for restoration before exception return to Thread B's process stack. USER MODE HANDLER MODE Thread A running on PSP Thread B resumes on PSP PendSV HANDLER 1. Save PSP, R4–R11 + LR 2. Record kernel frame in TCB 3. Select next runnable thread 4. Restore registers + PSP SysTick tick TCB list state + priority Context switch flow from SysTick through the PendSV handler Thread A runs on the user stack until SysTick pends PendSV. The PendSV wrapper saves Thread A's registers on its kernel stack. Its C handler records the frame in the TCB and calls the scheduler. The scheduler selects Thread B, whose saved frame returns to the same wrapper for restoration before exception return to Thread B's process stack. USER MODE Thread A running on PSP HANDLER MODE SysTick tick PendSV HANDLER 1. Save PSP, R4–R11 + LR 2. Record kernel frame in TCB 3. Select next runnable thread 4. Restore registers + PSP TCB list state + priority USER MODE Thread B resumes on PSP
SysTick requests PendSV. Selection and restoration happen within that one handler before Thread B resumes.

Task priorities are assigned by a rate-monotonic scheduler validated against the utilization bound, with an SVC path bridging user and kernel space, newlib support for printf and malloc, and priority-inheritance mutexes (HLP/IPCP) to bound priority inversion.

Scheduling on the bench

The selected PID, UART, and LCD parameters produce 71.25% modeled utilization, below the 77.98% rate-monotonic utilization bound for three tasks. The simulator below reconstructs that task set so you can explore the analytical schedulability margin.

Modeled task utilization
71.25% across PID, UART, and LCD threads
Schedulability bound
77.98% for three tasks
Scheduling result
Analytically schedulable under the selected RMS task parameters
Vehicle carrier board PCB layout
Vehicle carrier board PCB layoutAdjust the schedulerPointerClick the −/+ controls to change task timing.TouchTap the −/+ controls to change task timing.KeyboardTab to each control and press Enter or Space.

Test platform

I designed a two-layer carrier board in Autodesk Fusion to connect the STM32 Nucleo to the vehicle hardware. It distributes 5 V and 3.3 V through LM1084 regulators and routes signals to the L298 H-bridges, quadrature encoders, servos, and I²C LCD.

Carrier board for the STM32 Nucleo, with connections for power, motor drivers, encoders, and the LCD.

Motor-control workload

The vehicle used EXTI interrupts to decode quadrature encoder signals and hardware-timer PWM to drive the motors. With the PID loop running alongside UART telemetry and LCD updates, I measured less than 10% average wheel-speed error from the target using encoder telemetry over UART.

Keep exploring.

Back to the workshop
Kirby Companion embedded device

Kirby Companion

ESP32-S3 smart display in a custom 3D-printed Kirby enclosure, built as a standalone desktop companion with touch UI, weather, alarms, timers, minigame screens, Wi-Fi, and I2S audio.

Explore the project