Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

LẬP TRÌNH HỆ THỐNG

NHÚNG

BÙI QUỐC BẢO

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));

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 2

1
Function pointer declaration

 typedef int * (*FuncPtr)(int *);


 FuncPtr NewPtr;
NewPtr = &Function1;
 FuncPtr NewPtr;
NewPtr = Function1;
 Result = NewPtr(intPtr);
Result = (*NewPtr)(intPtr);

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 3

Check if function pointer is set

 if(NewPtr != 0U)
{
Result = (*NewPtr)(intPtr);
}

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 4

2
State machine

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 5

Struct to declare state machine

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 6

3
State variable and function

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 7

State function definition

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 8

4
State function definition

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 9

Update state machine

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 10

5
Real-time systems

 A real-time embedded system is a piece


of software that manages the resources
and the time behaviour of an embedded
device, usually build around a
microcontroller, emphasising on the
correctness of the computed values and
their availability at the expected time.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 11

Hard Realtime and Soft Realtime

 Hard real-time systems the tolerance to


missing deadlines is very low, since
missing a deadline often results in a
catastrophe, which may involve the loss
of human lives.
 Soft real-time systems this tolerance is
not as tight, since missing deadlines is
generally not as critical.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 12

6
DOS style

• Each program is started, utilized, and then terminated.


• We use Program 1, then Program 2, perhaps then take
a break, return to use Program 3, and then come back
to using Program 2 again.
• The second use of Program 2 is from scratch – it does
not continue from where it left off earlier

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 13

Windows style

Multiple programs appear to be running at the same time, with Windows


managing that illusion

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 14

7
iOS Style

 Programs are run in sequence, but the state of each


one is normally saved automatically so that it can
continue from where it left off

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 15

Embedded Software

The software structure – the program model – needs to be carefully


reviewed for every embedded software application

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 16

8
Simple loop model
The real-time performance of the system is
affected every time new code is added.

The super loop might be executing once


per 100 microseconds but suddenly add
new code and now it’s 110 microseconds

Very difficult to get predictable results for


hard real-time requirements

while (1) while (1)


{
{ Task1(); if (event1) Task1();
Task2(); if (event2) Task2();
}
}

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 17

Simple loop with Interrupts


Any code that has hard
real-time requirements or
are timing sensitive are
moved into an interrupt
service routine

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 18

9
Round Robin with Interrupts

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 19

Advantages of Round Robin Scheduler

 Requiring only one stack


 Result in simpler applications,
 The entire functionality can be performed
on ISRs, and the background logic is
reduced to an empty loop waiting for
interrupts

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 20

10
Disadvantages of round robin scheduler

 Design the entire logic as a set of state machines is


difficult.
 Difficult to maintenance as the program grows.
 The overall reaction speed is hard to design, because the
delay between the moment when the ISR makes available
the input and the moment when the background routine can
use it is not deterministic, depending on many other actions
that can happen at the same time in the superloop.
 To ensure that hard realtime actions are meet deadline,
they must be moved on the ISRs, lengthening them and
further worsening the reaction speed of the application

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 21

Operating system

 Divide an application into a number of


programs (which we will call tasks or
threads), which are run in an apparently
concurrent fashion
 Small ISRs are also likely to be included
in the system, but will mostly serve to
notify tasks or trigger action
 A RTOS is required

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 22

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

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 23

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

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 24

12
Scheduler

 All the tasks are running concurrently


 Each task to have a share of the processor
time
 The way that time is allocated between
tasks is termed “scheduling”
 The scheduler is the software that
determines which task should be run next

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 25

Run to Completion (RTC) 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

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 26

13
Round-robin scheduler

 Each task is run in turn (allowing for task suspension)


 The task does not execute a return, it can relinquish
the CPU at any time by making a call to the RTOS
 This call results in the kernel saving the context (all the
registers – including stack pointer and program
counter) and loading the context of the next task to be
run
BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 27

Queue

an array of function pointers (queue) is created that initially contains only NULL pointers

When a task needs to be executed, perhaps triggered by an interrupt for example, a


pointer is inserted into the first NULL location

Once a task has been executed, its function pointer location in the queue is returned to a
NULL pointer

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 28

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.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 29

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

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 30

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

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 31

LED red Task Example


LedRtask LedRtask

S1 S3 S4 S5 S50 S51
//ledRedStateMachine.h

#define TASKLEDREDPERIOD 50 //period is 50 ms


#define TASKLEDREDINIT 0 //start immediately
Extern uint32_t taskredledcounter;
extern uint32_t taskredledFlag;

void ledRedTask(void);
//ledRedStateMachine.c

static ledRState_t State = S_LEDOFF;


uint32_t taskredledcounter = TASKLEDREDINIT;
uint32_t taskredledFlag = 0;

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 32

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--;
}

Note: Open the example8_MultiStateMachine project and modify the


source code (marked as TODO)

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 33

Multi Tasking

 Task does not need to run too fast, we


just need to run it before “dead line”
 Example:
 For the previous example of controlling LEDs by SWs, we have
state machine 1 that check SW1 and control LED Red, and state
machine 2 that check SW2 and control LED Green.

 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.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 34

17
Design the timing of tasks

T1 T2 T1 T2

S1 S2 S51 S52 S53 S54

T1 T2 T1 T2

S1 S2 S51 S52 S53 S54

Which one is better

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 35

Example

 Note: Open the


Labexample10_TaskwithSystick2
project and modify the source code
(marked as TODO)

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 36

18
Multi Tasking

 Task does not need to run too fast, we


just need to run it before “dead line”
 Example:
 Write a program that can control Led Red by pressing SW1, and
turn on/off Led Green by command through UART.

 We only need to check if a switch is pressed or not every 50 ms


 We only need to process the command through UART every 100 ms.
 However, to get the character from UART we need to process fast
enough, or the data will be overwritten by next data

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 37

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.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 38

19
UART ISR

 If we use the UART Task to check if


there is any byte received, the UART
Task need to run every 868 uS if the
baud rate is 115200.
 Therefore, a UART ISR is used to
receive the data and put them to
command buffer. If a CR/LF byte is
received, a flag is set.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 39

SW Task

START

Turn off Led Red N Y Turn on Led Red


SW1
pressed?

END

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 40

20
UART Task

START

Y Clear Flag
Flag is set?
Process command
Clear Command
Buffer
N

END

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 41

UART ISR
START

Read Interrupt Flag


Clear Interrupt Flag

Y Y
Receive Data Read data
Interrupt? available?

N N Put to
Data=CR/
buffer
LF?
Y
Set Flag

END

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 42

21
Race condition

UART Task ISR


Flag

- Read (e.g. do_something(x) )


- Write (e.g. x = 5)
- Read-Modify-Write (e.g. x++)

Flag is a shared variable, it should be declared as volatile


ISR is performing a write to Flag
UART Task is performing Read-Modify-Write to Flag variable
Race condition occur if ISR is trigger right before the UART task write
back the value to Flag

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 43

Race condition

UART Task ISR


Command Buffer

Command Buffer is a shared variable


UART Task is performing Read-Modify-Write to Command Buffer
variable
Race condition occur if ISR is trigger right before or when the UART
task is clearing the buffer.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 44

22
Critical section

 Disable interrupt (enter_critical)


//Do some thing
//This is critical section
 Enable interrupt (exit_critical)

Use for very short critical action, e.g. the Flag example

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 45

Race condition on buffer


Carefully designed ring buffer an eliminated the race
condition
The ISR only modify write pointer, UART Task modify
read pointer.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 46

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.

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 47

Software layers

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 48

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.

typedef struct void ledRedTask(void *myobject)


{ {
void (*pfnFunction)(void *); ledRedStateMachineUpdate();
void *pvParam; }
uint32_t ui32FrequencyTicks;
uint32_t ui32LastCall;
bool bActive;
} void ledGreenTask(void *myobject)
tSchedulerTask; {
ledGreenStateUpdate();
}

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 49

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();

BM Kỹ Thuật Điện Tử - ĐH Bách Khoa TP.HCM 50

25

You might also like