Unit II Part 2
Unit II Part 2
Unit – II
Basic Design Using a Real-Time Operating System
T. Ramakrishnaiah
Associate Professor
Department of Electronics and Communication Engineering
VARDHAMAN COLLEGE OF ENGINEERING
Shamshabad, Hyderabad – 501218, India.
Embedded Systems (A2425)
Unit - II
Principles
Semaphores and Queues
Hard Real-Time Scheduling Considerations
Saving Memory and Power
An Example RTOS like µC-OS (open source)
2
Embedded Systems (A2425)
Principles
General Operation
the RTOS tasks spend most of the time blocked, waiting for an interrupt
routine or another task to send a message or cause an event or free a
semaphore to tell the task that there is something for it to do.
When an interrupt occurs, the interrupt routine uses the RTOS services
to signal one or more of the task, each of which then does its work and
each of which may then signal yet other task.
In this way, each interrupt can create a cascade of signals and task
activity.
3
Embedded Systems (A2425)
Telegraph System
4
Embedded Systems (A2425)
Principles Cont..
When the system receives a network frame, the hardware interrupts. The
interrupts routines reset the hardware and then passes a message containing
the received frame to the DDP (datagram delivery protocol) protocol task.
The DDP protocol task was blocked waiting for a message. When this
message arrives, the task wakes up and among many of other things,
determines if the frame was intended for telegraph or received by telegraph
by mistake .
If the frame was intended for telegraph, the DDP protocol task sends a
message containing the received frame to the ADSP (Apple Talk data Stream
Protocol) protocol task.
5
Embedded Systems (A2425)
Principles Cont..
This message unblocks the ADSP protocol task, if the frame contains print
data; the ADSP protocol task sends a message containing the data to the
serial-port task, which sends a data to the serial port hardware and
through it to the printer.
Similarly, when the system receives serial data from the printer, the
interrupt routine resets the hardware and forwards the data in a message
to the serial port task.
If the data contains printer status, the serial port task forward the status to
the ADSP, the ADSP uses it when respond to later status request from the
network.
6
Embedded Systems (A2425)
Principles Cont..
Best principles in design of an RTOS:
Write short interrupt routines
Encapsulation of tasks
7
Embedded Systems (A2425)
Principles Cont..
Write short interrupt routines:
Short interrupt routines are preferable than long interrupt routines.
8
Embedded Systems (A2425)
Principles Cont..
Suppose that we are writing the software for the system with the
following characteristics:
Commands arrive one at a time, the next command will not arrive
The serial port hardware can only store one received character at a
Principles Cont..
One wretched way to write this system is to do all of the work in the
interrupt routine that receives characters.
That interrupt routine will be long and complex and difficult to debug and it
will be slow response for every operation.
At the opposite extreme you could write this system with an entirely
brainless interrupt routine that simply forwards every character in an RTOS
as message to command parsing task.
One possible compromise design uses an interrupt routine that saves the
received characters in a buffer and watches for the carriage return that
ends each command.
When the carriage return arrives, the interrupt routine sends a single
message to the command parsing task, which reads the characters out of
the buffer.
1
0
Embedded Systems (A2425)
Principles Cont..
How Many Tasks?
system’s work into the RTOS tasks. This will make to have a large number of
tasks. There are some advantages and disadvantages of using a large
number of tasks.
Advantages:
With more tasks you have better control of the relative response time.
With more tasks you can sometimes encapsulate the data more effectively.
1
1
Embedded Systems (A2425)
Principles Cont..
Disadvantages:
With more tasks you are likely to have more data shared among two or more
tasks. This will make the system to have more semaphores, and hence into
more microprocessors time to handle semaphores and into more semaphore
related bugs
With more tasks you are likely to have more requirements to inter-task
communication through pipes, mailboxes queues and so on. This will also
translate more microprocessor time and more chances for bugs.
Each task requires a stack; therefore more tasks probably need more
Principles Cont..
Each time the RTOS switches tasks. It takes more
microprocessor time for saving the context of the task that is
stopping and restoring the context of the task about to run.
More tasks probably means more calls to the RTOS.
The moral is use as few tasks as you can get away with; add
1
3
Embedded Systems (A2425)
Principles Cont..
Task Priorities:
Decomposing based on ‘functionality’ and ‘time criticality,’
1
4
Embedded Systems (A2425)
Principles Cont..
Tasks for encapsulation:
It makes sense to have a separate task to deal with hardware shared by
problems.
When other task in the system has information to display, they send
properly; Simple logic in the display task can then decide which message
should be placed on the display.
1
5
Embedded Systems (A2425)
1
6
Embedded Systems (A2425)
Principles Cont..
private data and its life time. And how many number of times
that will execute, And from which task it reads data and to
which any number of other tasks it write.
A task declares its own private data.
1
7
Embedded Systems (A2425)
1
8
Embedded Systems (A2425)
Principles Cont..
Advantages of the task structure:
The task blocks in only one place. When another task puts a
request on its task queue; this task is not in the off waiting for
some other event that may or may not happen in a timely fashion.
When there is nothing for this task to do, its input queue will be
empty, and the task will block and use up no microprocessor time.
This task does not have public data that other tasks can share, so
Principles Cont..
Avoid creation and destroying tasks
Every RTOS allows you to create tasks as the system is starting.
Most RTOSs also allow to create and destroy tasks while system is
running.
There are two good reasons to avoid this:
The functions that create and destroy tasks are typically the most
Principles Cont..
Turning time- slicing off
The situation in which, if two or more ready tasks have the same priority and
those tasks.
Giving the microprocessor to one of the tasks for a short period of time, then
switching the microprocessor to the other task for a small period of time and
so on.
One might want to consider whether they want to have two tasks with the
priority or whether they could just as well be one task. RTOS allows to turn off
this option.
2
1
Embedded Systems (A2425)
An Example
Underground Tank monitoring System:
Summary of Problem Specification:
System of 8 underground tanks
Measures read:
Set an alarm on leaking tank (when level slowly and consistently falls over time)
System can override user display options and show warning messages
2
2
Embedded Systems (A2425)
2
3
Embedded Systems (A2425)
Encapsulating Semaphores
At least some of those bugs stem from undisciplined use. You can squash
these bugs before they get crawling simply by hiding the semaphore and
the data that it protects inside of a module.
In the above code , rather than letting just any code that wants the value of
the lSecondsToday variable read it directly and hopping for the best, this
construction forces any code that wants to know the value of
lSecondsToday to call lSecondSinceMidnight to get it.
lSecondSinceMidnight uses the semaphore correctly. This semaphore will
cause no more bugs.
2
4
Embedded Systems (A2425)
2
5
Embedded Systems (A2425)
2
6
Embedded Systems (A2425)
2
7
Embedded Systems (A2425)
Encapsulating Queues
Similarly, you should consider encapsulating queues that task use to receive messages
from other tasks. We should write code to handle a shared flash memory. That code
deals correctly with synchronizing the requests for reading from and writing to flash
memory. However, it would probably be a bad idea to ship that code in a real system.
Consider this list of potential bugs:
Even if everyone uses the correct structure, some body may assign a value to flash other
Any task might destroy the flash task input queue by mistake.
The flash task sends data it read from the flash back through another queue. Someone
2
8
Embedded Systems (A2425)
the deadline, which comes from fast code. A hard RTOS is one which
has predictable performance with no deadline miss.
Hard real time systems provide the following:
Some critical code in assembly to meet real time fast.
When hard real time tasks are running, all other interrupts of lower
priority are disabled.
Predictions of interrupt latencies and context switching latencies of the
tasks.
Pre-emption of higher priority task by lower priority task.
2
9
Embedded Systems (A2425)
Each task worst case execution time, Cn units of time and deadline of Dn
Question: SCn = S(Dn + Jn) < Tn, where Jn is some variability in task’s time
operation.
Check that our development tools aren’t taking much time for
development.
Configure our RTOS to contain only those functions that are needed.
3
1
Embedded Systems (A2425)
3
2
Embedded Systems (A2425)
Saving Power
Most embedded system microprocessors have at least one power-saving mode.
Software can typically put the microprocessor into one of these modes with a
special instruction
The modes have names such as:
Sleep mode
Low-power mode
Ideal mode
Standby mode
A very common power saving mode is one in which the microprocessor stops
executing instructions, stop any built in peripherals, and stops its clock circuit. This
saves a lot of power, but the drawback is that the only way to start the
microprocessor is to restart it.
3
3
Embedded Systems (A2425)
write fast software. The faster your software finishes its work, the sooner it can put
the microprocessor back into a power saving mode and stop using up the battery.
3
4
Embedded Systems (A2425)
1. Semaphore
2. Queues and
3. Mailboxes.
Other services that a μ C-OS provides are:
1. Mutual exclusion semaphore semaphores(to reduce priority inversions)
2. Event flags
3. Task management
a) Create a task
b) Delete a task
c) Change priority of a task.
d) Suspend or resume a task
4. Invariable size memory blocks management.
3 5. Time and time management.
5