SlideShare a Scribd company logo
Sherif Hammad
Real Time Operating System
Introduction & “FreeRTOS”
2
Agenda
• RTOS Basics
• RTOS sample State Machine
• RTOS scheduling criteria
• RTOS optimization criteria
• Soft/Hard Real Time requirements
• Tasks scheduling
3
4
5
6
7
8
CPU Scheduling Criteria
• CPU Utilization: CPU should be as busy as possible (40% to 90%)
• Throughput: No. of processes per unit time
• Turnaround time: For a particular process how long it takes to
execute. (Interval between time of submission to completion)
• Waiting time: Total time process spends in ready queue.
• Response: First response of process after submission
9
Optimization criteria
• It is desirable to
o Maximize CPU utilization
o Maximize throughput
o Minimize turnaround time
o Minimize start time
o Minimize waiting time
o Minimize response time
• In most cases, we strive to optimize the average
measure of each metric
• In other cases, it is more important to optimize the
minimum or maximum values rather than the average
10
Soft/Hard Real Time
• Soft real­time requirements: state a time deadline—but breaching
the deadline would not render the system useless.
• Hard real­time requirements: state a time deadline—and breaching
the deadline would result in absolute failure of the system.
• Cortex­M4 has only one core executing a single Thread at a time.
• The kernel decides which thread should be executing by examining
the priority assigned to each thread by the application designer.
• Application designer could assign higher priorities to hard­real­
time­threads and lower priorities to soft real­time
11
Why Use a Real-time Kernel?
• Abstracting away timing information
• Maintainability/Reusability/Extensibility
• Modularity
• Team development
• Improved efficiency (No Polling)
• Idle time:
The Idle task is created automatically when the kernel is started. It executes
whenever there are no application tasks wishing to execute. The idle task
can be used to measure spare processing capacity, to perform background
checks, or simply to place the processor into a low­power mode.
• Flexible interrupt handling:
Interrupt handlers can be kept very short by deferring most of the required
processing to handler RTOS tasks.
12
Task Functions
• Arbitrary naming: Must return void: Must take a void pointer
parameter:
• Normally run forever within an infinite loop, and will not exit.
• FreeRTOS tasks must not be allowed to return from their
implementing function in any way—they must not contain a
‘return’ statement and must not be allowed to execute past the end
of the function.
• A single task function definition can be used to create any number
of tasks—each created task being a separate execution instance
with its own stack and its own copy of any automatic (stack)
variables defined within the task itself.
13
Task Functions
14
Top Level Task States
• When a task is in the Running state, the processor is
executing its code.
• When a task is in the Not Running state, its status having
been saved ready for it to resume execution the next time
the scheduler decides it should enter the Running state.
• When a task resumes execution, it does so from the
instruction it was about to execute before it last left the
Running state.
14
Top Level Task States
• When a task is in the Running state, the processor is
executing its code.
• When a task is in the Not Running state, its status having
been saved ready for it to resume execution the next time
the scheduler decides it should enter the Running state.
• When a task resumes execution, it does so from the
instruction it was about to execute before it last left the
Running state.
14
Top Level Task States
• When a task is in the Running state, the processor is
executing its code.
• When a task is in the Not Running state, its status having
been saved ready for it to resume execution the next time
the scheduler decides it should enter the Running state.
• When a task resumes execution, it does so from the
instruction it was about to execute before it last left the
Running state.
15
Creating Tasks
The xTaskCreate() API Function
16
Example 1: Task Functions
17
Run Example 1
18
Tick Interrupt
• To be able to select the next task to run, the scheduler itself
must execute at the end of each time slice.
• A periodic interrupt, called the tick interrupt, is used for this
purpose.
• The length of the time slice is effectively set by the tick
interrupt frequency, which is configured by the
configTICK_RATE_HZ compile time configuration constant in
FreeRTOSConfig.h.
2
Agenda
• Tasks priorities
• Task State Machine
• Periodic tasks
• Tasks Blocking
2
Agenda
• Tasks priorities
• Task State Machine
• Periodic tasks
• Tasks Blocking
6
Task Creation After Schedule Started
(From Within Another Task)
7
Example 2: Single Task Function
“Instantiated Twice” (Two Task Instants)
8
Example 2: Single Task Function
“Instantiated Twice” (Two Task Instants)
9
Task Priorities
• The priority can be changed after the scheduler has been started by
using the vTaskPrioritySet() API function.
• The maximum number of priorities available is set by the
application­defined configMAX_PRIORITIES compile time
configuration constant within FreeRTOSConfig.h.
• The higher the configMAX_PRIORITIES value the more RAM the
kernel will consume, “keep it minimum”.
• Low numeric priority values denote low­priority tasks, with priority
0 being the lowest priority possible.
• Where more than one task of the same priority is able to run, the
scheduler will transition each task into and out of the Running
state, in turn.
• Each such task executes for a ‘time slice‘; it enters the Running
state at the start of the time slice and exits the Running state at the
end of the time slice.
10
Task Priorities; Example 3
11
Task Priorities: Example 3; Starvation
• Both Tasks are made periodic by the “dummy” loop
• Both Tasks only needs CPU for short execution
time!
• Task 2 (High Priority) takes CPU all the time
• Task 1 suffers starvation
• Wastes power and cycles!
• Is there another smarter way?
12
Using the Blocked state to create a delay
• vTaskDelay() places the calling task into the Blocked state for a fixed number of
tick interrupts.
• While in the Blocked state the task does not use any processing time
• vTaskDelay() API function is available only when. INCLUDE_vTaskDelay is set to 1
in FreeRTOSConfig.h
• The constant portTICK_RATE_MS can be used to convert milliseconds into ticks.
• Portmacro.h: #define portTICK_RATE_MS ( ( portTickType ) 1000 /
configTICK_RATE_HZ )
• FreeRTOSConfig.h: #define configTICK_RATE_HZ ( ( portTickType ) 1000 )
13
Task Blocking: Example 4
• Each time the tasks leave the Blocked state
they execute for a fraction of a tick period
before re-entering the Blocked state.
• Most of the time there are no application tasks
that are able to run; The idle task will run.
• Idle task time is a measure of the spare
processing capacity in the system.
14
Expanding the ‘Not Running’ State
The Blocked State
• A task that is waiting for an event is said to be in the ‘Blocked’ state,
which is a sub-state of the Not Running state.
• Tasks can enter the Blocked state to wait for two different types of
event:
o Temporal (time-related) events—the event being either a delay period
expiring, or an absolute time being reached. For example, a task may enter
the Blocked state to wait for 10 milliseconds to pass.
o Synchronization events—where the events originate from another task or
interrupt. For example, a task may enter the Blocked state to wait for data
to arrive on a queue. Synchronization events cover a broad range of event
types.
15
The Suspended State
• ‘Suspended’ is also a sub-state of Not Running.
• Tasks in the Suspended state are not available to the
scheduler.
• The only way into the Suspended state is through a call to the
vTaskSuspend() API function
• the only way out being through a call to the vTaskResume() or
xTaskResumeFromISR() API functions.
• Most applications do not use the Suspended state.
The Ready State
• Tasks that are in the Not Running state but are not Blocked or
Suspended are said to be in the Ready state.
• They are able to run, and therefore ‘ready’ to run, but their
priorities are not qualifying to be in the Running state.
16
Task Blocking: Example 4
17
FreeRTOS Task SM
17
FreeRTOS Task SM
2
Agenda
• Accurate task periods
• Continuous & Periodic Tasks
• Changing Tasks priorities
• Deleting tasks
3
Accurate Tasks Periods
• vTaskDelayUntil() is similar to vTaskDelay() “but”
• vTaskDelay() parameter specifies the number of tick interrupts that should occur
between a task calling vTaskDelay() and the same task once again transitioning
out of the Blocked state.
• The length of time the task remains in the blocked state is specified by the
vTaskDelay() parameter, but the actual time at which the task leaves the blocked
state is relative to the time at which vTaskDelay() was called.
• The parameters to vTaskDelayUntil() specify, instead, the exact tick count value
at which the calling task should be moved from the Blocked state into the Ready
state.
• vTaskDelayUntil() is the API function that should be used when a fixed execution
period is required (where you want your task to execute periodically with a fixed
frequency), as the time at which the calling task is unblocked is absolute, rather
than relative to when the function was called (as is the case with vTaskDelay()).
• vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil
is set to 1 in FreeRTOSConfig.h.
vTaskDelayUntil()
3
Accurate Tasks Periods
• vTaskDelayUntil() is similar to vTaskDelay() “but”
• vTaskDelay() parameter specifies the number of tick interrupts that should occur
between a task calling vTaskDelay() and the same task once again transitioning
out of the Blocked state.
• The length of time the task remains in the blocked state is specified by the
vTaskDelay() parameter, but the actual time at which the task leaves the blocked
state is relative to the time at which vTaskDelay() was called.
• The parameters to vTaskDelayUntil() specify, instead, the exact tick count value
at which the calling task should be moved from the Blocked state into the Ready
state.
• vTaskDelayUntil() is the API function that should be used when a fixed execution
period is required (where you want your task to execute periodically with a fixed
frequency), as the time at which the calling task is unblocked is absolute, rather
than relative to when the function was called (as is the case with vTaskDelay()).
• vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil
is set to 1 in FreeRTOSConfig.h.
vTaskDelayUntil()
• Delay a task until a specified time. This function can be used by periodic tasks to ensure
a constant execution frequency.
• This function differs from vTaskDelay() in one important aspect: vTaskDelay() specifies
a time at which the task wishes to unblock relative to the time at which vTaskDelay() is
called, whereas vTaskDelayUntil() specifies an absolute time at which the task wishes
to unblock.
• 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 unblocking following a call to vTaskDelay() and that task
next calling vTaskDelay() may not be fixed [the task may take a different path through the
code between calls, or may get interrupted or preempted a different number of times each
time it executes].
• Whereas vTaskDelay() specifies a wake time relative to the time at which the function is
called, vTaskDelayUntil() specifies the absolute (exact) time at which it wishes to unblock.
3
Accurate Tasks Periods
• vTaskDelayUntil() is similar to vTaskDelay() “but”
• vTaskDelay() parameter specifies the number of tick interrupts that should occur
between a task calling vTaskDelay() and the same task once again transitioning
out of the Blocked state.
• The length of time the task remains in the blocked state is specified by the
vTaskDelay() parameter, but the actual time at which the task leaves the blocked
state is relative to the time at which vTaskDelay() was called.
• The parameters to vTaskDelayUntil() specify, instead, the exact tick count value
at which the calling task should be moved from the Blocked state into the Ready
state.
• vTaskDelayUntil() is the API function that should be used when a fixed execution
period is required (where you want your task to execute periodically with a fixed
frequency), as the time at which the calling task is unblocked is absolute, rather
than relative to when the function was called (as is the case with vTaskDelay()).
• vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil
is set to 1 in FreeRTOSConfig.h.
• It should be noted that vTaskDelayUntil() will return immediately (without blocking) if it is
used to specify a wake time that is already in the past.
• Therefore a task using vTaskDelayUntil() to execute periodically will have to re-calculate
its required wake time if the periodic execution is halted for any reason
(for example, the task is temporarily placed into the Suspended state) causing the task
to miss one or more periodic executions.
• This can be detected by checking the variable passed by reference as the
pxPreviousWakeTime parameter against the current tick count. This is however
not necessary under most usage scenarios.
• The constant portTICK_PERIOD_MS can be used to calculate real time from the tick rate -
with the resolution of one tick period.
vTaskDelayUntil()
3
Accurate Tasks Periods
• vTaskDelayUntil() is similar to vTaskDelay() “but”
• vTaskDelay() parameter specifies the number of tick interrupts that should occur
between a task calling vTaskDelay() and the same task once again transitioning
out of the Blocked state.
• The length of time the task remains in the blocked state is specified by the
vTaskDelay() parameter, but the actual time at which the task leaves the blocked
state is relative to the time at which vTaskDelay() was called.
• The parameters to vTaskDelayUntil() specify, instead, the exact tick count value
at which the calling task should be moved from the Blocked state into the Ready
state.
• vTaskDelayUntil() is the API function that should be used when a fixed execution
period is required (where you want your task to execute periodically with a fixed
frequency), as the time at which the calling task is unblocked is absolute, rather
than relative to when the function was called (as is the case with vTaskDelay()).
• vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil
is set to 1 in FreeRTOSConfig.h.
4
Accurate Tasks Periods (Example 5)
5
Example 6: Continuous & Periodic Tasks
5
Example 6: Continuous & Periodic Tasks
6
Example 6: Continuous & Periodic Tasks
9
Priorities & Preemption
• 1_three_tasks_same_priority
• 2_three_tasks_different_priority
• 3_three_tasks_preemption_delete_task (Example 9)
2
The Idle Task and the Idle Task Hook
(Idle Task CallBack)
• An Idle task is automatically created by the scheduler when
vTaskStartScheduler() is called.
• The idle task does very little more than sit in a loop
• The idle task has the lowest possible priority (priority zero), to ensure it
never prevents a higher priority application task from entering the
Running state
• Idle task is always pre-empted by higher priority tasks
• idle hook (or idle callback) function—a function that is called
automatically by the idle task once per iteration of the idle task loop
• Common uses for the Idle task hook include:
o Executing low priority, background, or continuous processing.
o Measuring the amount of spare processing capacity.
o Placing the processor into a low power mode
• configUSE_IDLE_HOOK must be set to 1 within FreeRTOSConfig.h for the
idle hook function to get called.
3
Example 7: Idle Task CallBack
4
Example 7: Idle Task CallBack
5
Example 7: Idle Task CallBack
How many times IdelTask is called between tasks calls?
For how long the CPU is into Idle Task?
Real Time Operating System
“FreeRTOS”
Change Task Priority
Using the FreeRTOS Real Time Kernel - a Practical Guide - Cortex M3 Edition (FreeRTOS
Tutorial Books)
by Richard Barry
Sherif Hammad
Real Time Operating System
“FreeRTOS”
Change Task Priority
Using the FreeRTOS Real Time Kernel - a Practical Guide - Cortex M3 Edition (FreeRTOS
Tutorial Books)
by Richard Barry
Sherif Hammad
2
Changing task priorities; Example 8
3
Changing task priorities; Example 8
4
Changing task priorities; Example 8
5
Changing task priorities; Example 8
5
Changing task priorities; Example 8
5
Changing task priorities; Example 8
7
Deleting a Task; Example 9
7
Deleting a Task; Example 9
8
Deleting a Task; Example 9
Task 2
Preempts 1
Task 1 Resumes
9
Priorities & Preemption
• 1_three_tasks_same_priority
• 2_three_tasks_different_priority
• 3_three_tasks_preemption_delete_task (Example 9)
6
The Scheduling Algorithm—A Summary
• Fixed Prioritized Pre­emptive Scheduling
o Each task is assigned a priority.
o Each task can exist in one of several states.
o Only one task can exist in the Running state at any one time.
o The scheduler always selects the highest priority Ready state task to enter
the Running state.
• Fixed Priority’ because each task is assigned a priority that is not
altered by the kernel itself (only tasks can change priorities);
• ‘Pre­emptive’ because a task entering the Ready state or having its
priority altered will always pre­empt the Running state task, if the
Running state task has a lower priority.
7
The Scheduling Algorithm—A Summary

More Related Content

PPTX
FreeRTOS basics (Real time Operating System)
PPTX
Real Time Kernels
PPTX
Process management in Operating System_Unit-2
PPTX
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptx
PPTX
PPTX
Process scheduling
PPTX
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT V Real Time Operating System (RTOS)
PPTX
Unit 2_OS process management
FreeRTOS basics (Real time Operating System)
Real Time Kernels
Process management in Operating System_Unit-2
Embedded_ PPT_4-5 unit_Dr Monika-edited.pptx
Process scheduling
SYBSC IT SEM IV EMBEDDED SYSTEMS UNIT V Real Time Operating System (RTOS)
Unit 2_OS process management

Similar to FreeRTOS Slides annotations.pdf (20)

PDF
3_FreeRTOS_Kernel_Priorities_Part3.pdf
PPTX
synchronization in operating system structure
PPTX
Real Time System
PPTX
Real Time Operating Systems Basic Definitions
PPTX
RTOS_Keywords_with basic Definitions.pptx
PPTX
Lecture 4 process cpu scheduling
PPTX
Round Robin Algorithm.pptx
PPTX
CONTEXT SWITCHING,PREEMPTIVE,NONPREEMPTIVE.pptx
PPT
CPU Scheduling Algorithms(FCFS,SJF,RR).ppt
PPSX
Survey of task scheduler
PPTX
Operating System lab
PPTX
Lecture 7 cpu scheduling
PPTX
PROCESS.pptx
PDF
operating system (1).pdf
PPTX
CPU Scheduling Criteria CPU Scheduling Criteria (1).pptx
PDF
Operating Systems 1 (10/12) - Scheduling
PPTX
RTOS _Timer , Event, Memory, Device, File & IO Systems Management_10-07-25.pptx
PPT
PDF
Real time operating system
PPTX
Operating System
3_FreeRTOS_Kernel_Priorities_Part3.pdf
synchronization in operating system structure
Real Time System
Real Time Operating Systems Basic Definitions
RTOS_Keywords_with basic Definitions.pptx
Lecture 4 process cpu scheduling
Round Robin Algorithm.pptx
CONTEXT SWITCHING,PREEMPTIVE,NONPREEMPTIVE.pptx
CPU Scheduling Algorithms(FCFS,SJF,RR).ppt
Survey of task scheduler
Operating System lab
Lecture 7 cpu scheduling
PROCESS.pptx
operating system (1).pdf
CPU Scheduling Criteria CPU Scheduling Criteria (1).pptx
Operating Systems 1 (10/12) - Scheduling
RTOS _Timer , Event, Memory, Device, File & IO Systems Management_10-07-25.pptx
Real time operating system
Operating System
Ad

Recently uploaded (20)

PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Sustainable Sites - Green Building Construction
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Construction Project Organization Group 2.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
additive manufacturing of ss316l using mig welding
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
PPT on Performance Review to get promotions
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
OOP with Java - Java Introduction (Basics)
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CH1 Production IntroductoryConcepts.pptx
Sustainable Sites - Green Building Construction
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Construction Project Organization Group 2.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
additive manufacturing of ss316l using mig welding
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT on Performance Review to get promotions
bas. eng. economics group 4 presentation 1.pptx
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
OOP with Java - Java Introduction (Basics)
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Ad

FreeRTOS Slides annotations.pdf

  • 1. Sherif Hammad Real Time Operating System Introduction & “FreeRTOS”
  • 2. 2 Agenda • RTOS Basics • RTOS sample State Machine • RTOS scheduling criteria • RTOS optimization criteria • Soft/Hard Real Time requirements • Tasks scheduling
  • 3. 3
  • 4. 4
  • 5. 5
  • 6. 6
  • 7. 7
  • 8. 8 CPU Scheduling Criteria • CPU Utilization: CPU should be as busy as possible (40% to 90%) • Throughput: No. of processes per unit time • Turnaround time: For a particular process how long it takes to execute. (Interval between time of submission to completion) • Waiting time: Total time process spends in ready queue. • Response: First response of process after submission
  • 9. 9 Optimization criteria • It is desirable to o Maximize CPU utilization o Maximize throughput o Minimize turnaround time o Minimize start time o Minimize waiting time o Minimize response time • In most cases, we strive to optimize the average measure of each metric • In other cases, it is more important to optimize the minimum or maximum values rather than the average
  • 10. 10 Soft/Hard Real Time • Soft real­time requirements: state a time deadline—but breaching the deadline would not render the system useless. • Hard real­time requirements: state a time deadline—and breaching the deadline would result in absolute failure of the system. • Cortex­M4 has only one core executing a single Thread at a time. • The kernel decides which thread should be executing by examining the priority assigned to each thread by the application designer. • Application designer could assign higher priorities to hard­real­ time­threads and lower priorities to soft real­time
  • 11. 11 Why Use a Real-time Kernel? • Abstracting away timing information • Maintainability/Reusability/Extensibility • Modularity • Team development • Improved efficiency (No Polling) • Idle time: The Idle task is created automatically when the kernel is started. It executes whenever there are no application tasks wishing to execute. The idle task can be used to measure spare processing capacity, to perform background checks, or simply to place the processor into a low­power mode. • Flexible interrupt handling: Interrupt handlers can be kept very short by deferring most of the required processing to handler RTOS tasks.
  • 12. 12 Task Functions • Arbitrary naming: Must return void: Must take a void pointer parameter: • Normally run forever within an infinite loop, and will not exit. • FreeRTOS tasks must not be allowed to return from their implementing function in any way—they must not contain a ‘return’ statement and must not be allowed to execute past the end of the function. • A single task function definition can be used to create any number of tasks—each created task being a separate execution instance with its own stack and its own copy of any automatic (stack) variables defined within the task itself.
  • 14. 14 Top Level Task States • When a task is in the Running state, the processor is executing its code. • When a task is in the Not Running state, its status having been saved ready for it to resume execution the next time the scheduler decides it should enter the Running state. • When a task resumes execution, it does so from the instruction it was about to execute before it last left the Running state.
  • 15. 14 Top Level Task States • When a task is in the Running state, the processor is executing its code. • When a task is in the Not Running state, its status having been saved ready for it to resume execution the next time the scheduler decides it should enter the Running state. • When a task resumes execution, it does so from the instruction it was about to execute before it last left the Running state.
  • 16. 14 Top Level Task States • When a task is in the Running state, the processor is executing its code. • When a task is in the Not Running state, its status having been saved ready for it to resume execution the next time the scheduler decides it should enter the Running state. • When a task resumes execution, it does so from the instruction it was about to execute before it last left the Running state.
  • 18. 16 Example 1: Task Functions
  • 20. 18 Tick Interrupt • To be able to select the next task to run, the scheduler itself must execute at the end of each time slice. • A periodic interrupt, called the tick interrupt, is used for this purpose. • The length of the time slice is effectively set by the tick interrupt frequency, which is configured by the configTICK_RATE_HZ compile time configuration constant in FreeRTOSConfig.h.
  • 21. 2 Agenda • Tasks priorities • Task State Machine • Periodic tasks • Tasks Blocking
  • 22. 2 Agenda • Tasks priorities • Task State Machine • Periodic tasks • Tasks Blocking
  • 23. 6 Task Creation After Schedule Started (From Within Another Task)
  • 24. 7 Example 2: Single Task Function “Instantiated Twice” (Two Task Instants)
  • 25. 8 Example 2: Single Task Function “Instantiated Twice” (Two Task Instants)
  • 26. 9 Task Priorities • The priority can be changed after the scheduler has been started by using the vTaskPrioritySet() API function. • The maximum number of priorities available is set by the application­defined configMAX_PRIORITIES compile time configuration constant within FreeRTOSConfig.h. • The higher the configMAX_PRIORITIES value the more RAM the kernel will consume, “keep it minimum”. • Low numeric priority values denote low­priority tasks, with priority 0 being the lowest priority possible. • Where more than one task of the same priority is able to run, the scheduler will transition each task into and out of the Running state, in turn. • Each such task executes for a ‘time slice‘; it enters the Running state at the start of the time slice and exits the Running state at the end of the time slice.
  • 28. 11 Task Priorities: Example 3; Starvation • Both Tasks are made periodic by the “dummy” loop • Both Tasks only needs CPU for short execution time! • Task 2 (High Priority) takes CPU all the time • Task 1 suffers starvation • Wastes power and cycles! • Is there another smarter way?
  • 29. 12 Using the Blocked state to create a delay • vTaskDelay() places the calling task into the Blocked state for a fixed number of tick interrupts. • While in the Blocked state the task does not use any processing time • vTaskDelay() API function is available only when. INCLUDE_vTaskDelay is set to 1 in FreeRTOSConfig.h • The constant portTICK_RATE_MS can be used to convert milliseconds into ticks. • Portmacro.h: #define portTICK_RATE_MS ( ( portTickType ) 1000 / configTICK_RATE_HZ ) • FreeRTOSConfig.h: #define configTICK_RATE_HZ ( ( portTickType ) 1000 )
  • 30. 13 Task Blocking: Example 4 • Each time the tasks leave the Blocked state they execute for a fraction of a tick period before re-entering the Blocked state. • Most of the time there are no application tasks that are able to run; The idle task will run. • Idle task time is a measure of the spare processing capacity in the system.
  • 31. 14 Expanding the ‘Not Running’ State The Blocked State • A task that is waiting for an event is said to be in the ‘Blocked’ state, which is a sub-state of the Not Running state. • Tasks can enter the Blocked state to wait for two different types of event: o Temporal (time-related) events—the event being either a delay period expiring, or an absolute time being reached. For example, a task may enter the Blocked state to wait for 10 milliseconds to pass. o Synchronization events—where the events originate from another task or interrupt. For example, a task may enter the Blocked state to wait for data to arrive on a queue. Synchronization events cover a broad range of event types.
  • 32. 15 The Suspended State • ‘Suspended’ is also a sub-state of Not Running. • Tasks in the Suspended state are not available to the scheduler. • The only way into the Suspended state is through a call to the vTaskSuspend() API function • the only way out being through a call to the vTaskResume() or xTaskResumeFromISR() API functions. • Most applications do not use the Suspended state. The Ready State • Tasks that are in the Not Running state but are not Blocked or Suspended are said to be in the Ready state. • They are able to run, and therefore ‘ready’ to run, but their priorities are not qualifying to be in the Running state.
  • 36. 2 Agenda • Accurate task periods • Continuous & Periodic Tasks • Changing Tasks priorities • Deleting tasks
  • 37. 3 Accurate Tasks Periods • vTaskDelayUntil() is similar to vTaskDelay() “but” • vTaskDelay() parameter specifies the number of tick interrupts that should occur between a task calling vTaskDelay() and the same task once again transitioning out of the Blocked state. • The length of time the task remains in the blocked state is specified by the vTaskDelay() parameter, but the actual time at which the task leaves the blocked state is relative to the time at which vTaskDelay() was called. • The parameters to vTaskDelayUntil() specify, instead, the exact tick count value at which the calling task should be moved from the Blocked state into the Ready state. • vTaskDelayUntil() is the API function that should be used when a fixed execution period is required (where you want your task to execute periodically with a fixed frequency), as the time at which the calling task is unblocked is absolute, rather than relative to when the function was called (as is the case with vTaskDelay()). • vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil is set to 1 in FreeRTOSConfig.h. vTaskDelayUntil()
  • 38. 3 Accurate Tasks Periods • vTaskDelayUntil() is similar to vTaskDelay() “but” • vTaskDelay() parameter specifies the number of tick interrupts that should occur between a task calling vTaskDelay() and the same task once again transitioning out of the Blocked state. • The length of time the task remains in the blocked state is specified by the vTaskDelay() parameter, but the actual time at which the task leaves the blocked state is relative to the time at which vTaskDelay() was called. • The parameters to vTaskDelayUntil() specify, instead, the exact tick count value at which the calling task should be moved from the Blocked state into the Ready state. • vTaskDelayUntil() is the API function that should be used when a fixed execution period is required (where you want your task to execute periodically with a fixed frequency), as the time at which the calling task is unblocked is absolute, rather than relative to when the function was called (as is the case with vTaskDelay()). • vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil is set to 1 in FreeRTOSConfig.h. vTaskDelayUntil() • Delay a task until a specified time. This function can be used by periodic tasks to ensure a constant execution frequency. • This function differs from vTaskDelay() in one important aspect: vTaskDelay() specifies a time at which the task wishes to unblock relative to the time at which vTaskDelay() is called, whereas vTaskDelayUntil() specifies an absolute time at which the task wishes to unblock. • 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 unblocking following a call to vTaskDelay() and that task next calling vTaskDelay() may not be fixed [the task may take a different path through the code between calls, or may get interrupted or preempted a different number of times each time it executes]. • Whereas vTaskDelay() specifies a wake time relative to the time at which the function is called, vTaskDelayUntil() specifies the absolute (exact) time at which it wishes to unblock.
  • 39. 3 Accurate Tasks Periods • vTaskDelayUntil() is similar to vTaskDelay() “but” • vTaskDelay() parameter specifies the number of tick interrupts that should occur between a task calling vTaskDelay() and the same task once again transitioning out of the Blocked state. • The length of time the task remains in the blocked state is specified by the vTaskDelay() parameter, but the actual time at which the task leaves the blocked state is relative to the time at which vTaskDelay() was called. • The parameters to vTaskDelayUntil() specify, instead, the exact tick count value at which the calling task should be moved from the Blocked state into the Ready state. • vTaskDelayUntil() is the API function that should be used when a fixed execution period is required (where you want your task to execute periodically with a fixed frequency), as the time at which the calling task is unblocked is absolute, rather than relative to when the function was called (as is the case with vTaskDelay()). • vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil is set to 1 in FreeRTOSConfig.h. • It should be noted that vTaskDelayUntil() will return immediately (without blocking) if it is used to specify a wake time that is already in the past. • Therefore a task using vTaskDelayUntil() to execute periodically will have to re-calculate its required wake time if the periodic execution is halted for any reason (for example, the task is temporarily placed into the Suspended state) causing the task to miss one or more periodic executions. • This can be detected by checking the variable passed by reference as the pxPreviousWakeTime parameter against the current tick count. This is however not necessary under most usage scenarios. • The constant portTICK_PERIOD_MS can be used to calculate real time from the tick rate - with the resolution of one tick period. vTaskDelayUntil()
  • 40. 3 Accurate Tasks Periods • vTaskDelayUntil() is similar to vTaskDelay() “but” • vTaskDelay() parameter specifies the number of tick interrupts that should occur between a task calling vTaskDelay() and the same task once again transitioning out of the Blocked state. • The length of time the task remains in the blocked state is specified by the vTaskDelay() parameter, but the actual time at which the task leaves the blocked state is relative to the time at which vTaskDelay() was called. • The parameters to vTaskDelayUntil() specify, instead, the exact tick count value at which the calling task should be moved from the Blocked state into the Ready state. • vTaskDelayUntil() is the API function that should be used when a fixed execution period is required (where you want your task to execute periodically with a fixed frequency), as the time at which the calling task is unblocked is absolute, rather than relative to when the function was called (as is the case with vTaskDelay()). • vTaskDelayUntil() API function is available only when INCLUDE_vTaskDelayUntil is set to 1 in FreeRTOSConfig.h.
  • 42. 5 Example 6: Continuous & Periodic Tasks
  • 43. 5 Example 6: Continuous & Periodic Tasks
  • 44. 6 Example 6: Continuous & Periodic Tasks
  • 45. 9 Priorities & Preemption • 1_three_tasks_same_priority • 2_three_tasks_different_priority • 3_three_tasks_preemption_delete_task (Example 9)
  • 46. 2 The Idle Task and the Idle Task Hook (Idle Task CallBack) • An Idle task is automatically created by the scheduler when vTaskStartScheduler() is called. • The idle task does very little more than sit in a loop • The idle task has the lowest possible priority (priority zero), to ensure it never prevents a higher priority application task from entering the Running state • Idle task is always pre-empted by higher priority tasks • idle hook (or idle callback) function—a function that is called automatically by the idle task once per iteration of the idle task loop • Common uses for the Idle task hook include: o Executing low priority, background, or continuous processing. o Measuring the amount of spare processing capacity. o Placing the processor into a low power mode • configUSE_IDLE_HOOK must be set to 1 within FreeRTOSConfig.h for the idle hook function to get called.
  • 47. 3 Example 7: Idle Task CallBack
  • 48. 4 Example 7: Idle Task CallBack
  • 49. 5 Example 7: Idle Task CallBack How many times IdelTask is called between tasks calls? For how long the CPU is into Idle Task?
  • 50. Real Time Operating System “FreeRTOS” Change Task Priority Using the FreeRTOS Real Time Kernel - a Practical Guide - Cortex M3 Edition (FreeRTOS Tutorial Books) by Richard Barry Sherif Hammad
  • 51. Real Time Operating System “FreeRTOS” Change Task Priority Using the FreeRTOS Real Time Kernel - a Practical Guide - Cortex M3 Edition (FreeRTOS Tutorial Books) by Richard Barry Sherif Hammad
  • 58. 7 Deleting a Task; Example 9
  • 59. 7 Deleting a Task; Example 9
  • 60. 8 Deleting a Task; Example 9 Task 2 Preempts 1 Task 1 Resumes
  • 61. 9 Priorities & Preemption • 1_three_tasks_same_priority • 2_three_tasks_different_priority • 3_three_tasks_preemption_delete_task (Example 9)
  • 62. 6 The Scheduling Algorithm—A Summary • Fixed Prioritized Pre­emptive Scheduling o Each task is assigned a priority. o Each task can exist in one of several states. o Only one task can exist in the Running state at any one time. o The scheduler always selects the highest priority Ready state task to enter the Running state. • Fixed Priority’ because each task is assigned a priority that is not altered by the kernel itself (only tasks can change priorities); • ‘Pre­emptive’ because a task entering the Ready state or having its priority altered will always pre­empt the Running state task, if the Running state task has a lower priority.