0
0
FreeRTOSprogramming~30 mins

Preemptive scheduling behavior in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Preemptive Scheduling Behavior in FreeRTOS
📖 Scenario: You are working on a simple embedded system using FreeRTOS. You want to understand how preemptive scheduling works by creating two tasks with different priorities. The higher priority task should preempt the lower priority task when it becomes ready.
🎯 Goal: Build a FreeRTOS program with two tasks: TaskHigh and TaskLow. TaskHigh has higher priority and preempts TaskLow. You will observe the order of task execution through printed messages.
📋 What You'll Learn
Create two tasks named TaskHigh and TaskLow
TaskHigh must have higher priority than TaskLow
Use vTaskDelay to simulate work in each task
Print messages to show when each task runs
Start the FreeRTOS scheduler to run the tasks
💡 Why This Matters
🌍 Real World
Embedded systems often use FreeRTOS to manage multiple tasks with different priorities, such as sensor reading, communication, and user interface.
💼 Career
Understanding preemptive scheduling is essential for embedded software engineers working with real-time operating systems to ensure timely and predictable task execution.
Progress0 / 4 steps
1
Create two tasks with different priorities
Write code to create two tasks named TaskHigh and TaskLow using xTaskCreate. Set TaskHigh priority to 2 and TaskLow priority to 1. Use empty task functions for now.
FreeRTOS
Need a hint?

Use xTaskCreate to create tasks. The priority is the fifth argument.

2
Add print statements to tasks
Add printf statements inside TaskHigh and TaskLow to print "High priority task running" and "Low priority task running" respectively. Keep the vTaskDelay(pdMS_TO_TICKS(1000)) calls to simulate work.
FreeRTOS
Need a hint?

Use printf inside each task's infinite loop before the delay.

3
Add a counter to observe preemption
Inside TaskLow, add an integer counter variable count initialized to 0 before the loop. Increment count each time the task runs and print it with the message. This will help observe how often TaskLow runs compared to TaskHigh.
FreeRTOS
Need a hint?

Declare count before the loop and increment it inside the loop before printing.

4
Run the scheduler and observe output
Run the program and observe the printed output. The output should show "High priority task running" and "Low priority task running, count = X" messages. Because of preemptive scheduling, TaskHigh runs as soon as it is ready, preempting TaskLow. Write a printf statement in main before starting the scheduler to print "Starting scheduler".
FreeRTOS
Need a hint?

Print "Starting scheduler" before calling vTaskStartScheduler().