Writing an RTOS From Scratch!

עומר חורב
Another One BYTEs the Dust
5 min readFeb 15, 2019

and all in C++!

RTOS Diagram. Every task can access the hardware, minimal hardware abstraction

Let me begin by stating that this is the first blog-post in an ongoing attempt to share the knowledge I gathered through years of developing embedded, real-time, security-related applications.

In this blog post, I will share with you the basic point of how to write a functioning Real-Time Operating System (RTOS) for the ARM Cortex-M3 core using Eclipse GNU MCU SDK and the Eclipse GNU MCU QEMU emulator written in C++.

Part 1: So what exactly is an RTOS?

RTOS (Real-Time Operating System) is a type of operating system that is meant to serve real-time applications. Defining “real-time application” is non-trivial and differs between definitions, but has a common ground: If the system is late on processing input, it fails. Take the example of a vehicle breaking system CPU, If the driver hit the breaks, the system must perform a break immediately, if it is delayed in processing the input, or in this case the brakes, it means the vehicle is crashed thus the system failed.

Generally, in real-time applications, the focus is not on the throughput but on the latency. It doesn't matter how many breaks per minute we perform but the time it takes once a break was pressed until the vehicle breaks.

The RTOS is consistent and has fixed execution time.

Usually, real-time applications where latency is critical runs on embedded cores (like the ARM Cortex M3 we will use in this tutorial) and requires low-level access to the hardware to achieve low latency, thus has a very skinny and lightweight kernel.

Part 2: The Toolbox

For the project we will need:

  • Ubuntu 18.04.1 LTS with 4.15.0–44-generic kernel — You can probably use any version of Linux supported by Eclipse and Eclipse GNU MCU project.
  • Eclipse CDT — Eclipse IDE for C/C++ Developers Version: 2018–12 (4.10.0)
  • Eclipse GNU MCU version 4.5.1 —Can be downloaded from the Eclipse marketplace.
  • gcc-arm-non-eabi toolchain — For compiling, linking and other actions.

The code for the project will be on this Github page

To QEMU or not to QEMU, That the question!

When I first took the task of writing an RTOS I was wondering where am I going to execute it. These kinds of projects come with a certain urgency and I couldn’t wait for the EVB I ordered to arrive. So I decided to check what virtualization options are there.

QEMU is a popular open-source tool for hardware virtualization. At first, I took a look at the QEMU documentation:

we also support the Cortex-M3 and Cortex-M4 “M-profile” CPUs (which are microcontrollers used in very embedded boards. We only have two boards which use the M-profile CPU at the moment: “lm3s811evb” and “lm3s6965evb”

At first sight, it looked promising. But looking deeper we learn that though QEMU does support Cortex M3 virtualization, it lacks some basic features when debugging and emulating low-level behavior and thus only a very few Cortex M3 boards are supported in the mainline QEMU project.

Eclipse GNU MCU project

GNU MCU Eclipse is an open source project that includes a family of Eclipse plug-ins and tools for multi-platform embedded ARM and RISC-V development, based on GNU toolchains.

In short, The Eclipse GNU MCU project allows us to compile, link and debug a variety of “low-level embedded” MCUs and most important, to Emulate them

More specifically, The sub-project Eclipse GNU MCU QEMU is a fork of the QEMU project customized to support ARM Cortex-M cores.

Screenshot of one of the emulated boards in the Eclipse GNU MCU project

QEMU is a great project, but its original focus was to emulate boards with large cores, usually application class, able to run Unix/Linux kernels. Support for bare metal Cortex-M based boards was available only for a very limited range of Cortex-M3 cores, so of little use in GNU MCU Eclipse.

Even more, support for semihosting in the public QEMU version was broken, and the verbosity required for integration with the QEMU plug-in was missing, so it could not be used with the GNU MCU Eclipse plug-ins.

Having decided to focus on the Eclipse GNU MCU, now its time to setup it all, The first task is creating a working hello world using Ubuntu, Eclipse and Eclipse GNU MCU project.

Part 3: Hello (deep, black, dark) World

First, install gcc-arm-none-eabi, if you’re using Ubuntu like me, just hit:

sudo apt-get install gcc-arm-none-eabi

Then, Install Eclipse CDT (as previously mentioned), create an empty workspace and lunch Eclipse.

The Eclipse Marketplace

Enter Help>Eclipse Marketplace and install GNU MCU Eclipse (version 4.5.1, but probably will work on other versions too).

Next, select all the options (though not all of them are actually necessary). Click Continue and finish the installation and restart Eclipse.

Building a “Hello World” project!

Create a new project using File>New>C/C++ Project>C++ Managed Build and select the Hello World ARM C++ Project. Select the ARM Cross GCC toolchain and name the project “hello”.

Hit next, next, next and finish (you can tweak all sorts like the toolchain, author for the auto-generated messages and stuff, we’ll just ignore it).

Once the project is generated, we need to make sure we are building for the correct architecture. Right click on hello (project name)>Properties>C\C++ Build>Settings and make sure the settings are as follow:

The build settings for compiling and ARM Cortex M3 application

and voilà! you built a “Hello World” project for the ARM Cortex M3 core.

Running this Badboy!

The thing is, building the project is different than running it. We need to configure

Install ARM toolchain

--

--