📖 Scenario: You are working on a FreeRTOS-based embedded system where multiple tasks run concurrently. Sometimes, you need to stop a task that is no longer needed to free system resources.
🎯 Goal: Learn how to create a task, then remove it safely using vTaskDelete().
📋 What You'll Learn
Create a task function that toggles an LED
Create the task using xTaskCreate()
Use vTaskDelete() to remove the task
Print messages to show task creation and deletion
💡 Why This Matters
🌍 Real World
In embedded systems, tasks that are no longer needed should be removed to save memory and CPU time. Using <code>vTaskDelete()</code> helps manage system resources efficiently.
💼 Career
Understanding task creation and deletion is essential for embedded software developers working with FreeRTOS or similar real-time operating systems.
Progress0 / 4 steps
1
Create a task function called vLEDTask
Write a task function called vLEDTask that takes a void *pvParameters parameter and contains an infinite loop with a comment // Toggle LED here inside the loop.
FreeRTOS
Hint
Task functions in FreeRTOS have the form void TaskName(void *pvParameters) and usually run an infinite loop.
2
Create the task using xTaskCreate()
Use xTaskCreate() to create the task vLEDTask with the name "LED Task", stack size configMINIMAL_STACK_SIZE, no parameters (NULL), priority 1, and store the task handle in a variable called xLEDTaskHandle.
FreeRTOS
Hint
Remember to pass the address of the task handle variable to xTaskCreate() to store the created task's handle.
3
Delete the task using vTaskDelete()
Use vTaskDelete() to delete the task using the handle stored in xLEDTaskHandle.
FreeRTOS
Hint
Pass the task handle to vTaskDelete() to remove the task.
4
Print messages to show task creation and deletion
Add printf() statements to print "Task created" after creating the task and "Task deleted" after deleting the task.
FreeRTOS
Hint
Use printf() before and after the task creation and deletion calls to show messages.
Practice
(1/5)
1. What does the FreeRTOS function vTaskDelete() do?
easy
A. It removes a task from the scheduler and frees its resources.
B. It pauses a task temporarily without deleting it.
C. It creates a new task and adds it to the scheduler.
D. It suspends all tasks except the idle task.
Solution
Step 1: Understand the purpose of vTaskDelete()
This function is designed to remove a task from FreeRTOS and free the memory and resources it used.
Step 2: Compare with other task functions
Functions like suspending or pausing do not remove the task or free resources, only vTaskDelete() does that.
Final Answer:
It removes a task from the scheduler and frees its resources. -> Option A
Quick Check:
vTaskDelete() = Remove task and free resources [OK]
Hint: vTaskDelete() means task removal and resource freeing [OK]
Common Mistakes:
Confusing deletion with suspension or pausing
Thinking it creates or starts tasks
Assuming it only stops task temporarily
2. Which of the following is the correct syntax to delete the currently running task in FreeRTOS?
easy
A. vTaskDelete(NULL);
B. vTaskDelete(xTaskHandle);
C. vTaskDelete();
D. vTaskDelete(currentTask);
Solution
Step 1: Recall how to delete the current task
Passing NULL to vTaskDelete() tells FreeRTOS to delete the task that is currently running.
Step 2: Check other options
Options A and D use variables that may not be defined or correct. vTaskDelete(); misses the required parameter.
Final Answer:
vTaskDelete(NULL); -> Option A
Quick Check:
Delete current task = vTaskDelete(NULL) [OK]
Hint: Use NULL to delete the running task in vTaskDelete() [OK]
Common Mistakes:
Omitting the parameter in vTaskDelete()
Passing an undefined task handle
Using variable names instead of NULL for current task