Embedded Systems Programming: Ver Onica Gaspes
Embedded Systems Programming: Ver Onica Gaspes
Verónica Gaspes
www2.hh.se/staff/vero
Real Time?
Real Time?
Real Time?
Real Time?
Real Time?
Multitude of alternatives
Units? Seconds? Milliseconds? Cpu cycles?
Since when? Program start? System boot? Jan 1 1970?
Real time? Time that stops when other threads are running?
Time that stops when CPU sleeps? Time that cannot be set
and always increases?
Time Sampling time Real-time events
Multitude of alternatives
Units? Seconds? Milliseconds? Cpu cycles?
Since when? Program start? System boot? Jan 1 1970?
Real time? Time that stops when other threads are running?
Time that stops when CPU sleeps? Time that cannot be set
and always increases?
Time Sampling time Real-time events
Multitude of alternatives
Units? Seconds? Milliseconds? Cpu cycles?
Since when? Program start? System boot? Jan 1 1970?
Real time? Time that stops when other threads are running?
Time that stops when CPU sleeps? Time that cannot be set
and always increases?
Time Sampling time Real-time events
Multitude of alternatives
Units? Seconds? Milliseconds? Cpu cycles?
Since when? Program start? System boot? Jan 1 1970?
Real time? Time that stops when other threads are running?
Time that stops when CPU sleeps? Time that cannot be set
and always increases?
Time Sampling time Real-time events
Multitude of alternatives
Units? Seconds? Milliseconds? Cpu cycles?
Since when? Program start? System boot? Jan 1 1970?
Real time? Time that stops when other threads are running?
Time that stops when CPU sleeps? Time that cannot be set
and always increases?
Time Sampling time Real-time events
Units
CPU clock (8Mhz) divided by a programmable prescaling value (1,
8, 64, 256, 1024).
Since when
System reset, timer reset or timer overflow (whichever was last).
Real time
Shows real time although it can be stopped.
Units
CPU clock (8Mhz) divided by a programmable prescaling value (1,
8, 64, 256, 1024).
Since when
System reset, timer reset or timer overflow (whichever was last).
Real time
Shows real time although it can be stopped.
Units
CPU clock (8Mhz) divided by a programmable prescaling value (1,
8, 64, 256, 1024).
Since when
System reset, timer reset or timer overflow (whichever was last).
Real time
Shows real time although it can be stopped.
Units
CPU clock (8Mhz) divided by a programmable prescaling value (1,
8, 64, 256, 1024).
Since when
System reset, timer reset or timer overflow (whichever was last).
Real time
Shows real time although it can be stopped.
Units
CPU clock (8Mhz) divided by a programmable prescaling value (1,
8, 64, 256, 1024).
Since when
System reset, timer reset or timer overflow (whichever was last).
Real time
Shows real time although it can be stopped.
Timestamps
Example
The clock showed 11:25 when the teacher left.
Timestamps
Example
The clock showed 11:25 when the teacher left.
Timestamps
Example
The clock showed 11:25 when the teacher left.
Time spans
Time spans
Time spans
Time spans
Time spans
Time spans
In a scheduled system
What looks like . . .
Subsequent statements
In a scheduled system
What looks like . . .
Subsequent statements
Time-stamping events
Idea!
Read the clock inside the interrupt handler that detects the
asociated event
Other interrupts are disabled while the CPU runs an interrupt
handler, hence no scheduling of threads might interfere!
There is a tight upper bound on the timestamp error, which
can be calculated from CPU data and speed!
Example
One could use an arg of an interrupt driven method for passing a
timestamp!
Time Sampling time Real-time events
Time-stamping events
Idea!
Read the clock inside the interrupt handler that detects the
asociated event
Other interrupts are disabled while the CPU runs an interrupt
handler, hence no scheduling of threads might interfere!
There is a tight upper bound on the timestamp error, which
can be calculated from CPU data and speed!
Example
One could use an arg of an interrupt driven method for passing a
timestamp!
Time Sampling time Real-time events
Time-stamping events
Idea!
Read the clock inside the interrupt handler that detects the
asociated event
Other interrupts are disabled while the CPU runs an interrupt
handler, hence no scheduling of threads might interfere!
There is a tight upper bound on the timestamp error, which
can be calculated from CPU data and speed!
Example
One could use an arg of an interrupt driven method for passing a
timestamp!
Time Sampling time Real-time events
Time-stamping events
Idea!
Read the clock inside the interrupt handler that detects the
asociated event
Other interrupts are disabled while the CPU runs an interrupt
handler, hence no scheduling of threads might interfere!
There is a tight upper bound on the timestamp error, which
can be calculated from CPU data and speed!
Example
One could use an arg of an interrupt driven method for passing a
timestamp!
Time Sampling time Real-time events
Time-stamping events
Idea!
Read the clock inside the interrupt handler that detects the
asociated event
Other interrupts are disabled while the CPU runs an interrupt
handler, hence no scheduling of threads might interfere!
There is a tight upper bound on the timestamp error, which
can be calculated from CPU data and speed!
Example
One could use an arg of an interrupt driven method for passing a
timestamp!
Time Sampling time Real-time events
Example
typedef struct{
Detector Object super;
int previous;
} Speedo;
...
Speedo speedo:
Other client;
Tap INTERRUPT(SIG_XX,
ASYNC(&speedo,detect,TCNT1));
Time Sampling time Real-time events
Example
typedef struct{
Detector Object super;
int previous;
} Speedo;
...
Speedo speedo:
Other client;
Tap INTERRUPT(SIG_XX,
ASYNC(&speedo,detect,TCNT1));
Time Sampling time Real-time events
Example
Example
Real-time events
Example
The wheel is an engine crankshaft
and we have to emit ignition
signals to each cylinder
Real-time events
Example
The wheel is an engine crankshaft
and we have to emit ignition
signals to each cylinder
int i;
for(i=0;i<N;i++); // wait
do_future_action();
Problems
1 Determine N by testing!
int i;
for(i=0;i<N;i++); // wait
do_future_action();
Problems
1 Determine N by testing!
int i;
for(i=0;i<N;i++); // wait
do_future_action();
Problems
1 Determine N by testing!
int i;
for(i=0;i<N;i++); // wait
do_future_action();
Problems
1 Determine N by testing!
int i;
for(i=0;i<N;i++); // wait
do_future_action();
Problems
1 Determine N by testing!
Problems
1 Determine N by calculation so platform dependency disapears.
Problems
1 Determine N by calculation so platform dependency disapears.
Problems
1 Determine N by calculation so platform dependency disapears.
Problems
1 Determine N by calculation so platform dependency disapears.
No platform dependency!
No wasted CPU cycles (at the expense of complex OS
internals)
Still a problem . . .
. . . common to all solutions . . .
Time Sampling time Real-time events
No platform dependency!
No wasted CPU cycles (at the expense of complex OS
internals)
Still a problem . . .
. . . common to all solutions . . .
Time Sampling time Real-time events
No platform dependency!
No wasted CPU cycles (at the expense of complex OS
internals)
Still a problem . . .
. . . common to all solutions . . .
Time Sampling time Real-time events
No platform dependency!
No wasted CPU cycles (at the expense of complex OS
internals)
Still a problem . . .
. . . common to all solutions . . .
Time Sampling time Real-time events
No platform dependency!
No wasted CPU cycles (at the expense of complex OS
internals)
Still a problem . . .
. . . common to all solutions . . .
Time Sampling time Real-time events
In a scheduled system
What looks like . . .
Event detected call do_future_action()
delay(N)
subsequent statements
delay(N)
In a scheduled system
What looks like . . .
Event detected call do_future_action()
delay(N)
subsequent statements
delay(N)
Relative delays
The problem is that these delay services are always specified using
relative time:
The constructed real-time event will occur at a time obtained
by adding the delay parameter N to now.
But now is not a very meaningful time reference in a
scheduled system, as it is not related to any externally
observable signals.
Relative delays
The problem is that these delay services are always specified using
relative time:
The constructed real-time event will occur at a time obtained
by adding the delay parameter N to now.
But now is not a very meaningful time reference in a
scheduled system, as it is not related to any externally
observable signals.
Relative delays
The problem is that these delay services are always specified using
relative time:
The constructed real-time event will occur at a time obtained
by adding the delay parameter N to now.
But now is not a very meaningful time reference in a
scheduled system, as it is not related to any externally
observable signals.
Relative delays
The problem is that these delay services are always specified using
relative time:
The constructed real-time event will occur at a time obtained
by adding the delay parameter N to now.
But now is not a very meaningful time reference in a
scheduled system, as it is not related to any externally
observable signals.
Relative delays
The problem is that these delay services are always specified using
relative time:
The constructed real-time event will occur at a time obtained
by adding the delay parameter N to now.
But now is not a very meaningful time reference in a
scheduled system, as it is not related to any externally
observable signals.
Example
Consider a task running a CPU-heavy function do work() every
100 millisecods. Using delay(), the naive implementation would
be:
while(1){
do_work();
delay(100);
}
Time Sampling time Real-time events
Example
Consider a task running a CPU-heavy function do work() every
100 millisecods. Using delay(), the naive implementation would
be:
while(1){
do_work();
delay(100);
}
Time Sampling time Real-time events
Accumulating drift
X X X X
100 100 100
With relative delays, each turn in the loop will take at least 100+X
milliseconds.
Accumulating drift
X X X X
100 100 100
With relative delays, each turn in the loop will take at least 100+X
milliseconds.
Accumulating drift
X X X X
100 100 100
With relative delays, each turn in the loop will take at least 100+X
milliseconds.
Accumulating drift
X X X
100 100 100
With other threads in the system, the already bad scenario gets
worse!
Accumulating drift
X X X
100 100 100
With other threads in the system, the already bad scenario gets
worse!
A stable reference
Baselines
We introduce the baseline of a message to mean the earliest time a
message is allowed to start.
Interrupt signal
Time Sampling time Real-time events
A stable reference
Baselines
We introduce the baseline of a message to mean the earliest time a
message is allowed to start.
Interrupt signal
Time Sampling time Real-time events
A stable reference
Baselines
We introduce the baseline of a message to mean the earliest time a
message is allowed to start.
Interrupt signal
Time Sampling time Real-time events
A stable reference
SYNC
Calling methods with SYNC doesn’t change the baseline (the call
inherits the baseline)
A
SYNC(B,meth,arg)
Original event
B
Time Sampling time Real-time events
A stable reference
ASYNC
By default ASYNC method calls will inherit the baseline
A
ASYNC(B,meth,arg)
Original event
Pseudo parallel execution
B
Time Sampling time Real-time events
A stable reference
A
AFTER(N,B,meth,arg)
Original event
N
new baseline
Periodic tasks
To create a cyclic reaction, simply call self with the same method
and a new baseline:
AFTER(SEC(2),self,meth,arg)
etc
2 seconds 2 seconds
Periodic tasks
To create a cyclic reaction, simply call self with the same method
and a new baseline:
AFTER(SEC(2),self,meth,arg)
etc
2 seconds 2 seconds
Implementing AFTER
Implementing AFTER
Implementing AFTER
Implementing AFTER
Implementing AFTER
Implementing AFTER
Implementing AFTER
Implementing AFTER