Who uses real-time operating systems

Introduction to the RTOS world

Anyone dealing with the topic of real-time operating systems for the first time has to deal with a lot of new terms such as task, preemptive multitasking, semaphores and priority inversion. This article will shed more light on what all of this means.

Modern microcontrollers are becoming more and more complex and place increasing demands on the developers of embedded software. At the same time, such chips with complex peripheral elements and large integrated memory offer more performance than ever before. In order to be able to use all these advantages effectively, real-time operating systems (RTOS) are being used more and more often.

Figure 1: A real-time operating system divides an application program into several tasks.

What is a real-time operating system? Simply put, such software manages the time of a microprocessor or microcontroller. An RTOS also offers a multitasking environment in which several things happen at the same time. To do this, it divides a program into several tasks (Image 1). This measure leads the system to believe that several CPUs are available. In addition to multitasking, a real-time operating system also offers other valuable services: delays, the protection of shared resources and communication between individual tasks as well as synchronization are among its typical functions.

A task is a piece of program code that "thinks" that it has the CPU alone. This code can be independent or have interfaces to other software in the system, as is the case, for example, with a communication stack (TCP / IP, USB, etc.). In most cases, each task has its own stack space and a priority that corresponds to its importance. The application software can be split into several tasks. This division is based on modular function blocks, for example a GUI task, an audio task, a network stack, a motor controller, etc.

Listing 1

In principle, a task is formulated as a function that contains an infinite loop and therefore never returns to the initial call. Listing 1 shows a task formulated in pseudo-code. A task performs the function of the user application and then issues some blocking call. Such a blocking call can be a delay derived from the operating system, a wait state for a semaphore (Wait) or for another signal from an interrupt service routine (ISR) or another task. If a task is blocked and can no longer be executed, the operating system suspends it so that another task can run as scheduled.

After the delay time has expired or when the event occurs, the task is activated and the operating system takes it over to schedule its execution.

Fig. 2: E A task can have one of three states: blocked, ready and active (running).

A task can have one of three states (picture 2):

Blocked - the task waits for an event to occur or for a timeout to expire.

ready (ready) - the task is ready to be executed and waits for the real-time operating system to add it to the schedule for execution by the CPU.

active (running) - the task is currently being executed on the CPU; only one task can be in this state at a time.

Preemptive kernel and context switching

If you look at the specification of a real-time operating system, you will inevitably come across terms like "preemptive kernel" or "supports pre-emption". What exactly does that mean? Suppose a system is executing a low priority task while a higher priority task is blocked because it is waiting for a signal from an ISR. As soon as the interrupt occurs and the ISR is executed, the operating system interrupts the low priority task. After the end of the interrupt service routine, the program execution does not jump back to the task with low priority, but first processes the task with high priority. The task with high priority was given priority over the task with low priority.

Preemption helps a real-time operating system to react quickly, because the operational task with the highest priority is taken over by the RTOS for execution in the schedule and executed on the CPU. There are different scheduling algorithms:

  • priority-based scheduling,
  • FIFO (first in, first out),
  • "Round-Robin" (in order) and
  • Time slice.

The process in which one operating system stops one task and executes another is called a context switch. The context of a task consists of the contents of the CPU register including the stack pointer and the program counter. If a context switch occurs, the RTOS shifts the contents of the CPU register into the stack of a task, sets the task status to either "ready" or "blocked" and saves the value of the task pointer in the task control block of the corresponding task. The operating system then restores the context of the task to be executed on the CPU. To do this, it restores the stack pointer using the data from the task control block and reads the content of the CPU register from the task's stack back into the CPU. The RTOS sets the status of the corresponding task to "active" and the restored task is now continued.

During a context switch, the real-time operating system performs an internal operation so that the user application cannot run during this time. So the less time it takes to execute the context switch, the better. Real-time operating system vendors often specify the time it takes to perform a context switch for a particular family of CPUs.

The time required for a context switch can often be minimized by using special functions of the CPU used. For example, the CPU cores "H8S", "H8SX" and "SH-2A" from Renesas have special commands with which the contents of several CPU registers can be moved to or from the stack. Accordingly, only a single instruction has to be called up and executed in order to carry out several read-out or read-in processes. This reduces the amount of code and speeds up execution. The SH-2A-CPU also supports register banking. With this function, an ISR can be entered within just six cycles and all CPU registers can be copied into a register bank. This enables the system to react to interrupt signals in real time.

1. Introduction to the RTOS world
2. Control signals
3. Memory management and timers

Related articles

Renesas Electronics Europe GmbH