📖 Scenario: You are working on a small embedded device that controls a home automation system. It needs to handle urgent tasks like turning off the heater immediately when the temperature is too high, and also less urgent tasks like updating the display every few seconds.This project will help you understand how a real-time operating system (RTOS) like FreeRTOS schedules tasks differently from a general-purpose OS.
🎯 Goal: You will create two tasks in FreeRTOS: one high-priority task that simulates an urgent sensor check, and one low-priority task that simulates a display update. You will see how the RTOS runs the urgent task immediately when needed, unlike a general-purpose OS that might delay it.
📋 What You'll Learn
Create two FreeRTOS tasks with different priorities
Use vTaskDelay to simulate task timing
Print messages to show which task is running
Observe task switching behavior
💡 Why This Matters
🌍 Real World
Embedded systems like home automation, robotics, and automotive use real-time OS to handle urgent tasks reliably.
💼 Career
Understanding RTOS task scheduling is essential for embedded software engineers and developers working on time-critical applications.
Progress0 / 4 steps
1
Create two FreeRTOS tasks
Write code to create two tasks named UrgentTask and DisplayTask using xTaskCreate. Use the function prototypes void UrgentTask(void *pvParameters) and void DisplayTask(void *pvParameters). Do not start the scheduler yet.
FreeRTOS
Hint
Use xTaskCreate to create tasks with their function names and priorities.
2
Add task delays and print messages
Inside UrgentTask, add a printf statement to print "Urgent task running" before vTaskDelay(pdMS_TO_TICKS(1000)). Inside DisplayTask, add a printf statement to print "Display task running" before vTaskDelay(pdMS_TO_TICKS(2000)).
FreeRTOS
Hint
Use printf before the delay in each task to show which task runs.
3
Start the FreeRTOS scheduler
In the main function, after creating the tasks, add the call to vTaskStartScheduler() to start the RTOS scheduler.
FreeRTOS
Hint
Call vTaskStartScheduler() to begin running the tasks.
4
Observe and print the task switching output
Run the program and observe the printed output. Write a printf statement after vTaskStartScheduler() in main to print "Scheduler ended". Note that this line should never run if the scheduler works correctly. The expected output should show alternating messages from UrgentTask and DisplayTask.
FreeRTOS
Hint
The scheduler runs tasks repeatedly. You should see both messages printed repeatedly. The line "Scheduler ended" should not appear unless the scheduler stops.
Practice
(1/5)
1. Which of the following best describes a real-time operating system (RTOS) like FreeRTOS?
easy
A. It responds quickly and predictably to events.
B. It is designed mainly for running desktop applications.
C. It delays tasks unpredictably to save power.
D. It only runs one task at a time without multitasking.
Solution
Step 1: Understand RTOS purpose
RTOS is designed to respond quickly and predictably to events, ensuring timing is guaranteed.
Step 2: Compare options
Options B, C, and D describe general-purpose OS or incorrect behavior.
Final Answer:
It responds quickly and predictably to events. -> Option A
Quick Check:
RTOS = predictable quick response [OK]
Hint: RTOS means fast and predictable response to events [OK]
Common Mistakes:
Confusing RTOS with general-purpose OS
Thinking RTOS delays tasks unpredictably
Assuming RTOS runs only one task
2. Which syntax correctly creates a FreeRTOS task in C?
easy
A. createTask(TaskFunction, "Task1", 1000, NULL, 1, NULL);
B. xTaskCreate(TaskFunction, "Task1", 1000, NULL, 1, NULL);
C. xTaskStart(TaskFunction, "Task1", 1000, NULL, 1, NULL);
D. TaskCreate(xTask, "Task1", 1000, NULL, 1, NULL);
Solution
Step 1: Recall FreeRTOS task creation function
The correct function to create a task is xTaskCreate with parameters: function, name, stack size, parameters, priority, and handle.
Step 2: Check each option
Only xTaskCreate(TaskFunction, "Task1", 1000, NULL, 1, NULL); uses the correct function name and parameter order. Others use wrong function names.
Final Answer:
xTaskCreate(TaskFunction, "Task1", 1000, NULL, 1, NULL); -> Option B