By: Edward Nova
Associated Project: Yes
Associated Part Family: CY8C25xxx, CY8C26xxx
This Application Note is a demonstration of a Real-Time Operation System (RTOS) for PSoC microcontrollers. This can be used for variety of applications such as control systems or automotive industries.
PSoC microcontrollers have versatile interrupt capabilities, which is what modern embedded applications require.
It was considered that the implementation of a RTOS\u2019 Kernel, using the Rate Monotonic Scheduling (RMS) with task blocking method is best suited for PSoC. We will show how this Real-Time Multi-tasking Kernel provides means to run a number of tasks at a specific time interval by using Periodic Time Slot method. In this implementation, the Periodic Time Slots do not block each other. The task blocking is possible only by using the Interrupt services.
The implementation is very simple, runs very fast and does not consume valuable CPU time and memory. This framework also can be extended to accommodate much more complex scheduling methods.
This project has been developed with the PSoC Designer Version 3.10 Build 240.This Application will run with Pod 28 Rev D. or E.
To illustrate the Multithread feature of the RTOS, eight LEDs on the Pod (Or Pup) are being toggled. Each task has an associated LED. The LED will blink according to its task time slot. Every time slot is twice as long as the previous one.
Using this RTOS will enable you to create your project in much shorter time. At the same time, the structure of RTOS is helpful to organize your program modules.
There are many variations of RTOSs. The one described in this Application Note is designed to be very fast and simple to implement. As I will demonstrate, this RTOS does not take too many resources from PSoC. There are number of other advantages that will be discussed in more details later.
In Real-Time Systems a right answer at the wrong time is a wrong answer. In time-critical systems, operating system guarantees that all the critical sections of the program are processed in a timely manner and will meet their deadlines.
Depending on each application, the designer needs to choose a proper microcontroller. The CPU speed, memory space and the number of input and output are some of the features that must be considered when choosing a microcontroller. On the other hand, designers also need to choose how to handle the software requirements.
In Sequential Processing, tasks are handled one at a time and CPU is allocating all its time and other resources to this specific task. Traditional flowcharts are quite capable of illustrating these systems. Sequential Processing can be used in sections of a complex system, a small size system or an application that does not deal with multiple events or external interrupts.
State Machines are very useful and can simplify systems that have limited number of states. The transaction rules are not complicated. For example, scanning a keyboard or modeling of an elevator can be implemented with a simple State Machines.
A task is a small program section to do a job. It is a basic unit of resource allocation. Tasks are competing for CPU time, memory space or other resources. A complex problem can be divided to a number of smaller and simpler tasks. A big task also can be divided into several small time slots. A time slot may contain several tasks.
Every task handles only a small portion of the problem. These tasks can run at different priorities or at regular time intervals. Modern embedded systems with a number of external interrupts are inherently closer to Multitasking Operating Systems.
Time slot is the minimum time interval that can be allocated to a task. In this example the time slot is 1 ms so Task 1 is running at a periodic rate of every 2 ms for up to 1 ms. Task 2 executes at every 4 ms for up to 1 ms, and so on. Tasks requiring higher priority need shorter periodic rates.
Task 1 Period = 1 ms x 2^1 = 2 ms.
Task 2 Period = 1 ms x 2^2 = 4 ms.
Task 3 Period = 1 ms x 2^3 = 8 ms.
Task 4 Period = 1 ms x 2^4 = 16 ms.
Task 5 Period = 1 ms x 2^5 = 32 ms.
Task 6 Period = 1 ms x 2^6 = 64 ms.
A typical embedded system is cost sensitive. Processors alone usually determine the cost of the system. Speed and memory are major factor for a processor. To reduce the cost, designers choose the most cost-effective processor to get the job done.
Real-Time Kernels contain only the essential functions of RTOS such as task scheduling and interrupt handling.
It is true that developing a Real-Time Kernel needs extra work and takes time and memory from the processor, but more and more Real- Time Kernels are being used in embedded systems for various reasons.
Reliable Scheduling simplifies the process of calculating how much CPU time is being utilized. Having a reliable scheduling mechanism also allows the designer to use the state-driven techniques.
For example, in applications that need to de- bounce a key, the designer would use a task to test the status of the key and the time delay. This task will block the CPU from running other tasks.
State-driven keyboard de-bouncer is much more accurate and versatile and while the de-bounce task is in various states, the CPU can run other tasks.
Faster time to market becomes more and more vital for embedded systems and having a simple framework helps to add the application easily in the Real-Time Kernel.
Using Real-Time Kernel enables designers to simplify a complex operation into a few much smaller tasks. This will also allow multiple designers to share the CPU resources in an organized manner.
Scheduler in a RTOS is the function that determines the status of all the tasks. In this application a task state can be:
The Scheduler evaluates a number of factors such as task priority or interrupt status. Based on this evaluation it sets tasks to an appropriate state.
When the Scheduler determines that a task with higher priority needs to be executed, it will block the running task. Then it will store the information needed to resume its execution. This Information is called \u201cTask Context\u201d and it usually contains Program Counter (PC), CPU Registers and the Accumulator. The Scheduler runs the higher priority task. Upon completion it performs a reverse process of restoring the Task Context. Storing and restoring the Task Context by Scheduler is called \u201cContext Switching.\u201d
Stacks are commonly used to store Context Switching information, but some designers prefer to dedicate static memory location.
To have an efficient Context Switching, a good C compiler will examine all the registers in every task and store only those registers that are being changed. This not only makes the Context Switching faster but also reduces usage of valuable RAM and programming space.
Context Switching increases additional time and memory overhead. This is why in some applications a Non-Preemptive Scheduler is preferred.
Depending on the hardware and software in an embedded system, designers can choose one of many types of Real-Time Kernels. Two of the most commonly used are:
In systems that do not require interruption of tasks, a Non-Preemptive kernel is most common. In a Non-Preemptive kernel, the task started by the Scheduler gains the time and memory resource of the system. Only after this task is completed does the control go back to the Scheduler.
Implementing a Non-Preemptive kernel is relatively simple because it does not deal with Context Switching.
In a Preemptive kernel, the Scheduler can transfer the resources of the system by running the task to a task with a higher priority. In other words, tasks can be interrupted by higher priority tasks. That is why most Preemptive kernels are using an Interrupt mechanism to schedule higher priority tasks.
Some tasks or sections of a task need to have total control of resources. In this case no other task should be able to interrupt them. These critical sections need to be as short as possible so other tasks are able to meet their deadlines. In this case, a global interrupt disable can be used to disable all maskable interrupts. Special care should be taken to deal with the non-maskable interrupts.
Rate Monotonic Scheduling (RMS) is a method to implement the kernel in many Real-Time Operating Systems.
RMS is fairly simple to implement and is very reliable. In RMS, the priorities are determined and fixed at the design time. Periodic tasks with shorter periods get the higher priority.
Now bringing you back...
Does that email address look wrong? Try again with a different email.