1
votes

I am using FreeRTOS and have multiple tasks in my application of which two tasks are of least priority but needs to be executed initially. Lets name them as Task1, Task2, Task3, Task4.

xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &TaskHandle_1);
xTaskCreate(MyTask2, "Task2", 150, NULL, 1, &TaskHandle_2);
xTaskCreate(MyTask3, "Task3", 256, NULL, 2, &TaskHandle_3);
xTaskCreate(MyTask4, "Task4", 1024, NULL, 3, &TaskHandle_4);

Task1 and Task2 are of lowest priority but they needs to be executed first since Task4 consists of condition which depends on Task1 parameters.

Since Task4 is of highest priority, it starts executing immediately and Task1 is executed after sometime.

Least possible solutions I think are:

  • Make Task1 priority highest and then change its priority back to lowest.
  • Suspend current task and start Task1 then resume task

How can I make Task1 run before Task4?

3

3 Answers

2
votes

Two options that I can think of:

  • Call vTaskSuspend() at the beginning of Task4, resume it from Task1 when given condition is met
  • Block Task4 on semaphore at the start of its task function, set semaphore from Task1
1
votes

Maybe once Task1 is created and running its code could create Task4 after all conditions that it needs are satisfied, instead of creating Task 4 right from the start

0
votes

If these tasks are created in main() I believe you can suspend the higher priority tasks before starting the scheduler: https://www.freertos.org/a00130.html then resume them after the other tasks have done what they need to do.