0
0
FreeRTOSprogramming~30 mins

Time-slicing for equal priority tasks in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Time-slicing for equal priority tasks
📖 Scenario: You are working with FreeRTOS, a real-time operating system used in embedded devices. You want to understand how FreeRTOS shares CPU time between tasks that have the same priority. This is called time-slicing.Imagine two simple tasks that both print messages repeatedly. You will set up these tasks and observe how FreeRTOS switches between them to give each a fair share of CPU time.
🎯 Goal: Create two tasks with the same priority in FreeRTOS. Each task will print a message in a loop. You will configure the scheduler to enable time-slicing so both tasks get CPU time fairly. Finally, you will observe the output to see the tasks alternating.
📋 What You'll Learn
Create two tasks named Task1 and Task2
Set both tasks to the same priority level tskIDLE_PRIORITY + 1
Enable time-slicing in the FreeRTOS configuration
Print messages from each task to show they run alternately
💡 Why This Matters
🌍 Real World
Time-slicing is used in embedded systems to ensure multiple tasks with the same priority get CPU time fairly, such as in sensor reading and communication tasks running together.
💼 Career
Understanding time-slicing helps embedded developers design responsive multitasking applications where tasks share CPU time predictably.
Progress0 / 4 steps
1
Create two tasks with the same priority
Write code to create two FreeRTOS tasks named Task1 and Task2. Both tasks should have the priority tskIDLE_PRIORITY + 1. Each task should run a function that prints a message identifying the task in an infinite loop with a small delay. Use xTaskCreate to create the tasks.
FreeRTOS
Need a hint?

Use xTaskCreate to create each task. The priority should be tskIDLE_PRIORITY + 1 for both.

2
Enable time-slicing in FreeRTOSConfig.h
Add a configuration macro #define configUSE_TIME_SLICING 1 in your FreeRTOSConfig.h file to enable time-slicing between tasks of equal priority.
FreeRTOS
Need a hint?

In FreeRTOSConfig.h, add #define configUSE_TIME_SLICING 1 to enable time-slicing.

3
Start the scheduler to run the tasks
Add the call to vTaskStartScheduler() in main() after creating the tasks. This starts the FreeRTOS scheduler and begins running the tasks.
FreeRTOS
Need a hint?

Call vTaskStartScheduler() after creating the tasks to start running them.

4
Observe the output showing time-slicing
Run the program and observe the output printed by the two tasks. The output should alternate between Task 1 is running and Task 2 is running, showing that FreeRTOS is switching between the tasks fairly using time-slicing. Write a printf statement to print "Scheduler started, tasks are running..." before starting the scheduler.
FreeRTOS
Need a hint?

Print the message "Scheduler started, tasks are running..." before calling vTaskStartScheduler().