Professional Documents
Culture Documents
Scheduler
Scheduler
NHÚNG
Function Pointer
Function pointers are pointers point to a function instead of a
variable type.
The pointer can then be used to call the function that is being
pointed to
Function pointer is that the declaration name is preceded by a *
and then must be enclosed by parentheses
Ex void (*FuncPtr)(void);
void (*FuncPtr)(int);
int (*FuncPtr)(int);
int (*FuncPtr)(int, char *);
int * (*FuncPtr)(int, int, char, char *);
int * (*FuncPtr)(int, int, char, void (*FuncPtr2)(void));
1
Function pointer declaration
if(NewPtr != 0U)
{
Result = (*NewPtr)(intPtr);
}
2
State machine
3
State variable and function
4
State function definition
5
Real-time systems
6
DOS style
Windows style
7
iOS Style
Embedded Software
8
Simple loop model
The real-time performance of the system is
affected every time new code is added.
9
Round Robin with Interrupts
10
Disadvantages of round robin scheduler
Operating system
11
Multi-thread model
Most RTOSes used in embedded
applications employ a multi-thread
model.
A number of threads may be
running and they all share the
same address space
A context swap is primarily a
change from one set of CPU
register values to another
Potential hazard is the ability of
each thread to access memory
belonging to the others or to the
RTOS itself
Multi-process model
Each process has its own address space and cannot
access the memory associated with other processes
or the RTOS
This architecture is only possible with a processor that
supports an MMU
12
Scheduler
Tasks have full control of the CPU (interrupts aside) until the top
level function executes a return statement.
Simple, need for just a single stack and the portability of the code.
A task can “hog” the CPU, so careful program design is required
13
Round-robin scheduler
Queue
an array of function pointers (queue) is created that initially contains only NULL pointers
Once a task has been executed, its function pointer location in the queue is returned to a
NULL pointer
14
Cooperative
The system is first initialized and then a background timer is used to keep track of time,
Arm processors has the SysTick timer for this purpose.
The scheduler reads the system tick and then loops through each of the tasks defined
for the system and calculates whether it is time to execute the task or not
When more than one task needs to run, they are executed in a Round Robin fashion.
Cooperative
For task to run on a cooperative scheduler, they must
have predictable time (as short as possible), and give
up the CPU to other tasks when they finish.
Therefore, a loop to wait for an event is not
recommended.
Normally, a Systick occur after every 1ms or longer.
Worst case, must be considered
T1 T2 T2 T1 T2
S2 S3 S4 S5 S6 S7 S8
15
Led Red Task Example
Example:
For the previous example of controlling LEDs by SWs, we have
state machine 1 that check SW1 and control LED Red.
Because the SWs are pressed by hand, it can not be pressed and
released too fast. Therefore, we only need to check if a switch is
pressed or not every 50 ms.
The state machine need to be call after every 50 ms. If this deadline is
met, no press or release action are missed.
LedRtask LedRtask
S0 S1 S2 S3 S50 S51
S1 S3 S4 S5 S50 S51
//ledRedStateMachine.h
void ledRedTask(void);
//ledRedStateMachine.c
16
LED red Task Example
//int main(void)
void
SysTickIntHandler(void) while(1)
{ {
// if (ledGTimer>0) ledGTimer--; if (taskredledFlag)
if (taskredledcounter == 0) {
{ taskredledFlag = 0;
taskredledcounter = TASKLEDREDPERIOD; ledRedTask();
taskredledFlag = 1; }
//set Flag to signal the main loop to run }
the task
}
else taskredledcounter--;
}
Multi Tasking
Because the SWs are pressed by hand, it can not be pressed and
released too fast. Therefore, we only need to check if a switch is
pressed or not every 50 ms.
Each state machine need to be call after every 50 ms. If this deadline is
met, no press or release action are missed.
17
Design the timing of tasks
T1 T2 T1 T2
T1 T2 T1 T2
Example
18
Multi Tasking
Task design
Switch Task:
Read SW1, if SW1 is pressed turn on
the LED, other wise turn off the LED
UART task:
Check the command buffer and
process the command if flag is set.
19
UART ISR
SW Task
START
END
20
UART Task
START
Y Clear Flag
Flag is set?
Process command
Clear Command
Buffer
N
END
UART ISR
START
Y Y
Receive Data Read data
Interrupt? available?
N N Put to
Data=CR/
buffer
LF?
Y
Set Flag
END
21
Race condition
Race condition
22
Critical section
Use for very short critical action, e.g. the Flag example
23
Cooperative Scheduler
On the example10_MultiStateMachine,
each Task is managed by its own timer,
and the timer value is counted down
every Systick Timer interrupt.
The main loop look for the flag and run
the corresponding task.
Software layers
24
Task structure
A scheduler can be use to manage tasks on the project. A struct is used to
keep all information of the Task.
Task management
//task.c
tSchedulerTask g_psSchedulerTable[2]
= {{ledRedTask, ,(void*) 0,50,0,1},
{ledGreenTask,(void*) 0, 50,1,1}};
uint32_t g_ui32SchedulerNumTasks = 2;
//main.c
SchedulerInit(1000); //systick is 1 ms
while(1)
{
SchedulerRun();
25