(6 intermediate revisions by the same user not shown)
Line 11: Line 11:
 
<b>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>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>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. Please check [http://www.freertos.org/a00126.html this link] for detials. <br>
+
<b>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 the parameter. Please check [http://www.freertos.org/a00126.html this link] for details. <br>
  
 
<b>vTaskPrioritySet():</b> This function is used to change/Set the priority of a task.<br>
 
<b>vTaskPrioritySet():</b> This function is used to change/Set the priority of a task.<br>
Line 17: Line 17:
  
 
<b>vTaskPriorityGet():</b> This function is used to get the priority of a task.<br>
 
<b>vTaskPriorityGet():</b> This function is used to get the priority of a task.<br>
For this we need to pass the handle of the task and it will return the task. Check [http://www.freertos.org/a00128.html this link] for more details.
+
For this, we need to pass the handle of the task and it will return the task. Check [http://www.freertos.org/a00128.html this link] for more details.
 +
<br><br>
  
 
=Example1=
 
=Example1=
Line 28: Line 29:
  
 
=Output1=
 
=Output1=
[[FILE:TaskPriorityChange.png]]
+
[[FILE:TaskPriorityChange2.png]]
 
<br>
 
<br>
 
0. Serial port is initialised and Task1 is created with priority 1.
 
0. Serial port is initialised and Task1 is created with priority 1.
Line 34: Line 35:
 
#Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.  
 
#Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.  
 
#Now Task4 changes the priority of Task1 to 3 which is greater than Task2 but less than Task4. Hence Task4 still continues to run and enters waiting/blocked state.
 
#Now Task4 changes the priority of Task1 to 3 which is greater than Task2 but less than Task4. Hence Task4 still continues to run and enters waiting/blocked state.
#Now we have Task1(Prio:3), Task2(Prio:2), IDLE(Prio:0) ready to execute. Since Task1 has higher priority, it starts running and prints its priority as 3 and deletes all the task. The remaining part of Task2 and Task4 will never get executed.<br><br>
+
#Now we have Task1(Prio:3), Task2(Prio:2), IDLE(Prio:0) ready to execute. Since Task1 has higher priority, it starts running and prints its priority as 3 and deletes all the task. The remaining part of Task2 and Task4 will never get executed.<br><br><br>
  
 
=Example2=
 
=Example2=
 +
In earlier example, we changed the priority of Task1 from 1 to 3, because of which the Tasks with priority greater than 3 were able to run.<br>
 +
In this example, we will change the priority of Task1 from low(1) to highest(5).
 +
This task will delete all the tasks and deletes itself after completing its job.
 
<html><script src="https://gist.github.com/SaheblalBagwan/fa48be8158b10520f75896c252fd6020.js"></script></html>
 
<html><script src="https://gist.github.com/SaheblalBagwan/fa48be8158b10520f75896c252fd6020.js"></script></html>
  
 
=Output2=
 
=Output2=
[[FILE:TaskPriorityChange2.png]]
+
[[FILE:TaskPriorityChange.png]]
 
<br>
 
<br>
 
0. Serial port is initialised and Task1 is created with priority 1.
 
0. Serial port is initialised and Task1 is created with priority 1.
 
#As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.  
 
#As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.  
 
#Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.  
 
#Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.  
#Now Task4 changes the priority of Task1 to 4 which is highest of all. It will preemp Task4 and starts running.
+
#Now Task4 changes the priority of Task1 to 4 which is highest of all. It will preempt Task4 and starts running.
 
#Now Task1 deletes all the task. The remaining part of Task2 and Task4 will never get executed.
 
#Now Task1 deletes all the task. The remaining part of Task2 and Task4 will never get executed.
 +
 +
=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:58, 4 July 2016

In earlier tutorials, we saw how to create, use and then delete the tasks.
In this tutorial, we will see how to read the priority of task and also change it dynamically.


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

Example1

In this example, we will be changing the priority of Task1 from low(1) to medium(3).
This task will delete all the tasks which have priorities lower than it. The task with higher priorities will continue to run ahead of Task1, once their job is done they delete themselves. So we will be able to see the output of the tasks with priorities higher than Task1.

Output1

TaskPriorityChange2.png
0. Serial port is initialised and Task1 is created with priority 1.

  1. As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.
  2. Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
  3. Now Task4 changes the priority of Task1 to 3 which is greater than Task2 but less than Task4. Hence Task4 still continues to run and enters waiting/blocked state.
  4. Now we have Task1(Prio:3), Task2(Prio:2), IDLE(Prio:0) ready to execute. Since Task1 has higher priority, it starts running and prints its priority as 3 and deletes all the task. The remaining part of Task2 and Task4 will never get executed.


Example2

In earlier example, we changed the priority of Task1 from 1 to 3, because of which the Tasks with priority greater than 3 were able to run.
In this example, we will change the priority of Task1 from low(1) to highest(5). This task will delete all the tasks and deletes itself after completing its job.

Output2

TaskPriorityChange.png
0. Serial port is initialised and Task1 is created with priority 1.

  1. As we have only Task1 and Idle task, Task1 starts executing. Furter Task1 creates Task2 with priority 2. Now Task2 will preempt Task1 and starts running.
  2. Now Task2 will create Task4 with priority 4. Now Task2 will preempt Task1 and starts running.
  3. Now Task4 changes the priority of Task1 to 4 which is highest of all. It will preempt Task4 and starts running.
  4. Now Task1 deletes all the task. The remaining part of Task2 and Task4 will never get executed.

Downloads

Download the complete project and libraries from here.

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