RTOS ChibiOS/RT on Arduino

I decided to try the ChibiOS/RT RTOS on a m328 Arduino since I had good luck with it on ARM Cortex M3 processors.


I selected a striped down version and packaged it as an Arduino library. The results are impressive for a preemptive kernel on avr.

The test sketch below runs two threads and is designed to check the time for one thread to signal another thread and do a context switch.

The lower priority thread, thread2, toggles an LED so I can determine the time for digitalWrite and then uses a semaphore to activate a higher priority task that turns the LED off.

The total time for the semaphore and context switch is 15 usec. This is much faster than other RTOSes I have tried on the Arduino. It is more than three times as fast as a popular popular RTOS that forbids publishing benchmarks.

The total flash used by this sketch with 0022 is 2972 bytes.

// scope test to determine ChibiOS context switch time
#include <ChibiOS.h>

const uint8_t LED_PIN = 3;
// semaphore to trigger thread2
Semaphore sem;
// thread for LED off
static WORKING_AREA(waThread1, 64);

static msg_t Thread1(void *arg) {
  while (TRUE) {
    digitalWrite(LED_PIN, LOW);
  return 0;
// thread 2 - cause context switch to thread 1 with semaphore
static WORKING_AREA(waThread2, 64);

static msg_t Thread2(void *arg) {
  while (TRUE) {
    // toggle LED to get time for digitalWrite
    digitalWrite(LED_PIN, HIGH);    
    digitalWrite(LED_PIN, LOW);
    digitalWrite(LED_PIN, HIGH);
    // signal to switch to thread1 to get context switch time
    // wait 10 ms
  return 0;
void setup() {
  pinMode(LED_PIN, OUTPUT);
  chSemInit(&sem, 0);
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO + 2, Thread1, NULL);
  chThdCreateStatic(waThread2, sizeof(waThread2), NORMALPRIO + 1, Thread2, NULL);  
void loop() {
  // never called

damn, you are good!

Very cool, nice work! The idea of multitasking on a '328 is kind of mind-blowing :) I've played with ChibiOS on the STM32; methinks I know what I'm going to mess around with this weekend. Thanks for sharing!

Interesting! Did you take the AVR-ATmega128-GCC demo as the base? There is an interesting benchmark suite "TestThread(&SD1)" in the chibios demo for stm32 discovery, which lists performance of all possible rtos parameters (41 test cases), maybe it could fit into 328 as well.. http://www.chibios.org/dokuwiki/doku.php?id=chibios:articles:stm32vl_discovery see the last picture at the end of the page, that is only a small part of the benchmark output listing pictured there. I've run it when studying overclocking capabilities of the stm32f100 (100% easily with 2 modules). FYI: And the result ie. for stm32f100 kit: http://chibios.sourceforge.net/reports/STM32F100-24-IAR.txt The overclocked kit to 56MHz outperformed everything there (because of hardwired 0 waitstates :) ) http://www.chibios.org/dokuwiki/doku.php?id=chibios:metrics p.

I started with the AVR-ATmega128-GCC demo. I made system tick compatible with the Arduino use of timer0.

I may try the benchmark suite. If I do, I will make it a library. This is how the Arduino group does tests with the ArduinoTestSuite library.

I just got a STM32F4DISCOVERY in the mail. 1 MB Flash, 192 KB RAM, 168 MHz, Floating point, and DSP instructions http://www.st.com/internet/evalboard/product/252419.jsp.

STM32F4 - we diverge slowly from the Arduino Universe, I think :slight_smile: :slight_smile:
Benchmark suite - it would be nice to have it, maybe 1284p would be more suitable target for chibios rtos than 328. Some test may fit into 328, however.

"STM32F4 - we diverge slowly ", yes but they were free! Just need to figure out what to do with them.

Just need to figure out what to do with them. To get them, run a demo/benchmark, and then put them on a shelf to gather dust.. :) :) They are not free however, I've just seen Mouser wants 13.96Euro plus 40Euro postage from me :) :)

Not free because you are not in the US I think.

Yea, we still must pay for goods in EU, a pity.. :0

ChibiOS/RT can be valuable on m328 Arduinos.

I get a lot of Email from people who lose data points when logging data to an SD at rates over about 10 points/sec. SD cards can take as long as 200 ms for a write due to the overhead of the large flash erase size and wear leveling.

It is easy to implement a better data logging program with a RTOS. You write a small high priority task that is triggered to run by the OS system timer. The high priority task reads one or more analog channels or other sensors. It queues the data for a lower priority task that writes to the SD. The high priority task may execute a number of times while the lower priority task does a single write to the SD.

The context switch overhead is reasonable for ChibiOS/RT. An analogRead takes over 100 us while the context switch takes only 15 us.

On the discovery boards. I have been doing a lot of development and really like the st-link debug with SWD. Wish the Arduino had a similar feature.

You should try STM Studio.

STM Studio is a graphical user interface that allows sampling and visualizing in real time user's variables while the application is running. It is expected to run on PCs with Microsoft® Windows operating systems. The product relies on ST-LINK hardware providing the ability to read on-the-fly data from the microcontrollers RAM, without being intrusive.

Runs on PCs with Microsoft®Windows XP, Vista and Windows 7 OS Connects to any STM8 via ST-LINK, RLink or STICE (SWIM protocol) Connects to any STM32 via ST-LINK (JTAG or SWD protocols) Reads on-the-fly (non intrusive) variables from RAM while application is running Parses DWARF debugging information in the ELF application executable file 2 types of viewer: Variable viewer - Real-time waveforms, oscilloscope-like graphs TouchPoint viewer - Association of 2 variables, one on the X axis, one on the Y axis Possibility to log data into a file, and replay later (exhaustive record display, not real-time)

It can be used like a logic analyzer for software. You import variable from an .elf file and then setup tables and graphs to display them in real time.


FYI - I’ve removed dust from my STM32VL Discovery @56MHz and this is the result of the test suite:
PS: I would be very happy to see the results from STM32F4 Discovery kit!

STM32VL _DISCO_56MHz.txt (6.95 KB)


I am going to wait for Giovanni Di Sirio, the author of ChibiOS/RT, to add the STM32F4. He will probably do the benchmarks and tests.

He bought three of the STM32F4DISCOVERY kits and sounds very excited by the STM32F4. He said:

This new STM32F4 is a beast.

It will require at least a specific hal_lld driver and RCC helper driver, I doubt you will be able to use it right now unless you use the ST library for initialization and just use the Kernel.

Luckily the F4 is very close to the F2 so most of the required code is already in place.


Yea, let us monitor his site with news.. PS: It is interesting that Giovanni had to buy the kits (from an EU chipmaker with HQ in Geneva) while in US the kits are given for free..