Introduction: FreeRTOS With Arduino 08: Creating a Task From Other Tasks

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 a task in Initialization and use it. In this tutorial, we will see how to create a task from other tasks.

Step 1: 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 the parameter. Please check this link for details.

Step 2: Example :

In this example, we are going to create 2Tasks(Task1,Task3) with priorities 1&3.Later, Task3 will create 2 more tasks(Task2,Task4) with priorities 2&4. Each task will run for some time depending on the priority and goes to the blocked state allowing other tasks to run. Once the task is done with its job, it will delete itself.

ExploreEmbedded Copyright Notice **************************************************************************************************** * File: 04-CreatingTaskFromOtherTask * Version: 15.0 * Author: ExploreEmbedded * Website: * Description: File contains the free rtos example to demonstarte creating tasks from other tasks.

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 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:


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. **************************************************************************************************/


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 two tasks with priorities 1 and 3. Capture the Task details to respective handlers */ xTaskCreate(MyTask1, "Task1", 100, NULL, 1, &TaskHandle_1); xTaskCreate(MyTask3, "Task3", 120, 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 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 and About to delete itsel")); 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, Creating Task2 and Task4")); xTaskCreate(MyTask2, "Task2", 50, NULL, 2, &TaskHandle_2); xTaskCreate(MyTask4, "Task4", 100, NULL, 4, &TaskHandle_4);

Serial.println(F("Back in Task3 and About to delete itself")); vTaskDelete(TaskHandle_3); } }

/* Task4 with priority 4 */ static void MyTask4(void* pvParameters) { while(1) { Serial.println(F("Task4 Running and About to delete itself")); vTaskDelete(NULL); //Delete own task by passing NULL(TaskHandle_2 can also be used) } }

Step 3: Output :

0. Serial port is initialized and 2-Tasks are created with different priorities.

  1. Cpu chooses Task3 out of the tasks(1,3,and idle) as it has higher priority. Now Task3 will create two more tasks with priority2,4. Now CPU has tasks(1,2,3,4,IDLE). Since Task4 has the highest priority it will preempt the Task3 and starts running.
  2. Task4 will run for some time and deletes itself.
  3. Now Tasks(1,2,3,IDLE) are available and Task3 will run again because for its higher priority. It will Run for some time and deletes itself.
  4. Now Tasks(1,2,Idle) are remaining and Task2 will run and deletes itself once its job is done.
  5. Out of Task(1,Idle), Task1 runs and it also deletes itself.
  6. CPU is left out with the Idle task and it keeps running.

Step 4: Downloads :

Download the complete project and libraries from here.