📖 Scenario: You are learning how FreeRTOS works inside an embedded system. Understanding its architecture helps you write better multitasking programs.
🎯 Goal: Build a simple FreeRTOS program that creates tasks and uses the basic architecture components like tasks, scheduler, and queues.
📋 What You'll Learn
Create task functions with exact names
Create task handles with exact names
Use FreeRTOS API functions with correct parameters
Print output to show task execution order
💡 Why This Matters
🌍 Real World
FreeRTOS is used in embedded devices like IoT gadgets, wearables, and industrial controllers to manage multiple tasks efficiently.
💼 Career
Understanding FreeRTOS architecture is essential for embedded software engineers working on real-time systems and multitasking applications.
Progress0 / 4 steps
1
Create two FreeRTOS tasks
Create two task functions called Task1 and Task2 that each take a void *pvParameters argument. Also, declare two task handles called Task1Handle and Task2Handle initialized to NULL.
FreeRTOS
Hint
Define two functions with the exact names and parameters. Declare two task handles as TaskHandle_t variables set to NULL.
2
Create tasks in main and start scheduler
In the main function, create the two tasks using xTaskCreate with the exact names Task1 and Task2, stack size 100, priority 1, and store their handles in Task1Handle and Task2Handle. Then start the scheduler with vTaskStartScheduler().
FreeRTOS
Hint
Use xTaskCreate to create each task with the exact parameters. Then call vTaskStartScheduler() to start running tasks.
3
Add task code to print messages
Inside the infinite loop of Task1 and Task2, add code to print "Task1 is running" and "Task2 is running" respectively using printf. Add a call to vTaskDelay(1000 / portTICK_PERIOD_MS) after each print to delay for 1 second.
FreeRTOS
Hint
Use printf to show the task running message. Use vTaskDelay to pause the task for 1 second.
4
Run and observe task output
Run the program and observe the output. It should print alternating lines "Task1 is running" and "Task2 is running" every second.
FreeRTOS
Hint
Run the program on your FreeRTOS environment or simulator. You should see the two messages printed repeatedly every second.
Practice
(1/5)
1. What is the main role of the FreeRTOS scheduler in its architecture?
easy
A. To store data permanently
B. To decide which task runs and when
C. To compile the code
D. To manage hardware interrupts only
Solution
Step 1: Understand the scheduler's purpose
The scheduler controls task execution order and timing in FreeRTOS.
Step 2: Compare with other options
Storing data, compiling code, or only managing interrupts are not scheduler roles.
Final Answer:
To decide which task runs and when -> Option B
Quick Check:
Scheduler controls task timing = To decide which task runs and when [OK]
Hint: Scheduler manages task timing and switching [OK]
Common Mistakes:
Confusing scheduler with memory storage
Thinking scheduler compiles code
Assuming scheduler only handles interrupts
2. Which of the following is the correct way to create a task in FreeRTOS?
easy
A. task_create(TaskFunction, StackSize);
B. CreateTask(TaskFunction, Priority);
C. xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle);
D. StartTask(TaskFunction, "TaskName");
Solution
Step 1: Recall FreeRTOS task creation syntax
The correct function is xTaskCreate with parameters: function, name, stack size, parameters, priority, and handle.
Step 2: Check options for exact match
Only xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle); matches the correct FreeRTOS API call syntax.
Final Answer:
xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle); -> Option C
Quick Check:
Use xTaskCreate with full parameters = xTaskCreate(TaskFunction, "TaskName", StackSize, NULL, Priority, &TaskHandle); [OK]
Hint: Count xTaskCreate parameters; must be six [OK]
Common Mistakes:
Omitting task handle parameter
Misunderstanding infinite loops in tasks
Incorrect order of scheduler start
5. You want to design a FreeRTOS system where a high-priority task waits for data from a low-priority task without wasting CPU time. Which FreeRTOS feature best supports this design?
hard
A. Running both tasks at the same priority to share CPU equally
B. Using vTaskDelay in the high-priority task to wait
C. Using global variables with busy-wait loops
D. Using a queue to send data and block the high-priority task until data arrives
Solution
Step 1: Understand task synchronization needs
The high-priority task must wait efficiently without wasting CPU cycles.
Step 2: Evaluate FreeRTOS features
Queues allow blocking the high-priority task until data is available, avoiding busy-waiting.