Embedded Systems Programming: Ver Onica Gaspes
Embedded Systems Programming: Ver Onica Gaspes
Verónica Gaspes
www2.hh.se/staff/vero
Distance Servo
Controller
data signals
Object Sonar Output
Input
Control
Params
Radio
Decoder
packets
Input
The need for threads Mutual exclusion
read write
Program
read
The program
int sonar_read(){
while(SONAR_STATUS & READY == 0);
We can define functions.
return SONAR_DATA;
that create an illusion to
}
the rest of the program!
int sonar_read(){
while(SONAR_STATUS & READY == 0);
We can define functions.
return SONAR_DATA;
that create an illusion to
}
the rest of the program!
int sonar_read(){
while(SONAR_STATUS & READY == 0);
We can define functions.
return SONAR_DATA;
that create an illusion to
}
the rest of the program!
int sonar_read(){
while(SONAR_STATUS & READY == 0);
We can define functions.
return SONAR_DATA;
that create an illusion to
}
the rest of the program!
Control
void control(int dist, int *sig, struct Params *p);
Decode
void decode(struct Packet *pkt, struct Params *p)
Control
void control(int dist, int *sig, struct Params *p);
Decode
void decode(struct Packet *pkt, struct Params *p)
Control
void control(int dist, int *sig, struct Params *p);
Decode
void decode(struct Packet *pkt, struct Params *p)
main(){
struct Params params;
struct Packet packet;
int dist, signal;
while(1){
dist = sonar_read();
control(dist, &signal, ¶ms);
servo_write(signal);
radio_read(&packet);
decode(&packet,¶ms);
}
}
The need for threads Mutual exclusion
Problems?
radio
packets
sonar
echoes
We do not know what port will have new data next! The sonar
and the radio generate events that are unrelated to each other!
Our program will ignore all events of one kind that happen while
busy waiting for the other event!
The need for threads Mutual exclusion
Problems?
radio
packets
sonar
echoes
We do not know what port will have new data next! The sonar
and the radio generate events that are unrelated to each other!
Our program will ignore all events of one kind that happen while
busy waiting for the other event!
The need for threads Mutual exclusion
Problems?
radio
packets
sonar
echoes
We do not know what port will have new data next! The sonar
and the radio generate events that are unrelated to each other!
Our program will ignore all events of one kind that happen while
busy waiting for the other event!
The need for threads Mutual exclusion
The illusion that input is like reading from memory while blocking
waiting for data requires that we choose the source of input before
blocking!
The need for threads Mutual exclusion
The illusion that input is like reading from memory while blocking
waiting for data requires that we choose the source of input before
blocking!
The need for threads Mutual exclusion
The illusion that input is like reading from memory while blocking
waiting for data requires that we choose the source of input before
blocking!
The need for threads Mutual exclusion
The illusion that input is like reading from memory while blocking
waiting for data requires that we choose the source of input before
blocking!
The need for threads Mutual exclusion
The illusion that input is like reading from memory while blocking
waiting for data requires that we choose the source of input before
blocking!
The need for threads Mutual exclusion
while(1){
if(SONAR_STATUS & READY){
dist = SONAR_DATA;
control(dist,&signal,¶ms);
servo_write(signal);
} Destroy the functions for
if(RADIO_STATUS & READY){ reading and have only
packet->v1 = RADIO_DATA1; one busy waiting loop!
...;
packet->v2 = RADIO_DATAn;
decode(&packet,¶ms);
}
}
The need for threads Mutual exclusion
Problems?
radio
packets
sonar
echoes
Concurrent execution
Imagine . . .
. . . that we could interrupt execution of packet decoding when a
sonar echo arrives so that the control algorithm can be ran. Then
decoding could resume! The two tasks fragments are interleaved.
The need for threads Mutual exclusion
Concurrent execution
Imagine . . .
. . . that we could interrupt execution of packet decoding when a
sonar echo arrives so that the control algorithm can be ran. Then
decoding could resume! The two tasks fragments are interleaved.
The need for threads Mutual exclusion
Interleaving by hand
Interleaving by hand
Interleaving by hand
Interleaving by hand
Interleaving by hand
Interleaving by hand
Interleaving by hand
Interleaving by hand
Automatic interleaving?
Automatic interleaving?
Automatic interleaving?
Automatic interleaving?
Two CPUs
read write
CPU1
Controller
RAM parameters
CPU2 read
Controller
void controller_main(){
int dist, signal; void decoder_main(){
while(1){ struct Packet packet;
dist = sonar_read(); while(1){
control(dist, radio_read(&packet);
&signal, decode(&packet,¶ms);
¶ms); }
servo_write(signal); }
}
}
We need some way of making one program of this! We will deal
with it next lecture!
The need for threads Mutual exclusion
Concurrent Programming
Concurrent Programming
Concurrent Programming
A programming language?
As in Java or Ada. Programs are well organized and are
independent of the OS.
This course
For pedagogical purposes we choose to work with C and a small
kernel.
The need for threads Mutual exclusion
A programming language?
As in Java or Ada. Programs are well organized and are
independent of the OS.
This course
For pedagogical purposes we choose to work with C and a small
kernel.
The need for threads Mutual exclusion
A programming language?
As in Java or Ada. Programs are well organized and are
independent of the OS.
This course
For pedagogical purposes we choose to work with C and a small
kernel.
The need for threads Mutual exclusion
A programming language?
As in Java or Ada. Programs are well organized and are
independent of the OS.
This course
For pedagogical purposes we choose to work with C and a small
kernel.
The need for threads Mutual exclusion
void controller_main(){
int dist, signal;
void decoder_main(){
while(1){
struct Packet packet;
dist = sonar_read();
while(1){
control(dist,
radio_read(&packet);
&signal,
decode(&packet,¶ms);
¶ms);
}
servo_write(signal);
}
}
}
main(){
spawn(decoder_main);
controller_main();
}
The need for threads Mutual exclusion
What will happen if the params struct is read (by the controller)
at the same time it is written (by the decoder)?
What will happen if the params struct is read (by the controller)
at the same time it is written (by the decoder)?
What will happen if the params struct is read (by the controller)
at the same time it is written (by the decoder)?
int account = 0;
account = account + 500; account = account + 10000;
int account = 0;
account = account + 500; account = account + 10000;
load account,r1
add 500,r1
store r1, account
load account, r2
add 10000, r2
store r2, account
load account, r2
add 10000, r2
store r2, account
load account,r1
add 500,r1
store r1, account
load account,r1
load account, r2
add 10000, r2
add 500,r1
store r2, account
store r1, account
Possible interleaving
if(shopper == NONE)
if(shopper==NONE)
shopper = HUSBAND
shopper = WIFE
The need for threads Mutual exclusion
Possible interleaving
if(shopper == NONE)
if(shopper==NONE)
shopper = HUSBAND
shopper = WIFE
The need for threads Mutual exclusion
Exchanging parameters
struct Params p;
while(1){ while(1){
... local_minD = p.minDistance;
p.minDistance = e1; local_maxS = p.maxSpeed;
p.maxSpeed = e2; ...
} }
Possible interleaving
p.minDistance = 1;
p.maxSpeed = 1;
local_minD = 1;
p.minDistance = 200;
p.maxSpeed = 150;
local_maxS = 150
The need for threads Mutual exclusion
Exchanging parameters
struct Params p;
while(1){ while(1){
... local_minD = p.minDistance;
p.minDistance = e1; local_maxS = p.maxSpeed;
p.maxSpeed = e2; ...
} }
Possible interleaving
p.minDistance = 1;
p.maxSpeed = 1;
local_minD = 1;
p.minDistance = 200;
p.maxSpeed = 150;
local_maxS = 150
The need for threads Mutual exclusion
Mutual exclusion
Exchanging parameters
struct Params p;
mutex m;
while(1){ while(1){
... lock (&m)
lock (&m); local_minD = p.minDistance;
p.minDistance = e1; local_maxS = p.maxSpeed;
p.maxSpeed = e2; unlock (&m)
unlock (&m); ...
}
}
The need for threads Mutual exclusion
Comming next
Laboration 1
You will experiment with low level programming, busy waiting for
input and trying to put together 3 tasks by interleaving them by
hand.
Comming next
Laboration 1
You will experiment with low level programming, busy waiting for
input and trying to put together 3 tasks by interleaving them by
hand.
Comming next
Laboration 1
You will experiment with low level programming, busy waiting for
input and trying to put together 3 tasks by interleaving them by
hand.