W5.1 Task Management on FreeRTOS
W5.1 Task Management on FreeRTOS
FreeRTOS
▪ 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
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 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
▪ 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.