0
0
FreeRTOSprogramming~30 mins

vTaskGetRunTimeStats() for CPU usage in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Using vTaskGetRunTimeStats() to Monitor CPU Usage in FreeRTOS
📖 Scenario: You are working on a FreeRTOS embedded system project. You want to monitor how much CPU time each task is using to optimize your system performance.
🎯 Goal: Build a simple FreeRTOS program that creates two tasks and uses vTaskGetRunTimeStats() to display CPU usage statistics of these tasks.
📋 What You'll Learn
Create two FreeRTOS tasks named Task1 and Task2
Create a buffer string called runtimeStatsBuffer with size 256
Use vTaskGetRunTimeStats(runtimeStatsBuffer) to collect CPU usage stats
Print the CPU usage stats using printf
💡 Why This Matters
🌍 Real World
Monitoring CPU usage helps embedded developers optimize task scheduling and system responsiveness.
💼 Career
Understanding FreeRTOS task monitoring is essential for embedded software engineers working on real-time systems.
Progress0 / 4 steps
1
Create two FreeRTOS tasks
Create two tasks named Task1 and Task2 using xTaskCreate(). Each task should run an empty infinite loop with a small delay using vTaskDelay(pdMS_TO_TICKS(100)).
FreeRTOS
Need a hint?

Use xTaskCreate() to create tasks. Each task runs an infinite loop with vTaskDelay(pdMS_TO_TICKS(100)) to yield CPU.

2
Create a buffer for runtime stats
Create a character array called runtimeStatsBuffer with size 256 to hold the CPU usage statistics string.
FreeRTOS
Need a hint?

Declare char runtimeStatsBuffer[256]; outside main() or inside before usage.

3
Collect CPU usage stats with vTaskGetRunTimeStats()
Inside main(), after starting the scheduler, call vTaskGetRunTimeStats(runtimeStatsBuffer) to fill the buffer with CPU usage statistics.
FreeRTOS
Need a hint?

Call vTaskGetRunTimeStats(runtimeStatsBuffer); after vTaskStartScheduler();.

4
Print the CPU usage statistics
Add a printf statement to print the contents of runtimeStatsBuffer so you can see the CPU usage of each task.
FreeRTOS
Need a hint?

Use printf("%s", runtimeStatsBuffer); to display the CPU usage stats.