FreeRTOS With Arduino 04 : Deleting a Task
by ExploreEmbedded in Circuits > Arduino
3235 Views, 5 Favorites, 0 Comments
FreeRTOS With Arduino 04 : Deleting a Task
In earlier tutorials, we saw how to create tasks and used. In this tutorial, we will see how to delete a task once its job is done.
API Details :
Here we will discuss some of the most frequently used APIs related to tasks.
1.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.
2.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.
3.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 parameter.Please check this link for details.
Example :
In this example, we are going to create 3-Tasks with different priorities. Each task will run for some time and then goes to blocked state allowing the other tasks to run. Once the Task completes its job, it will delete itself.
/***************************************************************************************************
ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 03-TaskDeleteUsage * Version: 15.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the free rtos example to demonstarte the task delete.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;
void setup() { Serial.begin(9600); Serial.println(F("In Setup function"));
/* Create three tasks with priorities 1,2 and 3. 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); }
void loop() { // Hooked to IDle task, it will run whenever CPU is idle Serial.println(F("Loop function")); delay(50); }
/* Task1 with priority 1 */ static void MyTask1(void* pvParameters) { while(1) { Serial.println(F("Task1 Running")); vTaskDelay(100/portTICK_PERIOD_MS);
Serial.println(F("Back in Task1 and About to delete itself")); vTaskDelete(TaskHandle_1); // Delete the task using the TaskHandle_1 } }
/* Task2 with priority 2 */ static void MyTask2(void* pvParameters) { while(1) { Serial.println(F("Task2 Running")); vTaskDelay(150/portTICK_PERIOD_MS);
Serial.println(F("Back in Task2 and About to delete itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used) } }
/* Task3 with priority 3 */ static void MyTask3(void* pvParameters) { while(1) { Serial.println(F("Task3 Running")); vTaskDelay(150/portTICK_PERIOD_MS);
Serial.println(F("Back in Task3 and About to delete itself")); vTaskDelete(TaskHandle_3); } }
Output :
0. Serial port is initialized and 3-Tasks are created with different priorities.
- Cpu chooses Task3 out of the 4-Tasks(1,2,3,and idle) as it has higher priority. Task3 will run for some time and goes to wait/blocked state for 150ms.
At this point 3-Tasks(1,2,idle) are available and scheduler chooses Task2. It runs for some time and goes to blocked state for 150ms.
Now 2-Tasks(1,Idle) are available and Task1 runs because of its higher priority. It also goes to blocked state for 100ms .
Now scheduler is left out with the Idle task and it keeps running till one of the tasks waiting time is elapsed and comes out of blocked state.
Since Task1 was blocked for only 100ms, it will come out of the blocked state and starts running. And finally, it deletes itself once its job is done.
Now again Idle task will start running as there are no other tasks to run.
At this point Task3 will come out of the waiting/blocked state and preempts the Idle task and starts running. It also deletes itself.
By this time Task2 is also out of blocked state and runs for some time and deletes itself.
Now there is only one task ie.The idle task and it keeps running.
Downloads :
Download the complete project and libraries from here .