0% found this document useful (0 votes)
21 views35 pages

Unit II Part 2

Uploaded by

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

Unit II Part 2

Uploaded by

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

Embedded Systems (A2425)

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

 A very normal embedded system design technique is to have each of

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

 Decomposing a problem into tasks - How many Tasks?

 Assigning task priorities

 Encapsulation of tasks

 Use of Task structure

 Avoid creating and destroying tasks

 Turning off the time slicing

7
Embedded Systems (A2425)

Principles Cont..
Write short interrupt routines:
 Short interrupt routines are preferable than long interrupt routines.

Interrupt routine code requiring immediate / quick response.


There are two reasons for this:
 Even lowest priority IR’s are handled before the highest priority task

code (minimize task code response time), writing a longer interrupt


routines translates directly into slower task code response.
 IR’s are error prone and hard to debug than task code (due to

hardware-dependent software parts)

8
Embedded Systems (A2425)

Principles Cont..
Suppose that we are writing the software for the system with the
following characteristics:

 The system must responds to commands coming from a serial port

 Commands always end with a carriage-return (CR)

 Commands arrive one at a time, the next command will not arrive

until the system responds to the previous one.

 The serial port hardware can only store one received character at a

time, and characters arrive quickly.

 The system can respond to commands relatively slow.


9
Embedded Systems (A2425)

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?

 One of the first problem in an embedded system design is to divide your

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 your system can be somewhat more modular.

 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

memory, at least for stack space.


1
2
Embedded Systems (A2425)

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

more tasks to your design only for clear reasons.

1
3
Embedded Systems (A2425)

Principles Cont..

Task Priorities:
 Decomposing based on ‘functionality’ and ‘time criticality,’

separates components into tasks (naturally), task prioritization


will give the quicker response – high priority for time-critical
ones, and low priority for others.

1
4
Embedded Systems (A2425)

Principles Cont..
Tasks for encapsulation:
 It makes sense to have a separate task to deal with hardware shared by

different parts of the system.


 A single task that controls the hardware display can solve these

problems.
 When other task in the system has information to display, they send

messages to the display task.


 The RTOS will ensure that messages sent to the display task are queued

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..

Recommended Task Structure:


 The task structure will give the overall information about

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

there is no shared data, and there are no semaphores and


semaphore problems.
1
9
Embedded Systems (A2425)

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

time consuming functions in the RTOS, than getting a semaphore.


 Creating a task is relatively reliable operation, it is difficult to destroy

a task without leaving little pieces lying around to cause bugs .


2
0
Embedded Systems (A2425)

Principles Cont..
Turning time- slicing off
 The situation in which, if two or more ready tasks have the same priority and

no other ready task has a high priority.


 One option that most other task offer in this situation is to time slice among

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:

1. Temperature of gas (thermometer) read at any time

2. Float levels (float hardware) interrupted periodically by the microprocessor


 Calculate the number of gallons per tank using both measures

 Set an alarm on leaking tank (when level slowly and consistently falls over time)

 Set an alarm on overflow (level rising slowly close to full-level)

 User interface: a) 16-button control panel, LCD, thermal printer

 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

than one of the two legal values.


 Anybody accidentally write a message intended for the flash task to the wrong queue.

 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

might send an invalid queue ID.

2
8
Embedded Systems (A2425)

Hard Real-Time Scheduling Considerations


 A hard real time system is hard or software that must operate within

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)

Hard Real-Time Scheduling Considerations


Characterizing real-time systems:
 Made of task n that execute periodically every Tn units of time

 Each task worst case execution time, Cn units of time and deadline of Dn

 Assume task switching time is 0 and non-blocking on semaphore

 Each task has priority Pn

 Question: SCn = S(Dn + Jn) < Tn, where Jn is some variability in task’s time

 Predicting Cn is very important, and depends on avoiding ‘variability’ in

execution times for tasks, functions, access time of data


structures/buffers, semaphore blocking – any operation that can’t be
done in the same time units on each execution/access
3
0
Embedded Systems (A2425)

Saving Memory Space


Here are a few ways to save code space some of these techniques have
disadvantages. We should apply those only if they are needed to
squeeze our code into our ROM.
 Make sure that two functions do not use same thing or does same

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)

Saving Memory Space Cont..


 We should always look at the assembly language listing created by our

cross-compiler to see if certain of our C statement translate into huge


number of instructions.
 Use ‘static’ variable instead of relying on stack variables.

 For an 8-bit processor, use char instead of int variable

 We can save a lot of space by writing our code in assembly language.

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)

Saving Power Cont..


 Another typical power saving mode is one in which the microprocessor stops

executing instructions but the on board peripheral continue to operate. Any


interrupt starts the microprocessor up again, and the microprocessor will execute
the corresponding interrupt routine and then resume the task code from the
instruction that follows the one that put the microprocessor to sleep. This method
saves less power than the above method.
 Another common method for saving power is to turn off the entire system and user

turn it back on when it is needed.


 If we want to put our microprocessor in power saving mode, we should plan to

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)

An Example μC-OS RTOS:

 μ C-OS is a microcontroller operating system.


 μ C-OS is a simple, pre-emptive, multitasking RTOS in combination with microprocessor
and microcontroller.
 μ C-OS provides basic support for

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

You might also like