In-Depth Discussion on FreeRTOS Tasks

πŸ‘©πŸ’» [Unveiling the RTOS World] A Deep Dive into the Five States of Tasks, Enabling You to Master the Art of Embedded Scheduling!

πŸ‘‹ Have you ever wondered how smart devices like smartwatches, drones, and robotic vacuum cleaners manage various tasks in their “brains”? Today, we will explore the “Five Brothers of Task States” in FreeRTOS in a simple, humorous way!

⚠️ This article exceeds 3000 words, filled with images, examples, and metaphors, making it suitable for intermediate embedded engineers, hardware developers, and RTOS beginners to bookmark!

🧠 What is FreeRTOS?

FreeRTOS is a real-time operating system (RTOS) specifically designed for embedded devices. It acts like a “task scheduling master,” allowing the processor to switch between multiple tasks in an orderly manner.

So, how many states can a task have? How do they run? How do they switch? Do tasks conflict with each other? Is there any “palace intrigue”? Let’s officially enter the RTOS world!

🏹 The Five Task States, Unveiling Their Status in the RTOS World

First, let’s get to know the five major sects of RTOS, which are the five task states:

🟒 1. Running

πŸ“ Sect: CPU’s Chosen Main Character Sect

When a task is currently running on the CPU, it is in the [Running State]!

πŸ” Characteristics:

  • The processor is busy with it.

  • If the processor has only one core, only one task can be in this state.

🎬 Analogy: You are in line at a bank window, and when it’s finally your turn to handle your business, at that moment, you are the “running task”!

🟑 2. Ready

πŸ“ Sect: Always on Standby Sect

Task State: I can run, I want to run, but it’s not my turn yet!

πŸ” Characteristics:

  • Waiting for the CPU to be free.

  • A task with a higher priority than you is currently running.

🎬 Analogy: You are waiting to place an order at McDonald’s, the line is long, but you already have the menu in hand and are ready to pay.

πŸ“Œ Advanced Tip: FreeRTOS prioritizes scheduling higher priority tasks, so if your priority is low, you will have to wait.

πŸ”΄ 3. Blocked

πŸ“ Sect: Waiting for the Wind Sect

Task State: I have nothing to do for now, waiting for a “message” or “time”.

πŸ” Characteristics:

  • Waiting for a certain event (like receiving a semaphore, queue data, or the end of a delay).

  • Will not occupy the CPU.

🎬 Analogy: You ordered takeout, and you can’t just wait idly, so you do something else (or sleep) until the food arrives!

πŸ“₯ API Example:<span>vTaskDelay()</span> puts the task to sleep until it is awakened when the time is up.

βœ… Practical Scenario:

void vMyTask(void *pvParameters) {    while(1) {        printf("Preparing to sleep for a while...\n");        vTaskDelay(pdMS_TO_TICKS(1000)); // Wake up once every second!        printf("I'm awake! I will continue working~\n");    }}

🟠 4. Suspended

πŸ“ Sect: Passive Hang Sect

Task State: I have “manually put myself to sleep”; I will wake up when you call me!

πŸ” Characteristics:

  • Will not be scheduled.

  • No timeout mechanism; must use <span>vTaskSuspend()</span> and <span>vTaskResume()</span> to control.

🎬 Analogy: You are playing a game, and your mom calls you to take out the trash; you must pause! You can only continue playing after completing the task (being called back).

πŸ“₯ Example:

vTaskSuspend(NULL); // Suspend itself// ...then called by external code to vTaskResume() to wake up

⚫ 5. Deleted

πŸ“ Sect: Retired from the World Sect

Task State: I have “exited the world” and am completely deleted, no longer participating in scheduling.

πŸ” Characteristics:

  • Will not be scheduled again.

  • Can manually <span>vTaskDelete(NULL)</span>, releasing resources.

🎬 Analogy: You have completed your business and left; the window is now empty, and it’s the next person’s turn.

πŸ“₯ Example:

void vSelfDeleteTask(void *pvParameters) {    // ...business logic    vTaskDelete(NULL); // Self-termination}

πŸ“Š Priority & Scheduling Strategy Overview

⏫ How is Priority Assigned?

  • The higher the number, the higher the priority.

  • The default lowest priority is 0 (also called <span>tskIDLE_PRIORITY</span>).

  • If you want a task to be a VIP, just give it a high priority!

βš™οΈ Set the maximum priority value in the configuration file:

#define configMAX_PRIORITIES 5

🧠 Tip: Don’t set too many priorities, as it can waste memory.

πŸŽ›οΈ What is the Scheduling Method?

πŸ”Ί Preemptive Scheduling

Whoever is important runs first, cutting in line at any time!

As long as a higher priority task is ready, the current lower priority task will be immediately preempted.

πŸ”„ Time Slicing

Tasks of the same level take turns running, each for a time slice.

πŸ“Œ Control Macro:

#define configUSE_TIME_SLICING 1

πŸš€ What About Multi-Core Systems?

🧩 AMP Scheduling Strategy (Asymmetric Multi-Core)

  • Each core runs an independent FreeRTOS.

  • Inter-core communication relies on shared memory.

🧠 SMP Scheduling Strategy (Symmetric Multi-Core)

  • One FreeRTOS controls multiple cores.

  • The scheduler is smart enough to allow multiple tasks to run simultaneously on different cores.

🎯 Configuration Tip:

#define configUSE_CORE_AFFINITY 1

Then use:

vTaskCoreAffinitySet(xHandle, 0x01); // Restrict the task to run only on Core0

πŸ§ͺ Easy to Learn: Let’s Write a Task!

void vBreatheTask(void *pvParameters) {    for (;;) {        printf("Inhale... Exhale...\n");        vTaskDelay(pdMS_TO_TICKS(2000));    }}xTaskCreate(vBreatheTask, "Breath", 1000, NULL, 2, NULL);

🎯 Tip: Remember to create the task and start the scheduler in <span>main()</span><code><span>:</span>

vTaskStartScheduler();

πŸ“¦ Summary Table: Quick Reference for the Five Task States

State Is Running Can be Scheduled Waiting for Event Auto Recovery
Running βœ… βœ… ❌ βœ…
Ready ❌ βœ… ❌ βœ…
Blocked ❌ ❌ βœ… βœ… (Timeout or Event)
Suspended ❌ ❌ ❌ ❌ (Manual Recovery Required)
Deleted ❌ ❌ ❌ ❌

🧩 Further Reading

  • FreeRTOS Official Website: https://www.freertos.org

  • Recommended Chinese Documentation: FreeRTOS Chinese Community, RT-Thread Learning Materials

  • Example Source Code: STM32 + FreeRTOS Breathing Light/Serial Print Example

πŸ’¬ Final Thoughts

πŸŽ‰ If you found today’s content enlightening, feel free to give a thumbs up and bookmark it! Want to learn more about practical RTOS programming? In the next issue, we will discuss the secrets of Queues, Semaphores, and Event Groups!

πŸ“¬ Feel free to leave your project requirements and learning questions; let’s make the tedious embedded development more interesting!

πŸ‘‰ Follow me for daily fun technical insights; see you next time!

Leave a Comment