Follow★Star the public account to get information first
Today, let’s talk about something that is super important in embedded development but often overlooked—Real-Time Operating Systems (RTOS). If you are new to embedded development, this article is a must-read.

What is RTOS?
RTOS, or Real-Time Operating System, refers to an operating system that must complete specific tasks within a defined time frame; otherwise, problems may arise.

Compared to traditional operating systems, RTOS places greater emphasis on ensuring real-time performance, ensuring that the system can respond and process accurately and timely under strict time constraints. RTOS manages resources (including memory, peripherals, etc.), message management, task scheduling, and exception handling based on the requirements of various tasks.
In systems supported by RTOS, each task has a priority, and RTOS dynamically switches between tasks based on their priorities to meet real-time requirements.
Real-time operating systems are mainly divided into two categories: hard real-time systems and soft real-time systems. Hard real-time systems require tasks to be completed before strict deadlines, and any delay will result in system failure. Soft real-time systems also require timely responses, but occasional delays do not lead to system failure; they only degrade system performance.

Characteristics of Real-Time Operating Systems
Real-time operating systems have several notable characteristics:
Real-time Performance: The most significant feature of RTOS is its real-time performance, ensuring that tasks are completed within specified time frames. For example, in industrial control systems, if a task is not completed within the scheduled time, it may lead to equipment failure, so RTOS ensures timely task responses.
Multitasking: RTOS can handle multiple tasks simultaneously. Imagine your phone running multiple applications at the same time, such as playing music, browsing social media, and receiving message notifications; RTOS can allocate these tasks efficiently, allowing them to operate normally without interference.
Priority Scheduling: RTOS assigns different priorities to tasks. Important tasks are executed first, while less urgent tasks can be executed later. For instance, if your system needs to handle emergency alerts and regular data uploads simultaneously, the alert task will be prioritized.
Interrupt Management: RTOS can quickly respond to external events or interrupts. When an external device sends a signal, RTOS can immediately pause the current task to handle the high-priority interrupt event and resume the previous task afterward.
Low Latency: RTOS is typically designed to be very lightweight, with fast response times and low latency. This is especially important for applications that require high responsiveness, such as autonomous driving systems or medical devices.
Resource Management: RTOS is very adept at managing system resources, avoiding conflicts between tasks. It effectively manages memory, CPU time, and other resources to ensure stable system operation.
Advantages of Using RTOS in Embedded Development
In bare-metal development, developers often write all business logic within a single large while(1) loop. This means that all operations are tightly packed within this loop, and each function is executed serially. The problem is that many functions contain delay functions, causing the CPU to idle while waiting, wasting a lot of time, resulting in very low efficiency.

From a software engineering perspective, we usually advocate for high cohesion (concentrated functionality) and low coupling (minimal interference between modules). However, modularization in bare-metal development is very difficult because each function is tightly coupled with others, making it hard to separate modules for independent development. This approach is particularly unsuitable for large projects.
For example, returning to the main function’s large while(1) loop, you can imagine that all functions are crammed into one place, making it impossible to split them into independent modules, resulting in very messy code. This is like having a very complex project where all the code is piled together, making it impossible to manage, with high coupling, leading to problems in maintenance and expansion later on.
Moreover, some projects require the use of watchdog timers to prevent system deadlocks. If you use delay functions in these projects, you need to be particularly careful. Because delay functions can consume a lot of time, the main function may not have time to feed the watchdog, resulting in the watchdog being triggered, causing the system to reset, creating chaos. This means that a seemingly simple delay actually requires consideration of many additional factors. Bare-metal development involves a lot of concerns, placing a heavy burden on developers, making it difficult to apply this approach in large, complex projects.
In certain fields, such as industrial control and automation equipment, the requirements for real-time performance are very high. This means that every operation must be completed within a specific time frame without delay. If bare-metal development is used in these fields, the code becomes very large and complex, making the main program’s huge while(1) loop overwhelmed, with high coupling and many delays, making it nearly impossible to guarantee real-time performance. As a result, mechanical actions may not execute in the predetermined sequence, potentially leading to mechanical failures and even endangering human safety.
Common RTOS Comparisons
There are many types of RTOS, each with its unique characteristics. Here are a few common RTOS:
FreeRTOS
FreeRTOS (Free Real-Time Operating System) is a real-time operating system specifically designed for microcontrollers and small microprocessors. As a lightweight operating system, its kernel requires only a few C files to complete core functions, making it very suitable for resource-constrained embedded systems.

FreeRTOS has two startup methods, both yielding the same effect, depending on personal preference.
The first method: In the main function, initialize the hardware, initialize the RTOS system, create all tasks, and finally start the scheduler. Most chip SDKs seen so far use this method.
The second method: In the main function, first initialize the hardware and RTOS system, create only one task, then start the scheduler, and within this task, create other application tasks. Once all tasks are created successfully, the startup task deletes itself.
RT-Thread
RT-Thread is a domestic real-time operating system with rich middleware. It is widely used in various industries such as energy, automotive, medical, and consumer electronics, with a cumulative installation of tens of millions of units, making it the most mature, stable, and widely used open-source RTOS developed by Chinese developers.

Currently, RT-Thread has been ported to nearly 90 development boards, with most BSPs supporting MDK, IAR development environments, and GCC compilers, and has provided default MDK and IAR projects, allowing users to directly add their application code based on these projects. Each BSP’s directory structure is highly standardized and provides a README.md file containing a basic introduction to the BSP and corresponding instructions, facilitating quick user onboarding.
μClinux
μC/OS-II is a compact and efficient preemptive multitasking real-time kernel. It can manage up to 64 tasks simultaneously and provides comprehensive functions such as task scheduling and management, memory allocation, inter-task synchronization and communication, time management, and interrupt handling. Its high execution efficiency, small footprint, excellent real-time performance, and good scalability make it highly regarded in embedded system development.
There are many other real-time operating systems; you can search for more information to choose the one that best fits your project.

Conclusion
If you are developing embedded devices, especially in scenarios that involve multitasking, real-time responses, and high system stability requirements, consider using an RTOS.
It can not only help you improve development efficiency but also ensure that your device can respond quickly at critical moments. By mastering it, you can tackle more complex embedded development tasks, making your projects more efficient and reliable.
Recommended Reading
01 |Join the Embedded Development Group |
02 |Embedded Resource Acquisition |
03 |Detailed Explanation of STM32 Interrupt Priorities |
04 |New Approach to Downloading Programs on STM32—Using Serial Port |