The Power of RTOS for IoT
RTOS stands for Real-Time Operating System; as its name implies, its goal is to process commands in real-time.
If there’s something all IoT devices have in common is constraints in power, memory, and processing speed.
This is where RTOS comes to the rescue, and while it’s not the sole solution, it does help tremendously.
5G is the other part of the equation as it’s key for fast data transfer.
Think of business-critical devices that have sophisticated capabilities, like surveillance cameras, consumer transaction terminals, or industrial control systems.
These IoT devices require real-time monitoring and management, and RTOS is perfect for that as it delivers deterministic, hard real-time responses to external events.
In practice, that means that RTOS can deliver highly responsive processing for a determined number of tasks way faster than a traditional operating system.
Think of an oil and gas pipeline IoT monitoring device, an operation this dangerous needs an electronic device that’s able to transfer data in real-time and responds instantly to external commands.
As if the real-time response wasn’t good enough, here are other reasons to use RTOS instead of other operating systems.
These might be more exciting to read for engineers.
These advantages were first published in RTOS vs Bare Metal Programming: Pros & Cons
Advantages of using RTOS
1. RTOS ALREADY HAS DRIVERS
In a modern RTOS, you will find a collection of drivers bundled with the RTOS.
You can use these drivers to connect popular sensors like the Bosch BME280 Humidity, Pressure, Temperature Sensor, or something like the ESP8266 wifi module.
Coding a driver for say a display from scratch yourself can be a lot of work, and if your RTOS happens to have this driver ready to go, you can quicken the time to market for your device.
Some RTOS have Sensor Frameworks that will make sensor programming really simple. With a Sensor Framework, you only need a few lines of code to do this: “Look for the BME280 sensor, schedule the sensor to be polled every 10 seconds, and call my function with the sensor data”.
Here’s a tip: Even if you’re not using an RTOS, you can use its drivers! Apache Mynewt didn’t have a driver for the ESP8266 WiFi module, so we ported the open-source driver from another RTOS (Arm Mbed OS) and it worked beautifully.
So if you ever get stuck, consider borrowing bits of code from an open-source RTOS that works!
2. MULTITASKING IN RTOS
Most devices will need some form of multitasking, like reading a sensor while transmitting the data over a network (Bluetooth, WiFi, NB-IoT LoRa, …).
An RTOS lets you specify the priority of each task and schedule them accordingly, making the best use of the available CPU processing time.
An RTOS provides features for synchronizing the concurrent tasks, such as Semaphores (for locking access to shared resources) and Message Queues (for passing information between tasks).
Some patterns of multitasking may preclude the use of RTOS. For example, if all tasks on your device are high priority and you need precise rules to decide which task to run next.
Or when you absolutely need to operate the CPU at 100% capacity. If you’re an experienced developer, coding on Bare Metal with Coroutines would be a better solution. (And if you haven’t heard of Coroutines… then you really should use an RTOS!)
3. POWER MANAGEMENT IMPLEMENTATION IN RTOS
Is your device required to run for extended periods of time on battery power?
Pick an RTOS that supports Deep Sleep Mode. Deep Sleep programming on microcontrollers is tedious and error-prone because we need to configure the Real-Time Clock to wake our device at the right time.
A mistake in the firmware programming could cause your device to wake up too soon, thus wasting battery power. Or not waking up at all!
On the other hand, if you’re an experienced firmware programmer, coding your own Power Management functions on Bare Metal allows you to squeeze the very last mAh from your battery.
4. RTOS SOLVES SECURITY ISSUES
Writing secure code is hard… so better leave that to the professionals.
An RTOS contains cryptographic functions, network stacks (like TCP/IP), and firmware distribution methods that have been reviewed by professionals.
When you program your device firmware with these peer-reviewed functions, you can be sure that your firmware is safe from attacks. And you’ll get timely alerts when any security issues are found in these functions.
Coding these functions yourself is risky.
Unless you have security experts in your team, you may inadvertently create security issues and make your firmware vulnerable to attacks.
If you decide not to use an RTOS, then we really recommend using a trusted security library instead of coding it yourself.
5. RTOS ALLOWS MCU PORTABILITY
Suppose you design your device for an STM32 Arm microcontroller today. What if you need to switch to a Nordic nRF MCU? Or even a RISC-V microcontroller?
The RTOS offers a unified programming interface for the microcontroller hardware.
When you code your firmware with these functions, the firmware source code may be recompiled to run on various microcontrollers like STM32, Nordic nRF, RISC-V, …
The RTOS in a sense functions as a Hardware Abstraction Layer (HAL) between your program and the electronic hardware.
For some products, it may be worth it to adopt an RTOS to prevent being locked-in to a specific microcontroller platform, but it likely will cost extra resources such as ROM, RAM, and CPU power.
6. RTOS ENFORCES STANDARDS ACROSS TEAMS
When you adopt an RTOS, it enforces a common development standard across the project.
Developers who code the device firmware will all use the same libraries provided by the RTOS, the same drivers, the same task synchronization primitives, …
For large projects, with many coders, perhaps even from different firmware design firms, this ensures that all vendors will follow the same standard for coding your device firmware.
Without an RTOS, you would have to define your own coding guidelines, your own base libraries, your conventions for multitasking, memory allocation, and so on.
This increases the time to create the first version of the device firmware.
7. RTOS SPEEDS UP PROOF OF CONCEPTS (POC)
Out of the box, a modern RTOS provides a fast way to create a multitasking device that supports various microcontrollers, sensors, and networks.
Why not take advantage of this and use this to quickly build a Proof of Concept for the new IOT device that your company is considering to deploy?
Discover new ways of solving problems by mixing and matching different sensors: motion, sound, light, touch, … Explore ways to reduce power consumption by switching networks: Bluetooth, ZigBee, WiFi, NB-IoT, Sigfox, LoRa,
RTOS Disadvantage
As with everything else in life, RTOS has its negative sides too, in a nutshell, it’s expensive, especially in mass manufacturing.
If you care to read more about that please click here, Can you afford an RTOS in mass manufacturing?
Conclusion
RTOS is great for devices that need real-time operation such as IoTs, it comes along with many other advantages over bare metal programming, but you have to be careful, especially in the budget area.
My name is Case Engelen and I’ve been in the hardware manufacturing industry for 20 years now. If you want to learn more about what I and my team do please visit us at Titoma.com.