Task handles let you keep track of tasks you create in FreeRTOS. They help you control and communicate with tasks easily.
Task handle usage in FreeRTOS
TaskHandle_t xHandle = NULL;
xTaskCreate(
TaskFunction_t pvTaskCode,
const char * const pcName,
configSTACK_DEPTH_TYPE usStackDepth,
void *pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *pxCreatedTask
);The TaskHandle_t is a pointer type used to identify tasks.
Passing the address of a TaskHandle_t variable to xTaskCreate stores the created task's handle.
xTaskHandle.TaskHandle_t xTaskHandle = NULL;
xTaskCreate(
vTaskCode,
"Task1",
1000,
NULL,
1,
&xTaskHandle
);xTaskHandle.vTaskSuspend(xTaskHandle);
vTaskDelete(xTaskHandle);
This program creates a task and saves its handle. It runs the task printing a message every second. Then it suspends the task for 2 seconds, resumes it for 2 seconds, and finally deletes it.
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> TaskHandle_t xTaskHandle = NULL; void vTaskFunction(void *pvParameters) { for (;;) { printf("Task is running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } int main(void) { xTaskCreate( vTaskFunction, "MyTask", 1000, NULL, 1, &xTaskHandle ); vTaskDelay(pdMS_TO_TICKS(3000)); printf("Suspending task...\n"); vTaskSuspend(xTaskHandle); vTaskDelay(pdMS_TO_TICKS(2000)); printf("Resuming task...\n"); vTaskResume(xTaskHandle); vTaskDelay(pdMS_TO_TICKS(2000)); printf("Deleting task...\n"); vTaskDelete(xTaskHandle); for (;;) {} return 0; }
Always check if the task handle is not NULL before using it.
Deleting a task frees its resources, so use vTaskDelete carefully.
Task handles are useful for controlling tasks after creation.
Task handles identify tasks and let you control them.
Use task handles to suspend, resume, or delete tasks.
Store the handle when creating a task to use it later.
Practice
What is the main purpose of a task handle in FreeRTOS?
Solution
Step 1: Understand what a task handle represents
A task handle is a reference used to identify a task uniquely in FreeRTOS.Step 2: Identify the purpose of the handle
The handle allows you to control the task, such as suspending or deleting it.Final Answer:
To identify and control a specific task -> Option CQuick Check:
Task handle = task control reference [OK]
- Confusing handle with task priority
- Thinking handle allocates memory
- Mixing handle with stack size
Which of the following is the correct way to declare a task handle variable in FreeRTOS?
?
Solution
Step 1: Recall the FreeRTOS type for task handles
The correct type defined by FreeRTOS for task handles isTaskHandle_t.Step 2: Check the syntax of declaration
Declaring a variable asTaskHandle_t xTaskHandle;is the correct syntax.Final Answer:
TaskHandle_t xTaskHandle; -> Option AQuick Check:
Task handle type = TaskHandle_t [OK]
- Using incorrect type names
- Adding underscores wrongly
- Confusing with other handle types
What will be the output of the following code snippet?
TaskHandle_t xHandle = NULL;
void vTaskFunction(void *pvParameters) {
for (;;) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
int main() {
xTaskCreate(vTaskFunction, "Task1", 1000, NULL, 1, &xHandle);
if (xHandle != NULL) {
printf("Task created successfully\n");
} else {
printf("Task creation failed\n");
}
return 0;
}Solution
Step 1: Analyze task creation and handle assignment
The functionxTaskCreateassigns the created task's handle toxHandle. If successful,xHandleis not NULL.Step 2: Check the condition and output
The code checks ifxHandle != NULLand prints "Task created successfully" if true.Final Answer:
Task created successfully -> Option BQuick Check:
xHandle != NULL means success [OK]
- Assuming handle is NULL on success
- Ignoring handle assignment
- Confusing task creation return value
Identify the error in the following code snippet related to task handle usage:
TaskHandle_t xTaskHandle;
void vTaskFunction(void *pvParameters) {
for (;;) {
vTaskDelay(pdMS_TO_TICKS(500));
}
}
int main() {
xTaskCreate(vTaskFunction, "Task", 1000, NULL, 1, xTaskHandle);
vTaskSuspend(xTaskHandle);
return 0;
}Solution
Step 1: Check how task handle is passed to xTaskCreate
The last parameter ofxTaskCreateexpects a pointer to the task handle variable, so it should be&xTaskHandle.Step 2: Identify the error in the code
The code passesxTaskHandleinstead of its address, causing incorrect handle assignment.Final Answer:
xTaskCreate should pass &xTaskHandle, not xTaskHandle -> Option AQuick Check:
Pass handle address to xTaskCreate [OK]
- Passing handle instead of its address
- Assuming vTaskSuspend needs no handle
- Ignoring task priority parameter
You want to create two tasks and suspend the first one after creation using task handles. Which code snippet correctly achieves this?
void vTask1(void *pvParameters) { for(;;) {} }
void vTask2(void *pvParameters) { for(;;) {} }
int main() {
TaskHandle_t xHandle1 = NULL, xHandle2 = NULL;
// Create tasks here
// Suspend first task here
return 0;
}Solution
Step 1: Correctly create tasks with handles
Use&xHandle1and&xHandle2to store task handles during creation.Step 2: Suspend the first task using its handle
CallvTaskSuspend(xHandle1);to suspend the first task by passing the handle itself, not its address or NULL.Final Answer:
Correct creation with &xHandle and suspend with xHandle1 -> Option DQuick Check:
Create with &handle, suspend with handle [OK]
- Passing handle instead of &handle to xTaskCreate
- Passing &handle to vTaskSuspend instead of handle
- Suspending with NULL handle
Ready to prove it?
Pass the test and this pattern moves unknown
