0
0
FreeRTOSprogramming~10 mins

Task handle usage in FreeRTOS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task handle usage
Create Task
Store Task Handle
Use Handle to Control Task
Delete or Suspend Task Using Handle
End
Create a task and save its handle to control it later, like suspending or deleting.
Execution Sample
FreeRTOS
TaskHandle_t xHandle = NULL;

void vTaskCode(void * pvParameters) {
    for(;;) {
        // Task work here
    }
}

xTaskCreate(vTaskCode, "Task1", 1000, NULL, 1, &xHandle);
Creates a task and saves its handle in xHandle for later control.
Execution Table
StepActionVariable/Handle StateResult/Output
1Declare xHandle as NULLxHandle = NULLNo task created yet
2Define task function vTaskCodeNo changeTask code ready
3Call xTaskCreate with &xHandlexHandle assigned task handleTask created and handle stored
4Task runs in backgroundxHandle validTask executes loop
5Use xHandle to suspend taskxHandle unchangedTask suspended
6Use xHandle to delete taskxHandle unchangedTask deleted
7End of usagexHandle still holds handle valueHandle can be reused or set NULL
💡 Task handle allows control of the created task after creation
Variable Tracker
VariableStartAfter xTaskCreateAfter SuspendAfter Delete
xHandleNULLValid Task HandleValid Task HandleValid Task Handle (handle value remains)
Key Moments - 3 Insights
Why do we pass &xHandle to xTaskCreate instead of xHandle?
Because xTaskCreate needs to write the new task's handle back to xHandle, so we pass its address. See execution_table step 3.
Does the task handle change after suspending or deleting the task?
No, the handle variable keeps the same value until you change it manually. See execution_table steps 5 and 6.
Can we use the task handle before the task is created?
No, the handle is NULL before creation and only valid after xTaskCreate returns successfully. See execution_table steps 1 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of xHandle right after xTaskCreate is called?
ANULL
BUndefined
CValid Task Handle
D0
💡 Hint
Check execution_table row 3 under Variable/Handle State
At which step does the task start running its code?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look for when the task executes the loop in execution_table
If we never store the task handle, what can we NOT do later?
ACreate the task
BSuspend or delete the task
CRun the task code
DDefine the task function
💡 Hint
Refer to concept_flow and key_moments about handle usage for control
Concept Snapshot
Task Handle Usage in FreeRTOS:
- Declare TaskHandle_t variable (e.g., xHandle)
- Pass &xHandle to xTaskCreate to get handle
- Use handle to suspend, resume, or delete task
- Handle remains valid until task is deleted
- Handle is NULL before task creation
Full Transcript
In FreeRTOS, when you create a task, you can save its handle in a variable. This handle lets you control the task later, like pausing or deleting it. First, declare a TaskHandle_t variable and set it to NULL. Then, when calling xTaskCreate, pass the address of this variable so FreeRTOS can store the new task's handle there. After creation, the handle is valid and you can use it to suspend or delete the task. The handle value stays the same even after suspending or deleting the task until you change it manually. Without storing the handle, you cannot control the task after creation.