📖 Scenario: You are working on a simple embedded system using FreeRTOS. Your task is to set up the tick timer and create a basic scheduler that switches between two tasks periodically.
🎯 Goal: Build a FreeRTOS program that initializes the tick timer, creates two tasks, and uses the scheduler to switch between them, printing messages to show task execution.
📋 What You'll Learn
Create two tasks named Task1 and Task2
Set up the FreeRTOS tick timer with a tick rate of 1000 Hz
Use the FreeRTOS scheduler to switch between the tasks
Each task should print a message indicating it is running
Run the scheduler to start task switching
💡 Why This Matters
🌍 Real World
Embedded systems often use FreeRTOS to manage multiple tasks like sensor reading, communication, and control loops efficiently.
💼 Career
Understanding FreeRTOS tick timer and scheduler is essential for embedded software developers working on real-time applications.
Progress0 / 4 steps
1
Create two FreeRTOS tasks
Write code to create two tasks named Task1 and Task2 using xTaskCreate. Each task should run a function that prints a unique message in an infinite loop with a delay.
FreeRTOS
Hint
Use xTaskCreate to create each task with the function name, a name string, stack size, parameters as NULL, priority 1, and no task handle.
2
Configure the FreeRTOS tick timer rate
Add a macro definition to set the FreeRTOS tick rate to 1000 Hz by defining configTICK_RATE_HZ as 1000 before including FreeRTOS headers.
FreeRTOS
Hint
Define configTICK_RATE_HZ before including FreeRTOS headers to set the tick timer frequency.
3
Start the FreeRTOS scheduler
Add a call to vTaskStartScheduler() in main() after creating the tasks to start the FreeRTOS scheduler and begin task switching.
FreeRTOS
Hint
Call vTaskStartScheduler() after creating tasks to start the FreeRTOS scheduler.
4
Run and observe task switching output
Run the program and observe the output. It should print alternating messages from Task 1 is running and Task 2 is running every second.
FreeRTOS
Hint
Run the program on a FreeRTOS-supported environment or simulator to see the alternating task messages every second.
Practice
(1/5)
1. What is the main role of the tick timer in FreeRTOS?
easy
A. To handle input/output operations
B. To store task priorities
C. To allocate memory for tasks
D. To count time in small steps called ticks
Solution
Step 1: Understand tick timer function
The tick timer counts time in small units called ticks, which are the basic time measurement in FreeRTOS.
Step 2: Differentiate from other functions
Other options like storing priorities or handling I/O are not related to the tick timer's role.
Final Answer:
To count time in small steps called ticks -> Option D
Quick Check:
Tick timer = counts ticks [OK]
Hint: Tick timer counts time steps, not priorities or memory [OK]
Common Mistakes:
Confusing tick timer with task priority management
Thinking tick timer handles memory or I/O
Mixing tick timer with scheduler directly
2. Which of the following is the correct way to delay a task for 100 ticks in FreeRTOS?
easy
A. xTaskDelay(100);
B. vTaskDelay(1000);
C. vTaskDelay(100);
D. delayTask(100);
Solution
Step 1: Identify correct function name
The correct FreeRTOS function to delay a task is vTaskDelay().
Step 2: Check parameter correctness
The parameter is the number of ticks to delay. For 100 ticks, vTaskDelay(100); is correct.
Final Answer:
vTaskDelay(100); -> Option C
Quick Check:
Delay task = vTaskDelay(ticks) [OK]
Hint: Use vTaskDelay with tick count to pause tasks [OK]
Common Mistakes:
Using wrong function name like xTaskDelay or delayTask
Passing milliseconds instead of ticks
Using too large or incorrect delay values
3. Consider this code snippet in FreeRTOS:
TickType_t start = xTaskGetTickCount();
vTaskDelay(50);
TickType_t end = xTaskGetTickCount();
TickType_t diff = end - start;
What is the expected value of diff?
medium
A. 0
B. 50
C. 100
D. Depends on task priority
Solution
Step 1: Understand xTaskGetTickCount()
This function returns the current tick count since the scheduler started.
Step 2: Analyze vTaskDelay(50)
The task delays itself for 50 ticks, so the tick count advances by 50 during the delay.
Step 3: Calculate difference
Subtracting start from end gives the delay duration, which should be 50 ticks.
Final Answer:
50 -> Option B
Quick Check:
Delay ticks = 50 [OK]
Hint: Delay of 50 ticks means tick count difference is 50 [OK]