Embedded devices are the soul of the Internet of Things. Any IoT network is inherently a means to connect “things” powered by microcontrollers via the Internet. The microcontrollers at the heart of most IoT devices are simpler, resource-limited, low-power chips that cannot run a standard operating system. Microcontrollers are often updated with minimal firmware codes intended to perform specific jobs. Connecting to the Internet and communicating with cloud services in real time is a big deal for embedded software. Real-time operating systems (RTOS) are the business for embedded controllers.
RTOS are advanced operating systems that enable embedded controllers and processors to run large-scale embedded applications in an orderly manner so that multiple tasks run simultaneously on a single core while still satisfying real-time deadlines. In IoT ecosystems, RTOS enables devices to connect to cloud platforms and edge devices and efficiently exchange data in real-time while performing integrated multitasking tasks.
A popular RTOS is FreeRTOS. The embedded community widely adopts it because it is open source and freely available. There are other free and licensed derivatives of FreeRTOS available as well. One of the best things about FreeRTOS is that you can start tinkering with Arduino or ESP boards. Additionally, FreeRTOS can be extended with additional libraries to adapt to a specific application. This article discusses what FreeRTOS is and why we need it.
Real-time operating system (RTOS)
A real-time operating system (RTOS) is a multitasking operating system for time-critical applications. An RTOS executes tasks under real-time constraints, where tasks must be executed in the time interval between the occurrence of an event and the system's expected response to the event. RTOS tasks have predictable latencies that closely match expected deadlines.
RTOS are miniature operating systems, typically less than a megabyte in size. Not being a complete operating system, often these small operating systems may lack some common operating system features such as file system, networking, and command line interfaces. Still, they are well equipped with managing processes, resources, devices, and subsystems. An RTOS can be event-driven or time-sharing and allow only static or dynamic memory allocation according to a deterministic algorithm of the application program. The memory management unit (MMU) is disabled or memory locks are applied. Memory locks stop the exchange of pages between physical memory and disk, reducing instability and making task latencies predictable.
The scheduler in an RTOS is designed for a deterministic execution pattern. This is useful in embedded systems where the device must respond to an event within a strict deadline. Application program threads run in performance kernel space. This way, the application program has access to kernel codes as well as stack data and memory space, which can lead to an unprotected kernel.
An application program in RTOS is written as a collection of independent tasks; a thread of execution is one such task. An RTOS provides management of user-defined process priorities, with each task assigned a priority. User-level priorities, also called real-time priorities, are higher than priorities dynamically allocated to operating system functions and idle priorities assigned to low-priority threads. The RTOS preempts lower priority tasks when a message or event occurs to execute a higher priority task. For tasks with equal priority, an RTOS depends on dividing the execution time of the processes.
RTOS vs. General purpose OS
In a typical operating system, the scheduler is designed to remain responsive to the current application running by the user. In a multi-user operating system, the scheduler is designed to ensure fair processing time and memory sharing among multiple users. In RTOS, tasks or application threads are given priorities, and the scheduler is designed to complete the highest priority task first within the expected deadline.
An RTOS can have hard or soft real-time operation. It is called a complex real-time system if the RTOS operates to meet tough time constraints; and if the RTOS operates tolerating some latencies but in a predictable manner, it is called a smooth real-time system. Life-saving medical equipment is an example of a hard real-time system. The Aavideo decoder is an example of a soft real-time system.
Why use RTOS?
Real-time operating systems are often required for hard real-time systems. The only other scenario in which an RTOS can be employed is when an embedded device needs to handle many multitasking tasks. Most embedded systems are designed to perform a single specific task performed only with bare metal programming. However, many embedded applications require the simultaneous operation of multiple tasks or involve interprocess communication. This is where RTOS is the more viable option compared to writing a custom scheduler and designing a cross-process routine.
Along with real-time processing and multitasking, RTOS benefits an embedded application in several ways. RTOS is the best way to manage a complex or large application. It ensures that the embedded system executes high-priority tasks under real-time constraints so that all possibilities of risks, faults, and failures are addressed correctly. RTOS allows you to achieve this with smaller, simpler application code. Allows you to divide the application code into independent tasks with predictable latency. RTOS-based embedded systems are easier to maintain. Modules have fewer interdependencies and often rely on kernel resources rather than the underlying hardware. This way, hardware changes, such as a processor or peripheral, generally do not affect an RTOS-based application.
Because the application is designed as a set of independent tasks, most of the code is reusable. It is easier to test systems running RTOS due to well-established testing exercises. The test may not even require a hardware interface.
What is FreeRTOS?
FreeRTOS is an open source real-time operating system designed for microcontrollers and small microprocessors. It was developed by Richard Barry in 2003 and maintained by his company Real Time Engineers Ltd, until 2017. In 2017, administration of the FreeRTOS project was taken over by Amazon Web Services. The Amazon FreeRTOS derivation is an extension to the original FreeRTOS kernel and includes several IoT support libraries. Amazon FreeRTOS is widely known simply as “FreeRTOS”.
Most microcontrollers do not require full RTOS implementation. FreeRTOS is not a full RTOS, but a real-time scheduler with TCP/IP stack included. It is often used as an RTOS kernel for real-time scheduling, interprocess communication (IPC) management, and synchronization of tasks with IPCs. There is no file system, virtual memory, or security model in FreeRTOS. Additional features such as networking and command line interpreter can be added to FreeRTOS if necessary using add-on libraries.
Why FreeRTOS?
FreeRTOS is basically a real-time scheduler written in C programming language with a kernel consisting of just three .c files. It is the small size of the FreeRTOS kernel that makes it suitable for microcontrollers. FreeRTOS is available for over 40 architectures, including ARM. Since it is open source under an MIT license, it is freely available and can even be modified to be ported to new or custom hardware platforms. Because the source code is open source, embedded engineers can examine the kernel code whenever necessary to resolve a problem. Although FreeRTOS is not a complete RTOS implementation, the kernel ensures strict real-time constraints are met.
FreeRTOS can be embedded into an application without exposing proprietary source code. It provides built-in tracing support, a TCP/IP stack, and a command-line interpreter. As a trusted kernel developed by the collaboration of more than 15 chip manufacturers, it is designed to be readily integrated into IoT applications. It can be ported to microcontroller-based evaluation boards and tested with pre-configured demos that greatly speed time to market. Amazon offers IoT Device Tester, a test automation tool that allows you to automatically test whether a hardware platform supports FreeRTOS. FreeRTOS has evolved to interoperate with Amazon Web Services (AWS) and comes with all possible IoT features. The RTOS can be updated over the air (OTA) to install security patches and supplemental libraries whenever necessary.
Extending FreeRTOS
FreeRTOS' RTOS functionalities can be extended with add-on libraries. The FreeRTOS+TCP library allows the implementation of a lightweight and complete TCP/IP stack. The FreeRTOS+IO library adds POSIX-like interfaces to FreeRTOS for connecting to peripheral devices. The FreeRTOS+Trace library adds tracing support and allows us to monitor the real-time behavior of the RTOS. The FreeRTOS+CLI library helps you add a command-line interface to the operating system.
FreeRTOS applications
FreeRTOS is highly useful in IoT applications, especially if they need to interoperate with AWS. It is widely used in commercial applications in the industrial and consumer segments. Being a minimalist real-time scheduler, FreeRTOS is the preferred choice in many industrial hard real-time devices. It is largely run by microcontrollers in many consumer products such as smart lights, wearables, and various home appliances.
Alternatives to FreeRTOS
FreeRTOS may not always be the best choice. This is especially true when a device actually requires a full RTOS implementation. These applications may require a file system, virtual memory, or a high-level user interface. In such situations, an embedded Linux variant is suitable. Some alternative RTOS that may be suitable in such a situation include embOS, Yocto, Zephyr, ThreadX, Nucleus, Keil RTX, SafeRTOS, etc. For those applications that don't involve multitasking or real-time constraints, FreeRTOS will be overkill. Such applications must be solved with bare metal programming.
Introduction to FreeRTOS on Arduino or ESP
FreeRTOS is widely used in commercial embedded applications, is open source and freely available. The best thing is that it can be loaded onto popular microcontroller boards like Arduino, ESP and STM32. So anyone can quickly get started and try FreeRTOS on popular microcontroller boards.