2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
72 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
73 all the API functions to use the MPU wrappers. That should only be done when
\r
74 task.h is included from an application file. */
\r
75 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
77 #include "FreeRTOS.h"
\r
81 #if ( configUSE_CO_ROUTINES == 1 )
\r
82 #include "croutine.h"
\r
85 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
87 /* Constants used with the cRxLock and xTxLock structure members. */
\r
88 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
89 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
91 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
93 /* Effectively make a union out of the xQUEUE structure. */
\r
94 #define pxMutexHolder pcTail
\r
95 #define uxQueueType pcHead
\r
96 #define uxRecursiveCallCount pcReadFrom
\r
97 #define queueQUEUE_IS_MUTEX NULL
\r
99 /* Semaphores do not actually store or copy data, so have an items size of
\r
101 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )
\r
102 #define queueDONT_BLOCK ( ( portTickType ) 0U )
\r
103 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0U )
\r
107 * Definition of the queue used by the scheduler.
\r
108 * Items are queued by copy, not reference.
\r
110 typedef struct QueueDefinition
\r
112 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
113 signed char *pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
\r
115 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
116 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
118 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
119 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
121 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
122 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
123 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
125 volatile signed portBASE_TYPE xRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
126 volatile signed portBASE_TYPE xTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
128 #if ( configUSE_TRACE_FACILITY == 1 )
\r
129 unsigned char ucQueueNumber;
\r
130 unsigned char ucQueueType;
\r
133 #if ( configUSE_QUEUE_SETS == 1 )
\r
134 struct QueueDefinition *pxQueueSetContainer;
\r
138 /*-----------------------------------------------------------*/
\r
141 * The queue registry is just a means for kernel aware debuggers to locate
\r
142 * queue structures. It has no other purpose so is an optional component.
\r
144 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
146 /* The type stored within the queue registry array. This allows a name
\r
147 to be assigned to each queue making kernel aware debugging a little
\r
148 more user friendly. */
\r
149 typedef struct QUEUE_REGISTRY_ITEM
\r
151 signed char *pcQueueName;
\r
152 xQueueHandle xHandle;
\r
153 } xQueueRegistryItem;
\r
155 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
156 The pcQueueName member of a structure being NULL is indicative of the
\r
157 array position being vacant. */
\r
158 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
160 /* Removes a queue from the registry by simply setting the pcQueueName
\r
162 static void prvQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
164 #endif /* configQUEUE_REGISTRY_SIZE */
\r
167 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
168 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
169 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
170 * queue is locked it will instead increment the appropriate queue lock count
\r
171 * to indicate that a task may require unblocking. When the queue in unlocked
\r
172 * these lock counts are inspected, and the appropriate action taken.
\r
174 static void prvUnlockQueue( xQUEUE *pxQueue ) PRIVILEGED_FUNCTION;
\r
177 * Uses a critical section to determine if there is any data in a queue.
\r
179 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
181 static signed portBASE_TYPE prvIsQueueEmpty( const xQUEUE *pxQueue ) PRIVILEGED_FUNCTION;
\r
184 * Uses a critical section to determine if there is any space in a queue.
\r
186 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
188 static signed portBASE_TYPE prvIsQueueFull( const xQUEUE *pxQueue ) PRIVILEGED_FUNCTION;
\r
191 * Copies an item into the queue, either at the front of the queue or the
\r
192 * back of the queue.
\r
194 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
197 * Copies an item out of a queue.
\r
199 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
201 #if ( configUSE_QUEUE_SETS == 1 )
\r
203 * Checks to see if a queue is a member of a queue set, and if so, notifies
\r
204 * the queue set that the queue contains data.
\r
206 static portBASE_TYPE prvNotifyQueueSetContainer( xQUEUE *pxQueue, portBASE_TYPE xCopyPosition );
\r
209 /*-----------------------------------------------------------*/
\r
212 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
213 * accessing the queue event lists.
\r
215 #define prvLockQueue( pxQueue ) \
\r
216 taskENTER_CRITICAL(); \
\r
218 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
220 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
222 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
224 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
227 taskEXIT_CRITICAL()
\r
228 /*-----------------------------------------------------------*/
\r
230 portBASE_TYPE xQueueGenericReset( xQueueHandle xQueue, portBASE_TYPE xNewQueue )
\r
234 pxQueue = ( xQUEUE * ) xQueue;
\r
235 configASSERT( pxQueue );
\r
237 taskENTER_CRITICAL();
\r
239 pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
\r
240 pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
241 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
242 pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );
\r
243 pxQueue->xRxLock = queueUNLOCKED;
\r
244 pxQueue->xTxLock = queueUNLOCKED;
\r
246 if( xNewQueue == pdFALSE )
\r
248 /* If there are tasks blocked waiting to read from the queue, then
\r
249 the tasks will remain blocked as after this function exits the queue
\r
250 will still be empty. If there are tasks blocked waiting to write to
\r
251 the queue, then one should be unblocked as after this function exits
\r
252 it will be possible to write to it. */
\r
253 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
255 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
257 portYIELD_WITHIN_API();
\r
263 /* Ensure the event queues start in the correct state. */
\r
264 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
\r
265 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
\r
268 taskEXIT_CRITICAL();
\r
270 /* A value is returned for calling semantic consistency with previous
\r
274 /*-----------------------------------------------------------*/
\r
276 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )
\r
278 xQUEUE *pxNewQueue;
\r
279 size_t xQueueSizeInBytes;
\r
280 xQueueHandle xReturn = NULL;
\r
282 /* Remove compiler warnings about unused parameters should
\r
283 configUSE_TRACE_FACILITY not be set to 1. */
\r
284 ( void ) ucQueueType;
\r
286 /* Allocate the new queue structure. */
\r
287 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
289 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
290 if( pxNewQueue != NULL )
\r
292 /* Create the list of pointers to queue items. The queue is one byte
\r
293 longer than asked for to make wrap checking easier/faster. */
\r
294 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
296 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
297 if( pxNewQueue->pcHead != NULL )
\r
299 /* Initialise the queue members as described above where the
\r
300 queue type is defined. */
\r
301 pxNewQueue->uxLength = uxQueueLength;
\r
302 pxNewQueue->uxItemSize = uxItemSize;
\r
303 xQueueGenericReset( pxNewQueue, pdTRUE );
\r
305 #if ( configUSE_TRACE_FACILITY == 1 )
\r
307 pxNewQueue->ucQueueType = ucQueueType;
\r
309 #endif /* configUSE_TRACE_FACILITY */
\r
311 #if( configUSE_QUEUE_SETS == 1 )
\r
313 pxNewQueue->pxQueueSetContainer = NULL;
\r
315 #endif /* configUSE_QUEUE_SETS */
\r
317 traceQUEUE_CREATE( pxNewQueue );
\r
318 xReturn = pxNewQueue;
\r
322 traceQUEUE_CREATE_FAILED( ucQueueType );
\r
323 vPortFree( pxNewQueue );
\r
328 configASSERT( xReturn );
\r
332 /*-----------------------------------------------------------*/
\r
334 #if ( configUSE_MUTEXES == 1 )
\r
336 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )
\r
338 xQUEUE *pxNewQueue;
\r
340 /* Prevent compiler warnings about unused parameters if
\r
341 configUSE_TRACE_FACILITY does not equal 1. */
\r
342 ( void ) ucQueueType;
\r
344 /* Allocate the new queue structure. */
\r
345 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
346 if( pxNewQueue != NULL )
\r
348 /* Information required for priority inheritance. */
\r
349 pxNewQueue->pxMutexHolder = NULL;
\r
350 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
352 /* Queues used as a mutex no data is actually copied into or out
\r
354 pxNewQueue->pcWriteTo = NULL;
\r
355 pxNewQueue->pcReadFrom = NULL;
\r
357 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
358 an item size of 0 as nothing is actually copied into or out
\r
360 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
361 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
362 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
363 pxNewQueue->xRxLock = queueUNLOCKED;
\r
364 pxNewQueue->xTxLock = queueUNLOCKED;
\r
366 #if ( configUSE_TRACE_FACILITY == 1 )
\r
368 pxNewQueue->ucQueueType = ucQueueType;
\r
372 #if ( configUSE_QUEUE_SETS == 1 )
\r
374 pxNewQueue->pxQueueSetContainer = NULL;
\r
378 /* Ensure the event queues start with the correct state. */
\r
379 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
380 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
382 traceCREATE_MUTEX( pxNewQueue );
\r
384 /* Start with the semaphore in the expected state. */
\r
385 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
389 traceCREATE_MUTEX_FAILED();
\r
392 configASSERT( pxNewQueue );
\r
396 #endif /* configUSE_MUTEXES */
\r
397 /*-----------------------------------------------------------*/
\r
399 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xSemaphoreGetMutexHolder == 1 ) )
\r
401 void* xQueueGetMutexHolder( xQueueHandle xSemaphore )
\r
405 /* This function is called by xSemaphoreGetMutexHolder(), and should not
\r
406 be called directly. Note: This is is a good way of determining if the
\r
407 calling task is the mutex holder, but not a good way of determining the
\r
408 identity of the mutex holder, as the holder may change between the
\r
409 following critical section exiting and the function returning. */
\r
410 taskENTER_CRITICAL();
\r
412 if( ( ( xQUEUE * ) xSemaphore )->uxQueueType == queueQUEUE_IS_MUTEX )
\r
414 pxReturn = ( void * ) ( ( xQUEUE * ) xSemaphore )->pxMutexHolder;
\r
421 taskEXIT_CRITICAL();
\r
427 /*-----------------------------------------------------------*/
\r
429 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
431 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex )
\r
433 portBASE_TYPE xReturn;
\r
436 pxMutex = ( xQUEUE * ) xMutex;
\r
437 configASSERT( pxMutex );
\r
439 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
440 change outside of this task. If this task does not hold the mutex then
\r
441 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
442 this is the only condition we are interested in it does not matter if
\r
443 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
444 mutual exclusion is required to test the pxMutexHolder variable. */
\r
445 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
447 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
449 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
450 the task handle, therefore no underflow check is required. Also,
\r
451 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
452 there can only be one, no mutual exclusion is required to modify the
\r
453 uxRecursiveCallCount member. */
\r
454 ( pxMutex->uxRecursiveCallCount )--;
\r
456 /* Have we unwound the call count? */
\r
457 if( pxMutex->uxRecursiveCallCount == 0 )
\r
459 /* Return the mutex. This will automatically unblock any other
\r
460 task that might be waiting to access the mutex. */
\r
461 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
468 /* We cannot give the mutex because we are not the holder. */
\r
471 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
477 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
478 /*-----------------------------------------------------------*/
\r
480 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
482 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime )
\r
484 portBASE_TYPE xReturn;
\r
487 pxMutex = ( xQUEUE * ) xMutex;
\r
488 configASSERT( pxMutex );
\r
490 /* Comments regarding mutual exclusion as per those within
\r
491 xQueueGiveMutexRecursive(). */
\r
493 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
495 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
497 ( pxMutex->uxRecursiveCallCount )++;
\r
502 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
504 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
505 we may have blocked to reach here. */
\r
506 if( xReturn == pdPASS )
\r
508 ( pxMutex->uxRecursiveCallCount )++;
\r
512 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
519 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
520 /*-----------------------------------------------------------*/
\r
522 #if ( configUSE_COUNTING_SEMAPHORES == 1 )
\r
524 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
526 xQueueHandle xHandle;
\r
528 xHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
530 if( xHandle != NULL )
\r
532 ( ( xQUEUE * ) xHandle )->uxMessagesWaiting = uxInitialCount;
\r
534 traceCREATE_COUNTING_SEMAPHORE();
\r
538 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
541 configASSERT( xHandle );
\r
545 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
546 /*-----------------------------------------------------------*/
\r
548 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
550 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
551 xTimeOutType xTimeOut;
\r
554 pxQueue = ( xQUEUE * ) xQueue;
\r
555 configASSERT( pxQueue );
\r
556 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
558 /* This function relaxes the coding standard somewhat to allow return
\r
559 statements within the function itself. This is done in the interest
\r
560 of execution time efficiency. */
\r
563 taskENTER_CRITICAL();
\r
565 /* Is there room on the queue now? To be running we must be
\r
566 the highest priority task wanting to access the queue. */
\r
567 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
569 traceQUEUE_SEND( pxQueue );
\r
570 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
572 #if ( configUSE_QUEUE_SETS == 1 )
\r
574 if( pxQueue->pxQueueSetContainer != NULL )
\r
576 if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
\r
578 /* The queue is a member of a queue set, and posting
\r
579 to the queue set caused a higher priority task to
\r
580 unblock. A context switch is required. */
\r
581 portYIELD_WITHIN_API();
\r
586 /* If there was a task waiting for data to arrive on the
\r
587 queue then unblock it now. */
\r
588 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
590 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
592 /* The unblocked task has a priority higher than
\r
593 our own so yield immediately. Yes it is ok to
\r
594 do this from within the critical section - the
\r
595 kernel takes care of that. */
\r
596 portYIELD_WITHIN_API();
\r
601 #else /* configUSE_QUEUE_SETS */
\r
603 /* If there was a task waiting for data to arrive on the
\r
604 queue then unblock it now. */
\r
605 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
607 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
609 /* The unblocked task has a priority higher than
\r
610 our own so yield immediately. Yes it is ok to do
\r
611 this from within the critical section - the kernel
\r
612 takes care of that. */
\r
613 portYIELD_WITHIN_API();
\r
617 #endif /* configUSE_QUEUE_SETS */
\r
619 taskEXIT_CRITICAL();
\r
621 /* Return to the original privilege level before exiting the
\r
627 if( xTicksToWait == ( portTickType ) 0 )
\r
629 /* The queue was full and no block time is specified (or
\r
630 the block time has expired) so leave now. */
\r
631 taskEXIT_CRITICAL();
\r
633 /* Return to the original privilege level before exiting
\r
635 traceQUEUE_SEND_FAILED( pxQueue );
\r
636 return errQUEUE_FULL;
\r
638 else if( xEntryTimeSet == pdFALSE )
\r
640 /* The queue was full and a block time was specified so
\r
641 configure the timeout structure. */
\r
642 vTaskSetTimeOutState( &xTimeOut );
\r
643 xEntryTimeSet = pdTRUE;
\r
647 taskEXIT_CRITICAL();
\r
649 /* Interrupts and other tasks can send to and receive from the queue
\r
650 now the critical section has been exited. */
\r
653 prvLockQueue( pxQueue );
\r
655 /* Update the timeout state to see if it has expired yet. */
\r
656 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
658 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
660 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
661 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
663 /* Unlocking the queue means queue events can effect the
\r
664 event list. It is possible that interrupts occurring now
\r
665 remove this task from the event list again - but as the
\r
666 scheduler is suspended the task will go onto the pending
\r
667 ready last instead of the actual ready list. */
\r
668 prvUnlockQueue( pxQueue );
\r
670 /* Resuming the scheduler will move tasks from the pending
\r
671 ready list into the ready list - so it is feasible that this
\r
672 task is already in a ready list before it yields - in which
\r
673 case the yield will not cause a context switch unless there
\r
674 is also a higher priority task in the pending ready list. */
\r
675 if( xTaskResumeAll() == pdFALSE )
\r
677 portYIELD_WITHIN_API();
\r
683 prvUnlockQueue( pxQueue );
\r
684 ( void ) xTaskResumeAll();
\r
689 /* The timeout has expired. */
\r
690 prvUnlockQueue( pxQueue );
\r
691 ( void ) xTaskResumeAll();
\r
693 /* Return to the original privilege level before exiting the
\r
695 traceQUEUE_SEND_FAILED( pxQueue );
\r
696 return errQUEUE_FULL;
\r
700 /*-----------------------------------------------------------*/
\r
702 #if ( configUSE_ALTERNATIVE_API == 1 )
\r
704 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
706 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
707 xTimeOutType xTimeOut;
\r
710 pxQueue = ( xQUEUE * ) xQueue;
\r
711 configASSERT( pxQueue );
\r
712 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
716 taskENTER_CRITICAL();
\r
718 /* Is there room on the queue now? To be running we must be
\r
719 the highest priority task wanting to access the queue. */
\r
720 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
722 traceQUEUE_SEND( pxQueue );
\r
723 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
725 /* If there was a task waiting for data to arrive on the
\r
726 queue then unblock it now. */
\r
727 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
729 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
731 /* The unblocked task has a priority higher than
\r
732 our own so yield immediately. */
\r
733 portYIELD_WITHIN_API();
\r
737 taskEXIT_CRITICAL();
\r
742 if( xTicksToWait == ( portTickType ) 0 )
\r
744 taskEXIT_CRITICAL();
\r
745 return errQUEUE_FULL;
\r
747 else if( xEntryTimeSet == pdFALSE )
\r
749 vTaskSetTimeOutState( &xTimeOut );
\r
750 xEntryTimeSet = pdTRUE;
\r
754 taskEXIT_CRITICAL();
\r
756 taskENTER_CRITICAL();
\r
758 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
760 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
762 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
763 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
764 portYIELD_WITHIN_API();
\r
769 taskEXIT_CRITICAL();
\r
770 traceQUEUE_SEND_FAILED( pxQueue );
\r
771 return errQUEUE_FULL;
\r
774 taskEXIT_CRITICAL();
\r
778 #endif /* configUSE_ALTERNATIVE_API */
\r
779 /*-----------------------------------------------------------*/
\r
781 #if ( configUSE_ALTERNATIVE_API == 1 )
\r
783 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle xQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
785 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
786 xTimeOutType xTimeOut;
\r
787 signed char *pcOriginalReadPosition;
\r
790 pxQueue = ( xQUEUE * ) xQueue;
\r
791 configASSERT( pxQueue );
\r
792 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
796 taskENTER_CRITICAL();
\r
798 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
800 /* Remember our read position in case we are just peeking. */
\r
801 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
803 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
805 if( xJustPeeking == pdFALSE )
\r
807 traceQUEUE_RECEIVE( pxQueue );
\r
809 /* We are actually removing data. */
\r
810 --( pxQueue->uxMessagesWaiting );
\r
812 #if ( configUSE_MUTEXES == 1 )
\r
814 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
816 /* Record the information required to implement
\r
817 priority inheritance should it become necessary. */
\r
818 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
823 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
825 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
827 portYIELD_WITHIN_API();
\r
833 traceQUEUE_PEEK( pxQueue );
\r
835 /* We are not removing the data, so reset our read
\r
837 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
839 /* The data is being left in the queue, so see if there are
\r
840 any other tasks waiting for the data. */
\r
841 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
843 /* Tasks that are removed from the event list will get added to
\r
844 the pending ready list as the scheduler is still suspended. */
\r
845 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
847 /* The task waiting has a higher priority than this task. */
\r
848 portYIELD_WITHIN_API();
\r
854 taskEXIT_CRITICAL();
\r
859 if( xTicksToWait == ( portTickType ) 0 )
\r
861 taskEXIT_CRITICAL();
\r
862 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
863 return errQUEUE_EMPTY;
\r
865 else if( xEntryTimeSet == pdFALSE )
\r
867 vTaskSetTimeOutState( &xTimeOut );
\r
868 xEntryTimeSet = pdTRUE;
\r
872 taskEXIT_CRITICAL();
\r
874 taskENTER_CRITICAL();
\r
876 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
878 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
880 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
882 #if ( configUSE_MUTEXES == 1 )
\r
884 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
886 portENTER_CRITICAL();
\r
888 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
890 portEXIT_CRITICAL();
\r
895 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
896 portYIELD_WITHIN_API();
\r
901 taskEXIT_CRITICAL();
\r
902 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
903 return errQUEUE_EMPTY;
\r
906 taskEXIT_CRITICAL();
\r
911 #endif /* configUSE_ALTERNATIVE_API */
\r
912 /*-----------------------------------------------------------*/
\r
914 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle xQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
916 signed portBASE_TYPE xReturn;
\r
917 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
920 pxQueue = ( xQUEUE * ) xQueue;
\r
921 configASSERT( pxQueue );
\r
922 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
924 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
925 in the queue. Also we don't directly wake a task that was blocked on a
\r
926 queue read, instead we return a flag to say whether a context switch is
\r
927 required or not (i.e. has a task with a higher priority than us been woken
\r
929 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
931 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
933 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
935 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
937 /* If the queue is locked we do not alter the event list. This will
\r
938 be done when the queue is unlocked later. */
\r
939 if( pxQueue->xTxLock == queueUNLOCKED )
\r
941 #if ( configUSE_QUEUE_SETS == 1 )
\r
943 if( pxQueue->pxQueueSetContainer != NULL )
\r
945 if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE )
\r
947 /* The queue is a member of a queue set, and posting
\r
948 to the queue set caused a higher priority task to
\r
949 unblock. A context switch is required. */
\r
950 if( pxHigherPriorityTaskWoken != NULL )
\r
952 *pxHigherPriorityTaskWoken = pdTRUE;
\r
958 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
960 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
962 /* The task waiting has a higher priority so record that a
\r
963 context switch is required. */
\r
964 if( pxHigherPriorityTaskWoken != NULL )
\r
966 *pxHigherPriorityTaskWoken = pdTRUE;
\r
972 #else /* configUSE_QUEUE_SETS */
\r
974 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
976 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
978 /* The task waiting has a higher priority so record that a
\r
979 context switch is required. */
\r
980 if( pxHigherPriorityTaskWoken != NULL )
\r
982 *pxHigherPriorityTaskWoken = pdTRUE;
\r
987 #endif /* configUSE_QUEUE_SETS */
\r
991 /* Increment the lock count so the task that unlocks the queue
\r
992 knows that data was posted while it was locked. */
\r
993 ++( pxQueue->xTxLock );
\r
1000 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
1001 xReturn = errQUEUE_FULL;
\r
1004 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1008 /*-----------------------------------------------------------*/
\r
1010 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle xQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
1012 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
1013 xTimeOutType xTimeOut;
\r
1014 signed char *pcOriginalReadPosition;
\r
1017 pxQueue = ( xQUEUE * ) xQueue;
\r
1018 configASSERT( pxQueue );
\r
1019 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1021 /* This function relaxes the coding standard somewhat to allow return
\r
1022 statements within the function itself. This is done in the interest
\r
1023 of execution time efficiency. */
\r
1027 taskENTER_CRITICAL();
\r
1029 /* Is there data in the queue now? To be running we must be
\r
1030 the highest priority task wanting to access the queue. */
\r
1031 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1033 /* Remember our read position in case we are just peeking. */
\r
1034 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
1036 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1038 if( xJustPeeking == pdFALSE )
\r
1040 traceQUEUE_RECEIVE( pxQueue );
\r
1042 /* We are actually removing data. */
\r
1043 --( pxQueue->uxMessagesWaiting );
\r
1045 #if ( configUSE_MUTEXES == 1 )
\r
1047 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1049 /* Record the information required to implement
\r
1050 priority inheritance should it become necessary. */
\r
1051 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
1056 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1058 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
1060 portYIELD_WITHIN_API();
\r
1066 traceQUEUE_PEEK( pxQueue );
\r
1068 /* The data is not being removed, so reset the read
\r
1070 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
1072 /* The data is being left in the queue, so see if there are
\r
1073 any other tasks waiting for the data. */
\r
1074 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1076 /* Tasks that are removed from the event list will get added to
\r
1077 the pending ready list as the scheduler is still suspended. */
\r
1078 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1080 /* The task waiting has a higher priority than this task. */
\r
1081 portYIELD_WITHIN_API();
\r
1086 taskEXIT_CRITICAL();
\r
1091 if( xTicksToWait == ( portTickType ) 0 )
\r
1093 /* The queue was empty and no block time is specified (or
\r
1094 the block time has expired) so leave now. */
\r
1095 taskEXIT_CRITICAL();
\r
1096 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1097 return errQUEUE_EMPTY;
\r
1099 else if( xEntryTimeSet == pdFALSE )
\r
1101 /* The queue was empty and a block time was specified so
\r
1102 configure the timeout structure. */
\r
1103 vTaskSetTimeOutState( &xTimeOut );
\r
1104 xEntryTimeSet = pdTRUE;
\r
1108 taskEXIT_CRITICAL();
\r
1110 /* Interrupts and other tasks can send to and receive from the queue
\r
1111 now the critical section has been exited. */
\r
1113 vTaskSuspendAll();
\r
1114 prvLockQueue( pxQueue );
\r
1116 /* Update the timeout state to see if it has expired yet. */
\r
1117 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1119 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1121 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1123 #if ( configUSE_MUTEXES == 1 )
\r
1125 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1127 portENTER_CRITICAL();
\r
1129 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
1131 portEXIT_CRITICAL();
\r
1136 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1137 prvUnlockQueue( pxQueue );
\r
1138 if( xTaskResumeAll() == pdFALSE )
\r
1140 portYIELD_WITHIN_API();
\r
1146 prvUnlockQueue( pxQueue );
\r
1147 ( void ) xTaskResumeAll();
\r
1152 prvUnlockQueue( pxQueue );
\r
1153 ( void ) xTaskResumeAll();
\r
1154 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1155 return errQUEUE_EMPTY;
\r
1159 /*-----------------------------------------------------------*/
\r
1161 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle xQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken )
\r
1163 signed portBASE_TYPE xReturn;
\r
1164 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1167 pxQueue = ( xQUEUE * ) xQueue;
\r
1168 configASSERT( pxQueue );
\r
1169 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1171 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1173 /* We cannot block from an ISR, so check there is data available. */
\r
1174 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1176 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1178 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1179 --( pxQueue->uxMessagesWaiting );
\r
1181 /* If the queue is locked we will not modify the event list. Instead
\r
1182 we update the lock count so the task that unlocks the queue will know
\r
1183 that an ISR has removed data while the queue was locked. */
\r
1184 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1186 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1188 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1190 /* The task waiting has a higher priority than us so
\r
1191 force a context switch. */
\r
1192 if( pxHigherPriorityTaskWoken != NULL )
\r
1194 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1201 /* Increment the lock count so the task that unlocks the queue
\r
1202 knows that data was removed while it was locked. */
\r
1203 ++( pxQueue->xRxLock );
\r
1211 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1214 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1218 /*-----------------------------------------------------------*/
\r
1220 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle xQueue )
\r
1222 unsigned portBASE_TYPE uxReturn;
\r
1224 configASSERT( xQueue );
\r
1226 taskENTER_CRITICAL();
\r
1227 uxReturn = ( ( xQUEUE * ) xQueue )->uxMessagesWaiting;
\r
1228 taskEXIT_CRITICAL();
\r
1232 /*-----------------------------------------------------------*/
\r
1234 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle xQueue )
\r
1236 unsigned portBASE_TYPE uxReturn;
\r
1238 configASSERT( xQueue );
\r
1240 uxReturn = ( ( xQUEUE * ) xQueue )->uxMessagesWaiting;
\r
1244 /*-----------------------------------------------------------*/
\r
1246 void vQueueDelete( xQueueHandle xQueue )
\r
1250 pxQueue = ( xQUEUE * ) xQueue;
\r
1251 configASSERT( pxQueue );
\r
1253 traceQUEUE_DELETE( pxQueue );
\r
1254 prvQueueUnregisterQueue( pxQueue );
\r
1255 vPortFree( pxQueue->pcHead );
\r
1256 vPortFree( pxQueue );
\r
1258 /*-----------------------------------------------------------*/
\r
1260 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1262 unsigned char ucQueueGetQueueNumber( xQueueHandle xQueue )
\r
1264 return ( ( xQUEUE * ) xQueue )->ucQueueNumber;
\r
1267 #endif /* configUSE_TRACE_FACILITY */
\r
1268 /*-----------------------------------------------------------*/
\r
1270 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1272 void vQueueSetQueueNumber( xQueueHandle xQueue, unsigned char ucQueueNumber )
\r
1274 ( ( xQUEUE * ) xQueue )->ucQueueNumber = ucQueueNumber;
\r
1277 #endif /* configUSE_TRACE_FACILITY */
\r
1278 /*-----------------------------------------------------------*/
\r
1280 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1282 unsigned char ucQueueGetQueueType( xQueueHandle xQueue )
\r
1284 return ( ( xQUEUE * ) xQueue )->ucQueueType;
\r
1287 #endif /* configUSE_TRACE_FACILITY */
\r
1288 /*-----------------------------------------------------------*/
\r
1290 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1292 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1294 #if ( configUSE_MUTEXES == 1 )
\r
1296 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1298 /* The mutex is no longer being held. */
\r
1299 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1300 pxQueue->pxMutexHolder = NULL;
\r
1305 else if( xPosition == queueSEND_TO_BACK )
\r
1307 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1308 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1309 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1311 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1316 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1317 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1318 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1320 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1324 ++( pxQueue->uxMessagesWaiting );
\r
1326 /*-----------------------------------------------------------*/
\r
1328 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1330 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1332 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1333 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1335 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1337 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1340 /*-----------------------------------------------------------*/
\r
1342 static void prvUnlockQueue( xQUEUE *pxQueue )
\r
1344 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1346 /* The lock counts contains the number of extra data items placed or
\r
1347 removed from the queue while the queue was locked. When a queue is
\r
1348 locked items can be added or removed, but the event lists cannot be
\r
1350 taskENTER_CRITICAL();
\r
1352 /* See if data was added to the queue while it was locked. */
\r
1353 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1355 /* Data was posted while the queue was locked. Are any tasks
\r
1356 blocked waiting for data to become available? */
\r
1357 #if ( configUSE_QUEUE_SETS == 1 )
\r
1359 if( pxQueue->pxQueueSetContainer != NULL )
\r
1361 if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) == pdTRUE )
\r
1363 /* The queue is a member of a queue set, and posting to
\r
1364 the queue set caused a higher priority task to unblock.
\r
1365 A context switch is required. */
\r
1366 vTaskMissedYield();
\r
1371 /* Tasks that are removed from the event list will get added to
\r
1372 the pending ready list as the scheduler is still suspended. */
\r
1373 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1375 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1377 /* The task waiting has a higher priority so record that a
\r
1378 context switch is required. */
\r
1379 vTaskMissedYield();
\r
1388 #else /* configUSE_QUEUE_SETS */
\r
1390 /* Tasks that are removed from the event list will get added to
\r
1391 the pending ready list as the scheduler is still suspended. */
\r
1392 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1394 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1396 /* The task waiting has a higher priority so record that a
\r
1397 context switch is required. */
\r
1398 vTaskMissedYield();
\r
1406 #endif /* configUSE_QUEUE_SETS */
\r
1408 --( pxQueue->xTxLock );
\r
1411 pxQueue->xTxLock = queueUNLOCKED;
\r
1413 taskEXIT_CRITICAL();
\r
1415 /* Do the same for the Rx lock. */
\r
1416 taskENTER_CRITICAL();
\r
1418 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1420 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1422 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1424 vTaskMissedYield();
\r
1427 --( pxQueue->xRxLock );
\r
1435 pxQueue->xRxLock = queueUNLOCKED;
\r
1437 taskEXIT_CRITICAL();
\r
1439 /*-----------------------------------------------------------*/
\r
1441 static signed portBASE_TYPE prvIsQueueEmpty( const xQUEUE *pxQueue )
\r
1443 signed portBASE_TYPE xReturn;
\r
1445 taskENTER_CRITICAL();
\r
1447 if( pxQueue->uxMessagesWaiting == 0 )
\r
1453 xReturn = pdFALSE;
\r
1456 taskEXIT_CRITICAL();
\r
1460 /*-----------------------------------------------------------*/
\r
1462 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle xQueue )
\r
1464 signed portBASE_TYPE xReturn;
\r
1466 configASSERT( xQueue );
\r
1467 if( ( ( xQUEUE * ) xQueue )->uxMessagesWaiting == 0 )
\r
1473 xReturn = pdFALSE;
\r
1478 /*-----------------------------------------------------------*/
\r
1480 static signed portBASE_TYPE prvIsQueueFull( const xQUEUE *pxQueue )
\r
1482 signed portBASE_TYPE xReturn;
\r
1484 taskENTER_CRITICAL();
\r
1486 if( pxQueue->uxMessagesWaiting == pxQueue->uxLength )
\r
1492 xReturn = pdFALSE;
\r
1495 taskEXIT_CRITICAL();
\r
1499 /*-----------------------------------------------------------*/
\r
1501 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle xQueue )
\r
1503 signed portBASE_TYPE xReturn;
\r
1505 configASSERT( xQueue );
\r
1506 if( ( ( xQUEUE * ) xQueue )->uxMessagesWaiting == ( ( xQUEUE * ) xQueue )->uxLength )
\r
1512 xReturn = pdFALSE;
\r
1517 /*-----------------------------------------------------------*/
\r
1519 #if ( configUSE_CO_ROUTINES == 1 )
\r
1521 signed portBASE_TYPE xQueueCRSend( xQueueHandle xQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1523 signed portBASE_TYPE xReturn;
\r
1526 pxQueue = ( xQUEUE * ) xQueue;
\r
1528 /* If the queue is already full we may have to block. A critical section
\r
1529 is required to prevent an interrupt removing something from the queue
\r
1530 between the check to see if the queue is full and blocking on the queue. */
\r
1531 portDISABLE_INTERRUPTS();
\r
1533 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1535 /* The queue is full - do we want to block or just leave without
\r
1537 if( xTicksToWait > ( portTickType ) 0 )
\r
1539 /* As this is called from a coroutine we cannot block directly, but
\r
1540 return indicating that we need to block. */
\r
1541 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1542 portENABLE_INTERRUPTS();
\r
1543 return errQUEUE_BLOCKED;
\r
1547 portENABLE_INTERRUPTS();
\r
1548 return errQUEUE_FULL;
\r
1552 portENABLE_INTERRUPTS();
\r
1554 portDISABLE_INTERRUPTS();
\r
1556 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1558 /* There is room in the queue, copy the data into the queue. */
\r
1559 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1562 /* Were any co-routines waiting for data to become available? */
\r
1563 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1565 /* In this instance the co-routine could be placed directly
\r
1566 into the ready list as we are within a critical section.
\r
1567 Instead the same pending ready list mechanism is used as if
\r
1568 the event were caused from within an interrupt. */
\r
1569 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1571 /* The co-routine waiting has a higher priority so record
\r
1572 that a yield might be appropriate. */
\r
1573 xReturn = errQUEUE_YIELD;
\r
1579 xReturn = errQUEUE_FULL;
\r
1582 portENABLE_INTERRUPTS();
\r
1587 #endif /* configUSE_CO_ROUTINES */
\r
1588 /*-----------------------------------------------------------*/
\r
1590 #if ( configUSE_CO_ROUTINES == 1 )
\r
1592 signed portBASE_TYPE xQueueCRReceive( xQueueHandle xQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1594 signed portBASE_TYPE xReturn;
\r
1597 pxQueue = ( xQUEUE * ) xQueue;
\r
1599 /* If the queue is already empty we may have to block. A critical section
\r
1600 is required to prevent an interrupt adding something to the queue
\r
1601 between the check to see if the queue is empty and blocking on the queue. */
\r
1602 portDISABLE_INTERRUPTS();
\r
1604 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1606 /* There are no messages in the queue, do we want to block or just
\r
1607 leave with nothing? */
\r
1608 if( xTicksToWait > ( portTickType ) 0 )
\r
1610 /* As this is a co-routine we cannot block directly, but return
\r
1611 indicating that we need to block. */
\r
1612 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1613 portENABLE_INTERRUPTS();
\r
1614 return errQUEUE_BLOCKED;
\r
1618 portENABLE_INTERRUPTS();
\r
1619 return errQUEUE_FULL;
\r
1623 portENABLE_INTERRUPTS();
\r
1625 portDISABLE_INTERRUPTS();
\r
1627 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1629 /* Data is available from the queue. */
\r
1630 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1631 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1633 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1635 --( pxQueue->uxMessagesWaiting );
\r
1636 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1640 /* Were any co-routines waiting for space to become available? */
\r
1641 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1643 /* In this instance the co-routine could be placed directly
\r
1644 into the ready list as we are within a critical section.
\r
1645 Instead the same pending ready list mechanism is used as if
\r
1646 the event were caused from within an interrupt. */
\r
1647 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1649 xReturn = errQUEUE_YIELD;
\r
1658 portENABLE_INTERRUPTS();
\r
1663 #endif /* configUSE_CO_ROUTINES */
\r
1664 /*-----------------------------------------------------------*/
\r
1666 #if ( configUSE_CO_ROUTINES == 1 )
\r
1668 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle xQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1672 pxQueue = ( xQUEUE * ) xQueue;
\r
1674 /* Cannot block within an ISR so if there is no space on the queue then
\r
1675 exit without doing anything. */
\r
1676 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1678 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1680 /* We only want to wake one co-routine per ISR, so check that a
\r
1681 co-routine has not already been woken. */
\r
1682 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1684 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1686 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1694 return xCoRoutinePreviouslyWoken;
\r
1697 #endif /* configUSE_CO_ROUTINES */
\r
1698 /*-----------------------------------------------------------*/
\r
1700 #if ( configUSE_CO_ROUTINES == 1 )
\r
1702 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle xQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1704 signed portBASE_TYPE xReturn;
\r
1707 pxQueue = ( xQUEUE * ) xQueue;
\r
1709 /* We cannot block from an ISR, so check there is data available. If
\r
1710 not then just leave without doing anything. */
\r
1711 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1713 /* Copy the data from the queue. */
\r
1714 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1715 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1717 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1719 --( pxQueue->uxMessagesWaiting );
\r
1720 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1722 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1724 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1726 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1728 *pxCoRoutineWoken = pdTRUE;
\r
1743 #endif /* configUSE_CO_ROUTINES */
\r
1744 /*-----------------------------------------------------------*/
\r
1746 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
1748 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1750 unsigned portBASE_TYPE ux;
\r
1752 /* See if there is an empty space in the registry. A NULL name denotes
\r
1754 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1756 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1758 /* Store the information on this queue. */
\r
1759 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1760 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1766 #endif /* configQUEUE_REGISTRY_SIZE */
\r
1767 /*-----------------------------------------------------------*/
\r
1769 #if ( configQUEUE_REGISTRY_SIZE > 0 )
\r
1771 static void prvQueueUnregisterQueue( xQueueHandle xQueue )
\r
1773 unsigned portBASE_TYPE ux;
\r
1775 /* See if the handle of the queue being unregistered in actually in the
\r
1777 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1779 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1781 /* Set the name to NULL to show that this slot if free again. */
\r
1782 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1789 #endif /* configQUEUE_REGISTRY_SIZE */
\r
1790 /*-----------------------------------------------------------*/
\r
1792 #if ( configUSE_TIMERS == 1 )
\r
1794 void vQueueWaitForMessageRestricted( xQueueHandle xQueue, portTickType xTicksToWait )
\r
1798 pxQueue = ( xQUEUE * ) xQueue;
\r
1800 /* This function should not be called by application code hence the
\r
1801 'Restricted' in its name. It is not part of the public API. It is
\r
1802 designed for use by kernel code, and has special calling requirements.
\r
1803 It can result in vListInsert() being called on a list that can only
\r
1804 possibly ever have one item in it, so the list will be fast, but even
\r
1805 so it should be called with the scheduler locked and not from a critical
\r
1808 /* Only do anything if there are no messages in the queue. This function
\r
1809 will not actually cause the task to block, just place it on a blocked
\r
1810 list. It will not block until the scheduler is unlocked - at which
\r
1811 time a yield will be performed. If an item is added to the queue while
\r
1812 the queue is locked, and the calling task blocks on the queue, then the
\r
1813 calling task will be immediately unblocked when the queue is unlocked. */
\r
1814 prvLockQueue( pxQueue );
\r
1815 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1817 /* There is nothing in the queue, block for the specified period. */
\r
1818 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1820 prvUnlockQueue( pxQueue );
\r
1823 #endif /* configUSE_TIMERS */
\r
1824 /*-----------------------------------------------------------*/
\r
1826 #if ( configUSE_QUEUE_SETS == 1 )
\r
1828 xQueueSetHandle xQueueCreateSet( unsigned portBASE_TYPE uxEventQueueLength )
\r
1830 xQueueSetHandle pxQueue;
\r
1832 pxQueue = xQueueGenericCreate( uxEventQueueLength, sizeof( xQUEUE * ), queueQUEUE_TYPE_SET );
\r
1837 #endif /* configUSE_QUEUE_SETS */
\r
1838 /*-----------------------------------------------------------*/
\r
1840 #if ( configUSE_QUEUE_SETS == 1 )
\r
1842 portBASE_TYPE xQueueAddToSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet )
\r
1844 portBASE_TYPE xReturn;
\r
1846 if( ( ( xQUEUE * ) xQueueOrSemaphore )->pxQueueSetContainer != NULL )
\r
1852 taskENTER_CRITICAL();
\r
1854 ( ( xQUEUE * ) xQueueOrSemaphore )->pxQueueSetContainer = xQueueSet;
\r
1856 taskEXIT_CRITICAL();
\r
1863 #endif /* configUSE_QUEUE_SETS */
\r
1864 /*-----------------------------------------------------------*/
\r
1866 #if ( configUSE_QUEUE_SETS == 1 )
\r
1868 portBASE_TYPE xQueueRemoveFromSet( xQueueSetMemberHandle xQueueOrSemaphore, xQueueSetHandle xQueueSet )
\r
1870 portBASE_TYPE xReturn;
\r
1871 xQUEUE *pxQueueOrSemaphore;
\r
1873 pxQueueOrSemaphore = ( xQUEUE * ) xQueueOrSemaphore;
\r
1875 if( pxQueueOrSemaphore->pxQueueSetContainer != xQueueSet )
\r
1877 /* The queue was not a member of the set. */
\r
1880 else if( pxQueueOrSemaphore->uxMessagesWaiting != 0 )
\r
1882 /* It is dangerous to remove a queue from a set when the queue is
\r
1883 not empty because the queue set will still hold pending events for
\r
1889 taskENTER_CRITICAL();
\r
1891 /* The queue is no longer contained in the set. */
\r
1892 pxQueueOrSemaphore->pxQueueSetContainer = NULL;
\r
1894 taskEXIT_CRITICAL();
\r
1901 #endif /* configUSE_QUEUE_SETS */
\r
1902 /*-----------------------------------------------------------*/
\r
1904 #if ( configUSE_QUEUE_SETS == 1 )
\r
1906 xQueueSetMemberHandle xQueueSelectFromSet( xQueueSetHandle xQueueSet, portTickType xBlockTimeTicks )
\r
1908 xQueueSetMemberHandle xReturn = NULL;
\r
1910 xQueueGenericReceive( ( xQueueHandle ) xQueueSet, &xReturn, xBlockTimeTicks, pdFALSE );
\r
1914 #endif /* configUSE_QUEUE_SETS */
\r
1915 /*-----------------------------------------------------------*/
\r
1917 #if ( configUSE_QUEUE_SETS == 1 )
\r
1919 xQueueSetMemberHandle xQueueSelectFromSetFromISR( xQueueSetHandle xQueueSet )
\r
1921 xQueueSetMemberHandle xReturn = NULL;
\r
1923 xQueueReceiveFromISR( ( xQueueHandle ) xQueueSet, &xReturn, NULL );
\r
1927 #endif /* configUSE_QUEUE_SETS */
\r
1928 /*-----------------------------------------------------------*/
\r
1930 #if ( configUSE_QUEUE_SETS == 1 )
\r
1932 static portBASE_TYPE prvNotifyQueueSetContainer( xQUEUE *pxQueue, portBASE_TYPE xCopyPosition )
\r
1934 xQUEUE *pxQueueSetContainer = pxQueue->pxQueueSetContainer;
\r
1935 portBASE_TYPE xReturn = pdFALSE;
\r
1937 configASSERT( pxQueueSetContainer );
\r
1938 configASSERT( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength );
\r
1940 if( pxQueueSetContainer->uxMessagesWaiting < pxQueueSetContainer->uxLength )
\r
1942 traceQUEUE_SEND( pxQueueSetContainer );
\r
1943 /* The data copies is the handle of the queue that contains data. */
\r
1944 prvCopyDataToQueue( pxQueueSetContainer, &pxQueue, xCopyPosition );
\r
1945 if( listLIST_IS_EMPTY( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1947 if( xTaskRemoveFromEventList( &( pxQueueSetContainer->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1949 /* The task waiting has a higher priority */
\r
1958 #endif /* configUSE_QUEUE_SETS */
\r