m (Explorer moved page Semaphores to Read Task Info : vTaskList())
 
(14 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 be discussing a Semaphore and its types ie. Binary, Mutex, Counting.<br>
+
In earlier tutorials, we saw how to create, delete, suspend and resume the tasks.<br>
Later we will see each semaphore in detail with its pros and cons.
+
In this tutorial, we will see how to read the Task info(state, Stack size, priority.<br>
Also, we will be looking into priority inversion and priority inheritance.<br><br>
+
  
==Teminologies==
+
[[File:0ReadTaskInfo.png]]
#<b>LPT:</b> Low Priority Task
+
#<b>MPT:</b> Medium Priority Task
+
#<b>HPT:</b> High Priority Task
+
  
 +
=API Details=
 +
Here we will discuss some of the most frequently used APIs related to tasks.
  
=What is a Semaphore?=
+
<b>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>
Semaphore is a technique for synchronizing two/more task competing for the same resources. When a task wants to use a resource, it requests for the semaphore and will be allocated if the semaphore is available. If the semaphore is not available then the requesting task will go to blocked state till the semaphore becomes free.
+
  
Consider a situation where there are two persons who want to share a bike.
+
<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>
At one time only one person can use the bike. The one who has the bike key will get the chance to use it. And when this person gives the key to the 2nd person, then the 2nd person can use the bike.
+
  
Semaphore is just like this <b>Key</b> and the bike is the shared resource.
+
<b>vTaskSuspend():</b> This function is used to Suspend a task, the suspended remains in the same state util it is resumed.<br>
Whenever a task wants access to the shared resource, it must acquire the semaphore first.
+
For this, we need to pass the handle of the tasks that needs to be suspended. Passing NULL will suspend own task. Check [http://www.freertos.org/a00130.html this link] for more details.
The task should release the semaphore after it is done with the shared resource.  
+
Until this time all other tasks have to wait if they need access to shared resource as semaphore is not available.
+
Even if the task trying to acquire the semaphore is of higher priority than the task acquiring the semaphore,
+
it will be in the wait state until the semaphore is released by the lower priority task.
+
<br><br>
+
  
=Types of Semaphores=
+
<b>vTaskResume():</b> 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 state<br>
There are 3-types of semaphores namely Binary, Counting and Mutex semaphore.
+
For this, we need to pass the handle of the task to be resumed. Check [http://www.freertos.org/a00131.html this link] for more details.
*<b>Binary Semaphore</b>: Binary semaphore is used when there is only one shared resource.
+
Binary semaphore exists in two states ie.Acquired(Take), Released(Give).
+
Binary semaphores have no ownership  and can be released by any task or ISR regardless of who performed the last take operation. Because of this binary semaphores are often used to synchronize tasks with external events implemented as ISRs, for example waiting for a packet from a network or waiting for a button is pressed.
+
  
Because there is no ownership concept a binary semaphore object can be created to be either in the “taken” or “not taken” state initially.
+
<b>vTaskList():</b> This function is used to read the task details(name, state, priority, num). We need to pass a string pointer(buffer) to which it copies the above task details. The buffer size should be minimum 40 bytes per task. Check [http://www.freertos.org/a00021.html#vTaskList this link] for more details.
 
+
<br><br>
Cons:
+
#Priority Inversion : HPT needs to wait for the LPT as LPT is holding the resource required for HPT. In between if MPT task comes then LPT will be in blocked state thereby delaying HPT.
+
#Does not support recursion : If a task tries to take the semaphore twice then it gets blocked and will not come out of that state till someone releases that semaphore.
+
#No ownership : Anyone can release or delete the semaphore because of which the dependent tasks will always be in the blocked state.
+
 
+
*<b>Counting Semaphore</b>: To handle more than one shared resource of the same type, counting semaphore is used. Counting semaphore will be initialized with the count(N) and it will allocate the resource as long as count becomes zero after which the requesting task will enter blocked state.
+
  
*<b>Mutex Semaphore</b>: Mutex is very much similar to binary semaphore and takes care of priority inversion, ownership, and recursion.
+
=Example1=
 +
In this example, we will be creating 3-tasks which run for some time and enter the blocked state allowing other tasks to run. We will include INT0 falling edge interrupt to read and display the current task list with all the task info. Every time interrupt is generated it will take the snapshot of tasks(name, state, priority, num) and sends on Serial port.  
 +
<html><script src="https://gist.github.com/SaheblalBagwan/c696f165daba3e2826cd3b9ebf33785f.js"></script></html>
  
 +
=Output1=
 +
[[FILE:VTaskList().png]]
 +
<br>
 +
All the tasks will be running for some time and enter the blocked state allowing other tasks to run. Task3 suspends and resumes the Task1 alternatively.
 +
Whenever INT0 interrupt is generated, ISR will take the snapshot of all the task and sends it on the serial port.
  
=Priority Inversion=
 
This is a scenario where the HPT waits for the LPT as it is using the resource required by HPT.<br>
 
Let's see 3 different scenarios of priority inversion depending on the amount of time HPT waits for the lower priority tasks.
 
*<b>Normal Priority Inversion:</b>
 
Here an HPT tasks waits for the LPT tasks as it is holding the resource(semaphore).
 
#LPT starts running and acquires the semaphore.
 
#Now HPT is created and it preempts LPT and starts running. It makes the request to acquire the semaphore. Since the semaphore is already with LPT, HPT goes to blocked state.
 
#LPT starts executing again and releases the semaphore.
 
Immediately the HPT comes out of the blocked state and starts executing. It runs for some time and deletes itself.
 
#Now control goes back to LPT which completes its job and deletes itself.
 
#Finally the scheduler is left out with idle task and it keeps running.
 
  
In this scenario the HPT tasks waits for LPT from 2-4 which is the priority inversion period.
 
[[File:PriorityInversion.png]] <br><br>
 
  
*<b>Extended Priority Inversion:</b>
+
=Downloads=
[[File:ExtendedPriorityInversion.png]]
+
Download the complete project and libraries from [https://github.com/ExploreEmbedded/Arduino_FreeRTOS/archive/master.zip here].<br><br>
*<b>Worst Case Priority Inversion:</b>
+
[[File:0WorstCasePriorityInversion.png]]
+
  
=Priority Inheritance=
+
Have an opinion, suggestion , question or feedback about the article let it out here!
 +
{{DISQUS}}

Latest revision as of 17:54, 14 July 2016

In earlier tutorials, we saw how to create, delete, suspend and resume the tasks.
In this tutorial, we will see how to read the Task info(state, Stack size, priority.

0ReadTaskInfo.png


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.

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

vTaskList(): This function is used to read the task details(name, state, priority, num). We need to pass a string pointer(buffer) to which it copies the above task details. The buffer size should be minimum 40 bytes per task. Check this link for more details.

Example1

In this example, we will be creating 3-tasks which run for some time and enter the blocked state allowing other tasks to run. We will include INT0 falling edge interrupt to read and display the current task list with all the task info. Every time interrupt is generated it will take the snapshot of tasks(name, state, priority, num) and sends on Serial port.

Output1

VTaskList().png
All the tasks will be running for some time and enter the blocked state allowing other tasks to run. Task3 suspends and resumes the Task1 alternatively. Whenever INT0 interrupt is generated, ISR will take the snapshot of all the task and sends it on the serial port.


Downloads

Download the complete project and libraries from here.

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