2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 ***************************************************************************
9 * FreeRTOS tutorial books are available in pdf and paperback. *
10 * Complete, revised, and edited pdf reference manuals are also *
13 * Purchasing FreeRTOS documentation will not only help you, by *
14 * ensuring you get running as quickly as possible and with an *
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
16 * the FreeRTOS project to continue with its mission of providing *
17 * professional grade, cross platform, de facto standard solutions *
18 * for microcontrollers - completely free of charge! *
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
22 * Thank you for using FreeRTOS, and thank you for your support! *
24 ***************************************************************************
27 This file is part of the FreeRTOS distribution.
29 FreeRTOS is free software; you can redistribute it and/or modify it under
30 the terms of the GNU General Public License (version 2) as published by the
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
34 distribute a combined work that includes FreeRTOS without being obliged to
35 provide the source code for proprietary components outside of the FreeRTOS
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
41 details. You should have received a copy of the GNU General Public License
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
44 writing to Real Time Engineers Ltd., contact details for whom are available
45 on the FreeRTOS WEB site.
49 ***************************************************************************
51 * Having a problem? Start by reading the FAQ "My application does *
52 * not run, what could be wrong?" *
54 * http://www.FreeRTOS.org/FAQHelp.html *
56 ***************************************************************************
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
60 license and Real Time Engineers Ltd. contact details.
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
64 fully thread aware and reentrant UDP/IP stack.
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
67 Integrity Systems, who sell the code with commercial support,
68 indemnification and middleware, under the OpenRTOS brand.
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
71 engineered and independently SIL3 certified version for use in safety and
72 mission critical applications that require provable dependability.
79 #ifndef INC_FREERTOS_H
80 #error "include FreeRTOS.h" must appear in source files before "include queue.h"
88 #include "os/mpu_wrappers.h"
91 * Type by which queues are referenced. For example, a call to xQueueCreate()
92 * returns an xQueueHandle variable that can then be used as a parameter to
93 * xQueueSend(), xQueueReceive(), etc.
95 typedef void * xQueueHandle;
98 * Type by which queue sets are referenced. For example, a call to
99 * xQueueCreateSet() returns an xQueueSet variable that can then be used as a
100 * parameter to xQueueSelectFromSet(), xQueueAddToSet(), etc.
102 typedef void * xQueueSetHandle;
105 * Queue sets can contain both queues and semaphores, so the
106 * xQueueSetMemberHandle is defined as a type to be used where a parameter or
107 * return value can be either an xQueueHandle or an xSemaphoreHandle.
109 typedef void * xQueueSetMemberHandle;
111 /* For internal use only. */
112 #define queueSEND_TO_BACK ( 0 )
113 #define queueSEND_TO_FRONT ( 1 )
115 /* For internal use only. These definitions *must* match those in queue.c. */
116 #define queueQUEUE_TYPE_BASE ( 0U )
117 #define queueQUEUE_TYPE_SET ( 0U )
118 #define queueQUEUE_TYPE_MUTEX ( 1U )
119 #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( 2U )
120 #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( 3U )
121 #define queueQUEUE_TYPE_RECURSIVE_MUTEX ( 4U )
126 xQueueHandle xQueueCreate(
127 unsigned portBASE_TYPE uxQueueLength,
128 unsigned portBASE_TYPE uxItemSize
132 * Creates a new queue instance. This allocates the storage required by the
133 * new queue and returns a handle for the queue.
135 * @param uxQueueLength The maximum number of items that the queue can contain.
137 * @param uxItemSize The number of bytes each item in the queue will require.
138 * Items are queued by copy, not by reference, so this is the number of bytes
139 * that will be copied for each posted item. Each item on the queue must be
142 * @return If the queue is successfully create then a handle to the newly
143 * created queue is returned. If the queue cannot be created then 0 is
154 void vATask( void *pvParameters )
156 xQueueHandle xQueue1, xQueue2;
158 // Create a queue capable of containing 10 unsigned long values.
159 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
162 // Queue was not created and must not be used.
165 // Create a queue capable of containing 10 pointers to AMessage structures.
166 // These should be passed by pointer as they contain a lot of data.
167 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
170 // Queue was not created and must not be used.
173 // ... Rest of task code.
176 * @ingroup QueueManagement
178 #define xQueueCreate( uxQueueLength, uxItemSize ) xQueueGenericCreate( uxQueueLength, uxItemSize, queueQUEUE_TYPE_BASE )
183 portBASE_TYPE xQueueSendToToFront(
185 const void * pvItemToQueue,
186 portTickType xTicksToWait
190 * This is a macro that calls xQueueGenericSend().
192 * Post an item to the front of a queue. The item is queued by copy, not by
193 * reference. This function must not be called from an interrupt service
194 * routine. See xQueueSendFromISR () for an alternative which may be used
197 * @param xQueue The handle to the queue on which the item is to be posted.
199 * @param pvItemToQueue A pointer to the item that is to be placed on the
200 * queue. The size of the items the queue will hold was defined when the
201 * queue was created, so this many bytes will be copied from pvItemToQueue
202 * into the queue storage area.
204 * @param xTicksToWait The maximum amount of time the task should block
205 * waiting for space to become available on the queue, should it already
206 * be full. The call will return immediately if this is set to 0 and the
207 * queue is full. The time is defined in tick periods so the constant
208 * portTICK_RATE_MS should be used to convert to real time if this is required.
210 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
220 unsigned long ulVar = 10UL;
222 void vATask( void *pvParameters )
224 xQueueHandle xQueue1, xQueue2;
225 struct AMessage *pxMessage;
227 // Create a queue capable of containing 10 unsigned long values.
228 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
230 // Create a queue capable of containing 10 pointers to AMessage structures.
231 // These should be passed by pointer as they contain a lot of data.
232 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
238 // Send an unsigned long. Wait for 10 ticks for space to become
239 // available if necessary.
240 if( xQueueSendToFront( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
242 // Failed to post the message, even after 10 ticks.
248 // Send a pointer to a struct AMessage object. Don't block if the
249 // queue is already full.
250 pxMessage = & xMessage;
251 xQueueSendToFront( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
254 // ... Rest of task code.
257 * @ingroup QueueManagement
259 #define xQueueSendToFront( xQueue, pvItemToQueue, xTicksToWait ) xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_FRONT )
264 portBASE_TYPE xQueueSendToBack(
266 const void * pvItemToQueue,
267 portTickType xTicksToWait
271 * This is a macro that calls xQueueGenericSend().
273 * Post an item to the back of a queue. The item is queued by copy, not by
274 * reference. This function must not be called from an interrupt service
275 * routine. See xQueueSendFromISR () for an alternative which may be used
278 * @param xQueue The handle to the queue on which the item is to be posted.
280 * @param pvItemToQueue A pointer to the item that is to be placed on the
281 * queue. The size of the items the queue will hold was defined when the
282 * queue was created, so this many bytes will be copied from pvItemToQueue
283 * into the queue storage area.
285 * @param xTicksToWait The maximum amount of time the task should block
286 * waiting for space to become available on the queue, should it already
287 * be full. The call will return immediately if this is set to 0 and the queue
288 * is full. The time is defined in tick periods so the constant
289 * portTICK_RATE_MS should be used to convert to real time if this is required.
291 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
301 unsigned long ulVar = 10UL;
303 void vATask( void *pvParameters )
305 xQueueHandle xQueue1, xQueue2;
306 struct AMessage *pxMessage;
308 // Create a queue capable of containing 10 unsigned long values.
309 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
311 // Create a queue capable of containing 10 pointers to AMessage structures.
312 // These should be passed by pointer as they contain a lot of data.
313 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
319 // Send an unsigned long. Wait for 10 ticks for space to become
320 // available if necessary.
321 if( xQueueSendToBack( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
323 // Failed to post the message, even after 10 ticks.
329 // Send a pointer to a struct AMessage object. Don't block if the
330 // queue is already full.
331 pxMessage = & xMessage;
332 xQueueSendToBack( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
335 // ... Rest of task code.
338 * @ingroup QueueManagement
340 #define xQueueSendToBack( xQueue, pvItemToQueue, xTicksToWait ) xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_BACK )
345 portBASE_TYPE xQueueSend(
347 const void * pvItemToQueue,
348 portTickType xTicksToWait
352 * This is a macro that calls xQueueGenericSend(). It is included for
353 * backward compatibility with versions of FreeRTOS.org that did not
354 * include the xQueueSendToFront() and xQueueSendToBack() macros. It is
355 * equivalent to xQueueSendToBack().
357 * Post an item on a queue. The item is queued by copy, not by reference.
358 * This function must not be called from an interrupt service routine.
359 * See xQueueSendFromISR () for an alternative which may be used in an ISR.
361 * @param xQueue The handle to the queue on which the item is to be posted.
363 * @param pvItemToQueue A pointer to the item that is to be placed on the
364 * queue. The size of the items the queue will hold was defined when the
365 * queue was created, so this many bytes will be copied from pvItemToQueue
366 * into the queue storage area.
368 * @param xTicksToWait The maximum amount of time the task should block
369 * waiting for space to become available on the queue, should it already
370 * be full. The call will return immediately if this is set to 0 and the
371 * queue is full. The time is defined in tick periods so the constant
372 * portTICK_RATE_MS should be used to convert to real time if this is required.
374 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
384 unsigned long ulVar = 10UL;
386 void vATask( void *pvParameters )
388 xQueueHandle xQueue1, xQueue2;
389 struct AMessage *pxMessage;
391 // Create a queue capable of containing 10 unsigned long values.
392 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
394 // Create a queue capable of containing 10 pointers to AMessage structures.
395 // These should be passed by pointer as they contain a lot of data.
396 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
402 // Send an unsigned long. Wait for 10 ticks for space to become
403 // available if necessary.
404 if( xQueueSend( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
406 // Failed to post the message, even after 10 ticks.
412 // Send a pointer to a struct AMessage object. Don't block if the
413 // queue is already full.
414 pxMessage = & xMessage;
415 xQueueSend( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
418 // ... Rest of task code.
421 * @ingroup QueueManagement
423 #define xQueueSend( xQueue, pvItemToQueue, xTicksToWait ) xQueueGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_BACK )
429 portBASE_TYPE xQueueGenericSend(
431 const void * pvItemToQueue,
432 portTickType xTicksToWait
433 portBASE_TYPE xCopyPosition
437 * It is preferred that the macros xQueueSend(), xQueueSendToFront() and
438 * xQueueSendToBack() are used in place of calling this function directly.
440 * Post an item on a queue. The item is queued by copy, not by reference.
441 * This function must not be called from an interrupt service routine.
442 * See xQueueSendFromISR () for an alternative which may be used in an ISR.
444 * @param xQueue The handle to the queue on which the item is to be posted.
446 * @param pvItemToQueue A pointer to the item that is to be placed on the
447 * queue. The size of the items the queue will hold was defined when the
448 * queue was created, so this many bytes will be copied from pvItemToQueue
449 * into the queue storage area.
451 * @param xTicksToWait The maximum amount of time the task should block
452 * waiting for space to become available on the queue, should it already
453 * be full. The call will return immediately if this is set to 0 and the
454 * queue is full. The time is defined in tick periods so the constant
455 * portTICK_RATE_MS should be used to convert to real time if this is required.
457 * @param xCopyPosition Can take the value queueSEND_TO_BACK to place the
458 * item at the back of the queue, or queueSEND_TO_FRONT to place the item
459 * at the front of the queue (for high priority messages).
461 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
471 unsigned long ulVar = 10UL;
473 void vATask( void *pvParameters )
475 xQueueHandle xQueue1, xQueue2;
476 struct AMessage *pxMessage;
478 // Create a queue capable of containing 10 unsigned long values.
479 xQueue1 = xQueueCreate( 10, sizeof( unsigned long ) );
481 // Create a queue capable of containing 10 pointers to AMessage structures.
482 // These should be passed by pointer as they contain a lot of data.
483 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
489 // Send an unsigned long. Wait for 10 ticks for space to become
490 // available if necessary.
491 if( xQueueGenericSend( xQueue1, ( void * ) &ulVar, ( portTickType ) 10, queueSEND_TO_BACK ) != pdPASS )
493 // Failed to post the message, even after 10 ticks.
499 // Send a pointer to a struct AMessage object. Don't block if the
500 // queue is already full.
501 pxMessage = & xMessage;
502 xQueueGenericSend( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0, queueSEND_TO_BACK );
505 // ... Rest of task code.
508 * @ingroup QueueManagement
510 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
515 portBASE_TYPE xQueuePeek(
518 portTickType xTicksToWait
521 * This is a macro that calls the xQueueGenericReceive() function.
523 * Receive an item from a queue without removing the item from the queue.
524 * The item is received by copy so a buffer of adequate size must be
525 * provided. The number of bytes copied into the buffer was defined when
526 * the queue was created.
528 * Successfully received items remain on the queue so will be returned again
529 * by the next call, or a call to xQueueReceive().
531 * This macro must not be used in an interrupt service routine.
533 * @param xQueue The handle to the queue from which the item is to be
536 * @param pvBuffer Pointer to the buffer into which the received item will
539 * @param xTicksToWait The maximum amount of time the task should block
540 * waiting for an item to receive should the queue be empty at the time
541 * of the call. The time is defined in tick periods so the constant
542 * portTICK_RATE_MS should be used to convert to real time if this is required.
543 * xQueuePeek() will return immediately if xTicksToWait is 0 and the queue
546 * @return pdTRUE if an item was successfully received from the queue,
559 // Task to create a queue and post a value.
560 void vATask( void *pvParameters )
562 struct AMessage *pxMessage;
564 // Create a queue capable of containing 10 pointers to AMessage structures.
565 // These should be passed by pointer as they contain a lot of data.
566 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
569 // Failed to create the queue.
574 // Send a pointer to a struct AMessage object. Don't block if the
575 // queue is already full.
576 pxMessage = & xMessage;
577 xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
579 // ... Rest of task code.
582 // Task to peek the data from the queue.
583 void vADifferentTask( void *pvParameters )
585 struct AMessage *pxRxedMessage;
589 // Peek a message on the created queue. Block for 10 ticks if a
590 // message is not immediately available.
591 if( xQueuePeek( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
593 // pcRxedMessage now points to the struct AMessage variable posted
594 // by vATask, but the item still remains on the queue.
598 // ... Rest of task code.
601 * @ingroup QueueManagement
603 #define xQueuePeek( xQueue, pvBuffer, xTicksToWait ) xQueueGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdTRUE )
608 portBASE_TYPE xQueueReceive(
611 portTickType xTicksToWait
614 * This is a macro that calls the xQueueGenericReceive() function.
616 * Receive an item from a queue. The item is received by copy so a buffer of
617 * adequate size must be provided. The number of bytes copied into the buffer
618 * was defined when the queue was created.
620 * Successfully received items are removed from the queue.
622 * This function must not be used in an interrupt service routine. See
623 * xQueueReceiveFromISR for an alternative that can.
625 * @param xQueue The handle to the queue from which the item is to be
628 * @param pvBuffer Pointer to the buffer into which the received item will
631 * @param xTicksToWait The maximum amount of time the task should block
632 * waiting for an item to receive should the queue be empty at the time
633 * of the call. xQueueReceive() will return immediately if xTicksToWait
634 * is zero and the queue is empty. The time is defined in tick periods so the
635 * constant portTICK_RATE_MS should be used to convert to real time if this is
638 * @return pdTRUE if an item was successfully received from the queue,
651 // Task to create a queue and post a value.
652 void vATask( void *pvParameters )
654 struct AMessage *pxMessage;
656 // Create a queue capable of containing 10 pointers to AMessage structures.
657 // These should be passed by pointer as they contain a lot of data.
658 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
661 // Failed to create the queue.
666 // Send a pointer to a struct AMessage object. Don't block if the
667 // queue is already full.
668 pxMessage = & xMessage;
669 xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
671 // ... Rest of task code.
674 // Task to receive from the queue.
675 void vADifferentTask( void *pvParameters )
677 struct AMessage *pxRxedMessage;
681 // Receive a message on the created queue. Block for 10 ticks if a
682 // message is not immediately available.
683 if( xQueueReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
685 // pcRxedMessage now points to the struct AMessage variable posted
690 // ... Rest of task code.
693 * @ingroup QueueManagement
695 #define xQueueReceive( xQueue, pvBuffer, xTicksToWait ) xQueueGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdFALSE )
701 portBASE_TYPE xQueueGenericReceive(
704 portTickType xTicksToWait
705 portBASE_TYPE xJustPeek
708 * It is preferred that the macro xQueueReceive() be used rather than calling
709 * this function directly.
711 * Receive an item from a queue. The item is received by copy so a buffer of
712 * adequate size must be provided. The number of bytes copied into the buffer
713 * was defined when the queue was created.
715 * This function must not be used in an interrupt service routine. See
716 * xQueueReceiveFromISR for an alternative that can.
718 * @param xQueue The handle to the queue from which the item is to be
721 * @param pvBuffer Pointer to the buffer into which the received item will
724 * @param xTicksToWait The maximum amount of time the task should block
725 * waiting for an item to receive should the queue be empty at the time
726 * of the call. The time is defined in tick periods so the constant
727 * portTICK_RATE_MS should be used to convert to real time if this is required.
728 * xQueueGenericReceive() will return immediately if the queue is empty and
731 * @param xJustPeek When set to true, the item received from the queue is not
732 * actually removed from the queue - meaning a subsequent call to
733 * xQueueReceive() will return the same item. When set to false, the item
734 * being received from the queue is also removed from the queue.
736 * @return pdTRUE if an item was successfully received from the queue,
749 // Task to create a queue and post a value.
750 void vATask( void *pvParameters )
752 struct AMessage *pxMessage;
754 // Create a queue capable of containing 10 pointers to AMessage structures.
755 // These should be passed by pointer as they contain a lot of data.
756 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
759 // Failed to create the queue.
764 // Send a pointer to a struct AMessage object. Don't block if the
765 // queue is already full.
766 pxMessage = & xMessage;
767 xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
769 // ... Rest of task code.
772 // Task to receive from the queue.
773 void vADifferentTask( void *pvParameters )
775 struct AMessage *pxRxedMessage;
779 // Receive a message on the created queue. Block for 10 ticks if a
780 // message is not immediately available.
781 if( xQueueGenericReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
783 // pcRxedMessage now points to the struct AMessage variable posted
788 // ... Rest of task code.
791 * @ingroup QueueManagement
793 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle xQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeek );
797 * <pre>unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle xQueue );</pre>
799 * Return the number of messages stored in a queue.
801 * @param xQueue A handle to the queue being queried.
803 * @return The number of messages available in the queue.
805 * @ingroup QueueManagement
807 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle xQueue );
811 * <pre>void vQueueDelete( xQueueHandle xQueue );</pre>
813 * Delete a queue - freeing all the memory allocated for storing of items
814 * placed on the queue.
816 * @param xQueue A handle to the queue to be deleted.
818 * @ingroup QueueManagement
820 void vQueueDelete( xQueueHandle xQueue );
825 portBASE_TYPE xQueueSendToFrontFromISR(
827 const void *pvItemToQueue,
828 portBASE_TYPE *pxHigherPriorityTaskWoken
832 * This is a macro that calls xQueueGenericSendFromISR().
834 * Post an item to the front of a queue. It is safe to use this macro from
835 * within an interrupt service routine.
837 * Items are queued by copy not reference so it is preferable to only
838 * queue small items, especially when called from an ISR. In most cases
839 * it would be preferable to store a pointer to the item being queued.
841 * @param xQueue The handle to the queue on which the item is to be posted.
843 * @param pvItemToQueue A pointer to the item that is to be placed on the
844 * queue. The size of the items the queue will hold was defined when the
845 * queue was created, so this many bytes will be copied from pvItemToQueue
846 * into the queue storage area.
848 * @param pxHigherPriorityTaskWoken xQueueSendToFrontFromISR() will set
849 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
850 * to unblock, and the unblocked task has a priority higher than the currently
851 * running task. If xQueueSendToFromFromISR() sets this value to pdTRUE then
852 * a context switch should be requested before the interrupt is exited.
854 * @return pdTRUE if the data was successfully sent to the queue, otherwise
857 * Example usage for buffered IO (where the ISR can obtain more than one value
860 void vBufferISR( void )
863 portBASE_TYPE xHigherPrioritTaskWoken;
865 // We have not woken a task at the start of the ISR.
866 xHigherPriorityTaskWoken = pdFALSE;
868 // Loop until the buffer is empty.
871 // Obtain a byte from the buffer.
872 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
875 xQueueSendToFrontFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken );
877 } while( portINPUT_BYTE( BUFFER_COUNT ) );
879 // Now the buffer is empty we can switch context if necessary.
880 if( xHigherPriorityTaskWoken )
887 * @ingroup QueueManagement
889 #define xQueueSendToFrontFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( xQueue ), ( pvItemToQueue ), ( pxHigherPriorityTaskWoken ), queueSEND_TO_FRONT )
895 portBASE_TYPE xQueueSendToBackFromISR(
897 const void *pvItemToQueue,
898 portBASE_TYPE *pxHigherPriorityTaskWoken
902 * This is a macro that calls xQueueGenericSendFromISR().
904 * Post an item to the back of a queue. It is safe to use this macro from
905 * within an interrupt service routine.
907 * Items are queued by copy not reference so it is preferable to only
908 * queue small items, especially when called from an ISR. In most cases
909 * it would be preferable to store a pointer to the item being queued.
911 * @param xQueue The handle to the queue on which the item is to be posted.
913 * @param pvItemToQueue A pointer to the item that is to be placed on the
914 * queue. The size of the items the queue will hold was defined when the
915 * queue was created, so this many bytes will be copied from pvItemToQueue
916 * into the queue storage area.
918 * @param pxHigherPriorityTaskWoken xQueueSendToBackFromISR() will set
919 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
920 * to unblock, and the unblocked task has a priority higher than the currently
921 * running task. If xQueueSendToBackFromISR() sets this value to pdTRUE then
922 * a context switch should be requested before the interrupt is exited.
924 * @return pdTRUE if the data was successfully sent to the queue, otherwise
927 * Example usage for buffered IO (where the ISR can obtain more than one value
930 void vBufferISR( void )
933 portBASE_TYPE xHigherPriorityTaskWoken;
935 // We have not woken a task at the start of the ISR.
936 xHigherPriorityTaskWoken = pdFALSE;
938 // Loop until the buffer is empty.
941 // Obtain a byte from the buffer.
942 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
945 xQueueSendToBackFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken );
947 } while( portINPUT_BYTE( BUFFER_COUNT ) );
949 // Now the buffer is empty we can switch context if necessary.
950 if( xHigherPriorityTaskWoken )
957 * @ingroup QueueManagement
959 #define xQueueSendToBackFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( xQueue ), ( pvItemToQueue ), ( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK )
964 portBASE_TYPE xQueueSendFromISR(
966 const void *pvItemToQueue,
967 portBASE_TYPE *pxHigherPriorityTaskWoken
971 * This is a macro that calls xQueueGenericSendFromISR(). It is included
972 * for backward compatibility with versions of FreeRTOS.org that did not
973 * include the xQueueSendToBackFromISR() and xQueueSendToFrontFromISR()
976 * Post an item to the back of a queue. It is safe to use this function from
977 * within an interrupt service routine.
979 * Items are queued by copy not reference so it is preferable to only
980 * queue small items, especially when called from an ISR. In most cases
981 * it would be preferable to store a pointer to the item being queued.
983 * @param xQueue The handle to the queue on which the item is to be posted.
985 * @param pvItemToQueue A pointer to the item that is to be placed on the
986 * queue. The size of the items the queue will hold was defined when the
987 * queue was created, so this many bytes will be copied from pvItemToQueue
988 * into the queue storage area.
990 * @param pxHigherPriorityTaskWoken xQueueSendFromISR() will set
991 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
992 * to unblock, and the unblocked task has a priority higher than the currently
993 * running task. If xQueueSendFromISR() sets this value to pdTRUE then
994 * a context switch should be requested before the interrupt is exited.
996 * @return pdTRUE if the data was successfully sent to the queue, otherwise
999 * Example usage for buffered IO (where the ISR can obtain more than one value
1002 void vBufferISR( void )
1005 portBASE_TYPE xHigherPriorityTaskWoken;
1007 // We have not woken a task at the start of the ISR.
1008 xHigherPriorityTaskWoken = pdFALSE;
1010 // Loop until the buffer is empty.
1013 // Obtain a byte from the buffer.
1014 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
1017 xQueueSendFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken );
1019 } while( portINPUT_BYTE( BUFFER_COUNT ) );
1021 // Now the buffer is empty we can switch context if necessary.
1022 if( xHigherPriorityTaskWoken )
1024 // Actual macro used here is port specific.
1025 taskYIELD_FROM_ISR ();
1030 * @ingroup QueueManagement
1032 #define xQueueSendFromISR( xQueue, pvItemToQueue, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( xQueue ), ( pvItemToQueue ), ( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK )
1037 portBASE_TYPE xQueueGenericSendFromISR(
1038 xQueueHandle xQueue,
1039 const void *pvItemToQueue,
1040 portBASE_TYPE *pxHigherPriorityTaskWoken,
1041 portBASE_TYPE xCopyPosition
1045 * It is preferred that the macros xQueueSendFromISR(),
1046 * xQueueSendToFrontFromISR() and xQueueSendToBackFromISR() be used in place
1047 * of calling this function directly.
1049 * Post an item on a queue. It is safe to use this function from within an
1050 * interrupt service routine.
1052 * Items are queued by copy not reference so it is preferable to only
1053 * queue small items, especially when called from an ISR. In most cases
1054 * it would be preferable to store a pointer to the item being queued.
1056 * @param xQueue The handle to the queue on which the item is to be posted.
1058 * @param pvItemToQueue A pointer to the item that is to be placed on the
1059 * queue. The size of the items the queue will hold was defined when the
1060 * queue was created, so this many bytes will be copied from pvItemToQueue
1061 * into the queue storage area.
1063 * @param pxHigherPriorityTaskWoken xQueueGenericSendFromISR() will set
1064 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task
1065 * to unblock, and the unblocked task has a priority higher than the currently
1066 * running task. If xQueueGenericSendFromISR() sets this value to pdTRUE then
1067 * a context switch should be requested before the interrupt is exited.
1069 * @param xCopyPosition Can take the value queueSEND_TO_BACK to place the
1070 * item at the back of the queue, or queueSEND_TO_FRONT to place the item
1071 * at the front of the queue (for high priority messages).
1073 * @return pdTRUE if the data was successfully sent to the queue, otherwise
1076 * Example usage for buffered IO (where the ISR can obtain more than one value
1079 void vBufferISR( void )
1082 portBASE_TYPE xHigherPriorityTaskWokenByPost;
1084 // We have not woken a task at the start of the ISR.
1085 xHigherPriorityTaskWokenByPost = pdFALSE;
1087 // Loop until the buffer is empty.
1090 // Obtain a byte from the buffer.
1091 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
1094 xQueueGenericSendFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWokenByPost, queueSEND_TO_BACK );
1096 } while( portINPUT_BYTE( BUFFER_COUNT ) );
1098 // Now the buffer is empty we can switch context if necessary. Note that the
1099 // name of the yield function required is port specific.
1100 if( xHigherPriorityTaskWokenByPost )
1102 taskYIELD_YIELD_FROM_ISR();
1107 * @ingroup QueueManagement
1109 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle xQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition );
1114 portBASE_TYPE xQueueReceiveFromISR(
1115 xQueueHandle xQueue,
1117 portBASE_TYPE *pxTaskWoken
1121 * Receive an item from a queue. It is safe to use this function from within an
1122 * interrupt service routine.
1124 * @param xQueue The handle to the queue from which the item is to be
1127 * @param pvBuffer Pointer to the buffer into which the received item will
1130 * @param pxTaskWoken A task may be blocked waiting for space to become
1131 * available on the queue. If xQueueReceiveFromISR causes such a task to
1132 * unblock *pxTaskWoken will get set to pdTRUE, otherwise *pxTaskWoken will
1135 * @return pdTRUE if an item was successfully received from the queue,
1136 * otherwise pdFALSE.
1141 xQueueHandle xQueue;
1143 // Function to create a queue and post some values.
1144 void vAFunction( void *pvParameters )
1147 const portTickType xBlockTime = ( portTickType )0xff;
1149 // Create a queue capable of containing 10 characters.
1150 xQueue = xQueueCreate( 10, sizeof( char ) );
1153 // Failed to create the queue.
1158 // Post some characters that will be used within an ISR. If the queue
1159 // is full then this task will block for xBlockTime ticks.
1161 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
1163 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
1165 // ... keep posting characters ... this task may block when the queue
1169 xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
1172 // ISR that outputs all the characters received on the queue.
1173 void vISR_Routine( void )
1175 portBASE_TYPE xTaskWokenByReceive = pdFALSE;
1178 while( xQueueReceiveFromISR( xQueue, ( void * ) &cRxedChar, &xTaskWokenByReceive) )
1180 // A character was received. Output the character now.
1181 vOutputCharacter( cRxedChar );
1183 // If removing the character from the queue woke the task that was
1184 // posting onto the queue cTaskWokenByReceive will have been set to
1185 // pdTRUE. No matter how many times this loop iterates only one
1186 // task will be woken.
1189 if( cTaskWokenByPost != ( char ) pdFALSE;
1195 * @ingroup QueueManagement
1197 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle xQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken );
1200 * Utilities to query queues that are safe to use from an ISR. These utilities
1201 * should be used only from witin an ISR, or within a critical section.
1203 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle xQueue );
1204 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle xQueue );
1205 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle xQueue );
1209 * xQueueAltGenericSend() is an alternative version of xQueueGenericSend().
1210 * Likewise xQueueAltGenericReceive() is an alternative version of
1211 * xQueueGenericReceive().
1213 * The source code that implements the alternative (Alt) API is much
1214 * simpler because it executes everything from within a critical section.
1215 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
1216 * preferred fully featured API too. The fully featured API has more
1217 * complex code that takes longer to execute, but makes much less use of
1218 * critical sections. Therefore the alternative API sacrifices interrupt
1219 * responsiveness to gain execution speed, whereas the fully featured API
1220 * sacrifices execution speed to ensure better interrupt responsiveness.
1222 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition );
1223 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle xQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking );
1224 #define xQueueAltSendToFront( xQueue, pvItemToQueue, xTicksToWait ) xQueueAltGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_FRONT )
1225 #define xQueueAltSendToBack( xQueue, pvItemToQueue, xTicksToWait ) xQueueAltGenericSend( ( xQueue ), ( pvItemToQueue ), ( xTicksToWait ), queueSEND_TO_BACK )
1226 #define xQueueAltReceive( xQueue, pvBuffer, xTicksToWait ) xQueueAltGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdFALSE )
1227 #define xQueueAltPeek( xQueue, pvBuffer, xTicksToWait ) xQueueAltGenericReceive( ( xQueue ), ( pvBuffer ), ( xTicksToWait ), pdTRUE )
1230 * The functions defined above are for passing data to and from tasks. The
1231 * functions below are the equivalents for passing data to and from
1234 * These functions are called from the co-routine macro implementation and
1235 * should not be called directly from application code. Instead use the macro
1236 * wrappers defined within croutine.h.
1238 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle xQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
1239 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle xQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
1240 signed portBASE_TYPE xQueueCRSend( xQueueHandle xQueue, const void *pvItemToQueue, portTickType xTicksToWait );
1241 signed portBASE_TYPE xQueueCRReceive( xQueueHandle xQueue, void *pvBuffer, portTickType xTicksToWait );
1244 * For internal use only. Use xSemaphoreCreateMutex(),
1245 * xSemaphoreCreateCounting() or xSemaphoreGetMutexHolder() instead of calling
1246 * these functions directly.
1248 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType );
1249 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount );
1250 void* xQueueGetMutexHolder( xQueueHandle xSemaphore );
1253 * For internal use only. Use xSemaphoreTakeMutexRecursive() or
1254 * xSemaphoreGiveMutexRecursive() instead of calling these functions directly.
1256 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime );
1257 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex );
1260 * Reset a queue back to its original empty state. pdPASS is returned if the
1261 * queue is successfully reset. pdFAIL is returned if the queue could not be
1262 * reset because there are tasks blocked on the queue waiting to either
1263 * receive from the queue or send to the queue.
1265 #define xQueueReset( xQueue ) xQueueGenericReset( xQueue, pdFALSE )
1268 * The registry is provided as a means for kernel aware debuggers to
1269 * locate queues, semaphores and mutexes. Call vQueueAddToRegistry() add
1270 * a queue, semaphore or mutex handle to the registry if you want the handle
1271 * to be available to a kernel aware debugger. If you are not using a kernel
1272 * aware debugger then this function can be ignored.
1274 * configQUEUE_REGISTRY_SIZE defines the maximum number of handles the
1275 * registry can hold. configQUEUE_REGISTRY_SIZE must be greater than 0
1276 * within FreeRTOSConfig.h for the registry to be available. Its value
1277 * does not effect the number of queues, semaphores and mutexes that can be
1278 * created - just the number that the registry can hold.
1280 * @param xQueue The handle of the queue being added to the registry. This
1281 * is the handle returned by a call to xQueueCreate(). Semaphore and mutex
1282 * handles can also be passed in here.
1284 * @param pcName The name to be associated with the handle. This is the
1285 * name that the kernel aware debugger will display.
1287 #if configQUEUE_REGISTRY_SIZE > 0U
1288 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcName );
1292 * Generic version of the queue creation function, which is in turn called by
1293 * any queue, semaphore or mutex creation function or macro.
1295 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType );
1298 * Queue sets provide a mechanism to allow a task to block (pend) on a read
1299 * operation from multiple queues or semaphores simultaneously.
1301 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this
1304 * A queue set must be explicitly created using a call to xQueueCreateSet()
1305 * before it can be used. Once created, standard FreeRTOS queues and semaphores
1306 * can be added to the set using calls to xQueueAddToSet().
1307 * xQueueSelectFromSet() is then used to determine which, if any, of the queues
1308 * or semaphores contained in the set is in a state where a queue read or
1309 * semaphore take operation would be successful.
1311 * Note 1: See the documentation on http://wwwFreeRTOS.org/RTOS-queue-sets.html
1312 * for reasons why queue sets are very rarely needed in practice as there are
1313 * simpler methods of blocking on multiple objects.
1315 * Note 2: Blocking on a queue set that contains a mutex will not cause the
1316 * mutex holder to inherit the priority of the blocked task.
1318 * Note 3: An additional 4 bytes of RAM is required for each space in a every
1319 * queue added to a queue set. Therefore counting semaphores that have a high
1320 * maximum count value should not be added to a queue set.
1322 * Note 4: A receive (in the case of a queue) or take (in the case of a
1323 * semaphore) operation must not be performed on a member of a queue set unless
1324 * a call to xQueueSelectFromSet() has first returned a handle to that set member.
1326 * @param uxEventQueueLength Queue sets store events that occur on
1327 * the queues and semaphores contained in the set. uxEventQueueLength specifies
1328 * the maximum number of events that can be queued at once. To be absolutely
1329 * certain that events are not lost uxEventQueueLength should be set to the
1330 * total sum of the length of the queues added to the set, where binary
1331 * semaphores and mutexes have a length of 1, and counting semaphores have a
1332 * length set by their maximum count value. Examples:
1333 * + If a queue set is to hold a queue of length 5, another queue of length 12,
1334 * and a binary semaphore, then uxEventQueueLength should be set to
1335 * (5 + 12 + 1), or 18.
1336 * + If a queue set is to hold three binary semaphores then uxEventQueueLength
1337 * should be set to (1 + 1 + 1 ), or 3.
1338 * + If a queue set is to hold a counting semaphore that has a maximum count of
1339 * 5, and a counting semaphore that has a maximum count of 3, then
1340 * uxEventQueueLength should be set to (5 + 3), or 8.
1342 * @return If the queue set is created successfully then a handle to the created
1343 * queue set is returned. Otherwise NULL is returned.
1345 xQueueSetHandle xQueueCreateSet( unsigned portBASE_TYPE uxEventQueueLength );
1348 * Adds a queue or semaphore to a queue set that was previously created by a
1349 * call to xQueueCreateSet().
1351 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this
1354 * Note 1: A receive (in the case of a queue) or take (in the case of a
1355 * semaphore) operation must not be performed on a member of a queue set unless
1356 * a call to xQueueSelectFromSet() has first returned a handle to that set member.
1358 * @param xQueueOrSemaphore The handle of the queue or semaphore being added to
1359 * the queue set (cast to an xQueueSetMemberHandle type).
1361 * @param xQueueSet The handle of the queue set to which the queue or semaphore
1364 * @return If the queue or semaphore was successfully added to the queue set
1365 * then pdPASS is returned. If the queue could not be successfully added to the
1366 * queue set because it is already a member of a different queue set then pdFAIL
1369 portBASE_TYPE xQueueAddToSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet );
1372 * Removes a queue or semaphore from a queue set. A queue or semaphore can only
1373 * be removed from a set if the queue or semaphore is empty.
1375 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this
1378 * @param xQueueOrSemaphore The handle of the queue or semaphore being removed
1379 * from the queue set (cast to an xQueueSetMemberHandle type).
1381 * @param xQueueSet The handle of the queue set in which the queue or semaphore
1384 * @return If the queue or semaphore was successfully removed from the queue set
1385 * then pdPASS is returned. If the queue was not in the queue set, or the
1386 * queue (or semaphore) was not empty, then pdFAIL is returned.
1388 portBASE_TYPE xQueueRemoveFromSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet );
1391 * xQueueSelectFromSet() selects from the members of a queue set a queue or
1392 * semaphore that either contains data (in the case of a queue) or is available
1393 * to take (in the case of a semaphore). xQueueSelectFromSet() effectively
1394 * allows a task to block (pend) on a read operation on all the queues and
1395 * semaphores in a queue set simultaneously.
1397 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this
1400 * Note 1: See the documentation on http://wwwFreeRTOS.org/RTOS-queue-sets.html
1401 * for reasons why queue sets are very rarely needed in practice as there are
1402 * simpler methods of blocking on multiple objects.
1404 * Note 2: Blocking on a queue set that contains a mutex will not cause the
1405 * mutex holder to inherit the priority of the blocked task.
1407 * Note 3: A receive (in the case of a queue) or take (in the case of a
1408 * semaphore) operation must not be performed on a member of a queue set unless
1409 * a call to xQueueSelectFromSet() has first returned a handle to that set member.
1411 * @param xQueueSet The queue set on which the task will (potentially) block.
1413 * @param xBlockTimeTicks The maximum time, in ticks, that the calling task will
1414 * remain in the Blocked state (with other tasks executing) to wait for a member
1415 * of the queue set to be ready for a successful queue read or semaphore take
1418 * @return xQueueSelectFromSet() will return the handle of a queue (cast to
1419 * a xQueueSetMemberHandle type) contained in the queue set that contains data,
1420 * or the handle of a semaphore (cast to a xQueueSetMemberHandle type) contained
1421 * in the queue set that is available, or NULL if no such queue or semaphore
1422 * exists before before the specified block time expires.
1424 xQueueSetMemberHandle xQueueSelectFromSet( xQueueSetHandle xQueueSet, portTickType xBlockTimeTicks );
1427 * A version of xQueueSelectFromSet() that can be used from an ISR.
1429 xQueueSetMemberHandle xQueueSelectFromSetFromISR( xQueueSetHandle xQueueSet );
1431 /* Not public API functions. */
1432 void vQueueWaitForMessageRestricted( xQueueHandle xQueue, portTickType xTicksToWait );
1433 portBASE_TYPE xQueueGenericReset( xQueueHandle xQueue, portBASE_TYPE xNewQueue );
1434 void vQueueSetQueueNumber( xQueueHandle xQueue, unsigned char ucQueueNumber ) PRIVILEGED_FUNCTION;
1435 unsigned char ucQueueGetQueueType( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
1442 #endif /* QUEUE_H */