Running multiple tasks at the same time lets your program do many things together. This makes your device more efficient and responsive.
Multiple tasks running concurrently in FreeRTOS
Introduction
Syntax
FreeRTOS
void Task1(void *pvParameters) {
for(;;) {
// Task1 code here
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void Task2(void *pvParameters) {
for(;;) {
// Task2 code here
vTaskDelay(pdMS_TO_TICKS(500));
}
}
int main(void) {
xTaskCreate(Task1, "Task 1", 1000, NULL, 1, NULL);
xTaskCreate(Task2, "Task 2", 1000, NULL, 1, NULL);
vTaskStartScheduler();
for(;;);
}Each task is a function that runs in an infinite loop.
vTaskDelay pauses the task for a set time, letting other tasks run.
Examples
FreeRTOS
void BlinkLED(void *pvParameters) {
for(;;) {
// Toggle LED
vTaskDelay(pdMS_TO_TICKS(500));
}
}
void ReadSensor(void *pvParameters) {
for(;;) {
// Read sensor data
vTaskDelay(pdMS_TO_TICKS(1000));
}
}FreeRTOS
xTaskCreate(BlinkLED, "LED Task", 1000, NULL, 2, NULL); xTaskCreate(ReadSensor, "Sensor Task", 1000, NULL, 1, NULL);
Sample Program
This program creates two tasks that print messages at different intervals. Task 1 prints every 1 second, Task 2 every 0.5 seconds. They run together, showing multitasking.
FreeRTOS
#include "FreeRTOS.h" #include "task.h" #include <stdio.h> void Task1(void *pvParameters) { for(;;) { printf("Task 1 is running\n"); vTaskDelay(pdMS_TO_TICKS(1000)); } } void Task2(void *pvParameters) { for(;;) { printf("Task 2 is running\n"); vTaskDelay(pdMS_TO_TICKS(500)); } } int main(void) { xTaskCreate(Task1, "Task 1", 1000, NULL, 1, NULL); xTaskCreate(Task2, "Task 2", 1000, NULL, 1, NULL); vTaskStartScheduler(); for(;;); }
Important Notes
FreeRTOS switches between tasks quickly to make them run together.
Use vTaskDelay to let other tasks run and avoid blocking.
Task priorities help decide which task runs first if both are ready.
Summary
Multiple tasks let your program do many things at once.
Each task runs in a loop and uses vTaskDelay to pause.
FreeRTOS manages task switching automatically.
Practice
1. What is the main purpose of running multiple tasks concurrently in FreeRTOS?
easy
Solution
Step 1: Understand task concurrency
Multiple tasks let the program handle different jobs at once, improving efficiency.Step 2: Identify the purpose in FreeRTOS
FreeRTOS switches between tasks automatically, enabling multitasking.Final Answer:
To allow the program to perform several operations at the same time -> Option AQuick Check:
Multiple tasks = multitasking = To allow the program to perform several operations at the same time [OK]
Hint: Multiple tasks mean doing many things at once [OK]
Common Mistakes:
- Thinking tasks slow down the program
- Believing tasks waste memory
- Ignoring the role of delays
2. Which of the following is the correct way to create a task in FreeRTOS?
easy
Solution
Step 1: Recall FreeRTOS task creation function
The correct function to create a task is xTaskCreate.Step 2: Match the exact syntax
xTaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL); matches the correct function name and parameters.Final Answer:
xTaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL); -> Option CQuick Check:
Task creation function = xTaskCreate = xTaskCreate(TaskFunction, "TaskName", stackSize, NULL, priority, NULL); [OK]
Hint: Remember the function starts with 'xTaskCreate' [OK]
Common Mistakes:
- Using incorrect function names like createTask
- Mixing up parameter order
- Missing the 'x' prefix in function name
3. Consider this FreeRTOS code snippet:
void Task1(void *pvParameters) {
for (;;) {
printf("Task1 running\n");
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
void Task2(void *pvParameters) {
for (;;) {
printf("Task2 running\n");
vTaskDelay(pdMS_TO_TICKS(500));
}
}
What will be the output pattern when both tasks run concurrently?medium
Solution
Step 1: Analyze vTaskDelay timing for Task1
Task1 delays 1000 ms (1 second) between prints.Step 2: Analyze vTaskDelay timing for Task2
Task2 delays 500 ms (0.5 seconds) between prints.Step 3: Understand concurrent execution
Both tasks run independently, so Task2 prints twice as often as Task1.Final Answer:
Task1 prints every 1 second; Task2 prints every 0.5 seconds -> Option AQuick Check:
Delays control print rate = Task1 prints every 1 second; Task2 prints every 0.5 seconds [OK]
Hint: Check each task's delay time to predict print frequency [OK]
Common Mistakes:
- Assuming tasks print simultaneously
- Mixing up delay times
- Thinking one task blocks the other
4. Given this code snippet, what is the error?
void TaskA(void *pvParameters) {
while(1) {
// Do something
vTaskDelay(1000);
}
}
xTaskCreate(TaskA, "TaskA", 1000, NULL, 1, NULL);medium
Solution
Step 1: Check vTaskDelay parameter
vTaskDelay expects delay in ticks, not milliseconds.Step 2: Identify correct delay usage
Use pdMS_TO_TICKS(1000) to convert 1000 ms to ticks.Final Answer:
vTaskDelay requires delay in ticks, not milliseconds -> Option BQuick Check:
Delay unit must be ticks = vTaskDelay requires delay in ticks, not milliseconds [OK]
Hint: Use pdMS_TO_TICKS() to convert ms to ticks for vTaskDelay [OK]
Common Mistakes:
- Passing milliseconds directly to vTaskDelay
- Wrong return type for task function
- Incorrect stack size assumptions
5. You want two tasks to print messages alternately every 500 ms without overlapping prints. Which approach ensures this behavior in FreeRTOS?
hard
Solution
Step 1: Understand the need for synchronization
To avoid overlapping prints, tasks must coordinate access to the printing resource.Step 2: Identify synchronization method
A binary semaphore allows one task to print while the other waits, alternating access.Step 3: Evaluate other options
Different delays or same priority without sync can cause overlapping prints; global variables alone don't prevent conflicts.Final Answer:
Use a binary semaphore to synchronize tasks before printing -> Option DQuick Check:
Synchronization via semaphore = Use a binary semaphore to synchronize tasks before printing [OK]
Hint: Use semaphores to control shared resource access [OK]
Common Mistakes:
- Relying only on delays for synchronization
- Ignoring task priority effects
- Not using any synchronization mechanism
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
