0% found this document useful (0 votes)
18 views

W5.1 Task Management on FreeRTOS

FreeRTOS is an open-source real-time operating system designed for embedded systems, featuring task management through independent threads, preemptive or cooperative scheduling, and various synchronization mechanisms. Tasks are implemented as functions that run indefinitely unless terminated, and they can be created, modified, or deleted while managing their own unique stack. The system also includes timer interfaces for managing task delays and states, as well as handling interrupts with higher priority than tasks.

Uploaded by

Andrew Koh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

W5.1 Task Management on FreeRTOS

FreeRTOS is an open-source real-time operating system designed for embedded systems, featuring task management through independent threads, preemptive or cooperative scheduling, and various synchronization mechanisms. Tasks are implemented as functions that run indefinitely unless terminated, and they can be created, modified, or deleted while managing their own unique stack. The system also includes timer interfaces for managing task delays and states, as well as handling interrupts with higher priority than tasks.

Uploaded by

Andrew Koh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Task Management on FreeRTOS

FreeRTOS

FreeRTOS (http://www.freertos.org/) is a typical embedded operating system.


It is available for many hardware platforms, open source and widely used in industry.
• FreeRTOS is a real‐time kernel (or real‐time scheduler).
• Applications are organized as a collection of independent threads of execution.
• Characteristics: Pre‐emptive or co‐operative operation, queues, binary semaphores,
counting semaphores, mutexes (mutual exclusion), software timers, stack overflow
checking, trace recording, … .
FreeRTOS

• Typical directory structure:

• FreeRTOS is configured by a header file called FreeRTOSConfig.h that determines


almost all configurations (co‐operative scheduling vs. preemptive, time‐slicing, heap size,
mutex, semaphores, priority levels, timers, …)
FreeRTOS – Task Management

Tasks are implemented as threads.


▪ The functionality of a thread is implemented in form of a function:
▪ Prototype:

some name of task function pointer to task arguments

▪ Task functions are not allowed to return! They can be “terminated” by a specific call
to a FreeRTOS function, but usually run forever in an infinite loop.
▪ Task functions can instantiate other tasks. Each created task is a separate
execution instance, with its own stack.
▪ Example: void vTask1( void *pvParameters ) {
volatile uint32_t ul; /* volatile to ensure ul is implemented. */
for( ;; ) {
... /* do something repeatedly */
for( ul = 0; ul < 10000; ul++ ) { /* delay by busy loop */ }
}
}
FreeRTOS – Task Management
a pointer to the function
▪ Thread instantiation:
that implements the task

a descriptive name for the


task
returns pdPASS or pdFAIL
depending on the success each task has its own unique
of the thread creation stack that is allocated by the
kernel to the task when the
task is created; the
the priority at which the
usStackDepth value
task will execute; priority 0
determines the size of the
is the lowest priority
stack (in words)
pxCreatedTask can be task functions accept a
used to pass out a handle parameter of type pointer to
to the task being created. void; the value assigned to
pvParameters is the value passed
into the task.
FreeRTOS – Task Management

Examples for changing properties of tasks:


▪ Changing the priority of a task. In case of preemptive scheduling policy, the ready
task with the highest priority is automatically assigned to the “running” state.

handle of the task whose priority is being modified new priority (0 is lowest priority)

▪ A task can delete itself or any other task. Deleted tasks no longer exist and cannot
enter the “running” state again.

handle of the task who will be deleted; if NULL, then the caller will be deleted
FreeRTOS – Timers

▪ The operating system also provides interfaces to timers of the processor.


▪ As an example, we use the FreeRTOS timer interface to replace the busy loop by
a delay. In this case, the task is put into the “blocked” state instead of
continuously running.

time is measured in “tick” units that are defined in the


configuration of FreeRTOS (FreeRTOSConfig.h). The
function pdMS_TO_TICKS()converts ms to “ticks”.

void vTask1( void *pvParameters )


{ for( ;; ) {
... /* do something repeatedly */
vTaskDelay(pdMS_TO_TICKS(250)); /* delay by 250 ms
*/
}
}
FreeRTOS – Timers

▪ Problem: The task does not execute strictly periodically:


execution of “something” task in ready state again
t
task moved to ready state wait 250ms

▪ The parameters to vTaskDelayUntil() specify the exact tick count value at which
the calling task should be moved from the “blocked” state into the “ready” state.
Therefore, the task is put into the “ready” state periodically.
void vTask1( void *pvParameters ) { The xLastWakeTime variable needs to
TickType_t xLastWakeTime = be initialized with the current tick
xTaskGetTickCount(); for( ;; ) {
count. Note that this is the only time
... /* do something repeatedly */
the variable is written to explicitly.
vTaskDelayUntil(&xLastWakeTime,
} pdMS_TO_TICKS(250));
After this xLastWakeTime is
}
automatically updated within
vTaskDelayUntil().
automatically updated when task is unblocked time to next unblocking
FreeRTOS – Task States

What are the task states in FreeRTOS and the corresponding transitions?
5
▪ 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:
▪ Temporal (time‐related) events—the event
being either a delay period expiring, or an
absolute time being reached.
▪ Synchronization events—where the events
originate from another task or interrupt. For
example, queues, semaphores, and mutexes, can
be used to create synchronization events.
FreeRTOS – Task States

Example 1: Two threads with equal priority.


void vTask1( void *pvParameters ) void vTask2( void *pvParameters )
{ volatile uint32_t ul; { volatile uint32_t u2;
for( ;; ) { for( ;; ) {
... /* do something repeatedly */ ... /* do something repeatedly */
for( ul = 0; ul < 10000; ul++ ) { for( u2 = 0; u2 < 10000; u2++ ) {
} } } }
} }

int main( void ) {


xTaskCreate(vTask1, "Task 1", 1000, NULL, 1, NULL);
xTaskCreate(vTask2, "Task 2", 1000, NULL, 1, NULL);
vTaskStartScheduler();
for( ;; );
}

Both tasks have priority 1. In this case,


FreeRTOS uses time slicing, i.e., every
task is put into “running” state in turn.
FreeRTOS – Task States

Example 2: Two threads with delay timer.


void vTask1( void *pvParameters ) { int main( void ) {
TickType_t xLastWakeTime = xTaskGetTickCount(); xTaskCreate(vTask1,"Task 1",1000,NULL,1,NULL);
for( ;; ) { xTaskCreate(vTask2,"Task 2",1000,NULL,2,NULL);
... /* do something repeatedly */ vTaskStartScheduler();
vTaskDelayUntil(&xLastWakeTime,pdMS_TO_TICKS(250)); for( ;; );
} }
}

void vTask2( void *pvParameters ) {


TickType_t xLastWakeTime = xTaskGetTickCount();
for( ;; ) {
... /* do something repeatedly */
vTaskDelayUntil(&xLastWakeTime,pdMS_TO_TICKS(250));
}
}

If no user‐defined task is in the running state,


FreeRTOS chooses a built‐in Idle task with priority
0. One can associate a function to this task, e.g.,
in order to go to low power processor state.
FreeRTOS – Interrupts

How are tasks (threads) and hardware interrupts scheduled jointly?


▪ Although written in software, an interrupt service routine (ISR) is a hardware
feature because the hardware controls which interrupt service routine will run,
and when it will run.
▪ Tasks will only run when there are no ISRs running, so the lowest priority interrupt
will interrupt the highest priority task, and there is no way for a task to pre‐empt
an ISR. In other words, ISRs have always a higher priority than any other task.

▪ Usual pattern:
▪ ISRs are usually very short. They find out the reason for the interrupt, clear the
interrupt flag and determine what to do in order to handle the interrupt.
▪ Then, they unblock a regular task (thread) that performs the necessary processing
related to the interrupt.
▪ For blocking and unblocking, usually semaphores are used.
FreeRTOS – Interrupts

blocking and
unblocking is
typically
implemented
via semaphores
Please select ALL of t he Operating Systems you are
considering using in the next 12 months.

2019 Embedded Markets Study © 2019 Copyright by AspenCore. All rights reserved.

You might also like