0
0
FreeRTOSprogramming~30 mins

Task states (Ready, Running, Blocked, Suspended) in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding Task States in FreeRTOS
📖 Scenario: You are working on a simple embedded system using FreeRTOS. You want to understand how tasks move between different states: Ready, Running, Blocked, and Suspended.This project will help you create tasks and observe their states using FreeRTOS API calls.
🎯 Goal: Create three FreeRTOS tasks with different behaviors to demonstrate the task states: Ready, Running, Blocked, and Suspended. You will set up the tasks, configure a delay to block one task, suspend another, and print the states to the console.
📋 What You'll Learn
Create three tasks named Task1, Task2, and Task3.
Use a configuration variable delay_time to control blocking duration.
Implement task functions that demonstrate Ready, Running, Blocked, and Suspended states.
Print the state of each task using eTaskGetState().
💡 Why This Matters
🌍 Real World
Embedded systems often use FreeRTOS to manage multiple tasks. Understanding task states helps in debugging and optimizing system behavior.
💼 Career
Embedded software engineers and firmware developers need to manage task states to ensure responsive and efficient real-time applications.
Progress0 / 4 steps
1
Create three FreeRTOS tasks
Create three tasks named Task1, Task2, and Task3 using xTaskCreate(). Each task should run a simple function that loops infinitely. Use vTaskDelay(1) inside each task to allow context switching.
FreeRTOS
Need a hint?

Use xTaskCreate() to create each task with a stack size of 1000 and priority 1. Then start the scheduler with vTaskStartScheduler().

2
Add a delay configuration variable
Add a variable named delay_time of type TickType_t and set it to pdMS_TO_TICKS(1000) to represent a 1000 ms delay.
FreeRTOS
Need a hint?

Use TickType_t delay_time = pdMS_TO_TICKS(1000); to set a 1-second delay.

3
Make Task2 block and Task3 suspend
Modify Task2 to call vTaskDelay(delay_time) inside its loop to enter the Blocked state. Modify Task3 to suspend itself once using vTaskSuspend(NULL) to enter the Suspended state.
FreeRTOS
Need a hint?

Use vTaskDelay(delay_time); in Task2's loop and call vTaskSuspend(NULL); once at the start of Task3.

4
Print the state of each task
In main(), after creating the tasks, declare three TaskHandle_t variables named handle1, handle2, and handle3. Pass their addresses to xTaskCreate() to get the task handles. Then print the state of each task using eTaskGetState() and printf(). Use the format: "Task1 state: %d\n", "Task2 state: %d\n", and "Task3 state: %d\n".
FreeRTOS
Need a hint?

Use TaskHandle_t variables to get task handles from xTaskCreate(). Then print each task's state using eTaskGetState() and printf(). The states are integers representing Ready (2), Blocked (3), and Suspended (4).