RTOS Basics: Getting Started with Microcontrollers
If you’re just starting out with microcontroller units (MCUs), you may have heard of RTOS and how they’re extremely important for developing embedded applications. Today, I’m going to share all about RTOS basics and how you can get started, including:
- What RTOS is & How it Works
- Benefits of Using an RTOS
- Considerations when Choosing an RTOS
- RTOS Recommendations & Development Platforms
- How to multitask a microcontroller with FreeRTOS
What is RTOS?
RTOS stands for Real-Time Operating System. As the name suggests, it is an operating system (OS) that is designed to process or respond to incoming data in real time – but what does “real time” mean?
Real time systems are time-bound systems that have strict timing constraints and requirements. That means that the processor must complete each task within their given time constraints so that the following task can be executed on time, otherwise the system is considered to have failed.
The true value of RTOS, however, lies in its ability to maintain timing requirements while enabling multitasking.
Today, RTOS are used in many embedded applications, such as:
- Medical Equipment such as Pacemakers
- Defence Systems, such as Radars
- Air Traffic / Aircraft Control Systems
- Networked Multimedia Systems
How does RTOS work?
For context, each processor core in your computer can in reality only execute one program at a time. To provide the illusion of multitasking, the operating system’s scheduler is responsible for deciding which programs to run when. The scheduler switches rapidly between tasks to make it appear that different programs are being executed at the same time.
The scheduler in RTOS is designed to have a predictable pattern of task execution, which is why RTOS are often described as deterministic systems. This characteristic is especially important for embedded systems that often have real time requirements on the field, such as responding to an event within a strict period of time.
For example, an embedded microcontroller may be required to trigger a failsafe within milliseconds of receiving sensor data that suggests imminent danger. What if our MCU was, in those precious milliseconds, busy maintaining its connection with a remote server? As you’d expect – the results would be catastrophic.
Fortunately, RTOS provides several ways to circumvent this problem. One way would be to assign the task of sensor data processing with a higher priority than maintaining the remote connection. That way, the scheduler will switch out tasks to meet the timing requirement, even if sensor data arrives while the MCU is busy.
What are the benefits of RTOS?
Traditionally, microcontroller programming involves sequential processing loops and state machines. While that is typically sufficient for smaller applications, many issues arise when application complexity is increased.
One key problem lies in the fact that sequential code runs line by line to completion and cannot truly be interrupted. Continuing with our previous example: Once we call the function to maintain the server connection, the code must run to completion before any further action can be taken. On a large scale, this may introduce significant delays and make timing requirements difficult to meet without an RTOS.
To Summarise: While you can successfully implement complex, time-sensitive applications without the use of an RTOS, the process will certainly be more difficult.
Still not convinced? Here are my top 3 reasons to use an RTOS.
1. Applications built with RTOS are easy to maintain and scalable.
RTOS are built with a preemptive multitasking design paradigm, which is what allows tasks to switch from one to another based on need. Essentially, this allows you to build the code for each task separately, while leaving it to the RTOS to ensure that the timing requirements for each of them are met. Thus, if you add more tasks in the future, it will be easier to maintain your existing code!
2. RTOS comes with pre-integrated communication stacks and drivers.
An RTOS typically includes communication stacks such as TCP/IP and USB, along with the drivers required to support these protocols and other peripherals. By eliminating the need to implement such functionality from the ground up or introduce other uncertain third-party code, you can drastically reduce development time and costs!
3. RTOS debug & analysis tools help to improve your implementations.
As your applications increase in size, debugging becomes more difficult. Sometimes, problems such as unintended memory usage or leaks can be hard to diagnose without a strong understanding of system behaviour and resource usage. Fortunately, there are many tools for RTOS-aware debugging to help you monitor resource allocations to each task, such as Segger’s Ozone SDK.
Choosing an RTOS
So you’re convinced that an RTOS is going to be the way forward for your next microcontroller project, but you’re not sure which of the many options to choose from. While it ultimately boils down to your personal preference and your project requirements, here are some considerations to get you started.
Types of RTOS
- Hard Real Time – Task timings and deadlines are handled very strictly. (eg. critical healthcare, aircraft systems)
- Firm Real Time – Deadlines need to be followed, but not as strictly. (eg. video conferencing, GPS tracking)
- Soft Real Time – Timing constraints are not expressed as absolute values. (eg. online transactions, web browsing)
RTOS Characteristics
- Performance – Can the RTOS perform well given your MCUs processing and memory specifications?
- Features – What features do you need in your RTOS? (eg. dynamic memory allocation)
- Cost – Is the RTOS free for use? If your project is commercial, what are the costs of licensing the software?
- Ecosystem – Does the RTOS run on a variety of microprocessor architectures? Does it have community support?
- Middleware – Is there middleware support for integration with external software components?
RTOS Recommendations
As you can see, settling on an RTOS can be a real challenge when there are so many factors to consider. Yet, sometimes it’s best to simply get your feet wet and experiment with the various choices! Today, I’d like to share three popular RTOS that you might be interested in!
Azure RTOS
Azure RTOS is an embedded development suite offered by Microsoft, offering a small but powerful operating system on resource-constrained devices. Azure RTOS features a number of pre-integrated components, such as Azure RTOS NetX for TCP/IP protocols and Azure RTOS FileX for high performance filesystems. Coming from one of the biggest players in the market, you can expect Azure RTOS to feature predictably fast performance, robust security, and compliance with industry standards!
What makes Azure RTOS special is its seamless integration with Azure IoT, which is the IoT cloud platform by Microsoft. That means that you can connect, monitor, and control your IoT devices running Azure RTOS easily and conveniently! To accelerate your development process, choose from a suite of IoT protocols, reference templates, or simply use their IoT Plug and Play with the Azure IoT device SDK.
Azure RTOS is free to test and develop, with the full source code and getting started tutorials available on their GitHub repositories. If you are using one of the pre-licensed microcontrollers, you do not have to purchase an additional production license!
To learn more about what Azure RTOS has to offer, visit their landing page!
FreeRTOS
FreeRTOS is a well known open-source operating system in the IoT RTOS scene that has been extensively developed over more than a decade. It is offered as a kernel with modular IoT libraries, and was specially developed for microcontrollers (though it now has other uses as well). Hence, it also features a low memory footprint and power optimisation features. FreeRTOS is supported by Amazon, which means that you’ll be able to take advantage of their libraries and support to interface your microcontrollers with Amazon’s AWS IoT!
It is absolutely free to develop with FreeRTOS, thanks to its MIT license! You’ll also be happy to know that FreeRTOS supports over 40 MCU architectures, including the latest RISC-V and ARMv8-M microcontrollers. In addition, releases come in LTS (long term support) versions, so the longevity of your devices with FreeRTOS will be guaranteed!
To get started with FreeRTOS, visit their website for more information and tutorials!
Mbed OS
Mbed OS is also immensely popular for microcontrollers in IoT applications. Targeted at Arm Cortex-M microcontrollers, it includes all the features that you need to develop a connected product quickly, including libraries for security, storage, connectivity, device management and drivers for sensors & I/O.
One key advantage of MbedOS is their Mbed Studio desktop IDE, which allows you to write, debug code and deploy your applications in a comfortable and convenient manner. In addition, its popularity means you get to rely on thousands of developers all over the world for community support! Mbed OS is released under an Apache 2.0 license, so you can use it in commercial and personal projects at no cost!
Visit the Mbed OS website to learn more today!
RTOS Platforms: Microcontroller Recommendations
In RTOS development, choosing the right hardware is just as important as the software! Here are my top picks for microcontrollers if you’re looking to kickstart your RTOS journey.
Seeeduino XIAO
The Seeeduino XIAO is the smallest Arduino compatible board in the Seeeduino Family. Despite its small size, the Seeeduino XIAO is equipped with the powerful SAMD21 microchip and a variety of hardware interfaces. That means that you can use it with Azure RTOS, FreeRTOS and Mbed OS without a hitch!
Product Features:
- ARM Cortex-M0+ 32bit 48MHz microcontroller (SAMD21G18) with 256KB Flash, 32KB SRAM
- Compatible with Arduino IDE & MicroPython
- Easy Project Operation: Breadboard-friendly
- Small Size: As small as a thumb(20×17.5mm) for wearable devices and small projects.
- Multiple development interfaces: 11 digital/analog pins, 10 PWM Pins, 1 DAC output, 1 SWD Bonding pad interface, 1 I2C interface, 1 UART interface, 1 SPI interface.
Keen to learn more about the Seeeduino XIAO? Visit its product page on our Seeed Online Store now!
Wio Terminal
The Wio Terminal is a complete Arduino development platform based on the ATSAMD51, with wireless connectivity powered by Realtek RTL8720DN. As an all-in-one microcontroller, it has an onboard 2.4” LCD Display, IMU, microphone, buzzer, microSD card slot, light sensor & infrared emitter. Similarly, the flexible ATSAMD chip allows you to use the Wio Terminal with Azure RTOS, FreeRTOS and MbedOS!
Product Features:
- Powerful MCU: Microchip ATSAMD51P19 with ARM Cortex-M4F core running at 120MHz
- Reliable Wireless Connectivity: Equipped with Realtek RTL8720DN, dual-band 2.4GHz / 5GHz Wi-Fi (supported only by Arduino)
- Highly Integrated Design: 2.4” LCD Screen, IMU and more practical add-ons housed in a compact enclosure with built-in magnets & mounting holes
- Raspberry Pi 40-pin Compatible GPIO
- Compatible with over 300 plug&play Grove modules to explore with IoT
- USB OTG Support
- Support Arduino, CircuitPython, Micropython, ArduPy, AT Firmware, Visual Studio Code
- TELEC Certified
If you’re interested to pick up a Wio Terminal, please visit its product page on the Seeed Online Store!
Raspberry Pi Pico
Our last recommendation today is the Raspberry Pi Pico, which is the latest microcontroller offering from the Raspberry Pi Foundation! It runs on the Raspberry Pi RP2040, which is a Dual-core ARM Cortex M0+ processor that is capable of up to 133MHz clock speeds. While having been on the market for only a short while, RTOS developments have already begun for this flexible board, click here to read more!
Product Features:
- Dual-core ARM Cortex M0+ processor, flexible clock running up to 133 MHz
- 264KB of SRAM, and 2MB of on-board Flash memory
- Castellated module allows soldering direct to carrier boards
- USB 1.1 Host and Device support
- Low-power sleep and dormant modes
- Comprehensive C/C++/Micropython SDK, software examples, and documentation
- Drag & drop programming using mass storage over USB
- 26 × multi-function GPIO pins
- 2 × SPI, 2 × I2C, 2 × UART, 3 × 12-bit ADC, 16 × controllable PWM channels
- Accurate clock and timer on-chip
- Temperature sensor
- Accelerated floating-point libraries on-chip
- 8 × Programmable I/O (PIO) state machines for custom peripheral support
To learn more about the Raspberry Pi Pico, please visit its product page on the Seeed Online Store!
Demonstration: Running FreeRTOS on Wio Terminal
RTOS are very flexible and a great solution for making the most of our microcontrollers. Today, I’d like to share a simple tutorial and demonstration on how multitasking can be achieved with FreeRTOS on the Wio Terminal.
Required Materials
To follow along, you can use any of our microcontroller boards that are based on the SAMD microchip. This includes:
- Wio Terminal
- Seeeduino XIAO
- Seeeduino Zero Series:
- Seeeduino LoRaWAN
Quick Start with FreeRTOS For Arduino
Step 1: Download the Seeed_Arduino_FreeRTOS repository as a ZIP file.
Step 2: Install the ZIP file as a library through the Arduino IDE. Please check here for detailed instructions.
Step 3: Copy the following code into a new sketch and upload it to your Arduino board.
#include <Seeed_Arduino_FreeRTOS.h>
TaskHandle_t Handle_aTask;
TaskHandle_t Handle_bTask;
static void ThreadA(void* pvParameters) {
Serial.println("Thread A: Started");
while (1) {
Serial.println("Hello World!");
delay(1000);
}
}
static void ThreadB(void* pvParameters) {
Serial.println("Thread B: Started");
for (int i = 0; i < 10; i++) {
Serial.println("---This is Thread B---");
delay(2000);
}
Serial.println("Thread B: Deleting");
vTaskDelete(NULL);
}
void setup() {
Serial.begin(115200);
vNopDelayMS(1000); // prevents usb driver crash on startup, do not omit this
while(!Serial); // Wait for Serial terminal to open port before starting program
Serial.println("");
Serial.println("******************************");
Serial.println(" Program start ");
Serial.println("******************************");
// Create the threads that will be managed by the rtos
// Sets the stack size and priority of each task
// Also initializes a handler pointer to each task, which are important to communicate with and retrieve info from tasks
xTaskCreate(ThreadA, "Task A", 256, NULL, tskIDLE_PRIORITY + 2, &Handle_aTask);
xTaskCreate(ThreadB, "Task B", 256, NULL, tskIDLE_PRIORITY + 1, &Handle_bTask);
// Start the RTOS, this function will never return and will schedule the tasks.
vTaskStartScheduler();
}
void loop() {
// NOTHING
}
Step 4: Open the Serial Monitor on the Arduino IDE and watch the magic happen!
This Hello World Example creates two threads that print different strings to the Serial Monitor at a different rate.
- Thread A prints “Hello World”,
- while Thread B prints “—This is Thread B—”!
And that concludes this quick tutorial!
Conclusion & More Resources
With that, we’ve come to the end of this article! I hope you’ve gotten a clearer idea of what RTOS basics and why they’re important for microcontroller development. If you’re keen to explore more RTOS beyond what I’ve shared in detail, you may also want to explore RT-Thread OS, μC/OS, or SAFERTOS.
Here are also some additional resources that may be helpful!
- Real Time Application Design Tutorial with FreeRTOS
- FreeRTOS Tasks & Co-Routines
- RTOS Debugging – Tips, Tricks and Tools
- Arduino FreeRTOS Tutorial – Seeed Wiki