In earlier tutorials, we saw how to create and use and delete the tasks.
In this tutorial, we will see how to Suspend and Resume the tasks.


API Details

Here we will discuss some of the most frequently used APIs related to tasks.

xTaskCreate(): This interface is used to create a new Task, if the task is successfully created then it returns pdPass(1) or else errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY(-1). Check this link for more details.

vTaskDelay(): This function is used to delay/block the task for specified delay time(ticks). INCLUDE_vTaskDelay needs to be set to 1 in FreeRtosConfig.h file for using this function. Check this link for more details.

vTaskDelete():This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.
To delete the own task we should pass NULL as the parameter. Please check this link for more details.

vTaskPrioritySet(): This function is used to change/Set the priority of a task.
For this we need to pass the handle of the task and new priority to vTaskPrioritySet() function. Check this link for more details.

vTaskPriorityGet(): This function is used to get the priority of a task.
For this, we need to pass the handle of the task and it will return the task. Check this link for more details.

vTaskSuspend(): This function is used to Suspend a task, the suspended remains in the same state util it is resumed.
For this, we need to pass the handle of the tasks that needs to be suspended. Passing NULL will suspend own task. Check this link for more details.

vTaskResume(): This function is used to resume a suspended task. If the Resumed task has higher priority than the running task then it will preempt the running task or else stays in ready state
For this, we need to pass the handle of the task to be resumed. Check this link for more details.

Example1

Output1

TaskSuspendAndResume.png
0. Serial port is initialised and Task1 and Task3 are created with priority 1&3.

  1. Task3 starts executing as it has higher priority. NowTask3 creates Task2 and Task4 with priorities 2 and 4. Since Task4 has higher priority compared to Task3, Task4 starts running preempting Task3
  2. Now Task4 Suspends Task2,Task3 and itself. Now Task2,Task3 and Task4 are in suspended state and will not run until they are resumed.
  3. At this point Task1 and IDLE are available for the scheduler, So Task1 starts running. It resumes Task2 and immediately Task2 starts running as it has higher priority compared Task1.
  4. Task2 will run for some time and deletes itself.
  5. Again we are left with Task1 and IDLE, So Task1 starts running. It resumes Task3 and immediately Task3 starts running as it has higher priority compared to Task1.
  6. Task3 runs for some time and deletes itself.
  7. Again we are left with Task1 and IDLE, So Task1 starts running. It resumes Task4 and immediately Task4 starts running as it has higher priority compared to Task1.
  8. Task4 runs for some time and deletes itself.
  9. Now we are left with Task1 and Idle, so Task1 completes it job and deletes itself.
  10. Finally we are left with IDLE task which continues to run.

Example2

Output2

TaskSuspendAndResume2.png
0. Serial port is initialised and Task1 and Task3 are created with priority 1&3.

  1. Task3 starts executing as it has higher priority. NowTask3 creates Task2 and Task4 with priorities 2 and 4. Since Task4 has higher priority compared to Task3, Task4 starts running preempting Task3
  2. Now Task4 Suspends Task2,Task3 and itself. Now Task2,Task3,Task4 are in suspended state and will not run until they are resumed.
  3. At this point Task1 and IDLE are available for the scheduler, So Task1 starts running. It resumes Task2 and immediately Task2 starts running as it has higher priority compared Task1.