(Created page with "category: Free RTOS with Arduino In this tutorial, we will see what is a task and its different states. Later we will see how to create a FreeRTOS tasks and finally we wil...")
 
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[category: Free RTOS with Arduino]]
 
[[category: Free RTOS with Arduino]]
In this tutorial, we will see what is a task and its different states. Later we will see how to create a FreeRTOS tasks and finally we will write a simple example to see how the task works and task switching happens.
+
In earlier tutorials, we saw how to create tasks and used.<br>
 +
In this tutorial, we will see how to delete a task once its job is done.<br>
 +
 
 +
 
 +
=API Details=
 +
Here we will discuss some of the most frequently used APIs related to tasks.
 +
 
 +
<b>1.xTaskCreate()</b>: 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 [http://www.freertos.org/a00125.html this link] for more details.<br>
 +
 
 +
<b>2.vTaskDelay()</b>: 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 [http://www.freertos.org/a00127.html this link] for more details.<br>
 +
 
 +
<b>3.vTaskDelete():</b>This function is used to delete as task. We need to pass the taskHandle of the task to be deleted.<br> To delete the own task we should pass NULL as parameter.<br>
 +
Please check [http://www.freertos.org/a00126.html this link] for details. <br><br>
 +
 
 +
=Example=
 +
In this example, we are going to create 3-Tasks with different priorities.<br>
 +
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.
 +
<html> <script src="https://gist.github.com/SaheblalBagwan/36e4817cd307be96ddb7dfb9378443ba.js"></script> </html>
 +
<br><br>
 +
 
 +
=Output=
 +
[[FILE:TaskDeleteUsage.jpg]]
 +
<br>
 +
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 [https://github.com/ExploreEmbedded/Arduino_FreeRTOS/archive/master.zip here].<br><br>
 +
 
 +
Have an opinion, suggestion , question or feedback about the article let it out here!
 +
{{DISQUS}}

Latest revision as of 15:56, 4 July 2016

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.

Output

TaskDeleteUsage.jpg
0. Serial port is initialized and 3-Tasks are created with different priorities.

  1. 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.
  2. 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.
  3. Now 2-Tasks(1,Idle) are available and Task1 runs because of its higher priority. It also goes to blocked state for 100ms
  4. 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.
  5. 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.
  6. Now again Idle task will start running as there are no other tasks to run.
  7. At this point Task3 will come out of the waiting/blocked state and preempts the Idle task and starts running. It also deletes itself.
  8. By this time Task2 is also out of blocked state and runs for some time and deletes itself.
  9. Now there is only one task ie.The idle task and it keeps running.

Downloads

Download the complete project and libraries from here.

Have an opinion, suggestion , question or feedback about the article let it out here!