You are on page 1of 31

Timing requiriments

ELT048 – EOS
Review
Review
• Processes
 Rescheduling
• Kernel

U
UNN II F
FEE II
microkernel(3);

//return code
#define SUCCESS 0
#define FAIL 1
#define REPEAT 2

//function pointer declaration


typedef char(*ptrFunc)(void);

//process struct
typedef struct {
ptrFunc func;
} process;

Process * pool[POOL_SIZE];
microkernel(3);

char kernelInit(void){
start = 0;
end = 0;
return SUCCESS;
}
char kernelAddProc(process * newProc){
//checking for free space
if ( ((end+1)%POOL_SIZE) != start){
pool[end] = newProc;
end = (end+1)%POOL_SIZE;
return SUCCESS;
}
return FAIL;
}
microkernel(3);

void kernelLoop(void){
for(;;){
//Do we have any process to execute?
if (start != end){
//check if there is need to reschedule
if (pool[start]->func() == REPEAT){
kernelAddProc(pool[start]);
}
//prepare to get the next process;
start = (start+1)%POOL_SIZE;
}
}
}
microkernel(3);

• Presenting the new processes


char tst1(void){
BitFlp(PORTD,0);
return REPEAT;
}
char tst2(void){
BitFlp(PORTD,1);
return SUCCESS;
}
char tst3(void){
BitFlp(PORTD,2);
return REPEAT;
U }
UNN II F
FEE II
microkernel(3);

void main(void){
//declaring the processes
process p1 = {tst1};
process p2 = {tst2};
process p3 = {tst3};
kernelInit();
//Test if the process were added
if (kernelAddProc(&p1) == FAIL){
for(;;);}
if (kernelAddProc(&p2) == FAIL){
for(;;);}
if (kernelAddProc(&p3) == FAIL){
for(;;);}
kernelLoop();
}
Exercise

• Create kernel.c, kernel.h.


• Adapt the source code to run on the board.
• Test processes reeschedule.
• Create two processes to print.
• Create one process to print and reeschedule.
• Create one process to print how many times it has
being runned.

U
UNN II F
FEE II
Hands on!
Real time
Timing requirements
• In most embedded systems it is necessary to ensure
that some functions are performed at a certain
frequency. Some systems may even fail if these
requirements are not met.

U
UNN II F
FEE II
Timing requirements
• Real time
 Speed
 Instantaneity

U
UNN II F
FEE II
Real time
• The ability of a system
to guarantee the
periodicity of a task

business2community.com
U
UNN II F
FEE II
Real time
• Determinism
• Periodicity
• Time Service

U
UNN II F
FEE II
Real time
• Hard real time • Soft real time
 A failure / delay in  An eventual failure /
meeting the temporal delay in meeting the
requirements will temporal requirements
CAUSE problems does not cause problems
 Power-electronic driver  Delay in receiving data
(zero-crossing) packets for video
transmission

U
UNN II F
FEE II
Timing requirements
• To implement a system that works with time
requirements:
1. There must be a clock that works with a precise
frequency.
2. The kernel must be informed of the frequency, or
period, of execution of each process.
3. The sum of the times of each process must "fit" in the
available time of the processor.

U
UNN II F
FEE II
Timing requirements
• 1st condition:
 A timer that can generate an interrupt.
• 2nd condition:
 Add the information in the process structure
• 3rd condition:
 Test, test and test.
 In case of failure:
• Faster Chip
• Optimization

U
UNN II F
FEE II
Scheduling
• The use of a finite timer to measure time can cause
overflow
• Example: scheduling 2 processes for 10 and 50
seconds (1 bit = 1ms)

U
UNN II F
FEE II
Scheduling
• What if the two processes are scheduled for the
same time?

U
UNN II F
FEE II
Scheduling
• Assuming the first process was executed:
 From the time diagram below the process P2 is delayed
10(s) or has been scheduled to happen 55(s) from now?

U
UNN II F
FEE II
Scheduling
• Solution:
 Use a decreasing timed counter for each process instead
of a scheduling value.
• Problem:
 Each of the counters must be decremented in the
interrupt routine.
 Is that really a problem for your application?
 Collateral advantages: ability to verify "backward
processes"

U
UNN II F
FEE II
Kernel with timing requirements
• The first modification is in the process definition.
• You should add:
 One counter for each process
 A variable to store the execution period

//process struct
typedef struct {
ptrFunc function;
int period;
int start;
} process;

U
UNN II F
FEE II
Kernel with timing requirements
• An interrupt function must be created to decrement
each of the process counters
• The intrinsic details of the process of creating and
handling interrupts are beyond the scope of this
lesson.

U
UNN II F
FEE II
Interrupt function

void ISR (void) __interrupt 1 {


//TIMER0: Overflow
if (BitTst(INTCON,2)){
// 65535 – 2*10.000 = 10mS
TMR0H = (45535 >> 8);
TMR0L = (45535 & 0x00FF );

KernelClock();

//limpa a flag
BitClr(INTCON,2);
}
}
Interrupt function

void initTimer (void) {


//Inicializa Interrupção do Timer 0
BitSet(INTCON,5);
BitSet(INTCON,6);
BitSet(INTCON,7);
BitSet(RCON,7);
//Inicializa Timer 0
T0CON = 0b00001000;
BitSet(T0CON,7);
}
Interrupt function
//add at kernel.c
#define MIN_INT -30000
void KernelClock(void){
unsigned char i;
i = start;
while(i!=end){
if((pool[i]->start)>(MIN_INT)){
pool[i]->start--;
}
i = (i+1)%POOLSIZE;
}
}

//add at kernel.h
void KernelClock(void);
Kernel with timing requirements
• The process addition function must initialize
variables correctly

char AddProc(process* newProc){


//checking for free space
if ( ((end+1)%POOLSIZE) != start){
pool[end] = newProc;
//increment start timer with period
pool[end]->start += newProc->period;
end = (end+1)%POOLSIZE;
return SUCESS;
}
return FAIL;
U}N II FF E
UN E II
Kernel with timing requirements
• The kernel execution function has the biggest
impact.
 The scheduling process changes from a FIFO model
(previous class) to a scheduler based on the least time to
start.
 The process that is closest to having its timer zeroed is
selected.
 If there are overdue processes, the most delayed process
will be chosen.

U
UNN II F
FEE II
if (start != end){
//Find the process with the lowest timer
next = start;
j = (start+1) % POOLSIZE;
while(j!=end){
if (pool[j]->start < pool[next]->start){
next = j;
}
j = (j+1)%POOLSIZE;
}
//Exchanging processes positions
tempProc = pool[next];
pool[next] = pool[start];
pool[start] = tempProc;
//waiting for the process to be ready
while(pool[start]->start > 0){
//Great place for energy saving
}
if (pool[start]->func() == REPEAT ){
kernelAddProc(pool[start]);
}
start = (start+1)%POOLSIZE;
}
Exercise
• Building a clock
 One process updates the seven segments display (ssd)
 One process makes the counting and change the digits
value
• Update the last class kernel
 Interrupt
 KernelAddProc()
 KernelClock()
 KernelLoop()

U
UNN II F
FEE II

You might also like