SW Architectures
SW Architectures
Round-Robin
Function-Queue Scheduling
Selecting an Architecture
1
Software Architectures
When you are designing embedded software, what
architecture will be the most appropriate for a given
system?
2
Decision Factors
The most important factor
how much control you need to have over
system response.
Good response
Absolute response time requirements
The speed of your microprocessor
and the other processing requirements
Few, loose reqts simple architecture
Many, stringent reqts complex architecture
3
Some Examples
The control of an air conditioner
This system can be written with a very simple
software architecture.
The response time can be within a number of
tens of seconds.
The major function is to monitor the
temperature readings and turn on and off the
air conditioner.
A timer may be needed to provide the turn-on and
turn-off time.
4
Some Examples
The software design of the control of an air
conditioner
A simple assembly program for a low-end
microprocessor
Inputs
Input buttons
Temperature readings
Timer readings
Outputs
The on-off control of the air conditioner
The power control
5
Some Examples
Digital telephone answering machine
A telephone answering machine with digital
memory, using speech compression.
The performance and functions
It should be able to record about 30 minutes of
total voice.
Voice data are sampled at the standard
telephone rate of 8kHz.
OGM of up to 10 seconds
Three basic modes
Default / play back / OGM editing mode
6
Some Examples
The class diagram for the answering machine
Microphone
Line-in Record
Line-out
Buttons
Lights
Speaker
7
Some Examples
The state diagram for the controls activate
behavior
8
Some Examples
The software design for the answering
machine
It must respond rapidly to many different
events.
It has various processing requirements.
It has different deadlines and different
priorities.
A more complex architecture
9
4 Basic SW Architectures
Round-Robin
Increasing
Round-Robin with Interrupts
Complexity
Function-Queue Scheduling
10
Round-Robin Architecture
Very simple
No interrupts
No shared data
No latency concerns
Main loop:
checks each I/O device in turn
services any device requests
E.g.: Digital Multimeter
11
Round-Robin Architecture
The simplest architecture
Device A
Device B
Device Z
12
An Application
Digital multimeter
Measures
R, I, and V readings
I/O
Two probes
A digital display
A rotary switch
Function
Continuous measurements
Update display
13
Digital Multimeter
The possible pseudo-code
14
Digital Multimeter
Round-robin works well for this system because:
only 3 I/O devices
no lengthy processing
no tight response requirements
Emergency control
No such requirements
Users are unlikely to notice the few fractions of a
second it takes for the microprocessor to get around
the loop
Adequate because it is a SIMPLE system!
15
Discussion
Advantages
Simplicity
Low development cost
Short development cycle
Shortcomings
This architecture cannot handle complex
problems.
16
Shortcomings
If any one device needs response in less time
Two possible improvements for the RR
architecture
Squeezing the loop
Carefully arranging the sequence
(A,Z,B,Z,C,Z,D,Z,…)
If there is any lengthy processing to do
Every other event is also postponed.
This architecture is fragile
A single additional device or requirement may
break everything. 17
Round-Robin with Interrupts
A little bit more control
In this architecture,
ISRs deal with the very urgent needs of the
hardware and set corresponding flags
the main loop polls the flags and does any follow-
up processing
ISR can get good response
All of the processing that you put into the ISR
has a higher priority than the task code
18
A Little Bit More Control
You can control the priorities among the ISR
as well.
The software is more event-driven.
Shared Variables
19
The Architecture
Two main parts
Interrupt Service Routines The main loop
20
RR vs. RR-INT
Priority levels
21
Discussion
Advantage
The processing is more efficient.
Response time is shorter.
Disadvantage
All of the shared-data problems can potentially
jump and bite you.
22
An Example of A Simple Bridge
A device with two ports on it that forwards data traffic
received on the first port to the second and vice versa.
23
Some Assumptions
Whenever a character is received on one of
the communication links, it causes an
interrupt.
The Interrupt must be serviced reasonably
quickly.
Interrupt
24
Some Assumptions
The microprocessor must write characters to
the I/O hardware one at a time.
The I/O transmitter hardware on that
communication link will be busy, while it
sends the character.
After transmitting a character, I/O transmitter
will interrupt microprocessor to indicate that it
is ready for the next character.
25
Some Assumptions
We have routines that will
read characters from queues,
write characters to queues, and
test whether a queue is empty or not
These routines can be called from ISRs, as
well as, from the task code.
They deal correctly with the shared-data
problems.
Encrypt / decrypt one character at a time
26
Possible Code
Data structures
27
Possible Code
Interrupt service routines
Interrupts
upon receiving
characters
Interrupts
upon sending
characters
28
Possible Code
The main loop
29
Possible Code
encrypt() and decrypt()
30
Bridge code
Interrupt routines:
read characters from hardware
put them into queues: qDataFromLink[AB]
Main routine:
reads data from queues: qDataFromLink[AB]
encrypts and decrypts data
write data to queues: qDataToLink[AB]
I/O Hardware:
2 vars to keep track: fLink[AB]ReadyToSend
31
Bridge code
Shared-Data Problem:
disable / enable interrupts
Response Time:
Characters received from hardware by
interrupt routines, thus HIGHER priority
moving characters among queues, encrypting,
decrypting, sending them out, etc. are of
LOWER priority
Burst of characters will not overrun system
32
Cordless Bar-Code Scanner
Get data from laser reading bar codes
Send data out on the radio
Only real response requirements
Service hardware quickly enough
Thus, round-robin-with-interrupts is sufficient
33
Characteristics of RR-with-Interrupts
Shortcomings:
Not as simple as RR
All task code executes at the same priority
Possible Solutions:
Move task code for C into interrupt routine
ISR exec time will increase by 200 ms
Lower priority devices will have to wait
Change sequence: A, C, B, C, D, E, C, …
Response time for C improves
Response times for other devices may be not
acceptable
Tuning Fragile
35
Characteristics of RR-with-Interrupts
36
Examples of Systems for which
RR-with-Interrupts does not work well
Laser printer
Calculating locations for black dots is very
time consuming
Underground tank-monitoring system
Calculating gasoline level in tank is very time
consuming
Processor hog Task code gets stuck
37
Function Queue Scheduling
Architecture
In this architecture, the interrupt service
routines add function pointers to a queue of
function pointers.
foo()
*foo() {
38
Function-Queue Scheduling
Interrupt routines:
add function pointers to a queue
Main routine:
reads pointers from queue
calls the functions
Main need not call functions in the order of
occurrence
A priority scheme can be used for ordering
the function pointers
39
The Framework of FQS
Three parts
40
Worst-case Execution Time
Worst wait for highest-priority task code
function = length of longest task code function
Better than RR-with-Interrupts
Trade-off
Response for lower-priority task code
functions may get worse
Problem
Starvation: lower-priority task code may never
get executed!
41
Real-Time Operating System
Interrupt routines
take care of most urgent operations
“signal” that there is work for task code to do
Differences with other architectures:
Signaling between interrupt routines and task
code is handled by RTOS
no need of shared variables
No main loop deciding what to do next, RTOS
decides the scheduling
Preemption by RTOS scheduler
RTOS can suspend on task code subroutine to
run another task
42
A Paradigm
The sample code
43
Worst case execution
Suppose Task1 has higher priority
Suppose Task2 is running
Interrupt occurs and ISR vHandleDeviceA
sets signal X
Task2 is suspended
Preemption
Task1 is started
Worst case execution time for the highest
priority task code subroutine = 0 (+ ISR time)
44
Advantages / Disadvantages of RTOS
46
Selecting an Architecture
Select the simplest architecture that will meet
your response requirements
If your response constraints requires an
RTOS, then buy one and use it because there
are also several debugging tools for it
You can create hybrids of the architectures.
RTOS / RR
main task code can poll slow hardware devices
that do not need fast response
Use interrupts for faster hardware
47
Characteristics of Architectures
Round-robin
Round-robin with
Interrupts
Function-queue
scheduling
Real-time
operating system
48