2 FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
39 typedef void * xQueueHandle;
\r
44 xQueueHandle xQueueCreate(
\r
45 unsigned portBASE_TYPE uxQueueLength,
\r
46 unsigned portBASE_TYPE uxItemSize
\r
50 * Creates a new queue instance. This allocates the storage required by the
\r
51 * new queue and returns a handle for the queue.
\r
53 * @param uxQueueLength The maximum number of items that the queue can contain.
\r
55 * @param uxItemSize The number of bytes each item in the queue will require.
\r
56 * Items are queued by copy, not by reference, so this is the number of bytes
\r
57 * that will be copied for each posted item. Each item on the queue must be
\r
60 * @return If the queue is successfully create then a handle to the newly
\r
61 * created queue is returned. If the queue cannot be created then 0 is
\r
68 portCHAR ucMessageID;
\r
69 portCHAR ucData[ 20 ];
\r
72 void vATask( void *pvParameters )
\r
74 xQueueHandle xQueue1, xQueue2;
\r
76 // Create a queue capable of containing 10 unsigned long values.
\r
77 xQueue1 = xQueueCreate( 10, sizeof( unsigned portLONG ) );
\r
80 // Queue was not created and must not be used.
\r
83 // Create a queue capable of containing 10 pointers to AMessage structures.
\r
84 // These should be passed by pointer as they contain a lot of data.
\r
85 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
\r
88 // Queue was not created and must not be used.
\r
91 // ... Rest of task code.
\r
94 * \defgroup xQueueCreate xQueueCreate
\r
95 * \ingroup QueueManagement
\r
97 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
\r
102 portBASE_TYPE xQueueSend(
\r
103 xQueueHandle xQueue,
\r
104 const void * pvItemToQueue,
\r
105 portTickType xTicksToWait
\r
109 * Post an item on a queue. The item is queued by copy, not by reference.
\r
110 * This function must not be called from an interrupt service routine.
\r
111 * See xQueueSendFromISR () for an alternative which may be used in an ISR.
\r
113 * @param xQueue The handle to the queue on which the item is to be posted.
\r
115 * @param pvItemToQueue A pointer to the item that is to be placed on the
\r
116 * queue. The size of the items the queue will hold was defined when the
\r
117 * queue was created, so this many bytes will be copied from pvItemToQueue
\r
118 * into the queue storage area.
\r
120 * @param xTicksToWait The maximum amount of time the task should block
\r
121 * waiting for space to become available on the queue, should it already
\r
122 * be full. The call will return immediately if this is set to 0. The
\r
123 * time is defined in tick periods so the constant portTICK_RATE_MS
\r
124 * should be used to convert to real time if this is required.
\r
126 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
\r
132 portCHAR ucMessageID;
\r
133 portCHAR ucData[ 20 ];
\r
136 unsigned portLONG ulVar = 10UL;
\r
138 void vATask( void *pvParameters )
\r
140 xQueueHandle xQueue1, xQueue2;
\r
141 struct AMessage *pxMessage;
\r
143 // Create a queue capable of containing 10 unsigned long values.
\r
144 xQueue1 = xQueueCreate( 10, sizeof( unsigned portLONG ) );
\r
146 // Create a queue capable of containing 10 pointers to AMessage structures.
\r
147 // These should be passed by pointer as they contain a lot of data.
\r
148 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
\r
154 // Send an unsigned long. Wait for 10 ticks for space to become
\r
155 // available if necessary.
\r
156 if( xQueueSend( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
\r
158 // Failed to post the message, even after 10 ticks.
\r
164 // Send a pointer to a struct AMessage object. Don't block if the
\r
165 // queue is already full.
\r
166 pxMessage = & xMessage;
\r
167 xQueueSend( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
\r
170 // ... Rest of task code.
\r
173 * \defgroup xQueueSend xQueueSend
\r
174 * \ingroup QueueManagement
\r
176 signed portBASE_TYPE xQueueSend( xQueueHandle xQueue, const void * pvItemToQueue, portTickType xTicksToWait );
\r
181 portBASE_TYPE xQueueReceive(
\r
182 xQueueHandle xQueue,
\r
184 portTickType xTicksToWait
\r
187 * Receive an item from a queue. The item is received by copy so a buffer of
\r
188 * adequate size must be provided. The number of bytes copied into the buffer
\r
189 * was defined when the queue was created.
\r
191 * This function must not be used in an interrupt service routine. See
\r
192 * xQueueReceiveFromISR for an alternative that can.
\r
194 * @param pxQueue The handle to the queue from which the item is to be
\r
197 * @param pvBuffer Pointer to the buffer into which the received item will
\r
200 * @param xTicksToWait The maximum amount of time the task should block
\r
201 * waiting for an item to receive should the queue be empty at the time
\r
202 * of the call. The time is defined in tick periods so the constant
\r
203 * portTICK_RATE_MS should be used to convert to real time if this is required.
\r
205 * @return pdTRUE if an item was successfully received from the queue,
\r
206 * otherwise pdFALSE.
\r
212 portCHAR ucMessageID;
\r
213 portCHAR ucData[ 20 ];
\r
216 xQueueHandle xQueue;
\r
218 // Task to create a queue and post a value.
\r
219 void vATask( void *pvParameters )
\r
221 struct AMessage *pxMessage;
\r
223 // Create a queue capable of containing 10 pointers to AMessage structures.
\r
224 // These should be passed by pointer as they contain a lot of data.
\r
225 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
\r
228 // Failed to create the queue.
\r
233 // Send a pointer to a struct AMessage object. Don't block if the
\r
234 // queue is already full.
\r
235 pxMessage = & xMessage;
\r
236 xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
\r
238 // ... Rest of task code.
\r
241 // Task to receive from the queue.
\r
242 void vADifferentTask( void *pvParameters )
\r
244 struct AMessage *pxRxedMessage;
\r
248 // Receive a message on the created queue. Block for 10 ticks if a
\r
249 // message is not immediately available.
\r
250 if( xQueueReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
\r
252 // pcRxedMessage now points to the struct AMessage variable posted
\r
257 // ... Rest of task code.
\r
260 * \defgroup xQueueReceive xQueueReceive
\r
261 * \ingroup QueueManagement
\r
263 signed portBASE_TYPE xQueueReceive( xQueueHandle xQueue, void *pvBuffer, portTickType xTicksToWait );
\r
267 * <pre>unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle xQueue );</pre>
\r
269 * Return the number of messages stored in a queue.
\r
271 * @param xQueue A handle to the queue being queried.
\r
273 * @return The number of messages available in the queue.
\r
275 * \page uxQueueMessagesWaiting uxQueueMessagesWaiting
\r
276 * \ingroup QueueManagement
\r
278 unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle xQueue );
\r
282 * <pre>void vQueueDelete( xQueueHandle xQueue );</pre>
\r
284 * Delete a queue - freeing all the memory allocated for storing of items
\r
285 * placed on the queue.
\r
287 * @param xQueue A handle to the queue to be deleted.
\r
289 * \page vQueueDelete vQueueDelete
\r
290 * \ingroup QueueManagement
\r
292 void vQueueDelete( xQueueHandle xQueue );
\r
297 portBASE_TYPE xQueueSendFromISR(
\r
298 xQueueHandle pxQueue,
\r
299 const void *pvItemToQueue,
\r
300 portBASE_TYPE xTaskPreviouslyWoken
\r
304 * Post an item on a queue. It is safe to use this function from within an
\r
305 * interrupt service routine.
\r
307 * Items are queued by copy not reference so it is preferable to only
\r
308 * queue small items, especially when called from an ISR. In most cases
\r
309 * it would be preferable to store a pointer to the item being queued.
\r
311 * @param xQueue The handle to the queue on which the item is to be posted.
\r
313 * @param pvItemToQueue A pointer to the item that is to be placed on the
\r
314 * queue. The size of the items the queue will hold was defined when the
\r
315 * queue was created, so this many bytes will be copied from pvItemToQueue
\r
316 * into the queue storage area.
\r
318 * @param cTaskPreviouslyWoken This is included so an ISR can post onto
\r
319 * the same queue multiple times from a single interrupt. The first call
\r
320 * should always pass in pdFALSE. Subsequent calls should pass in
\r
321 * the value returned from the previous call. See the file serial .c in the
\r
322 * PC port for a good example of this mechanism.
\r
324 * @return pdTRUE if a task was woken by posting onto the queue. This is
\r
325 * used by the ISR to determine if a context switch may be required following
\r
328 * Example usage for buffered IO (where the ISR can obtain more than one value
\r
331 void vBufferISR( void )
\r
334 portBASE_TYPE xTaskWokenByPost;
\r
336 // We have not woken a task at the start of the ISR.
\r
337 cTaskWokenByPost = pdFALSE;
\r
339 // Loop until the buffer is empty.
\r
342 // Obtain a byte from the buffer.
\r
343 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
\r
345 // Post the byte. The first time round the loop cTaskWokenByPost
\r
346 // will be pdFALSE. If the queue send causes a task to wake we do
\r
347 // not want the task to run until we have finished the ISR, so
\r
348 // xQueueSendFromISR does not cause a context switch. Also we
\r
349 // don't want subsequent posts to wake any other tasks, so we store
\r
350 // the return value back into cTaskWokenByPost so xQueueSendFromISR
\r
351 // knows not to wake any task the next iteration of the loop.
\r
352 xTaskWokenByPost = xQueueSendFromISR( xRxQueue, &cIn, cTaskWokenByPost );
\r
354 } while( portINPUT_BYTE( BUFFER_COUNT ) );
\r
356 // Now the buffer is empty we can switch context if necessary.
\r
357 if( cTaskWokenByPost )
\r
364 * \defgroup xQueueSendFromISR xQueueSendFromISR
\r
365 * \ingroup QueueManagement
\r
367 signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken );
\r
372 portBASE_TYPE xQueueReceiveFromISR(
\r
373 xQueueHandle pxQueue,
\r
375 portBASE_TYPE *pxTaskWoken
\r
379 * Receive an item from a queue. It is safe to use this function from within an
\r
380 * interrupt service routine.
\r
382 * @param pxQueue The handle to the queue from which the item is to be
\r
385 * @param pvBuffer Pointer to the buffer into which the received item will
\r
388 * @param pxTaskWoken A task may be blocked waiting for space to become
\r
389 * available on the queue. If xQueueReceiveFromISR causes such a task to
\r
390 * unblock *pxTaskWoken will get set to pdTRUE, otherwise *pxTaskWoken will
\r
391 * remain unchanged.
\r
393 * @return pdTRUE if an item was successfully received from the queue,
\r
394 * otherwise pdFALSE.
\r
399 xQueueHandle xQueue;
\r
401 // Function to create a queue and post some values.
\r
402 void vAFunction( void *pvParameters )
\r
404 portCHAR cValueToPost;
\r
405 const portTickType xBlockTime = ( portTickType )0xff;
\r
407 // Create a queue capable of containing 10 characters.
\r
408 xQueue = xQueueCreate( 10, sizeof( portCHAR ) );
\r
411 // Failed to create the queue.
\r
416 // Post some characters that will be used within an ISR. If the queue
\r
417 // is full then this task will block for xBlockTime ticks.
\r
418 cValueToPost = 'a';
\r
419 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
\r
420 cValueToPost = 'b';
\r
421 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
\r
423 // ... keep posting characters ... this task may block when the queue
\r
426 cValueToPost = 'c';
\r
427 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
\r
430 // ISR that outputs all the characters received on the queue.
\r
431 void vISR_Routine( void )
\r
433 portBASE_TYPE xTaskWokenByReceive = pdFALSE;
\r
434 portCHAR cRxedChar;
\r
436 while( xQueueReceiveFromISR( xQueue, ( void * ) &cRxedChar, &xTaskWokenByReceive) )
\r
438 // A character was received. Output the character now.
\r
439 vOutputCharacter( cRxedChar );
\r
441 // If removing the character from the queue woke the task that was
\r
442 // posting onto the queue cTaskWokenByReceive will have been set to
\r
443 // pdTRUE. No matter how many times this loop iterates only one
\r
444 // task will be woken.
\r
447 if( cTaskWokenByPost != ( portCHAR ) pdFALSE;
\r
453 * \defgroup xQueueReceiveFromISR xQueueReceiveFromISR
\r
454 * \ingroup QueueManagement
\r
456 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
460 * The functions defined above are for passing data to and from tasks. The
\r
461 * functions below are the equivalents for passing data to and from
\r
464 * These functions are called from the co-routine macro implementation and
\r
465 * should not be called directly from application code. Instead use the macro
\r
466 * wrappers defined within croutine.h.
\r
468 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
\r
469 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
\r
470 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
\r
471 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
\r