RTOS New
RTOS New
System (RTOS)
Prof. P.B.Borole
Embedded Software
Written by designer. Not by user.
Designer must be expert in software and
hardware designs.
Two types – Non real time and real time.
The real time system processes events in short
time.
Event occurred on system input causes other
event to occur as system output. E.g. Pressing
a key, change in temperature are the events.
Real time system
Design goals – Minimize response time
- Improve reliability.
- Increase security
- Increase correctness at function level and
timing level.
There are two types – Single task system and
Multitasking system.
In single task system, RTOS is not required.
Single task system software can be developed
in assembly language program.
Real time System
It is very difficult to develop multitasking software
without RTOS.
Real time system software has many foreground and
background routines.
Real time system handles two or more activities –
some activities are independent while some activities
are dependent. Some activities are synchronous while
some activities are asynchronous.
Synchronous activity occurs at particular instant,
hence the CPU will not keep watch on this activity
continuously. RTOS may not be required.
Asynchronous activity occurs at any time, hence CPU
has to keep watch on this activity continuously.
RTOS is required.
Real Time System
In real time system, the CPU has to handle
some activities in parallel.
Some activities are periodic with short period
while some are with very large period.
Activities have different priority of processing.
Some activities must be processed in short
period while others can take more time.
Some activities are event driven.
Some activities are based on elapsed time.
Real Time System
The ordinary microcontroller programs are
sequential. These programs do all activities
sequentially. If any activity is blocked due
absence of external event, then the next
activities are stalled.
In real time system, some activities should be
done in parallel.
Each activity can be treated as a task.
Multitasking -1
Sequential without interrupts:
Start
Initialize system
All tasks are synchronized.
Task N Low
All low priority tasks are
invoked one
By one. Used in small embedded system
Multitasking - 3
Start
Initialize system
Interrupts form
all tasks
Wait for interrupts
Task 1
Wait for interrupts
Task 2
The entire system is driven by a timed Task switching is done
Interrupts. and task switching memory
Task N
is required.
}
Code thread 2
{
}
RTOS
In RTOS, interrupts instruct task code to do work. On conventional
OS, the OS takes help from the interrupt.
The necessary signaling between the ISR and the task code is
handled by the RTOS. Shared variable should not be used.
User code does not decide what needs to be done next. RTOS
decides which of the task code function should run.
RTOS knows about the various task code subroutine and will run
most urgent routine at any given time.
The RTOS can suspend one task code subroutine in the middle.
The RTOS can control task code response and ISR response.
RTOS arranges priorities of ISRs and priorities of task code ( 256
levels).
The system response will not change even if we change the task
code.
Most complicated OS.
RTOS can be background program.
RTOS
RTOS identifies time critical tasks.
RTOS schedules the tasks in such a way that all the tasks are
completed and meets the dead line of time critical task.
RTOS decides when to send interrupt to the processor.
RTOS identifies the shared resources and solves shared resources
problems.
RTOS manages inter task communication.
RTOS keeps track of the time.
Manages memory.
We can compile and link application program separately from the
OS.
Application programs and RTOS are tightly coupled.
RTOS does not protect itself from application program. Hence the
bad application program crash whole system.
Interrupt latency is low.
Deterministic OS i.e. worst case response time can be predicted.
RTOS
We can configure RTOS.
We can write our own RTOS.
Follows POSIX standard developed by IEEE. I.e. API commands are
POSIX standard. Hence application developed for one RTOS is
portable with other RTOS.
Provides TCP/IP support for network related services.
Provides device drivers.
Kernel requires few kilobytes.
Supports many processors.
It can be hardware based OS.
It may not be royalty free OS because the designer is going to
download Application program and RTOS kernel into ROM of the
embedded system.
Priority of Tasks can be changed dynamically.
Divided into three categories – Embedded OS, RTOS and Mobile OS.
RTOS
Concept of RTOS:
What is Task?
Communication software
File system
Kernel OS
Libraries Device manager
RTOS
Kernel is the heart of the OS.
Manages tasks through scheduling to achieve
desired performance.
Provides inter task communication.
Kernel consists of objects such as tasks, mutexes,
ISRs, events, message box, mail boxes, pipes and
timers.
Kernel provides memory management services,
interrupt handling services and device management
services.
Device manager manages the IO devices through
interrupts and device drivers.
RTOS
Communication software consists of
communication interfaces such as Ethernets, USB
protocols, TCP/IP stack etc. This is optional.
Required in networked embedded system.
Libraries may have some C/C++ library files in
object code which can be used through API calls.
File system divides flash memory into small files
and manages these files.
API is used to develop application software on
RTOS.
Kernel Architecture
Message queues
Signals
Event registers
Mail boxes
Scheduler
ISRs
Pipes
Timer
System tasks
Mutexes
Semaphores
Kernel
Kernel has its own system tasks with priorities.
The kernel tasks are start up task, exception
handling tasks, Log In task and Idle task.
Idle task – If no task is running, then idle task is
executed so that the CPU is always engaged. Low
priority.
All tasks have to share the CPU time in discipline
manner.
Task states – At a time only one task is being
executed by the CPU whereas other tasks are
waiting for external events or waiting for CPU
time.
RTOS state diagram
Ready to run
Running
Waiting
ISR
State diagram
Running: Only one task will be in this state at
a time in single processor system. In
multiprocessor system, this state is assigned to
two or more tasks.
Waiting or blocked: In this state CPU was
available, but task wants to wait for external
event. Any number of tasks can be in this state.
Ready to run: Task is ready but waiting for
CPU time. Any number of tasks can be in this
state.
kernel
Task scheduling – Only one CPU has to handle multiple
tasks. All tasks have to share the CPU time in discipline
manner.
Task should not take long time.
Critical task should have higher priority.
Task scheduler decides which task will get CPU time next.
All the time dead line should not be violated.
In deterministic OS, the time required to execute the task
can be estimated.
Scheduler keeps the track of the state of each task.
Task has full control on scheduler in RTOS. In ordinary
OS this is not possible.
The scheduler of RTOS is simple minded. In desk top OS,
the scheduler is strict minded.
Kernel
Tasks have to share the system resources such as CPU
registers, external memory and IO devices.
In RTOS, two tasks can be written independently of one
another and system will still respond well.
RTOS has its own private data structure which is not
available to any of task.
Task scheduling Algorithm –
1 FIFO. Executes tasks on first come first served basis.
2 Round robin. Executes tasks in round robin fashion.
3 Round robin. Executes tasks in round robin with priority
except that the high priority task can interrupt low priority
task.
kernel
4 Shortest Job first. Highest priority is assigned to short job
and lowest priority is assigned to long job.
Algorithm 1 to 4 are used in soft real kernel.
5 Non preemptive. Called cooperative. The high priority
task can its CPU time to low priority task for completion.
The priority of current task is respected by the other task.
But high priority task has to wait for long time. This is not
suitable for real time embedded system.
6 Preemptive. The high priority task suspends execution
of low priority task if CPU time is allocated to it.
The preemptive kernel is deterministic. It is used in real
time system.
RTOS
Mutual exclusion : When two or more tasks access
shared resources without corrupting data is called
Mutual exclusion.
Semaphores: used for shared resources and task
synchronization.
Resource synchronization -
Display
Task 1 Display
Task 2 Display humidity
temperature
There should be proper discipline to share display for task 1 and task 2
RTOS
Task Synchronization:
Memory
Task ID
Task Name
Task state ‘Reset ‘ for disabling task run.
‘Set’ for enabling run.
Event awaited
Signal mask
Timer setting
Stack pointer
Return address
RTX51
RTX51 is a multitasking real-time operating system for
the 8051 family of processors.
RTX51 simplifies software design of complex, time-
critical projects.
There are two distinct versions of RTX51 available:
RTX51 Full : Performs both round-robin and
preemptive task switching using up to four task
priorities.
RTX51 works in parallel with interrupt functions.
Signals and messages may be passed between tasks
using
a mailbox system.
You can allocate and free memory from a memory pool.
You can force a task to wait for an interrupt, timeout, or
signal or message from another task or interrupt
RTX51
RTX51 Tiny: Is a subset of RTX51 that will
easily run on single-chip 8051 systems without
any external data memory.
RTX51 Tiny supports many of the features
found in RTX51 with the following
exceptions:
RTX51 Tiny only supports round-robin and
the use of signals for task switching.
Preemptive task switching is not supported.
No message routines are included. No memory
pool allocation routines are available.
RTX51
Many microcontroller applications require simultaneous
execution of multiple jobs or tasks.
For such applications, a real-time operating system
(RTOS) allows flexible scheduling of system resources
(CPU, memory, etc.) to several tasks.
RTX51 implements a powerful RTOS which is easy to use.
RTX51 works with all 8051 derivatives.
You write and compile RTX51 programs using standard C
constructs and compiling them with C51.
Only a few deviations from standard C are required in
order to specify the task ID and priority.
RTX51 programs also require that you include the real-
time executive header file and link using the BL5
Linker/Locator and the appropriate RTX51 library file.
RTX 51
The system clock for RTX 51 full is set between
1000 and 40000 cycles per second.
RTX 51 Tiny is set between 1000 and 65535 cycles
per second.
RTX51 Tiny uses the standard 8051 Timer 0 (mode 1) to generate a
periodic interrupt.
This interrupt is the RTX51 Tiny Timer Tick. Timeout and interval
values specified for the RTX51 Tiny library routines are measured
using the RTX51 Tiny Timer Tick.
RTX51 tiny supports a maximum of 16 tasks.
Tasks are simply C functions that have void return type and
void argument.
RTX 51
Task syntax
void func (void) _task_ task_id
func - name of the task function.
task_id is id number of task
void job0 (void) _task_ 0
{ while (1)
{ counter0++; /* increment counter */ }
}
All tasks should be implemented as endless loops. A task
should never return.
There is no main function.
RTX 51
Signal is used to synchronize all tasks to run the software.
In RTX 51, the task can be in ready, deleted, running, time
out and waiting states.
The number of semaphores in RTX 51 full is maximum 8
(binary semaphores). Semaphores are not available in
RTX51 tiny.
RTX51full has a maximum 8 mail boxes. The message can
be a maximum length of 8 integers.
Message boxes are not available in tiny version.
RTX 51 uses standard C constructs.
RTX 51 is compiled by C51.
#include <rtx51tny.h> or #include < rtx51full.h> should be
included in C program.
RTX 51
RTX 51 tiny has 900 bytes of code. It requires on chip 7
bytes of RAM and 3 bytes per task indirectly addressable
data RAM.
Programming rules:
1Be sure to include the RTX51TNY.H header file.
2Do not create a main C function. RTX51 Tiny has its own
main function. The main C function is included in the
RTX51 Tiny Library to start the operating system and task
0.
3You program must contain at least one task function.
4Your program must call at least one RTX51 Tiny library
routine (like os_wait). Otherwise, the linker will not
include the RTX51 Tiny Library.
RTX 51
5Task 0 is the first function in your program that
executes. You must call the os_create_task function
from task 0 to run other tasks.
6Task functions must never exit or return.
7The task must repeat using a while(1) or similar
construct.
8Use the os_delete_task function to halt a running
task.
9You must specify RTX51 Tiny in µVision2 or on
the linker command line.
RTX 51
µVision2 IDE
To create RTX51 Tiny programs with
µVision2...
Open the Options for Target dialog (select
Options for Target from the Project menu).
Select the Target tab.
Select RTX-51 Tiny from the Operating
System option list.
For debugging select RTX51 Tiny Task list
from the Peripherals menu to display the
dialog. The example of RTX51
RTX 51 example
The os_wait Function