0
0
FreeRTOSprogramming~30 mins

Graceful shutdown sequence in FreeRTOS - Mini Project: Build & Apply

Choose your learning style9 modes available
Graceful shutdown sequence
📖 Scenario: You are developing a FreeRTOS application for a smart home device. The device needs to shut down gracefully when a shutdown signal is received. This means it should stop accepting new tasks, finish current tasks, release resources, and then power off safely.
🎯 Goal: Build a graceful shutdown sequence in FreeRTOS that uses a flag to signal shutdown, stops task creation, waits for running tasks to finish, and then prints a shutdown complete message.
📋 What You'll Learn
Create a global flag variable shutdown_requested initialized to false.
Create a task called WorkerTask that runs a loop checking shutdown_requested and exits when it is true.
Create a task called ShutdownTask that sets shutdown_requested to true after a delay.
Print "Shutdown complete" after all tasks have stopped.
💡 Why This Matters
🌍 Real World
Smart devices and embedded systems often need to stop safely to avoid data loss or hardware damage.
💼 Career
Embedded software engineers must implement graceful shutdowns to ensure reliable and safe device operation.
Progress0 / 4 steps
1
Create the shutdown flag variable
Create a global variable called shutdown_requested of type bool and initialize it to false.
FreeRTOS
Need a hint?

This flag will tell tasks when to stop running.

2
Create the WorkerTask that checks the shutdown flag
Create a FreeRTOS task function called WorkerTask that runs an infinite loop. Inside the loop, check if shutdown_requested is true. If it is, break the loop to exit the task. Use vTaskDelay(100 / portTICK_PERIOD_MS) inside the loop to simulate work.
FreeRTOS
Need a hint?

The task should stop running when shutdown_requested becomes true.

3
Create the ShutdownTask to trigger shutdown
Create a FreeRTOS task function called ShutdownTask that waits for 500 milliseconds using vTaskDelay(500 / portTICK_PERIOD_MS) and then sets shutdown_requested to true. After setting the flag, delete itself with vTaskDelete(NULL).
FreeRTOS
Need a hint?

This task triggers the shutdown after a short delay.

4
Print shutdown complete after tasks stop
In the main function, create the WorkerTask and ShutdownTask using xTaskCreate. Then start the scheduler with vTaskStartScheduler(). After the scheduler ends, print "Shutdown complete" using printf.
FreeRTOS
Need a hint?

The scheduler runs tasks until shutdown is requested. After it ends, print the shutdown message.