Introduction: FreeRTOS With Arduino 06 : Task Suspend and Resume

About: We make Embedded Design tools, boards, sensors, peripherals, DIY kits and awesome tutorials. We also help companies and individuals prototype embedded electronics.

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

Step 1: 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 stateFor this, we need to pass the handle of the task to be resumed. Check this link for more details.

Step 2: Example1 :

In this example, we will be creating 4-Tasks with priorities(1-4). Task4 will suspend all the tasks except Task1.As Task2-Task4 will be in suspended state, Task1 will get the chance to run. Now Task1 will resume one task at a time and every time it resumes a task it will be preempted as the resumed task will have higher priority.The resumed task will run for some time and deletes itself. Because of this, Task1 will get alternative chance to Run.It resumes all the tasks one after another and finally deletes itself.

/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 07-TaskSuspendAndResume * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte task Suspend and Resume.

This code has been developed and tested on ExploreEmbedded boards. We strongly believe that the library works on any of development boards for respective controllers. Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT. ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider buying the ExploreEmbedded boards. The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license). See also: http://www.opensource.org/licenses/bsd-license.php

EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION RELATED TO UPDATES.

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that this copyright notices appear in all copies and that both those copyright notices and this permission notice appear in supporting documentation. **************************************************************************************************/

#include

TaskHandle_t TaskHandle_1; TaskHandle_t TaskHandle_2; TaskHandle_t TaskHandle_3; TaskHandle_t TaskHandle_4;

void setup() { Serial.begin(9600); Serial.println(F("In Setup function"));

/* Create 4-tasks with priorities 1-4. Capture the Task details to respective handlers */ xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &TaskHandle_1); xTaskCreate(MyTask2, "Task2", 100, NULL, 2, &TaskHandle_2); xTaskCreate(MyTask3, "Task3", 100, NULL, 3, &TaskHandle_3); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4); }

void loop() { // Hooked to Idle Task, will run when CPU is Idle Serial.println(F("Loop function")); delay(50); }

/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { Serial.println(F("Task1 Resuming Task2")); vTaskResume(TaskHandle_2); Serial.println(F("Task1 Resuming Task3")); vTaskResume(TaskHandle_3);

Serial.println(F("Task1 Resuming Task4")); vTaskResume(TaskHandle_4);

Serial.println(F("Task1 Deleting Itself")); vTaskDelete(TaskHandle_1); }

/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { Serial.println(F("Task2, Deleting itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used) }

/* Task3 with priority 3 */ static void MyTask3(void* pvParameters) { Serial.println(F("Task3, Deleting Itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_3 can also be used) }

/* Task4 with priority 4 */ static void MyTask4(void* pvParameters) { Serial.println(F("Task4 Running, Suspending all tasks")); vTaskSuspend(TaskHandle_2); //Suspend Task2/3 vTaskSuspend(TaskHandle_3); vTaskSuspend(NULL); //Suspend Own Task

Serial.println(F("Back in Task4, Deleting Itself")); vTaskDelete(TaskHandle_4); }

Step 3: Output1 :

0. Serial port is initialised and 4-Tasks are created with priorities 1-4.

  1. Task4 starts executing as it has the highest priority and suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed.
  2. 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.
  3. Task2 will run for some time and deletes itself.
  4. 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.
  5. Task3 runs for some time and deletes itself.
  6. 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.
  7. Task4 runs for some time and deletes itself.
  8. Now we are left with Task1 and Idle, so Task1 completes it job and deletes itself.
  9. Finally we are left with IDLE task which continues to run.

Step 4: Example2 :

This example will be similar to above one except that the Task1 priority will be changed to highest.Task1 starts resuming the tasks(2-4) one by one, as it has the highest priority it will continue to run even after resuming other tasks and finally deletes itself.After this, all the resumed task will start running one after the other depending on their priorities.Compare the order of task execution in both the output images for better understanding.

/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 08-TaskSuspendAndResume * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte task Suspend and Resume.

This code has been developed and tested on ExploreEmbedded boards. We strongly believe that the library works on any of development boards for respective controllers. Check this link http://www.exploreembedded.com/wiki for awesome tutorials on 8051,PIC,AVR,ARM,Robotics,RTOS,IOT. ExploreEmbedded invests substantial time and effort developing open source HW and SW tools, to support consider buying the ExploreEmbedded boards. The ExploreEmbedded libraries and examples are licensed under the terms of the new-bsd license(two-clause bsd license). See also: http://www.opensource.org/licenses/bsd-license.php

EXPLOREEMBEDDED DISCLAIMS ANY KIND OF HARDWARE FAILURE RESULTING OUT OF USAGE OF LIBRARIES, DIRECTLY OR INDIRECTLY. FILES MAY BE SUBJECT TO CHANGE WITHOUT PRIOR NOTICE. THE REVISION HISTORY CONTAINS THE INFORMATION RELATED TO UPDATES.

Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that this copyright notices appear in all copies and that both those copyright notices and this permission notice appear in supporting documentation. **************************************************************************************************/

#include

TaskHandle_t TaskHandle_1; TaskHandle_t TaskHandle_2; TaskHandle_t TaskHandle_3; TaskHandle_t TaskHandle_4;

void setup() { Serial.begin(9600); Serial.println(F("In Setup function"));

/* Create 4-tasks with priorities 1-4. Capture the Task details to respective handlers */ xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &TaskHandle_1); xTaskCreate(MyTask2, "Task2", 100, NULL, 2, &TaskHandle_2); xTaskCreate(MyTask3, "Task3", 100, NULL, 3, &TaskHandle_3); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4); }

void loop() { // Hooked to Idle Task, will run when CPU is Idle Serial.println(F("Loop function")); delay(50); }

/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { Serial.println("Task1 Changing its priority to 5"); vTaskPrioritySet(TaskHandle_1,5); //Now task1 is of highest priority Serial.println("Task1 Resuming Task2"); vTaskResume(TaskHandle_2); Serial.println("Task1 Resuming Task3"); vTaskResume(TaskHandle_3);

Serial.println("Task1 Resuming Task4"); vTaskResume(TaskHandle_4);

Serial.println("Task1 Deleting Itself"); vTaskDelete(TaskHandle_1); }

/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { Serial.println(F("Task2, Deleting itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used) }

/* Task3 with priority 3 */ static void MyTask3(void* pvParameters) { Serial.println(F("Task3, Deleting Itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_3 can also be used) }

/* Task4 with priority 4 */ static void MyTask4(void* pvParameters) { Serial.println(F("Task4 Running, Suspending all tasks")); vTaskSuspend(TaskHandle_2); //Suspend Task2/3 vTaskSuspend(TaskHandle_3); vTaskSuspend(NULL); //Suspend Own Task

Serial.println(F("Back in Task4, Deleting Itself")); vTaskDelete(TaskHandle_4); }

Step 5: Output2 :

0. Serial port is initialised and 4-Tasks are created with priorities 1-4.

  1. Task4 starts executing as it has the highest priority and suspends Task2,Task3 and itself. Now Task2, Task3 and Task4 are in suspended state and will not run until they are resumed.
  2. At this point Task1 and IDLE are available for the scheduler, So Task1 starts running. Now the Task1 changes its priority to 5 which is highest of all. It resumes Task2, Task3 and Tas4 and continues to run as it has the highest priority and deletes itself at the end. Now we have Task2, Task3, Task1 and Idle. All will run one after other depending on priorities.
  3. Now Task4 starts running and deletes itself.
  4. Task3 runs and deletes itself.
  5. Task2 runs and deletes itself.
  6. Finally IDLE task will run continuously.

Step 6: Downloads :

Download the complete project and libraries from here.