m
m
 
(8 intermediate revisions by the same user not shown)
Line 12: Line 12:
 
The size of data item and maximum number of data items are fixed when queue is created.
 
The size of data item and maximum number of data items are fixed when queue is created.
  
 +
Queue can also be used as semaphore, mutex, event flag, etc. FreeRTOS does the same. It reduces memory usage in case of using multiple RTOS services e.g. semaphore and queue in same application.
  
 
==Operations on queue==
 
==Operations on queue==
Line 22: Line 23:
 
==API Details==
 
==API Details==
  
 
+
====Creating a queue====
 
{|{{Widget:LibCol}}
 
{|{{Widget:LibCol}}
 
{{#Widget:LibTable}}
 
{{#Widget:LibTable}}
 
|-
 
|-
|Defination ||xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength,
+
|Defination ||xQueueHandle xQueueCreate (
 +
:unsigned portBASE_TYPE uxQueueLength,
 
:unsigned portBASE_TYPE uxItemSize)
 
:unsigned portBASE_TYPE uxItemSize)
 
|-
 
|-
Line 42: Line 44:
 
MyQueueHandleId = xQueueCreate(3,20);
 
MyQueueHandleId = xQueueCreate(3,20);
 
|}
 
|}
 
 
 
 
====Writting data to the queue====
 
====Writting data to the queue====
 
{|{{Widget:LibCol}}
 
{|{{Widget:LibCol}}
 
{{#Widget:LibTable}}
 
{{#Widget:LibTable}}
 
|-
 
|-
|Defination ||portBASE_TYPE xQueueSend(
+
|Defination ||portBASE_TYPE xQueueSend (
 
:xQueueHandle xQueue,
 
:xQueueHandle xQueue,
 
:const void * pvItemToQueue,
 
:const void * pvItemToQueue,
Line 60: Line 59:
 
xQueueSend(MyQueueHandleId,TxBuffer,100)
 
xQueueSend(MyQueueHandleId,TxBuffer,100)
 
|}
 
|}
 
 
 
 
====Writting data at the front of queue====
 
====Writting data at the front of queue====
 
{|{{Widget:LibCol}}
 
{|{{Widget:LibCol}}
 
{{#Widget:LibTable}}
 
{{#Widget:LibTable}}
 
|-
 
|-
|Defination ||portBASE_TYPE xQueueSendToFront( xQueueHandle xQueue,
+
|Defination ||portBASE_TYPE xQueueSendToFront (
 +
:xQueueHandle xQueue,
 
:const void * pvItemToQueue,
 
:const void * pvItemToQueue,
 
:portTickType xTicksToWait)
 
:portTickType xTicksToWait)
Line 83: Line 81:
 
xQueueSendToFront(MyQueueHandleId,TxBuffer,100)
 
xQueueSendToFront(MyQueueHandleId,TxBuffer,100)
 
|}
 
|}
 
 
====Writting data at the back of queue====
 
====Writting data at the back of queue====
 
 
  {|{{Widget:LibCol}}
 
  {|{{Widget:LibCol}}
 
{{#Widget:LibTable}}
 
{{#Widget:LibTable}}
 
|-
 
|-
|Defination ||portBASE_TYPE xQueueSendToBack( xQueueHandle xQueue,
+
|Defination ||portBASE_TYPE xQueueSendToBack (  
 +
:xQueueHandle xQueue,
 
:const void * pvItemToQueue,
 
:const void * pvItemToQueue,
 
:portTickType xTicksToWait)
 
:portTickType xTicksToWait)
Line 106: Line 103:
 
xQueueSendToBack(MyQueueHandleId,TxBuffer,100)
 
xQueueSendToBack(MyQueueHandleId,TxBuffer,100)
 
|}
 
|}
 
 
====Receiving data from the queue====
 
====Receiving data from the queue====
 
 
{|{{Widget:LibCol}}
 
{|{{Widget:LibCol}}
 
{{#Widget:LibTable}}
 
{{#Widget:LibTable}}
 
|-
 
|-
|Defination ||portBASE_TYPE xQueueReceive(xQueueHandle xQueue,
+
|Defination ||portBASE_TYPE xQueueReceive (
 +
:xQueueHandle xQueue,
 
:void *pvBuffer,
 
:void *pvBuffer,
 
:portTickType xTicksToWait)
 
:portTickType xTicksToWait)
 
|-
 
|-
| Input Arguments ||#xQueueHandle : handle to the queue in which data write is desired.
+
| Input Arguments ||
 +
#xQueueHandle : handle to the queue in which data write is desired.
 
#void * : pointer to the buffer where received item will be copied  
 
#void * : pointer to the buffer where received item will be copied  
 
#portTickType : the max amount of time for which task will wait for a item to become available on the queue if queue is empty
 
#portTickType : the max amount of time for which task will wait for a item to become available on the queue if queue is empty
Line 122: Line 119:
 
| Return Value|| returns TRUE on successfully posting the item else FALSE
 
| Return Value|| returns TRUE on successfully posting the item else FALSE
 
|-
 
|-
| Description ||  
+
| Description || It reads data from the front of the queue.
 
|-
 
|-
 
| Usage || char RxBuffer[MaxElementsPerQueue];
 
| Usage || char RxBuffer[MaxElementsPerQueue];

Latest revision as of 17:48, 27 April 2015

Amruta (talk) 15:58, 14 April 2015 (IST)


Intro

A queue is a FIFO (First In First Out) type buffer where data is written to the end (tail) of the queue and removed from the front (head) of the queue. It is also possible to write to the front of a queue.

A queue can either hold the data or pointer to the data. In FreeRTOS, the data items are directly copied to the queue. Each data item is of fixed size. The size of data item and maximum number of data items are fixed when queue is created.

Queue can also be used as semaphore, mutex, event flag, etc. FreeRTOS does the same. It reduces memory usage in case of using multiple RTOS services e.g. semaphore and queue in same application.

Operations on queue

  1. Create
  2. Read
In read operation, the data item is returned. If queue is empty the requesting task will wait for specified time.If multiple tasks are waiting, then data item will be returned to the either highest priority task or the one which made the request first depending upon RTOS implementation.For the second case waiting list is maintained with each queue. In FreeRTOS, queue is implemented in FIRST way.
  1. Write(Pend)
In write operation, the data item is directly copied to queue. If queue is full the requesting task will wait for specified time. For multiple tasks in waite state, process is same as read operation.

API Details

Creating a queue

{{#Widget:LibTable}}
Defination xQueueHandle xQueueCreate (
unsigned portBASE_TYPE uxQueueLength,
unsigned portBASE_TYPE uxItemSize)
Input Arguments
  1. unsigned portBASE_TYPE : The maximum number of items that the queue can hold at a time.
  2. unsigned portBASE_TYPE : The size in bytes of data item to be stored in the queue.
Return Value xQueueHandle : NULL when queue is not created

Non NULL when queue is created.

Description It creates queue.
Usage xQueueHandle MyQueueHandleId;

MyQueueHandleId = xQueueCreate(3,20);

Writting data to the queue

{{#Widget:LibTable}}
Defination portBASE_TYPE xQueueSend (
xQueueHandle xQueue,
const void * pvItemToQueue,
portTickType xTicksToWait)
Description It is equivalent to xQueueSendToBack().
Usage char TxBuffer[MaxElementsPerQueue]={"Hello world"};

xQueueSend(MyQueueHandleId,TxBuffer,100)

Writting data at the front of queue

{{#Widget:LibTable}}
Defination portBASE_TYPE xQueueSendToFront (
xQueueHandle xQueue,
const void * pvItemToQueue,
portTickType xTicksToWait)
Input Arguments
  1. xQueueHandle : handle to the queue in which data is to be posted
  2. const void * : pointer to the item to be posted
  3. portTickType : the max amount of time for which task will wait for a space to become available on the queue if queue is full
Return Value returns TRUE on successfully posting the item else FALSE
Description It posts item at the back of the queue.
Usage char TxBuffer[MaxElementsPerQueue]={"Hello world"};

xQueueSendToFront(MyQueueHandleId,TxBuffer,100)

Writting data at the back of queue

{{#Widget:LibTable}}
Defination portBASE_TYPE xQueueSendToBack (
xQueueHandle xQueue,
const void * pvItemToQueue,
portTickType xTicksToWait)
Input Arguments
  1. xQueueHandle : handle to the queue in which data is to be posted
  2. const void * : pointer to the item to be posted
  3. portTickType : the max amount of time for which task will wait for a space to become available on the queue if queue is full
Return Value returns TRUE on successfully posting the item else FALSE
Description It posts item at the front of the queue.
Usage char TxBuffer[MaxElementsPerQueue]={"Hello world"};

xQueueSendToBack(MyQueueHandleId,TxBuffer,100)

Receiving data from the queue

{{#Widget:LibTable}}
Defination portBASE_TYPE xQueueReceive (
xQueueHandle xQueue,
void *pvBuffer,
portTickType xTicksToWait)
Input Arguments
  1. xQueueHandle : handle to the queue in which data write is desired.
  2. void * : pointer to the buffer where received item will be copied
  3. portTickType : the max amount of time for which task will wait for a item to become available on the queue if queue is empty
Return Value returns TRUE on successfully posting the item else FALSE
Description It reads data from the front of the queue.
Usage char RxBuffer[MaxElementsPerQueue];

xQueueReceive(MyQueueHandleId,RxBuffer,100);