0% found this document useful (0 votes)
83 views

Embedded Systems Ch-4

This document provides information about real-time operating systems (RTOS). It defines an RTOS and describes how they differ from general purpose operating systems in prioritizing tasks and meeting deadlines. The document outlines types of real-time systems and examples of RTOS like FreeRTOS. It discusses features of RTOS like short context switching times and how languages like C are commonly used. Benefits and drawbacks of using an RTOS are also summarized.

Uploaded by

NatyB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

Embedded Systems Ch-4

This document provides information about real-time operating systems (RTOS). It defines an RTOS and describes how they differ from general purpose operating systems in prioritizing tasks and meeting deadlines. The document outlines types of real-time systems and examples of RTOS like FreeRTOS. It discusses features of RTOS like short context switching times and how languages like C are commonly used. Benefits and drawbacks of using an RTOS are also summarized.

Uploaded by

NatyB
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Embedded Systems

Chapter Four
Real Time Operating Systems (RTOS)
What is an OS?
• An organized collection of software extensions of hardware that serve
as...
➢control routines
for operating a
computer (for
example, to gain
access to computer
resources (like file
I/O))
➢an environment
for execution of
programs
A more formal definition of OS!
Operating system is a piece of software which
controls all the computer’s resources and provides
the base upon which the application programs can
be written
Where your OS sits in your System?

compilers
databases
word processors

CPU
memory
I/O devices
TYPES OF OPERATING SYSTEMS
➢Stand-Alone Operating system
➢Network Operating systems
➢Embedded Operating systems
Stand-Alone Operating system
It is a complete operating system that works on a desktop or
notebook computer.
Examples of stand-alone operating systems are:
➢DOS (Disk Operating System) – runs from a disk drive
➢Windows 2000 professional – client version of windows,
first released by Microsoft in February 2000.
➢Mac (Macintosh) OS X –is a Unix-like operating system
developed and marketed by Apple Inc. since 2001.
Network Operating systems
It is an operating system that provides extensive support for
computer networks. A network operating system typically
resides on a server.
Examples of a network Operating system are:
➢Windows 2000 server
➢Unix – first developed in 1960s.
➢Linux - In fact, one of the most popular platforms on the
planet, Android, is powered by the Linux operating system.
➢Solaris –UNIX-based operating system of Sun
Microsystems
Embedded Operating system
You can find this operating system on handheld computers
and small devices. It resides on a ROM chip.
Examples of embedded operating systems are :
➢ Windows CE – windows embedded compact - is an
operating system subfamily developed by Microsoft as
part of its Windows Embedded family of products.
➢ Pocket PC 2002
➢ Palm OS
What is RTOS?
• An operating system that supports real-time applications and
embedded systems.
• Real-time applications have the requirement to meet task deadlines
in addition to the logical correctness of the results.

➢ Multiple events handled by a single processor


➢ Events may occur simultaneously
➢ Processor must handle multiple, often competing events
Difference Between a General Purpose Operating
System (GPOS) and an RTOS
➢A GPOS is used for systems/applications that are not time critical.

➢In the case of a GPOS – task scheduling is not based on “priority",


it is programmed to achieve high throughput (performance)

➢A GPOS is made for high end, general purpose systems whereas


RTOS is usually designed for a low end, stand alone device

➢All the process and threads in RTOS has got bounded latencies –
which means –a process/thread will get executed within a specified
time limit
.
User interaction is the main focus of Most RTOSs are designed to run on
GPOS design, Some latency is microcontrollers, and include a
acceptable (so long as users don’t scheduler, resource management,
notice a long lag time). and device drivers.
Classification of real time systems(RTS)

➢Hard Real Time System

➢Firm Real Time System

➢Soft Real Time System


Hard Real Time
Here missing an individual deadline results in catastrophic
failure of the system which also causes a great human and
financial loss .

The examples for Hard real time systems are:


▪ Air traffic control
▪ Nuclear power plant control
Firm Real time:
In this, missing a deadline results in unacceptable quality
reduction. Technically there is no difference with hard
Real time, but economically the disaster risk is limited.

Examples for Firm real time are :


▪Failure of Ignition of a automobile
▪Failure of opening of a safe
Soft real time:
Here the dead line may not be fulfilled and can be
recovered from. The reduction in system quality and
performance is at an acceptable level.

Examples of Soft real time systems :


▪Multimedia transmission and reception
▪Networking, telecom (Mobile) networks
▪websites and services
▪Computer games
Features of RTOS:
•Context switching latency should be short. This means that the time
taken while saving the context of current task and then switching
over to another task should be short.

•The time taken between executing the last instruction of an


interrupted task and executing the first instruction of interrupt
handler should be predictable and short. This is also known as
interrupt latency.
•Similarlythe
•Similarly thetime
timetaken
taken between
between executing
executing theinstruction
the last
last
of theinstruction of the and
interrupt handler interrupt handler
executing andtask should also
the next
executing
be short andthe next taskThis
predictable. should also
is also be short
known and
as interrupt
predictable.
dispatch This is also known as interrupt
latency.
dispatch latency.
•Reliable and time bound inter process mechanisms should be
•Reliable
in place forand time bound
processes inter process
to communicate with each other in a
mechanisms
timely manner. should be in place for processes to.
•A RTOS should have support for multitasking and task
preemption. Preemption means to switch from a currently
executing task to a high priority task ready and waiting to be
executed.

•RTOS should be fault tolerant


Some of the most widely used RTOS are:-
•LynxOS
•OSE
•QNX
•RTLinux
•VxWorks
•Windows CE
•FreeRTOS
Programming languages Used

➢C is most widely used in RTOS programming

➢C++ and Ada are the next more popular for large projects.

➢Java may also be a choice for some applications


Super Loop vs. Multi-threaded Application
• Super loop: no operating system, and the structure is fairly simple:
in your main() function, you set up any variables, drivers, libraries,
etc. and then perform one or more periodic tasks in a while(true)
loop.
• In Arduino, this is embodied by the setup() and loop() functions
• If you have multiple tasks to accomplish during the
main loop, you generally execute them in a round-
robin fashion. The problem comes in when you start
adding so many tasks that some start missing deadlines
or preventing other features from working. This is
where an RTOS can help. Rather than execute
everything in a round-robin fashion, you can essentially
execute everything concurrently.
Cont.
• You can still have interrupts--they would halt whatever task was being
run at the moment to execute the ISR and then return execution to the
task.
• A task is a set of instructions loaded into memory. It can also mean some
unit of work or goal that needs to be accomplished.
• A thread is a unit of central processor
(CPU) utilization with its own program
counter and stack memory.
• A process is an instance of a computer
program. A process may have multiple threads
Most embedded programs are written as a
single process, but a GPOS usually has
many processes running at the same time.
Benefits of Using RTOS
• Allows you to split and prioritize the application code
• The RTOS always runs the highest priority task that is ready
• Adding low-priority tasks don’t affect the responsiveness of high priority tasks
• Tasks wait for events
• Avoids polling
• RTOSs make it easy to add middleware components
• TCP/IP stack
• USB stacks
• File System
• Graphical User Interface (GUI) Etc.
• Creates a framework for developing applications
• Facilitate teams of multiple developers
Cont.
• It’s possible to meet all the deadlines of an application
• Rate Monotonic Analysis (RMA) could be used to determine schedulability
• Most RTOSs have undergone thorough testing
• Some are third-party certifiable, and even certified (DO-178B, IEC-61508,
IEC-62304, etc.)
• It’s unlikely that you will find bugs in RTOSs
• RTOSs typically support many different CPU architectures
• Very easy to add power management
Drawbacks of Using an RTOS
• The RTOS itself is code and thus requires more Flash
• Typically between 6-20K bytes
• An RTOS requires extra RAM
• Each task requires its own stack
• The size of each task depends on the application
• Each task needs to be assigned a Task Control Block (TCB)
• About 32 to 128 bytes of RAM
• About 256 bytes for the RTOS variables
• You have to assign task priorities
• Deciding on what priority to give tasks is not always trivial
• The services provided by the RTOS consume CPU time
• Overhead is typically 2-10% of the CPU cycles
• There is a learning curve associated with the RTOS you select
FreeRTOS in Arduino
• FreeRTOS is a class of RTOS for embedded devices which is small enough
to be run on 8/16-bit microcontrollers, although its use is not limited to
these microcontrollers.
• It is a real-time kernel (or real-time scheduler) on top of which embedded
applications can be built to meet their hard real-time requirements;
• It allows applications to be organized as a collection of independent threads
of execution;
• The kernel (nucleus of an OS) decides which thread should be executing by
examining the priority assigned to each thread by the application designer;
• In the simplest case, the application designer could assign higher priorities
to threads that implement hard real-time requirements, and lower priorities
to threads that implement soft real-time requirements
• In FreeRTOS, each thread of execution is called a ‘task’.
How RTOS Work?
• If you want to perform some task, then it should be scheduled using
kernel delay or using interrupts.
Lets’ take two tasks, Led Blink with a 1 sec
interval and Idle (no) task and put Led Blink at
high priority.
• It can be seen that execution starts with an Led
task and it runs for a specified time then for
remaining time, the idle task runs until a tick
interrupt occurs.
• Kernel decides which task has to be executed
according to the priority of the task and total
elapsed time of the Led task.
• The Led task preempts the idle task.
• If there are more than two tasks with the same
priority then they will run in round-robin
fashion for a specified time.
Cont.
• The switching of the non-running task into running state is shown in
the below state diagram.
• Every newly created task goes in Ready state (part of not running
state). If the created task (Task1) has the highest priority than other
tasks, then it will move to running state.
• If this running task preempts by the other task, then it will go
back to the ready state again. Application Programming Interface
• Else if Task1 is blocked by using blocking API, then CPU will
not engage with this task until the timeout defined by the user.
• If Task1 is suspended in running state using Suspend APIs, then
Task1 will go to Suspended state and it is not available to the
scheduler again.
• If you resume Task1 in the suspended state then it will go back
to the ready state as you can see in the block diagram.
Task Functions
• Tasks are implemented as C functions.
• The only thing special about them is their prototype, which must
return void and take a void pointer parameter.
• Each task is a small program in its own right. It has an entry point, will
normally run forever within an infinite loop, and will not exit.
Task skeleton

Task prototype
Creating Tasks
• Tasks are created using the FreeRTOS xTaskCreate() API function.
• pvTaskCode: The pvTaskCode parameter is simply a pointer to the function that
implements the task (in effect, just the name of the function);
• pcName: A descriptive name for the task. This is not used by FreeRTOS in any
way. It is included purely as a debugging aid;
usStackDepth: Each task has its own unique stack Prototype
that is allocated by the kernel to the task when the task xTaskCreate( TaskFunction_t
is created. The value specifies the number of words the pvTaskCode, const char *
stack can hold, not the number of bytes. For example, const pcName, uint16_t
if the stack is 32-bits wide and usStackDepth is passed usStackDepth, void
in as 100, then 400 bytes of stack space will be *pvParameters,
allocated (100 * 4 bytes) in RAM. Use this wisely UBaseType_t uxPriority,
because Arduino Uno has only 2Kbytes of RAM. TaskHandle_t
*pxCreatedTask );
Cont.
• pvParameters: A value that is passed as the parameter to the created task
(can be NULL).
• uxPriority: Defines the priority at which the task will execute. Priorities
can be assigned from 0, which is the lowest priority to the highest priority;
• pxCreatedTask: can be used to pass out a handle to the task being created.
This handle can then be used to reference the task in API calls that, for
example, change the task priority or delete the task. If your application has
no use for the task handle, then pxCreatedTask can be set to NULL;
• There are two possible return values: pdPASS or pdFAIL. Fail indicates
that the task has not been created because there is insufficient heap memory
available for FreeRTOS to allocate enough RAM to hold the task data
structures and stack
Creating a Delay
• Most of the code needs delay function to stop the running task but in
RTOS it is not suggested to use Delay() function as it stops the CPU
and hence RTOS also stops working. So FreeRTOS has a kernel API
to block the task for a specific time.

• This API can be used for delay purposes. This API delay a task for a
given number of ticks. The actual time for which the task remains
blocked depends on the tick rate.
• The constant portTICK_PERIOD_MS can be used to calculate real-
time from the tick rate.
• This means if you want a delay of 200ms, just write this line
Example1 (without RTOS)
With RTOS
Example 2 (Tasks and priority)

You might also like