Professional Documents
Culture Documents
Embedded Systems Programming: Ver Onica Gaspes
Embedded Systems Programming: Ver Onica Gaspes
Embedded Systems Programming: Ver Onica Gaspes
Verónica Gaspes
www2.hh.se/staff/vero
from lecture 1
The programming techniques we learn about in this course address
the fact that embedded computer systems are interfaced to
physical equipment that they monitor and control.
Summary of the course Preparing for the exam Infamous real-time systems
But also . . .
from lecture 1
In embedded systems it is often the case that the programs we
write have to directly access the hardware that is conected to the
processor.
But also . . .
from lecture 1
In embedded systems it is often the case that the programs we
write have to directly access the hardware that is conected to the
processor.
Focus
Concurrency
How to write programs using parallel threads of execution
Reactivity
How to write programs whose purpose is to react to events
(ultimately in the form of interrupts)
Real-time
How to write programs whose correctness depends on their
real-time behaviour
Summary of the course Preparing for the exam Infamous real-time systems
Question
The segments of an LCD are controlled via 8-bit registers
LCDDRx. The lower bits of LCDDRx are used to control 4 of the
segments used to build a character in one position, while the
higher bits are used to control the same segments but at another
position. If you know that variable nibble contains the bit
configuration needed for turning on some segments, show what
you have to assign to LCDDRx in order to turn on these segments
in each of the two positions.
Answer
LCDDRx = LCDDRx & 0xF0 | nibble -- lower part
LCDDRx = LCDDRx & 0x0F | (nibble<<4) -- higher part
Summary of the course Preparing for the exam Infamous real-time systems
Question
The segments of an LCD are controlled via 8-bit registers
LCDDRx. The lower bits of LCDDRx are used to control 4 of the
segments used to build a character in one position, while the
higher bits are used to control the same segments but at another
position. If you know that variable nibble contains the bit
configuration needed for turning on some segments, show what
you have to assign to LCDDRx in order to turn on these segments
in each of the two positions.
Answer
LCDDRx = LCDDRx & 0xF0 | nibble -- lower part
LCDDRx = LCDDRx & 0x0F | (nibble<<4) -- higher part
Summary of the course Preparing for the exam Infamous real-time systems
Question
The busy waiting functions are
void waitForA(){
while(!PORTA_READY);
}
void waitForB(){
while(!PORTB_READY);
}
Summary of the course Preparing for the exam Infamous real-time systems
Question
The busy waiting functions are
void waitForA(){
while(!PORTA_READY);
}
void waitForB(){
while(!PORTB_READY);
}
Summary of the course Preparing for the exam Infamous real-time systems
Answer
while(1){
sleepUntilNextTimerInterrupt();
if(PORTA READY){// record it is port A}
if(PORTB READY){// record it is port B}
// do what has to be done.
}
Answer
The busy-waiting functions had to be destroyed to be able to
detect either of them (a sequence of two busy waiting functions
imposes an order and might shadow one of the events). The timer
is used to avoid using all CPU cycles while waiting. Its period
should be set to trade power againstresponsiveness.
Summary of the course Preparing for the exam Infamous real-time systems
Answer
while(1){
sleepUntilNextTimerInterrupt();
if(PORTA READY){// record it is port A}
if(PORTB READY){// record it is port B}
// do what has to be done.
}
Answer
The busy-waiting functions had to be destroyed to be able to
detect either of them (a sequence of two busy waiting functions
imposes an order and might shadow one of the events). The timer
is used to avoid using all CPU cycles while waiting. Its period
should be set to trade power againstresponsiveness.
Summary of the course Preparing for the exam Infamous real-time systems
Answer
global variable int x;
fragment A: fragment B:
x++; x++;
**************************************************
global variable int x;
global variable mutex m;
lock(&m) lock(&m)
x++; x++;
unlock(&m) unlock(&m)
Summary of the course Preparing for the exam Infamous real-time systems
Answer
global variable int x;
fragment A: fragment B:
x++; x++;
**************************************************
global variable int x;
global variable mutex m;
lock(&m) lock(&m)
x++; x++;
unlock(&m) unlock(&m)
Summary of the course Preparing for the exam Infamous real-time systems
Question
Define a (TinyTimber) class that turns a port into a reactive object
(might be needed if there are several threads trying to read/write
to the same port in one program: it is a critical section!).
Answer
typedef struct{
Object super;
unsigned char *port;
}Proxy;
Question
Define a (TinyTimber) class that turns a port into a reactive object
(might be needed if there are several threads trying to read/write
to the same port in one program: it is a critical section!).
Answer
typedef struct{
Object super;
unsigned char *port;
}Proxy;
Question
Define a (TinyTimber) class that turns a port into a reactive object
(might be needed if there are several threads trying to read/write
to the same port in one program: it is a critical section!).
Answer
typedef struct{
Object super;
unsigned char *port;
}Proxy;
Answer
int set(Proxy *self, unsigned char bits){
*self->port = *self->port|bits;
}
Answer
typedef struct{
Object super;
int T;
int D;
}Wave;
#define initWave(t,d) = {initObject,t,d}
Summary of the course Preparing for the exam Infamous real-time systems
Answer
typedef struct{
Object super;
int T;
int D;
}Wave;
#define initWave(t,d) = {initObject,t,d}
Summary of the course Preparing for the exam Infamous real-time systems
Question
Explain RM and EDF priority scheduling.
Answer
Rate monotonic assigns priorities according to the period: the
shorter the period, the higher the priority. With the typical
inverted priority scale, the period might be the priority value. Only
the relative priorities are relevant. It is a static method.
EDF assigns priorities according to time to (absolute) deadline: the
shorter the time to deadline, the higher the priority. Different
activations of the same task might get different pririties. It is a
dynamic method.
Summary of the course Preparing for the exam Infamous real-time systems
Question
Explain RM and EDF priority scheduling.
Answer
Rate monotonic assigns priorities according to the period: the
shorter the period, the higher the priority. With the typical
inverted priority scale, the period might be the priority value. Only
the relative priorities are relevant. It is a static method.
EDF assigns priorities according to time to (absolute) deadline: the
shorter the time to deadline, the higher the priority. Different
activations of the same task might get different pririties. It is a
dynamic method.
Summary of the course Preparing for the exam Infamous real-time systems
Question
Show how to implement a bounded buffer in Ada using tasks with
entries.
Answer
task type Bounded_Buffer is
entry Deposit(N : in Integer);
entry Fetch(N : out Integer);
end Bounded_Buffer;
Summary of the course Preparing for the exam Infamous real-time systems
Question
Show how to implement a bounded buffer in Ada using tasks with
entries.
Answer
task type Bounded_Buffer is
entry Deposit(N : in Integer);
entry Fetch(N : out Integer);
end Bounded_Buffer;
Summary of the course Preparing for the exam Infamous real-time systems
Ada
Answer
task body Bounded_Buffer is
SIZE : Positive := 10;Count : Natural := 0;
Front : Natural := 0; Rear : Natural := 0;
Buffer : array (0..SIZE-1) of Integer;
begin
loop
select
when Count < SIZE =>
accept Deposit(N : in Integer) do
Buffer(Rear):=N; Rear := (Rear + 1) mod SIZE;Count := Count + 1;
end Deposit;
or
when Count > 0 =>
accept Fetch(N: out Integer) do
N := Buffer(Front); Front := (Front + 1) mod SIZE;Count := Count-1;
end Fetch;
end select;
end loop;
end Bounded_Buffer;
Summary of the course Preparing for the exam Infamous real-time systems
Therac 25
http://en.wikipedia.org/wiki/Therac-25
Summary of the course Preparing for the exam Infamous real-time systems
Therac 25
Background
The Therac-25 was a computer controlled therapeutic
radiation machine for the treatment of tumors
Deployed in the mid 80’s, it was a modernized successor to a
highly successful, but slow and bulky machine: the Therac-20
Massive radiation overdozes generated by the machine
resulted in deaths or severe injuries for at least six people in
the USA and Canada 1985-1987
The machine was redesigned in 1987 as the result of multiple
federal investigations
Summary of the course Preparing for the exam Infamous real-time systems
Therac 25
Background
The Therac-25 was a computer controlled therapeutic
radiation machine for the treatment of tumors
Deployed in the mid 80’s, it was a modernized successor to a
highly successful, but slow and bulky machine: the Therac-20
Massive radiation overdozes generated by the machine
resulted in deaths or severe injuries for at least six people in
the USA and Canada 1985-1987
The machine was redesigned in 1987 as the result of multiple
federal investigations
Summary of the course Preparing for the exam Infamous real-time systems
Therac 25
Background
The Therac-25 was a computer controlled therapeutic
radiation machine for the treatment of tumors
Deployed in the mid 80’s, it was a modernized successor to a
highly successful, but slow and bulky machine: the Therac-20
Massive radiation overdozes generated by the machine
resulted in deaths or severe injuries for at least six people in
the USA and Canada 1985-1987
The machine was redesigned in 1987 as the result of multiple
federal investigations
Summary of the course Preparing for the exam Infamous real-time systems
Therac 25
Background
The Therac-25 was a computer controlled therapeutic
radiation machine for the treatment of tumors
Deployed in the mid 80’s, it was a modernized successor to a
highly successful, but slow and bulky machine: the Therac-20
Massive radiation overdozes generated by the machine
resulted in deaths or severe injuries for at least six people in
the USA and Canada 1985-1987
The machine was redesigned in 1987 as the result of multiple
federal investigations
Summary of the course Preparing for the exam Infamous real-time systems
Therac 25
Background
The Therac-25 was a computer controlled therapeutic
radiation machine for the treatment of tumors
Deployed in the mid 80’s, it was a modernized successor to a
highly successful, but slow and bulky machine: the Therac-20
Massive radiation overdozes generated by the machine
resulted in deaths or severe injuries for at least six people in
the USA and Canada 1985-1987
The machine was redesigned in 1987 as the result of multiple
federal investigations
Summary of the course Preparing for the exam Infamous real-time systems
Therac-25 functionality
Therac-25 computer
Therac-25 computer
Therac-25 computer
Therac-25 computer
Therac-25 computer
Therac-25 computer
Therac-25 computer
During that time window, the treatment phase task is ignoring the
keyboard entry flag because it is delaying in a busy-wait loop while
bending magnets are being set up.
Thus the new mode is never copied over to the variable read by
the gun emission control task
During that time window, the treatment phase task is ignoring the
keyboard entry flag because it is delaying in a busy-wait loop while
bending magnets are being set up.
Thus the new mode is never copied over to the variable read by
the gun emission control task
During that time window, the treatment phase task is ignoring the
keyboard entry flag because it is delaying in a busy-wait loop while
bending magnets are being set up.
Thus the new mode is never copied over to the variable read by
the gun emission control task
During that time window, the treatment phase task is ignoring the
keyboard entry flag because it is delaying in a busy-wait loop while
bending magnets are being set up.
Thus the new mode is never copied over to the variable read by
the gun emission control task
During that time window, the treatment phase task is ignoring the
keyboard entry flag because it is delaying in a busy-wait loop while
bending magnets are being set up.
Thus the new mode is never copied over to the variable read by
the gun emission control task
Additional facts
Few people seem to have had any clear idea of how the software
really worked
Safety analyses for the machine never took timing errors into
account
It turned out that the Therac-20 also contained the same bugs,
but there hardware sensors and fuses were used as an extra safety
net against high radiation
Basic lesson
Never synchronize using flags!
Summary of the course Preparing for the exam Infamous real-time systems
Additional facts
Few people seem to have had any clear idea of how the software
really worked
Safety analyses for the machine never took timing errors into
account
It turned out that the Therac-20 also contained the same bugs,
but there hardware sensors and fuses were used as an extra safety
net against high radiation
Basic lesson
Never synchronize using flags!
Summary of the course Preparing for the exam Infamous real-time systems
Additional facts
Few people seem to have had any clear idea of how the software
really worked
Safety analyses for the machine never took timing errors into
account
It turned out that the Therac-20 also contained the same bugs,
but there hardware sensors and fuses were used as an extra safety
net against high radiation
Basic lesson
Never synchronize using flags!
Summary of the course Preparing for the exam Infamous real-time systems
Additional facts
Few people seem to have had any clear idea of how the software
really worked
Safety analyses for the machine never took timing errors into
account
It turned out that the Therac-20 also contained the same bugs,
but there hardware sensors and fuses were used as an extra safety
net against high radiation
Basic lesson
Never synchronize using flags!
Summary of the course Preparing for the exam Infamous real-time systems
Additional facts
Few people seem to have had any clear idea of how the software
really worked
Safety analyses for the machine never took timing errors into
account
It turned out that the Therac-20 also contained the same bugs,
but there hardware sensors and fuses were used as an extra safety
net against high radiation
Basic lesson
Never synchronize using flags!
Summary of the course Preparing for the exam Infamous real-time systems
Additional facts
Few people seem to have had any clear idea of how the software
really worked
Safety analyses for the machine never took timing errors into
account
It turned out that the Therac-20 also contained the same bugs,
but there hardware sensors and fuses were used as an extra safety
net against high radiation
Basic lesson
Never synchronize using flags!