2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
5 ***************************************************************************
7 * FreeRTOS tutorial books are available in pdf and paperback. *
8 * Complete, revised, and edited pdf reference manuals are also *
11 * Purchasing FreeRTOS documentation will not only help you, by *
12 * ensuring you get running as quickly as possible and with an *
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
14 * the FreeRTOS project to continue with its mission of providing *
15 * professional grade, cross platform, de facto standard solutions *
16 * for microcontrollers - completely free of charge! *
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
20 * Thank you for using FreeRTOS, and thank you for your support! *
22 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 >>>NOTE<<< The modification to the GPL is included to allow you to
31 distribute a combined work that includes FreeRTOS without being obliged to
32 provide the source code for proprietary components outside of the FreeRTOS
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
44 http://www.FreeRTOS.org - Documentation, latest information, license and
47 http://www.SafeRTOS.com - A version that is certified for use in safety
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
58 #ifndef INC_FREERTOS_H
59 #error "#include FreeRTOS.h" must appear in source files before "#include queue.h"
67 #include "os/mpu_wrappers.h"
70 * Type by which queues are referenced. For example, a call to xQueueCreate
71 * returns (via a pointer parameter) an xQueueHandle variable that can then
72 * be used as a parameter to xQueueSend(), xQueueReceive(), etc.
74 typedef void * xQueueHandle;
77 /* For internal use only. */
78 #define queueSEND_TO_BACK ( 0 )
79 #define queueSEND_TO_FRONT ( 1 )
85 xQueueHandle xQueueCreate(
86 unsigned portBASE_TYPE uxQueueLength,
87 unsigned portBASE_TYPE uxItemSize
91 * Creates a new queue instance. This allocates the storage required by the
92 * new queue and returns a handle for the queue.
94 * @param uxQueueLength The maximum number of items that the queue can contain.
96 * @param uxItemSize The number of bytes each item in the queue will require.
97 * Items are queued by copy, not by reference, so this is the number of bytes
98 * that will be copied for each posted item. Each item on the queue must be
101 * @return If the queue is successfully create then a handle to the newly
102 * created queue is returned. If the queue cannot be created then 0 is
113 void vATask( void *pvParameters )
115 xQueueHandle xQueue1, xQueue2;
117 // Create a queue capable of containing 10 unsigned long values.
118 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
121 // Queue was not created and must not be used.
124 // Create a queue capable of containing 10 pointers to AMessage structures.
125 // These should be passed by pointer as they contain a lot of data.
126 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
129 // Queue was not created and must not be used.
132 // ... Rest of task code.
135 * \defgroup xQueueCreate xQueueCreate
136 * \ingroup QueueManagement
138 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
143 portBASE_TYPE xQueueSendToToFront(
145 const void * pvItemToQueue,
146 portTickType xTicksToWait
150 * This is a macro that calls xQueueGenericSend().
152 * Post an item to the front of a queue. The item is queued by copy, not by
153 * reference. This function must not be called from an interrupt service
154 * routine. See xQueueSendFromISR () for an alternative which may be used
157 * @param xQueue The handle to the queue on which the item is to be posted.
159 * @param pvItemToQueue A pointer to the item that is to be placed on the
160 * queue. The size of the items the queue will hold was defined when the
161 * queue was created, so this many bytes will be copied from pvItemToQueue
162 * into the queue storage area.
164 * @param xTicksToWait The maximum amount of time the task should block
165 * waiting for space to become available on the queue, should it already
166 * be full. The call will return immediately if this is set to 0 and the
167 * queue is full. The time is defined in tick periods so the constant
168 * portTICK_RATE_MS should be used to convert to real time if this is required.
170 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
180 unsigned long ulVar = 10UL;
182 void vATask( void *pvParameters )
184 xQueueHandle xQueue1, xQueue2;
185 struct AMessage *pxMessage;
187 // Create a queue capable of containing 10 unsigned long values.
188 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
190 // Create a queue capable of containing 10 pointers to AMessage structures.
191 // These should be passed by pointer as they contain a lot of data.
192 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
198 // Send an unsigned long. Wait for 10 ticks for space to become
199 // available if necessary.
200 if( xQueueSendToFront( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
202 // Failed to post the message, even after 10 ticks.
208 // Send a pointer to a struct AMessage object. Don't block if the
209 // queue is already full.
210 pxMessage = & xMessage;
211 xQueueSendToFront( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
214 // ... Rest of task code.
217 * \defgroup xQueueSend xQueueSend
218 * \ingroup QueueManagement
220 #define xQueueSendToFront( xQueue, pvItemToQueue, xTicksToWait ) xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_FRONT )
225 portBASE_TYPE xQueueSendToBack(
227 const void * pvItemToQueue,
228 portTickType xTicksToWait
232 * This is a macro that calls xQueueGenericSend().
234 * Post an item to the back of a queue. The item is queued by copy, not by
235 * reference. This function must not be called from an interrupt service
236 * routine. See xQueueSendFromISR () for an alternative which may be used
239 * @param xQueue The handle to the queue on which the item is to be posted.
241 * @param pvItemToQueue A pointer to the item that is to be placed on the
242 * queue. The size of the items the queue will hold was defined when the
243 * queue was created, so this many bytes will be copied from pvItemToQueue
244 * into the queue storage area.
246 * @param xTicksToWait The maximum amount of time the task should block
247 * waiting for space to become available on the queue, should it already
248 * be full. The call will return immediately if this is set to 0 and the queue
249 * is full. The time is defined in tick periods so the constant
250 * portTICK_RATE_MS should be used to convert to real time if this is required.
252 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
262 unsigned long ulVar = 10UL;
264 void vATask( void *pvParameters )
266 xQueueHandle xQueue1, xQueue2;
267 struct AMessage *pxMessage;
269 // Create a queue capable of containing 10 unsigned long values.
270 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
272 // Create a queue capable of containing 10 pointers to AMessage structures.
273 // These should be passed by pointer as they contain a lot of data.
274 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
280 // Send an unsigned long. Wait for 10 ticks for space to become
281 // available if necessary.
282 if( xQueueSendToBack( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
284 // Failed to post the message, even after 10 ticks.
290 // Send a pointer to a struct AMessage object. Don't block if the
291 // queue is already full.
292 pxMessage = & xMessage;
293 xQueueSendToBack( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
296 // ... Rest of task code.
299 * \defgroup xQueueSend xQueueSend
300 * \ingroup QueueManagement
302 #define xQueueSendToBack( xQueue, pvItemToQueue, xTicksToWait ) xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_BACK )
307 portBASE_TYPE xQueueSend(
309 const void * pvItemToQueue,
310 portTickType xTicksToWait
314 * This is a macro that calls xQueueGenericSend(). It is included for
315 * backward compatibility with versions of FreeRTOS.org that did not
316 * include the xQueueSendToFront() and xQueueSendToBack() macros. It is
317 * equivalent to xQueueSendToBack().
319 * Post an item on a queue. The item is queued by copy, not by reference.
320 * This function must not be called from an interrupt service routine.
321 * See xQueueSendFromISR () for an alternative which may be used in an ISR.
323 * @param xQueue The handle to the queue on which the item is to be posted.
325 * @param pvItemToQueue A pointer to the item that is to be placed on the
326 * queue. The size of the items the queue will hold was defined when the
327 * queue was created, so this many bytes will be copied from pvItemToQueue
328 * into the queue storage area.
330 * @param xTicksToWait The maximum amount of time the task should block
331 * waiting for space to become available on the queue, should it already
332 * be full. The call will return immediately if this is set to 0 and the
333 * queue is full. The time is defined in tick periods so the constant
334 * portTICK_RATE_MS should be used to convert to real time if this is required.
336 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
346 unsigned long ulVar = 10UL;
348 void vATask( void *pvParameters )
350 xQueueHandle xQueue1, xQueue2;
351 struct AMessage *pxMessage;
353 // Create a queue capable of containing 10 unsigned long values.
354 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
356 // Create a queue capable of containing 10 pointers to AMessage structures.
357 // These should be passed by pointer as they contain a lot of data.
358 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
364 // Send an unsigned long. Wait for 10 ticks for space to become
365 // available if necessary.
366 if( xQueueSend( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
368 // Failed to post the message, even after 10 ticks.
374 // Send a pointer to a struct AMessage object. Don't block if the
375 // queue is already full.
376 pxMessage = & xMessage;
377 xQueueSend( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
380 // ... Rest of task code.
383 * \defgroup xQueueSend xQueueSend
384 * \ingroup QueueManagement
386 #define xQueueSend( xQueue, pvItemToQueue, xTicksToWait ) xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_BACK )
392 portBASE_TYPE xQueueGenericSend(
394 const void * pvItemToQueue,
395 portTickType xTicksToWait
396 portBASE_TYPE xCopyPosition
400 * It is preferred that the macros xQueueSend(), xQueueSendToFront() and
401 * xQueueSendToBack() are used in place of calling this function directly.
403 * Post an item on a queue. The item is queued by copy, not by reference.
404 * This function must not be called from an interrupt service routine.
405 * See xQueueSendFromISR () for an alternative which may be used in an ISR.
407 * @param xQueue The handle to the queue on which the item is to be posted.
409 * @param pvItemToQueue A pointer to the item that is to be placed on the
410 * queue. The size of the items the queue will hold was defined when the
411 * queue was created, so this many bytes will be copied from pvItemToQueue
412 * into the queue storage area.
414 * @param xTicksToWait The maximum amount of time the task should block
415 * waiting for space to become available on the queue, should it already
416 * be full. The call will return immediately if this is set to 0 and the
417 * queue is full. The time is defined in tick periods so the constant
418 * portTICK_RATE_MS should be used to convert to real time if this is required.
420 * @param xCopyPosition Can take the value queueSEND_TO_BACK to place the
421 * item at the back of the queue, or queueSEND_TO_FRONT to place the item
422 * at the front of the queue (for high priority messages).
424 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
434 unsigned long ulVar = 10UL;
436 void vATask( void *pvParameters )
438 xQueueHandle xQueue1, xQueue2;
439 struct AMessage *pxMessage;
441 // Create a queue capable of containing 10 unsigned long values.
442 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
444 // Create a queue capable of containing 10 pointers to AMessage structures.
445 // These should be passed by pointer as they contain a lot of data.
446 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
452 // Send an unsigned long. Wait for 10 ticks for space to become
453 // available if necessary.
454 if( xQueueGenericSend( xQueue1, ( void * ) &ulVar, ( portTickType ) 10, queueSEND_TO_BACK ) != pdPASS )
456 // Failed to post the message, even after 10 ticks.
462 // Send a pointer to a struct AMessage object. Don't block if the
463 // queue is already full.
464 pxMessage = & xMessage;
465 xQueueGenericSend( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0, queueSEND_TO_BACK );
468 // ... Rest of task code.
471 * \defgroup xQueueSend xQueueSend
472 * \ingroup QueueManagement
474 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
479 portBASE_TYPE xQueuePeek(
482 portTickType xTicksToWait
485 * This is a macro that calls the xQueueGenericReceive() function.
487 * Receive an item from a queue without removing the item from the queue.
488 * The item is received by copy so a buffer of adequate size must be
489 * provided. The number of bytes copied into the buffer was defined when
490 * the queue was created.
492 * Successfully received items remain on the queue so will be returned again
493 * by the next call, or a call to xQueueReceive().
495 * This macro must not be used in an interrupt service routine.
497 * @param pxQueue The handle to the queue from which the item is to be
500 * @param pvBuffer Pointer to the buffer into which the received item will
503 * @param xTicksToWait The maximum amount of time the task should block
504 * waiting for an item to receive should the queue be empty at the time
505 * of the call. The time is defined in tick periods so the constant
506 * portTICK_RATE_MS should be used to convert to real time if this is required.
507 * xQueuePeek() will return immediately if xTicksToWait is 0 and the queue
510 * @return pdTRUE if an item was successfully received from the queue,
523 // Task to create a queue and post a value.
524 void vATask( void *pvParameters )
526 struct AMessage *pxMessage;
528 // Create a queue capable of containing 10 pointers to AMessage structures.
529 // These should be passed by pointer as they contain a lot of data.
530 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
533 // Failed to create the queue.
538 // Send a pointer to a struct AMessage object. Don't block if the
539 // queue is already full.
540 pxMessage = & xMessage;
541 xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
543 // ... Rest of task code.
546 // Task to peek the data from the queue.
547 void vADifferentTask( void *pvParameters )
549 struct AMessage *pxRxedMessage;
553 // Peek a message on the created queue. Block for 10 ticks if a
554 // message is not immediately available.
555 if( xQueuePeek( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
557 // pcRxedMessage now points to the struct AMessage variable posted
558 // by vATask, but the item still remains on the queue.
562 // ... Rest of task code.
565 * \defgroup xQueueReceive xQueueReceive
566 * \ingroup QueueManagement
568 #define xQueuePeek( xQueue, pvBuffer, xTicksToWait ) xQueueGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdTRUE )
573 portBASE_TYPE xQueueReceive(
576 portTickType xTicksToWait
579 * This is a macro that calls the xQueueGenericReceive() function.
581 * Receive an item from a queue. The item is received by copy so a buffer of
582 * adequate size must be provided. The number of bytes copied into the buffer
583 * was defined when the queue was created.
585 * Successfully received items are removed from the queue.
587 * This function must not be used in an interrupt service routine. See
588 * xQueueReceiveFromISR for an alternative that can.
590 * @param pxQueue The handle to the queue from which the item is to be
593 * @param pvBuffer Pointer to the buffer into which the received item will
596 * @param xTicksToWait The maximum amount of time the task should block
597 * waiting for an item to receive should the queue be empty at the time
598 * of the call. xQueueReceive() will return immediately if xTicksToWait
599 * is zero and the queue is empty. The time is defined in tick periods so the
600 * constant portTICK_RATE_MS should be used to convert to real time if this is
603 * @return pdTRUE if an item was successfully received from the queue,
616 // Task to create a queue and post a value.
617 void vATask( void *pvParameters )
619 struct AMessage *pxMessage;
621 // Create a queue capable of containing 10 pointers to AMessage structures.
622 // These should be passed by pointer as they contain a lot of data.
623 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
626 // Failed to create the queue.
631 // Send a pointer to a struct AMessage object. Don't block if the
632 // queue is already full.
633 pxMessage = & xMessage;
634 xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
636 // ... Rest of task code.
639 // Task to receive from the queue.
640 void vADifferentTask( void *pvParameters )
642 struct AMessage *pxRxedMessage;
646 // Receive a message on the created queue. Block for 10 ticks if a
647 // message is not immediately available.
648 if( xQueueReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
650 // pcRxedMessage now points to the struct AMessage variable posted
655 // ... Rest of task code.
658 * \defgroup xQueueReceive xQueueReceive
659 * \ingroup QueueManagement
661 #define xQueueReceive( xQueue, pvBuffer, xTicksToWait ) xQueueGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdFALSE )
667 portBASE_TYPE xQueueGenericReceive(
670 portTickType xTicksToWait
671 portBASE_TYPE xJustPeek
674 * It is preferred that the macro xQueueReceive() be used rather than calling
675 * this function directly.
677 * Receive an item from a queue. The item is received by copy so a buffer of
678 * adequate size must be provided. The number of bytes copied into the buffer
679 * was defined when the queue was created.
681 * This function must not be used in an interrupt service routine. See
682 * xQueueReceiveFromISR for an alternative that can.
684 * @param pxQueue The handle to the queue from which the item is to be
687 * @param pvBuffer Pointer to the buffer into which the received item will
690 * @param xTicksToWait The maximum amount of time the task should block
691 * waiting for an item to receive should the queue be empty at the time
692 * of the call. The time is defined in tick periods so the constant
693 * portTICK_RATE_MS should be used to convert to real time if this is required.
694 * xQueueGenericReceive() will return immediately if the queue is empty and
697 * @param xJustPeek When set to true, the item received from the queue is not
698 * actually removed from the queue - meaning a subsequent call to
699 * xQueueReceive() will return the same item. When set to false, the item
700 * being received from the queue is also removed from the queue.
702 * @return pdTRUE if an item was successfully received from the queue,
715 // Task to create a queue and post a value.
716 void vATask( void *pvParameters )
718 struct AMessage *pxMessage;
720 // Create a queue capable of containing 10 pointers to AMessage structures.
721 // These should be passed by pointer as they contain a lot of data.
722 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
725 // Failed to create the queue.
730 // Send a pointer to a struct AMessage object. Don't block if the
731 // queue is already full.
732 pxMessage = & xMessage;
733 xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
735 // ... Rest of task code.
738 // Task to receive from the queue.
739 void vADifferentTask( void *pvParameters )
741 struct AMessage *pxRxedMessage;
745 // Receive a message on the created queue. Block for 10 ticks if a
746 // message is not immediately available.
747 if( xQueueGenericReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
749 // pcRxedMessage now points to the struct AMessage variable posted
754 // ... Rest of task code.
757 * \defgroup xQueueReceive xQueueReceive
758 * \ingroup QueueManagement
760 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle xQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeek );
764 * <pre>unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle xQueue );</pre>
766 * Return the number of messages stored in a queue.
768 * @param xQueue A handle to the queue being queried.
770 * @return The number of messages available in the queue.
772 * \page uxQueueMessagesWaiting uxQueueMessagesWaiting
773 * \ingroup QueueManagement
775 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle xQueue );
779 * <pre>void vQueueDelete( xQueueHandle xQueue );</pre>
781 * Delete a queue - freeing all the memory allocated for storing of items
782 * placed on the queue.
784 * @param xQueue A handle to the queue to be deleted.
786 * \page vQueueDelete vQueueDelete
787 * \ingroup QueueManagement
789 void vQueueDelete( xQueueHandle pxQueue );
794 portBASE_TYPE xQueueSendToFrontFromISR(
795 xQueueHandle pxQueue,
796 const void *pvItemToQueue,
797 portBASE_TYPE *pxHigherPriorityTaskWoken
801 * This is a macro that calls xQueueGenericSendFromISR().
803 * Post an item to the front of a queue. It is safe to use this macro from
804 * within an interrupt service routine.
806 * Items are queued by copy not reference so it is preferable to only
807 * queue small items, especially when called from an ISR. In most cases
808 * it would be preferable to store a pointer to the item being queued.
810 * @param xQueue The handle to the queue on which the item is to be posted.
812 * @param pvItemToQueue A pointer to the item that is to be placed on the
813 * queue. The size of the items the queue will hold was defined when the
814 * queue was created, so this many bytes will be copied from pvItemToQueue
815 * into the queue storage area.
817 * @param pxHigherPriorityTaskWoken xQueueSendToFrontFromISR() will set
818 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
819 * to unblock, and the unblocked task has a priority higher than the currently
820 * running task. If xQueueSendToFromFromISR() sets this value to pdTRUE then
821 * a context switch should be requested before the interrupt is exited.
823 * @return pdTRUE if the data was successfully sent to the queue, otherwise
826 * Example usage for buffered IO (where the ISR can obtain more than one value
829 void vBufferISR( void )
832 portBASE_TYPE xHigherPrioritTaskWoken;
834 // We have not woken a task at the start of the ISR.
835 xHigherPriorityTaskWoken = pdFALSE;
837 // Loop until the buffer is empty.
840 // Obtain a byte from the buffer.
841 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
844 xQueueSendToFrontFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken );
846 } while( portINPUT_BYTE( BUFFER_COUNT ) );
848 // Now the buffer is empty we can switch context if necessary.
849 if( xHigherPriorityTaskWoken )
856 * \defgroup xQueueSendFromISR xQueueSendFromISR
857 * \ingroup QueueManagement
859 #define xQueueSendToFrontFromISR( pxQueue, pvItemToQueue, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( pxHigherPriorityTaskWoken ), queueSEND_TO_FRONT )
865 portBASE_TYPE xQueueSendToBackFromISR(
866 xQueueHandle pxQueue,
867 const void *pvItemToQueue,
868 portBASE_TYPE *pxHigherPriorityTaskWoken
872 * This is a macro that calls xQueueGenericSendFromISR().
874 * Post an item to the back of a queue. It is safe to use this macro from
875 * within an interrupt service routine.
877 * Items are queued by copy not reference so it is preferable to only
878 * queue small items, especially when called from an ISR. In most cases
879 * it would be preferable to store a pointer to the item being queued.
881 * @param xQueue The handle to the queue on which the item is to be posted.
883 * @param pvItemToQueue A pointer to the item that is to be placed on the
884 * queue. The size of the items the queue will hold was defined when the
885 * queue was created, so this many bytes will be copied from pvItemToQueue
886 * into the queue storage area.
888 * @param pxHigherPriorityTaskWoken xQueueSendToBackFromISR() will set
889 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
890 * to unblock, and the unblocked task has a priority higher than the currently
891 * running task. If xQueueSendToBackFromISR() sets this value to pdTRUE then
892 * a context switch should be requested before the interrupt is exited.
894 * @return pdTRUE if the data was successfully sent to the queue, otherwise
897 * Example usage for buffered IO (where the ISR can obtain more than one value
900 void vBufferISR( void )
903 portBASE_TYPE xHigherPriorityTaskWoken;
905 // We have not woken a task at the start of the ISR.
906 xHigherPriorityTaskWoken = pdFALSE;
908 // Loop until the buffer is empty.
911 // Obtain a byte from the buffer.
912 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
915 xQueueSendToBackFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken );
917 } while( portINPUT_BYTE( BUFFER_COUNT ) );
919 // Now the buffer is empty we can switch context if necessary.
920 if( xHigherPriorityTaskWoken )
927 * \defgroup xQueueSendFromISR xQueueSendFromISR
928 * \ingroup QueueManagement
930 #define xQueueSendToBackFromISR( pxQueue, pvItemToQueue, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK )
935 portBASE_TYPE xQueueSendFromISR(
936 xQueueHandle pxQueue,
937 const void *pvItemToQueue,
938 portBASE_TYPE *pxHigherPriorityTaskWoken
942 * This is a macro that calls xQueueGenericSendFromISR(). It is included
943 * for backward compatibility with versions of FreeRTOS.org that did not
944 * include the xQueueSendToBackFromISR() and xQueueSendToFrontFromISR()
947 * Post an item to the back of a queue. It is safe to use this function from
948 * within an interrupt service routine.
950 * Items are queued by copy not reference so it is preferable to only
951 * queue small items, especially when called from an ISR. In most cases
952 * it would be preferable to store a pointer to the item being queued.
954 * @param xQueue The handle to the queue on which the item is to be posted.
956 * @param pvItemToQueue A pointer to the item that is to be placed on the
957 * queue. The size of the items the queue will hold was defined when the
958 * queue was created, so this many bytes will be copied from pvItemToQueue
959 * into the queue storage area.
961 * @param pxHigherPriorityTaskWoken xQueueSendFromISR() will set
962 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
963 * to unblock, and the unblocked task has a priority higher than the currently
964 * running task. If xQueueSendFromISR() sets this value to pdTRUE then
965 * a context switch should be requested before the interrupt is exited.
967 * @return pdTRUE if the data was successfully sent to the queue, otherwise
970 * Example usage for buffered IO (where the ISR can obtain more than one value
973 void vBufferISR( void )
976 portBASE_TYPE xHigherPriorityTaskWoken;
978 // We have not woken a task at the start of the ISR.
979 xHigherPriorityTaskWoken = pdFALSE;
981 // Loop until the buffer is empty.
984 // Obtain a byte from the buffer.
985 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
988 xQueueSendFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken );
990 } while( portINPUT_BYTE( BUFFER_COUNT ) );
992 // Now the buffer is empty we can switch context if necessary.
993 if( xHigherPriorityTaskWoken )
995 // Actual macro used here is port specific.
996 taskYIELD_FROM_ISR ();
1001 * \defgroup xQueueSendFromISR xQueueSendFromISR
1002 * \ingroup QueueManagement
1004 #define xQueueSendFromISR( pxQueue, pvItemToQueue, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK )
1009 portBASE_TYPE xQueueGenericSendFromISR(
1010 xQueueHandle pxQueue,
1011 const void *pvItemToQueue,
1012 portBASE_TYPE *pxHigherPriorityTaskWoken,
1013 portBASE_TYPE xCopyPosition
1017 * It is preferred that the macros xQueueSendFromISR(),
1018 * xQueueSendToFrontFromISR() and xQueueSendToBackFromISR() be used in place
1019 * of calling this function directly.
1021 * Post an item on a queue. It is safe to use this function from within an
1022 * interrupt service routine.
1024 * Items are queued by copy not reference so it is preferable to only
1025 * queue small items, especially when called from an ISR. In most cases
1026 * it would be preferable to store a pointer to the item being queued.
1028 * @param xQueue The handle to the queue on which the item is to be posted.
1030 * @param pvItemToQueue A pointer to the item that is to be placed on the
1031 * queue. The size of the items the queue will hold was defined when the
1032 * queue was created, so this many bytes will be copied from pvItemToQueue
1033 * into the queue storage area.
1035 * @param pxHigherPriorityTaskWoken xQueueGenericSendFromISR() will set
1036 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
1037 * to unblock, and the unblocked task has a priority higher than the currently
1038 * running task. If xQueueGenericSendFromISR() sets this value to pdTRUE then
1039 * a context switch should be requested before the interrupt is exited.
1041 * @param xCopyPosition Can take the value queueSEND_TO_BACK to place the
1042 * item at the back of the queue, or queueSEND_TO_FRONT to place the item
1043 * at the front of the queue (for high priority messages).
1045 * @return pdTRUE if the data was successfully sent to the queue, otherwise
1048 * Example usage for buffered IO (where the ISR can obtain more than one value
1051 void vBufferISR( void )
1054 portBASE_TYPE xHigherPriorityTaskWokenByPost;
1056 // We have not woken a task at the start of the ISR.
1057 xHigherPriorityTaskWokenByPost = pdFALSE;
1059 // Loop until the buffer is empty.
1062 // Obtain a byte from the buffer.
1063 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
1066 xQueueGenericSendFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWokenByPost, queueSEND_TO_BACK );
1068 } while( portINPUT_BYTE( BUFFER_COUNT ) );
1070 // Now the buffer is empty we can switch context if necessary. Note that the
1071 // name of the yield function required is port specific.
1072 if( xHigherPriorityTaskWokenByPost )
1074 taskYIELD_YIELD_FROM_ISR();
1079 * \defgroup xQueueSendFromISR xQueueSendFromISR
1080 * \ingroup QueueManagement
1082 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition );
1087 portBASE_TYPE xQueueReceiveFromISR(
1088 xQueueHandle pxQueue,
1090 portBASE_TYPE *pxTaskWoken
1094 * Receive an item from a queue. It is safe to use this function from within an
1095 * interrupt service routine.
1097 * @param pxQueue The handle to the queue from which the item is to be
1100 * @param pvBuffer Pointer to the buffer into which the received item will
1103 * @param pxTaskWoken A task may be blocked waiting for space to become
1104 * available on the queue. If xQueueReceiveFromISR causes such a task to
1105 * unblock *pxTaskWoken will get set to pdTRUE, otherwise *pxTaskWoken will
1108 * @return pdTRUE if an item was successfully received from the queue,
1109 * otherwise pdFALSE.
1114 xQueueHandle xQueue;
1116 // Function to create a queue and post some values.
1117 void vAFunction( void *pvParameters )
1120 const portTickType xBlockTime = ( portTickType )0xff;
1122 // Create a queue capable of containing 10 characters.
1123 xQueue = xQueueCreate( 10, sizeof( char ) );
1126 // Failed to create the queue.
1131 // Post some characters that will be used within an ISR. If the queue
1132 // is full then this task will block for xBlockTime ticks.
1134 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
1136 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
1138 // ... keep posting characters ... this task may block when the queue
1142 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
1145 // ISR that outputs all the characters received on the queue.
1146 void vISR_Routine( void )
1148 portBASE_TYPE xTaskWokenByReceive = pdFALSE;
1151 while( xQueueReceiveFromISR( xQueue, ( void * ) &cRxedChar, &xTaskWokenByReceive) )
1153 // A character was received. Output the character now.
1154 vOutputCharacter( cRxedChar );
1156 // If removing the character from the queue woke the task that was
1157 // posting onto the queue cTaskWokenByReceive will have been set to
1158 // pdTRUE. No matter how many times this loop iterates only one
1159 // task will be woken.
1162 if( cTaskWokenByPost != ( char ) pdFALSE;
1168 * \defgroup xQueueReceiveFromISR xQueueReceiveFromISR
1169 * \ingroup QueueManagement
1171 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken );
1174 * Utilities to query queue that are safe to use from an ISR. These utilities
1175 * should be used only from witin an ISR, or within a critical section.
1177 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue );
1178 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue );
1179 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue );
1183 * xQueueAltGenericSend() is an alternative version of xQueueGenericSend().
1184 * Likewise xQueueAltGenericReceive() is an alternative version of
1185 * xQueueGenericReceive().
1187 * The source code that implements the alternative (Alt) API is much
1188 * simpler because it executes everything from within a critical section.
1189 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
1190 * preferred fully featured API too. The fully featured API has more
1191 * complex code that takes longer to execute, but makes much less use of
1192 * critical sections. Therefore the alternative API sacrifices interrupt
1193 * responsiveness to gain execution speed, whereas the fully featured API
1194 * sacrifices execution speed to ensure better interrupt responsiveness.
1196 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
1197 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
1198 #define xQueueAltSendToFront( xQueue, pvItemToQueue, xTicksToWait ) xQueueAltGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_FRONT )
1199 #define xQueueAltSendToBack( xQueue, pvItemToQueue, xTicksToWait ) xQueueAltGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_BACK )
1200 #define xQueueAltReceive( xQueue, pvBuffer, xTicksToWait ) xQueueAltGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdFALSE )
1201 #define xQueueAltPeek( xQueue, pvBuffer, xTicksToWait ) xQueueAltGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdTRUE )
1204 * The functions defined above are for passing data to and from tasks. The
1205 * functions below are the equivalents for passing data to and from
1208 * These functions are called from the co-routine macro implementation and
1209 * should not be called directly from application code. Instead use the macro
1210 * wrappers defined within croutine.h.
1212 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
1213 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
1214 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
1215 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
1218 * For internal use only. Use xSemaphoreCreateMutex() or
1219 * xSemaphoreCreateCounting() instead of calling these functions directly.
1221 xQueueHandle xQueueCreateMutex( void );
1222 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount );
1225 * For internal use only. Use xSemaphoreTakeMutexRecursive() or
1226 * xSemaphoreGiveMutexRecursive() instead of calling these functions directly.
1228 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime );
1229 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex );
1232 * The registry is provided as a means for kernel aware debuggers to
1233 * locate queues, semaphores and mutexes. Call vQueueAddToRegistry() add
1234 * a queue, semaphore or mutex handle to the registry if you want the handle
1235 * to be available to a kernel aware debugger. If you are not using a kernel
1236 * aware debugger then this function can be ignored.
1238 * configQUEUE_REGISTRY_SIZE defines the maximum number of handles the
1239 * registry can hold. configQUEUE_REGISTRY_SIZE must be greater than 0
1240 * within FreeRTOSConfig.h for the registry to be available. Its value
1241 * does not effect the number of queues, semaphores and mutexes that can be
1242 * created - just the number that the registry can hold.
1244 * @param xQueue The handle of the queue being added to the registry. This
1245 * is the handle returned by a call to xQueueCreate(). Semaphore and mutex
1246 * handles can also be passed in here.
1248 * @param pcName The name to be associated with the handle. This is the
1249 * name that the kernel aware debugger will display.
1251 #if configQUEUE_REGISTRY_SIZE > 0U
1252 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcName );
1255 /* Not a public API function, hence the 'Restricted' in the name. */
1256 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait );
1263 #endif /* QUEUE_H */