Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

FreeRTOS - Tasks

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

FreeRTOS

Task Management
Task (Implementing) Function
Task States

“Not Running” is a “Super State”, encompassing multiple states


Creating a Task

• pvTaskCode – A pointer to the task function


• pcName – Descriptive task name used during profiling
• usStackDepth – Stack depth/size
• pvParameters – Pointer to task parameters
• uxPriority – Task priority
• pxCreatedTask – Returns a task “handle”

Returns success or failure


Example 1 – Task 1
Task 2
Creating the Tasks
Execution Behavior
Task Execution
Using the Task Parameters

Rather than duplicate the task function, use the


parameters to differentiate two instances
Example 2 – Task Parameters

Same behavior,
but less program
memory space
Task Priorities
• When one or more tasks of equal priority are
all “Ready”, the scheduler will cycle through
each one.
Task Priorities

• On the other hand, if one of the tasks is a


higher priority, it will always be selected …
Example 3 – Task Priority
Example 3 – Task Execution
Task Starvation
• Even though each task has a SW delay loop,
the task continues to “run” if it is in the
“Running” state, polling the SW variable.

• Thus, Task 1 is “starved” because Task 2 is a


higher priority and is always “Ready.”

• Need a way for Task 2 to “wait”, but not run …


“Not Running” Super State
• When “Not Running,” a task may be in one of
three sub-states:

• “Ready” – The task is ready to run if selected

• “Blocked” – The task is waiting for an event

• “Suspended” – The task enters and exits this


state only through API functions (rarely used)
Full Task State Machine
Tasks can enter the “Blocked” state to wait for
two types of events:

-Temporal Events, e.g. delaying for a fixed


amount or until an absolute time

-Synchronization Events, e.g., waiting for data

Synchronization events can come in many forms


and can be combined with delay events, e.g. wait
for data until a time period has elapsed.
Task Delay using vTaskDelay()
Example 4
Example 4 – Task Execution
The Idle Task
• Created when the scheduler starts

• Ensures “something” can always run

• Runs at the lowest priority (0)

• Later, we will add background processing to


the Idle Task
Mixing Blocking/Non-Blocking Tasks
• Previous example had polling of a software
variable or “blocking” for a fixed tick count

• Next example mixes “continuous” processing


(always “ready” or “running”) with blocking

• Two non-blocking tasks at priority 1 and a


blocking task at priority 2
Continuous Task - Example 6
Blocking Task – Example 6

vTaskDelayUntil() is better for accurate scheduling of periodic tasks. Why? 


Execution – Example 6
The Idle “Hook/Callback” Function
• Must be enabled in FreeRTOSConfig.h
• Must be named vApplicationIdleHook()
• No arguments, no return value
• Must allow Idle Task to run
– Must never block or suspend
– If a task is deleted (vTaskDelete) then the Idle
hook must return within “reasonable” time
– This allows the Idle Task to perform “cleanup”, i.e.,
release unneeded resources
Simple Idle Hook Function
Task Function
Output Result – Example 7

The Idle Hook function is called one per iteration of the Idle Task loop. The Idle Task
yields at the end of every loop iteration, changing its state to “Ready” and allowing
other tasks to run, even if they are the same priority. If all other tasks are blocked or
suspended, then the Idle Hook function can run multiple times in one tick interval.
Miscellaneous
• A task can be created from within another task
implementation function, after the scheduler has
run

• Task priority can be changed through the task


handle by another task, or by the task using a
NULL handle

• Similarly, can query to get a task priority, or


delete a task through API functions

You might also like