Ground Embedded KO Subscribe

Notes / Embedded Linux

Real-time Linux: PREEMPT_RT and dividing the roles

Linux can get close to real time, but it is not the same as an MCU. How to measure latency and how to decide what to leave to the MCU.

GROUND EMBEDDED · NOTEEmbedded Linux 08Real-time Linux 1$ make ARCH=arm 2$ insmod gnd_gpio.ko 3[ 1.024] gnd: probe ok 4$ cat /dev/gnd0 50x5A 6$ _

What real time means

Not "fast" but "responds within the deadline even in the worst case". An average of 10 µs is not real time if it occasionally takes 5 ms.

PREEMPT_RT

It makes most of the kernel preemptible, so a high-priority real-time thread is not blocked for long by kernel work. It has been part of the mainline kernel since 6.12.

Measuring latency

cyclictest -p 90 -t1 -i 1000 -l 100000 -m

The measure is the maximum latency under load (stress, network traffic). An ordinary kernel shows several ms; PREEMPT_RT brings it down to tens of µs, depending on the hardware.

RoadmapSee where this note sits in Embedded Linux→

Writing a real-time application

  • Set a SCHED_FIFO priority
  • Lock memory with mlockall() (prevents page faults)
  • No dynamic allocation or file I/O inside the real-time loop
  • Isolate one CPU core for the real-time work

Dividing the roles

Linuxnetwork, UI, storageUART · SPI · CANMCUmotor control, safety
Microsecond-level control goes to the MCU; communication and display go to Linux.
Leave to the MCU Leave to Linux
Current control at 10 kHz and above, PWM generation Higher-level path planning, image processing
µs-level protection such as overcurrent UI, network, log storage
Safety functions Updates, cloud connection

Robots and vehicles commonly use a Linux SoC and an MCU together, connected by UART, SPI, CAN or shared memory (RPMsg on combined Cortex-A + M chips).

FREE PDFGet the 3 free ebooksIf you want to keep reading this topic as one book: BLDC motor control, serial communication and sensors at book length. PDFs are in Korean.Get them free →