📖 Scenario: You are working on a simple embedded system using FreeRTOS. You want to create a task that blinks an LED repeatedly.
🎯 Goal: Learn how to use the xTaskCreate() function to create a FreeRTOS task that runs a simple LED blinking function.
📋 What You'll Learn
Create a task function called vBlinkTask that toggles an LED.
Create a task handle variable called xBlinkTaskHandle.
Use xTaskCreate() to create the vBlinkTask task.
Start the FreeRTOS scheduler with vTaskStartScheduler().
Print a message inside the task to indicate it is running.
💡 Why This Matters
🌍 Real World
Creating tasks is essential in embedded systems to run multiple functions like sensor reading, communication, and control in parallel.
💼 Career
Understanding <code>xTaskCreate()</code> is fundamental for embedded software engineers working with FreeRTOS to build responsive and efficient applications.
Progress0 / 4 steps
1
Define the task function vBlinkTask
Write a task function called vBlinkTask that takes a void *pvParameters argument 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 run an infinite loop.
2
Declare a task handle variable xBlinkTaskHandle
Declare a variable called xBlinkTaskHandle of type TaskHandle_t and initialize it to NULL.
FreeRTOS
Hint
Task handles are used to reference tasks after creation. Initialize it to NULL.
3
Create the task using xTaskCreate()
Use xTaskCreate() to create the task vBlinkTask with the name "BlinkTask", stack size configMINIMAL_STACK_SIZE, no parameters (NULL), priority 1, and store the handle in &xBlinkTaskHandle.
FreeRTOS
Hint
The xTaskCreate() function creates a task. Pass the function name, task name, stack size, parameters, priority, and handle pointer.
4
Start the scheduler and print inside the task
Add a printf("Blink task running\n") inside the infinite loop of vBlinkTask. Then call vTaskStartScheduler() after creating the task.
FreeRTOS
Hint
Use printf to show the task is running. Then start the FreeRTOS scheduler with vTaskStartScheduler().
Practice
(1/5)
1. What is the main purpose of the xTaskCreate() function in FreeRTOS?
easy
A. To start a new task that runs concurrently with others
B. To delete an existing task
C. To pause the scheduler temporarily
D. To allocate memory for variables
Solution
Step 1: Understand the role of xTaskCreate()
This function is designed to create and start a new task in FreeRTOS, allowing multitasking.
Step 2: Compare with other options
Deleting tasks, pausing the scheduler, or allocating memory are handled by different functions, not xTaskCreate().
Final Answer:
To start a new task that runs concurrently with others -> Option A
Quick Check:
Task creation = start new task [OK]
Hint: xTaskCreate always starts a new task [OK]
Common Mistakes:
Confusing task creation with task deletion
Thinking it pauses the scheduler
Assuming it allocates general memory
2. Which of the following is the correct syntax for xTaskCreate() in FreeRTOS?
easy
A. xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority);
B. xTaskCreate(TaskFunction, Name, StackSize, Parameters, Priority, TaskHandle);
C. xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, TaskHandle);
D. xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle);
Solution
Step 1: Recall the correct parameter list
The correct syntax requires the task function, a string name in quotes, stack size, parameters, priority, and a pointer to the task handle.
Step 2: Check each option carefully
xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle); matches the correct syntax exactly, including quotes around the name and the address-of operator for the task handle.
Final Answer:
xTaskCreate(TaskFunction, "Name", StackSize, Parameters, Priority, &TaskHandle); -> Option D
Quick Check:
Correct syntax includes &TaskHandle [OK]
Hint: Task name must be a string and handle passed by address [OK]
The last parameter must be a pointer to the task handle variable, so it requires the address operator &.
Step 2: Verify other parameters
The task name is correctly a string in quotes, stack size 500 is valid, and priority 1 is allowed.
Final Answer:
Missing & before xHandle in last parameter -> Option C
Quick Check:
Task handle needs & operator [OK]
Hint: Always pass &TaskHandle, not TaskHandle alone [OK]
Common Mistakes:
Passing task handle without &
Removing quotes from task name
Incorrect assumptions about stack size or priority
5. You want to create two tasks with different priorities using xTaskCreate(). Which code snippet correctly creates both tasks and stores their handles?
Each task handle must be passed by address using &. TaskHandle_t handle1, handle2;
xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1);
xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); correctly does this for both handles.
Step 2: Check task names and priorities
Task names are strings in quotes. Priorities differ (3 and 5) as required. TaskHandle_t handle1, handle2;
xTaskCreate(Task1, "Task1", 1000, NULL, 3, &handle1);
xTaskCreate(Task2, "Task2", 1000, NULL, 5, &handle2); matches this correctly.