0
0
FreeRTOSprogramming~30 mins

Why tasks are the building blocks in FreeRTOS - See It in Action

Choose your learning style9 modes available
Why tasks are the building blocks
📖 Scenario: You are working on a small embedded system project using FreeRTOS. You want to understand how tasks work as the main building blocks to run different parts of your program independently.
🎯 Goal: Build a simple FreeRTOS program that creates two tasks. Each task will print a message to show how tasks run separately and repeatedly.
📋 What You'll Learn
Create two tasks with specific names
Use the FreeRTOS API to create tasks
Each task should print a unique message
Use a delay to allow task switching
Print output to show tasks running independently
💡 Why This Matters
🌍 Real World
Embedded systems often need to run multiple functions at once, like reading sensors and controlling motors. Tasks let you organize these functions clearly.
💼 Career
Understanding tasks is essential for embedded software developers working with real-time operating systems like FreeRTOS.
Progress0 / 4 steps
1
Create two task functions
Write two functions called Task1 and Task2 that take a void *pvParameters argument and run an infinite loop. Inside each loop, add a comment // Task running to indicate where code will go.
FreeRTOS
Need a hint?

Each task function must have an infinite loop to keep running.

2
Create task handles and stack size
Declare two variables called xTaskHandle1 and xTaskHandle2 of type TaskHandle_t. Also, create a constant STACK_SIZE with value configMINIMAL_STACK_SIZE.
FreeRTOS
Need a hint?

Task handles are used to manage tasks after creation. Stack size defines memory for each task.

3
Create the two tasks in main
In the main function, use xTaskCreate to create Task1 and Task2. Use STACK_SIZE for stack size, NULL for parameters, priority 1, and store handles in xTaskHandle1 and xTaskHandle2. Then start the scheduler with vTaskStartScheduler().
FreeRTOS
Need a hint?

Use xTaskCreate to make tasks and vTaskStartScheduler to run them.

4
Add print statements and delay inside tasks
Inside the infinite loop of Task1, add printf("Task1 is running\n") and vTaskDelay(pdMS_TO_TICKS(1000)). Inside Task2, add printf("Task2 is running\n") and vTaskDelay(pdMS_TO_TICKS(1500)). This will show each task running independently with delays.
FreeRTOS
Need a hint?

Use printf to show task messages and vTaskDelay to pause each task.