Embedded devices are the soul of the Internet of Things. Any IoT network is inherently a medium to connect microcontroller-powered “things” via the internet. Microcontrollers at the heart of most IoT devices are simpler, resource-constrained, low-power chips that cannot run a standard operating system. Microcontrollers are often flashed with minimal firmware codes intended to execute 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) deal the deal for embedded controllers.
RTOS are cut-above operating systems that enable embedded controllers and processors to run large-scale embedded applications in a sorted manner such that multiple tasks are executed simultaneously on a single core, still satisfying real-time deadlines. In IoT ecosystems, RTOS enables devices to connect with cloud platforms and edge devices and efficiently exchange data in real-time while multitasking embedded tasks.
One popular RTOS is FreeRTOS. The embedded community widely adopts it because it is open-source and freely available. There are other free and licensed derivations of FreeRTOS available as well. One of the best things about FreeRTOS is that you can start tinkering with Arduino or ESP boards. Not just that, FreeRTOS can be extended with additional libraries to adapt to a particular 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. A RTOS executes tasks in real-time constraints where the tasks must be executed in the time interval between the occurrence of an event and the system-expected response to the event. RTOS tasks have predictable latencies that strictly match their expected deadlines.
RTOS are miniature operating systems typically less than a megabyte in size. Not being a full operating system, often these tiny OS may lack some common OS features like file system, networking, and command-line interfaces. Still, they are well-equipped with the process, resource, device, and sub-system management. An RTOS can be event-driven or time-sharing, and allow only static memory allocation or dynamic memory allocation according to a deterministic algorithm by the application program. The memory management unit (MMU) is either disabled, or memory locks are applied. The memory locks stop page swapping between physical memory and disk, reducing jitter and making task latencies predictable.
The scheduler in a 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 time deadline. The application program threads run in kernel space that boasts the performance. This way, the application program has access to kernel codes as well as data and stack memory space, which may lead to an unprotected kernel.
An application program under RTOS is written as a collection of independent tasks; a thread of execution is such a task. A RTOS provides user-defined process priorities management with each task assigned a priority. The user-level priorities, also called real-time priorities, are higher than the dynamically allocated priorities to OS functions and the idle priorities assigned to low-priority threads. RTOS preempts lower-priority tasks as a message or event to run a higher-priority task occurs. For tasks with equal priority, a RTOS relies on time slicing of the execution of processes.
RTOS vs. general-purpose OS
In a typical operating system, the scheduler is designed to remain responsive to the current application run 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, the tasks or application threads are assigned priorities, and the scheduler is designed to complete the highest priority task first within the expected time deadline.
A RTOS may have a hard or soft real-time operation. It is called a complex real-time system if the RTOS operates to meet harsh time constraints; and, if the RTOS operates tolerating some latencies but predictably, it is called a soft real-time system. Life-saving medical equipment is an example of a hard real-time system. Aavideo decoder is an example of a soft real-time system.
Why use RTOS?
Real-time operating systems are usually required for hard real-time systems. The only other scenario where a RTOS may be employed is when an embedded device has to deal with lots of multitasking. Most embedded systems are designed to perform a single specific task just done with bare metal programming. But, many embedded applications require the simultaneous operation of multiple tasks or involve inter-process communication. This is where RTOS is the most viable option compared to writing a custom scheduler and designing an inter-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 accomplishes top-priority tasks in real-time constraints so that all possibilities of risks, faults, and failures are correctly addressed. RTOS enables achieving this with a more straightforward and smaller application code. It allows dividing application code into independent tasks having predictable latency. RTOS-based embedded systems are more maintainable. The modules have fewer interdependencies and often rely on kernel features rather than the underlying hardware. In this way, hardware changes like the processor or a peripheral often do not affect a RTOS-based application.
As 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 test exercises. The testing may not even require interfacing hardware.
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, the stewardship of the FreeRTOS project was taken by Amazon Web Services. The derivation Amazon FreeRTOS is an extension of the original FreeRTOS kernel and includes several IoT support libraries. Amazon FreeRTOS is widely known as simply “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 a RTOS kernel for real-time scheduling, managing inter-process communication (IPC), and synchronizing tasks with IPCs. There is no file system, virtual memory, or security model in FreeRTOS. Additional features like networking and command line interpreter can be added to FreeRTOS if required using add-on libraries.
FreeRTOS is basically a real-time scheduler written in C programming language with a kernel consisting of only three .c files. It is the small size of the FreeRTOS kernel that makes it suitable for microcontrollers. FreeRTOS is available for more than 40 architectures, including ARM. As it is open-source under an MIT license, it is freely available and even can be modified to port onto new or custom hardware platforms. The source code being open-source, embedded engineers can scrutinize the kernel code whenever required to sort out a problem. Although FreeRTOS is not a full RTOS implementation, the kernel ensures meeting hard real-time constraints.
FreeRTOS can be embedded in an application without exposing proprietary source code. It provides built-in tracing support, a TCP/IP stack, and a command-line interpreter. Being a trusted kernel evolved by a 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 accelerate the time to market. Amazon offers IoT device tester, a test automation tool that lets self-test whether a hardware platform supports FreeRTOS. FreeRTOS has evolved to interoperate with Amazon Web Services (AWS) and comes with all possible IoT capabilities. The RTOS can be updated over the air (OTA) for installing security patches and add-on libraries whenever required.
The RTOS functionalities of FreeRTOS can be extended with add-on libraries. The FreeRTOS+TCP library allows the implementation of a full-fledged lightweight TCP/IP stack. The FreeRTOS+IO library adds POSIX-like interfaces on FreeRTOS to connect with peripheral devices. The FreeRTOS+Trace library adds tracing support and lets us monitor the real-time behavior of the RTOS. The FreeRTOS+CLI library helps include a command line interface to the operating system.
Applications of FreeRTOS
FreeRTOS is highly useful in IoT applications, particularly if they have to interoperate with AWS. It is widely used in commercial applications of both industrial and consumer segments. Being a minimalist real-time scheduler, FreeRTOS is the preferred choice in many hard real-time industrial devices. It is widely run by microcontrollers in many consumer products like smart lights, wearables, and several domestic appliances.
FreeRTOS may not be the best choice always. This is especially true when a device actually requires a full RTOS implementation. Such applications may require a file system, virtual memory, or a high-level user interface. In such situations, an embedded Linux variant fits the bill. 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 neither involve multitasking nor real-time constraints, FreeRTOS will be overkill. Such applications must be settled with bare metal programming.
Getting started with FreeRTOS on Arduino or ESP
FreeRTOS is widely used in commercial embedded applications, is open-source, and is freely available. The best thing about it is that it can be uploaded to popular microcontroller boards like Arduino, ESP, and STM32. So, anybody can quickly get started and try FreeRTOS on popular microcontroller boards.
You may also like:
Filed Under: Embedded, What Is