44
Task Management (slides are based on http://www.freertos.org) Dr. Kuan-Hsun Chen LS 12, TU Dortmund October 31, 2019 Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)

Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Task Management(slides are based on http://www.freertos.org)

Dr. Kuan-Hsun Chen

LS 12, TU Dortmund

October 31, 2019

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 1 / 36

Page 2: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Outline

Introduction

Preliminaries: FreeRTOS Start Up

Task creation and Management

FreeRTOS Scheduling

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 2 / 36

Page 3: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Outline

Introduction

Preliminaries: FreeRTOS Start Up

Task creation and Management

FreeRTOS Scheduling

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 3 / 36

Page 4: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Timing parameters of a job Jj• Arrival time (aj) or release time (rj) is the time at which the job becomes ready for

execution• Computation (execution) time (Cj) is the time necessary to the processor for executing

the job without interruption.• Absolute deadline (dj) is the time at which the job should be completed.• Relative deadline (Dj) is the time length between the arrival time and the absolute

deadline.• Start time (sj) is the time at which the job starts its execution.• Finishing time (fj) is the time at which the job finishes its execution.• Response time (Rj) is the time length at which the job finishes its execution after its

arrival, which is fj − aj .

time

Cj

sj fj djajRj

DjDr. Kuan-Hsun Chen (LS 12, TU Dortmund) 4 / 36

Page 5: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

State of Jobs

• A job is either running or not running:

ready executing terminatedactivate

schedule

preempt

completion

• All the jobs that are ready for executions are put into a ready queue

• A “scheduler” decides which job in the ready queue obtains the privilege of thecomputing resources to run

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 5 / 36

Page 6: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Schedules for a set of jobs {J1, J2, . . . , JN}• A schedule is an assignment of jobs to the processor, such that each job is

executed until completion.

• A schedule can be defined as an integer step function σ : R→ N, where σ(t) = jdenotes job Jj is executed at time t, and σ(t) = 0 denotes the system is idle attime t.

• If σ(t) changes its value at some time t, then the processor performs a contextswitch at time t.

• Non-preemptive scheduling: there is only one interval with σ(t) = j for every Jj ,where t is covered by the interval.

• Preemptive scheduling: there could be more than one interval with σ(t) = j .

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 6 / 36

Page 7: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Scheduling Concept: Preemptive

Schedule: σ : R→ N function of processor time to jobs

J1 J2 J1 J3

0 1 2 3 4 5 6 7 8 9 10

σ(t)

1

2

3

s1 s2 f2 f1 s3 f3

ContextSwitches

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 7 / 36

Page 8: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Scheduling Concept: Preemptive

Schedule: σ : R→ N function of processor time to jobs

J1 J2 J1 J3

0 1 2 3 4 5 6 7 8 9 10

σ(t)

1

2

3

s1 s2 f2 f1 s3 f3

ContextSwitches

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 7 / 36

Page 9: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Scheduling Concept: Preemptive

Schedule: σ : R→ N function of processor time to jobs

J1 J2 J1 J3

0 1 2 3 4 5 6 7 8 9 10

σ(t)

1

2

3

s1 s2 f2 f1 s3 f3

ContextSwitches

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 7 / 36

Page 10: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Recurrent Tasks

• When jobs (usually with the same computation requirement) are releasedrecurrently, these jobs can be modeled by a recurrent task• Periodic Task τi :• A job is released exactly and periodically by a period Ti

• A phase φi indicates when the first job is released• A relative deadline Di for each job from task τi• (φi ,Ci ,Ti ,Di ) is the specification of periodic task τi , where Ci is the worst-case

execution time.

• Sporadic Task τi :• Ti is the minimal time between any two consecutive job releases• A relative deadline Di for each job from task τi• (Ci ,Ti ,Di ) is the specification of sporadic task τi , where Ci is the worst-case

execution time.

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 8 / 36

Page 11: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Recurrent Tasks

• When jobs (usually with the same computation requirement) are releasedrecurrently, these jobs can be modeled by a recurrent task• Periodic Task τi :• A job is released exactly and periodically by a period Ti

• A phase φi indicates when the first job is released• A relative deadline Di for each job from task τi• (φi ,Ci ,Ti ,Di ) is the specification of periodic task τi , where Ci is the worst-case

execution time.

• Sporadic Task τi :• Ti is the minimal time between any two consecutive job releases• A relative deadline Di for each job from task τi• (Ci ,Ti ,Di ) is the specification of sporadic task τi , where Ci is the worst-case

execution time.

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 8 / 36

Page 12: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Examples of Recurrent Task Models

Periodic task: (φi ,Ci ,Ti ,Di ) = (2, 2, 6, 6)

release deadline

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28

J1 J1 J1 J1

Sporadic task: (Ci ,Ti ,Di ) = (2, 6, 6)

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28

J1 J1 J1

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 9 / 36

Page 13: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Multi-Tasking

• The execution entities (tasks, processes, threads, etc.) are competing from eachother for shared resources• In FreeRTOS, we will only use “tasks” for the rest of the whole course.

• Scheduling decision policy is needed• When to schedule an entity?

• Which entity to schedule?• How to schedule entities?

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 10 / 36

Page 14: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Multi-Tasking

• The execution entities (tasks, processes, threads, etc.) are competing from eachother for shared resources• In FreeRTOS, we will only use “tasks” for the rest of the whole course.

• Scheduling decision policy is needed• When to schedule an entity?• Which entity to schedule?

• How to schedule entities?

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 10 / 36

Page 15: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Multi-Tasking

• The execution entities (tasks, processes, threads, etc.) are competing from eachother for shared resources• In FreeRTOS, we will only use “tasks” for the rest of the whole course.

• Scheduling decision policy is needed• When to schedule an entity?• Which entity to schedule?• How to schedule entities?

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 10 / 36

Page 16: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Multi-Tasking

[http://www.freertos.org]

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 11 / 36

Page 17: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Outline

Introduction

Preliminaries: FreeRTOS Start Up

Task creation and Management

FreeRTOS Scheduling

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 12 / 36

Page 18: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Task States

• Ready: A task that is able to executeis not currently running due to itslower priority

• Running: When a task is actuallyexecuting

• Blocked: Waiting for either a temporalor external event (Always has atimeout)

• Suspended: No timeout period allowed

[http://www.freertos.org]

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 13 / 36

Page 19: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Task States

• Ready: A task that is able to executeis not currently running due to itslower priority

• Running: When a task is actuallyexecuting

• Blocked: Waiting for either a temporalor external event (Always has atimeout)

• Suspended: No timeout period allowed

[http://www.freertos.org]

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 13 / 36

Page 20: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Task States

• Ready: A task that is able to executeis not currently running due to itslower priority

• Running: When a task is actuallyexecuting

• Blocked: Waiting for either a temporalor external event (Always has atimeout)

• Suspended: No timeout period allowed

[http://www.freertos.org]

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 13 / 36

Page 21: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Task States

• Ready: A task that is able to executeis not currently running due to itslower priority

• Running: When a task is actuallyexecuting

• Blocked: Waiting for either a temporalor external event (Always has atimeout)

• Suspended: No timeout period allowed

[http://www.freertos.org]

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 13 / 36

Page 22: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Data Types in FreeRTOS

Macro or typedef Actual typeportCHAR char

portSHORT short

portLONG long

portTickTypeUsed to store the tick count and specifyblock times

portBASE TYPEUsed to represent the most efficient datatype for the architecture

• Signed or unsigned char types should be always specified

• Plain int types should never be used

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 14 / 36

Page 23: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Variables and Functions Naming

• Variables: prefixes• ’c’: for char• ’s’: for short• ’l’: for long• ’x’: for portBASE TYPE and any others• ’u’: for unsigned• ’p’: pointer• combinations are possible

• Functions: prefixes• by the returning data type• ’v’: for void

• Common macros:• pdTRUE is 1, pdFALSE is 0• pdPASS is 1, pdFAIL is 0

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 15 / 36

Page 24: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Macro prefixes

Prefix Locationport (e.g., portMAX DELAY) portable.h

task (e.g., taskENTER CRITICAL()) task.h

pd (e.g., pdTRUE) projdefs.h

config (e.g., configUSE PREEMPTION) FreeRTOSConfig.h

err (e.g., errQUEUE FULL) projdefs.h

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 16 / 36

Page 25: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Configurations: FreeRTOSConfig.h

Some important fields/features:• configUSE PREEMPTION: This is set to 1 if the preemptive kernel is desired.

• configUSE IDLE HOOK: An idle task hook will execute a function during each cycle of the idletask.

• configUSE TICK HOOK: A tick hook function will execute on each RTOS tick interrupt if thisvalue is set to 1.

• configTICK RATE HZ: This is the frequency at which the RTOS tick will operate.

• configMAX PRIORITIES: The total number of priority levels that can be assigned whenprioritizing a task.

• configUSE COUNTING SEMAPHORES: This is set to 1 if counting semaphores are required.

• configUSE MUTEXES: This is set to 1 if mutexes are needed. Priority inheritance will then beenforced.

• etc...

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 17 / 36

Page 26: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Outline

Introduction

Preliminaries: FreeRTOS Start Up

Task creation and Management

FreeRTOS Scheduling

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 18 / 36

Page 27: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Structure of the Task

1 void vATaskFunction ( void ∗pvParameters )2 {3 for ( ; ; )4 {5 −− Task application code here . −−6 }7

8 /∗ Tasks must not attempt to r e t u r n from t h e i r i m p l e m e n t i n g ←↩f u n c t i o n o r o t h e r w i s e e x i t . I f i t i s n e c e s s a r y f o r a t a s k ←↩to e x i t then have th e t a s k c a l l v T a s k D e l e t e ( NULL ) to ←↩e n s u r e i t s e x i t i s c l e a n . ∗/

9 vTaskDelete ( NULL ) ;10 }

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 19 / 36

Page 28: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Create A Task

1 portBASE_TYPE xTaskCreate ( pdTASK_CODE pvTaskCode ,2 const signed portCHAR ∗ const pcName ,3 unsigned portSHORT usStackDepth ,4 void ∗pvParameters ,5 unsigned portBASE_TYPE uxPriority ,6 xTaskHandle ∗pxCreatedTask7 ) ;

• calls xTaskGenericCreate( ( pvTaskCode ), ( pcName ), ( usStackDepth ), ( pvParameters), ( uxPriority ), ( pxCreatedTask ), ( NULL ), ( NULL ) )

• pvTaskCode: A function that performs the computation of the task

• pcName: Name of the task used for debugging

• usStackDepth: Stack size of the process (task)

• pvParameters: Parameters passed to the process (task)

• uxPriority: Priority level indexed from 0 (lowest) to configMAX PRIORITIES-1(highest)

• pxCreatedTask: Handler when creating a task

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 20 / 36

Page 29: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Create A Task (cont.)

• return value of xTaskCreate()• pdTRUE: the task is created successfully• errCOULD NOT ALLOCATE REQUIRED MEMORY: insufficient heap memory

available for FreeRTOS to allocate enough RAM to hold the task data structuresand stack

• All the related information of a task is stored in a task control block (TCB) sothat the operating systems can use it for operating on a task.

Top of Stack pointer to the last item place on the stack for the task

Task State list item used to place the TCB in ready and blocked queues

Event List list item used to place the TCB in the event lists

Priority task priority (0=lowest)

Stack Start pointer to the start of the process stack

Others other information

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 21 / 36

Page 30: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Example - Task to be Crated

1 /∗ Task to be c r e a t e d . ∗/2 void vTaskCode ( void ∗ pvParameters )3 {4 /∗ The pa r a m e te r v a l u e i s e x p e c t e d to be 1 as 1 i s p a s s e d i n th e ←↩

pvParameters v a l u e i n t h e c a l l to xTaskCreate ( ) below . ∗/5

6 configASSERT ( ( ( uint32_t ) pvParameters ) = = 1 ) ;7

8 for ( ; ; )9 {

10 /∗ Task code goes h e r e . ∗/11 }12 }

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 22 / 36

Page 31: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Example - Function to Create a Task

1 /∗ F u n c t i o n t h a t c r e a t e s a t a s k . ∗/2 void vOtherFunction ( void )3 {4 portBASE TYPE xReturned ;5 xTaskHandle xHandle = NULL ;6 /∗ C r e a t e t he task , s t o r i n g t h e h a n d l e . ∗/7 xReturned = xTaskCreate (8 vTaskCode , /∗ F u n c t i o n t h a t implements th e t a s k . ∗/9 "NAME" , /∗ Text name f o r t he t a s k . ∗/

10 1000 , /∗ Stack s i z e i n words , not b y t e s . ∗/11 ( void ∗) 1 , /∗ Parameter p a s s e d i n t o t h e t a s k . ( i f not , NULL) ∗/12 1 , /∗ P r i o r i t y a t which t h e t a s k i s c r e a t e d . ∗/13 &xHandle ) ; /∗ Used to p a s s out t h e c r e a t e d h a n d l e o f t h e t a s k . ←↩

( i f not , p a s s NULL) ∗/

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 23 / 36

Page 32: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Example - Function to Create a Task (cont.)

1 if ( xReturned == pdPASS )2 {3 /∗ The t a s k was c r e a t e d . Use t h e h a n d l e o f t h e t a s k to d e l e t e t h e ←↩

t a s k . ∗/4 vTaskDelete ( xHandle ) ;5 }6 }

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 24 / 36

Page 33: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

States of A Task

Create Task Suspended

Ready Running

BlockedDeleted

xTaskCreate

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 25 / 36

Page 34: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Examples of Recurrent Task Models

Periodic task: (φi ,Ci ,Ti ,Di ) = (2, 2, 6, 6)

release deadline

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28

J1 J1 J1 J1

Sporadic task: (Ci ,Ti ,Di ) = (2, 6, 6)

0 2 4 6 8 10 12 14 16 18 20 22 24 26 28

J1 J1 J1

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 26 / 36

Page 35: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Example: Sporadic Control System

Pseudo-code for this system

while (true)

• start := get the system tick;

• perform analog-to-digital conversion to get y ;

• compute control output u;

• output u and do digital-to-analog conversion;

• end := get the system tick;

• sleepTime := T − (end − start);

• sleep sleepTime;

end while

Control System

A/D

(The system

being controlled)

plant

actuator

D/AControl−law

computation

sensor

y(t) u(t)

ukyk

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 27 / 36

Page 36: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

void vTaskDelay(portTickType xTicksToDelay)

Delay a task for a given number of ticks. The actual time that the task remainsblocked depends on the tick rate. The constant portTICK RATE MS can be used tocalculate real time from the tick rate - with the resolution of one tick period.

• xTicksToDelay: The amount of time, in tick periods, that the calling task shouldblock.

• INCLUDE vTaskDelay must be defined as 1 for this function to be available.

• vTaskDelay() specifies a time at which the task wishes to unblock relative to thetime at which vTaskDelay() is called.

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 28 / 36

Page 37: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Example: Periodic Control System

Pseudo-code for this system

set timer to interrupt periodically with periodT ;

at each timer interruptdo

• perform analog-to-digital conversion toget y ;

• compute control output u;

• output u and do digital-to-analogconversion;

od

Control System

A/D

(The system

being controlled)

plant

actuator

D/AControl−law

computation

sensor

y(t) u(t)

ukyk

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 29 / 36

Page 38: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

void vTaskDelayUntil( portTickType *pxPreviousWakeTime, portTickType xTimeIncrement )

Delay a task until a specified time. This function can be used by periodic tasks toensure a constant execution frequency (period).

• INCLUDE vTaskDelayUntil must be defined as 1 for this function to be available.• It differs from vTaskDelay ():• vTaskDelay () will cause a task to block for the specified number of ticks from the

time vTaskDelay () is called.• It is therefore difficult to use vTaskDelay () by itself to generate a fixed execution

frequency as the time between a task starting to execute and that task callingvTaskDelay () may not be fixed

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 30 / 36

Page 39: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

An Example for vTaskDelayUntil

1 void vTaskFunction ( void ∗ pvParameters )2 {3 portTickType xLastWakeTime ;4 const portTickType xFrequency = 1 0 ;5 xLastWakeTime = xTaskGetTickCount ( ) ;6 for ( ; ; )7 {8 // do someth ing9 vTaskDelayUntil ( &xLastWakeTime , xFrequency ) ;

10 }11 }

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 31 / 36

Page 40: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

States of A Task

Create Task Suspended

Ready Running

BlockedDeleted

xTaskCreate

vTaskDelayvTaskDelayUntil

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 32 / 36

Page 41: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Outline

Introduction

Preliminaries: FreeRTOS Start Up

Task creation and Management

FreeRTOS Scheduling

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 33 / 36

Page 42: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

vTaskStartScheduler()

1 int main ( void ) {2 xTaskCreate ( hello , "Task 1" , 1000 , NULL , 1 , NULL ) ;3

4 vTaskStartScheduler ( ) ;5

6 for ( ; ; ) ;7 }

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 34 / 36

Page 43: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

vTaskStartScheduler()

• Starts the real-time kernel tick processing.

• After calling, the kernel has control over which tasks are executed and when.

• This function does not return until an executing task calls vTaskEndScheduler ().

• At least one task should be created via a call to xTaskCreate () before callingvTaskStartScheduler ().• An idle task is created automatically when the function is called.• It has a priority of zero (tskIDLE PRIORITY).

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 35 / 36

Page 44: Task Management - ls12-€¦ · Task creation and Management FreeRTOS Scheduling Dr. Kuan-Hsun Chen (LS 12, TU Dortmund)3 / 36. Timing parameters of a job J j Arrival time (a

Idle Task

• The processor requires something to execute, unless it is halt

• An idle task is a dummy procedure• We can add a hook function vApplicationIdleHook() to an idle task• e.g., to measure the amount of processing capacity• e.g., to place the processor to the low-power mode• vApplicationIdleHook() must not, under any circumstances, call a function that

might block. This will result in a scenario that no task is available to enter theRunning state

• The idle task is also responsible for cleaning up the kernel resources used by adeleted task. It should be done in a reasonable amount of time.• The idle task can be configured to be preempted• only at ticks (#define configIDLE SHOULD YIELD 0) or• at any time (#define configIDLE SHOULD YIELD 1)

Dr. Kuan-Hsun Chen (LS 12, TU Dortmund) 36 / 36