2 FreeRTOS V7.1.0 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
58 all the API functions to use the MPU wrappers. That should only be done when
\r
59 task.h is included from an application file. */
\r
60 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
62 #include "FreeRTOS.h"
\r
65 #if ( configUSE_CO_ROUTINES == 1 )
\r
66 #include "croutine.h"
\r
69 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
71 /*-----------------------------------------------------------
\r
72 * PUBLIC LIST API documented in list.h
\r
73 *----------------------------------------------------------*/
\r
75 /* Constants used with the cRxLock and cTxLock structure members. */
\r
76 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
77 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
79 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
81 /* For internal use only. */
\r
82 #define queueSEND_TO_BACK ( 0 )
\r
83 #define queueSEND_TO_FRONT ( 1 )
\r
85 /* Effectively make a union out of the xQUEUE structure. */
\r
86 #define pxMutexHolder pcTail
\r
87 #define uxQueueType pcHead
\r
88 #define uxRecursiveCallCount pcReadFrom
\r
89 #define queueQUEUE_IS_MUTEX NULL
\r
91 /* Semaphores do not actually store or copy data, so have an items size of
\r
93 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )
\r
94 #define queueDONT_BLOCK ( ( portTickType ) 0U )
\r
95 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0U )
\r
97 /* These definitions *must* match those in queue.h. */
\r
98 #define queueQUEUE_TYPE_BASE ( 0U )
\r
99 #define queueQUEUE_TYPE_MUTEX ( 1U )
\r
100 #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( 2U )
\r
101 #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( 3U )
\r
102 #define queueQUEUE_TYPE_RECURSIVE_MUTEX ( 4U )
\r
105 * Definition of the queue used by the scheduler.
\r
106 * Items are queued by copy, not reference.
\r
108 typedef struct QueueDefinition
\r
110 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
111 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
113 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
114 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
116 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
117 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
119 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
120 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
121 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
123 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
124 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
126 #if ( configUSE_TRACE_FACILITY == 1 )
\r
127 unsigned char ucQueueNumber;
\r
128 unsigned char ucQueueType;
\r
132 /*-----------------------------------------------------------*/
\r
135 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
136 * To keep the definition private the API header file defines it as a
\r
139 typedef xQUEUE * xQueueHandle;
\r
142 * Prototypes for public functions are included here so we don't have to
\r
143 * include the API header file (as it defines xQueueHandle differently). These
\r
144 * functions are documented in the API header file.
\r
146 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
147 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
148 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
149 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
150 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
151 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
152 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
153 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
154 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
155 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
156 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
157 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
158 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
159 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
160 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
161 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
162 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
163 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
164 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber ) PRIVILEGED_FUNCTION;
\r
165 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
166 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue ) PRIVILEGED_FUNCTION;
\r
167 xTaskHandle xQueueGetMutexHolder( xQueueHandle xSemaphore ) PRIVILEGED_FUNCTION;
\r
170 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
171 * an optional component.
\r
173 #if configUSE_CO_ROUTINES == 1
\r
174 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
175 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
176 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
177 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
181 * The queue registry is just a means for kernel aware debuggers to locate
\r
182 * queue structures. It has no other purpose so is an optional component.
\r
184 #if configQUEUE_REGISTRY_SIZE > 0
\r
186 /* The type stored within the queue registry array. This allows a name
\r
187 to be assigned to each queue making kernel aware debugging a little
\r
188 more user friendly. */
\r
189 typedef struct QUEUE_REGISTRY_ITEM
\r
191 signed char *pcQueueName;
\r
192 xQueueHandle xHandle;
\r
193 } xQueueRegistryItem;
\r
195 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
196 The pcQueueName member of a structure being NULL is indicative of the
\r
197 array position being vacant. */
\r
198 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
200 /* Removes a queue from the registry by simply setting the pcQueueName
\r
202 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
203 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
207 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
208 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
209 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
210 * queue is locked it will instead increment the appropriate queue lock count
\r
211 * to indicate that a task may require unblocking. When the queue in unlocked
\r
212 * these lock counts are inspected, and the appropriate action taken.
\r
214 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
217 * Uses a critical section to determine if there is any data in a queue.
\r
219 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
221 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
224 * Uses a critical section to determine if there is any space in a queue.
\r
226 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
228 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
231 * Copies an item into the queue, either at the front of the queue or the
\r
232 * back of the queue.
\r
234 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
237 * Copies an item out of a queue.
\r
239 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
240 /*-----------------------------------------------------------*/
\r
243 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
244 * accessing the queue event lists.
\r
246 #define prvLockQueue( pxQueue ) \
\r
247 taskENTER_CRITICAL(); \
\r
249 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
251 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
253 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
255 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
258 taskEXIT_CRITICAL()
\r
259 /*-----------------------------------------------------------*/
\r
262 /*-----------------------------------------------------------
\r
263 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
264 *----------------------------------------------------------*/
\r
266 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue )
\r
268 portBASE_TYPE xReturn = pdPASS;
\r
270 configASSERT( pxQueue );
\r
272 /* If the queue being reset has already been used (has not just been
\r
273 created), then only reset the queue if its event lists are empty. */
\r
274 if( xNewQueue != pdTRUE )
\r
276 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
281 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
287 if( xReturn == pdPASS )
\r
289 pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
\r
290 pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
291 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
292 pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );
\r
293 pxQueue->xRxLock = queueUNLOCKED;
\r
294 pxQueue->xTxLock = queueUNLOCKED;
\r
296 /* Ensure the event queues start with the correct state. */
\r
297 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
\r
298 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
\r
303 /*-----------------------------------------------------------*/
\r
305 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )
\r
307 xQUEUE *pxNewQueue;
\r
308 size_t xQueueSizeInBytes;
\r
309 xQueueHandle xReturn = NULL;
\r
311 /* Remove compiler warnings about unused parameters should
\r
312 configUSE_TRACE_FACILITY not be set to 1. */
\r
313 ( void ) ucQueueType;
\r
315 /* Allocate the new queue structure. */
\r
316 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
318 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
319 if( pxNewQueue != NULL )
\r
321 /* Create the list of pointers to queue items. The queue is one byte
\r
322 longer than asked for to make wrap checking easier/faster. */
\r
323 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
325 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
326 if( pxNewQueue->pcHead != NULL )
\r
328 /* Initialise the queue members as described above where the
\r
329 queue type is defined. */
\r
330 pxNewQueue->uxLength = uxQueueLength;
\r
331 pxNewQueue->uxItemSize = uxItemSize;
\r
332 xQueueGenericReset( pxNewQueue, pdTRUE );
\r
333 #if ( configUSE_TRACE_FACILITY == 1 )
\r
335 pxNewQueue->ucQueueType = ucQueueType;
\r
337 #endif /* configUSE_TRACE_FACILITY */
\r
339 traceQUEUE_CREATE( pxNewQueue );
\r
340 xReturn = pxNewQueue;
\r
344 traceQUEUE_CREATE_FAILED( ucQueueType );
\r
345 vPortFree( pxNewQueue );
\r
350 configASSERT( xReturn );
\r
354 /*-----------------------------------------------------------*/
\r
356 #if ( configUSE_MUTEXES == 1 )
\r
358 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )
\r
360 xQUEUE *pxNewQueue;
\r
362 /* Prevent compiler warnings about unused parameters if
\r
363 configUSE_TRACE_FACILITY does not equal 1. */
\r
364 ( void ) ucQueueType;
\r
366 /* Allocate the new queue structure. */
\r
367 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
368 if( pxNewQueue != NULL )
\r
370 /* Information required for priority inheritance. */
\r
371 pxNewQueue->pxMutexHolder = NULL;
\r
372 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
374 /* Queues used as a mutex no data is actually copied into or out
\r
376 pxNewQueue->pcWriteTo = NULL;
\r
377 pxNewQueue->pcReadFrom = NULL;
\r
379 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
380 an item size of 0 as nothing is actually copied into or out
\r
382 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
383 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
384 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
385 pxNewQueue->xRxLock = queueUNLOCKED;
\r
386 pxNewQueue->xTxLock = queueUNLOCKED;
\r
388 #if ( configUSE_TRACE_FACILITY == 1 )
\r
390 pxNewQueue->ucQueueType = ucQueueType;
\r
394 /* Ensure the event queues start with the correct state. */
\r
395 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
396 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
398 traceCREATE_MUTEX( pxNewQueue );
\r
400 /* Start with the semaphore in the expected state. */
\r
401 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
405 traceCREATE_MUTEX_FAILED();
\r
408 configASSERT( pxNewQueue );
\r
412 #endif /* configUSE_MUTEXES */
\r
413 /*-----------------------------------------------------------*/
\r
415 #if ( configUSE_MUTEXES == 1 )
\r
417 void* xQueueGetMutexHolder( xQueueHandle xSemaphore )
\r
421 /* This function is called by xSemaphoreGetMutexHolder(), and should not
\r
422 be called directly. Note: This is is a good way of determining if the
\r
423 calling task is the mutex holder, but not a good way of determining the
\r
424 identity of the mutex holder, as the holder may change between the
\r
425 following critical section exiting and the function returning. */
\r
426 taskENTER_CRITICAL();
\r
428 if( xSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
\r
430 pxReturn = ( void * ) xSemaphore->pxMutexHolder;
\r
437 taskEXIT_CRITICAL();
\r
443 /*-----------------------------------------------------------*/
\r
445 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
447 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
449 portBASE_TYPE xReturn;
\r
451 configASSERT( pxMutex );
\r
453 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
454 change outside of this task. If this task does not hold the mutex then
\r
455 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
456 this is the only condition we are interested in it does not matter if
\r
457 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
458 mutual exclusion is required to test the pxMutexHolder variable. */
\r
459 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
461 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
463 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
464 the task handle, therefore no underflow check is required. Also,
\r
465 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
466 there can only be one, no mutual exclusion is required to modify the
\r
467 uxRecursiveCallCount member. */
\r
468 ( pxMutex->uxRecursiveCallCount )--;
\r
470 /* Have we unwound the call count? */
\r
471 if( pxMutex->uxRecursiveCallCount == 0 )
\r
473 /* Return the mutex. This will automatically unblock any other
\r
474 task that might be waiting to access the mutex. */
\r
475 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
482 /* We cannot give the mutex because we are not the holder. */
\r
485 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
491 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
492 /*-----------------------------------------------------------*/
\r
494 #if configUSE_RECURSIVE_MUTEXES == 1
\r
496 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
498 portBASE_TYPE xReturn;
\r
500 configASSERT( pxMutex );
\r
502 /* Comments regarding mutual exclusion as per those within
\r
503 xQueueGiveMutexRecursive(). */
\r
505 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
507 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
509 ( pxMutex->uxRecursiveCallCount )++;
\r
514 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
516 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
517 we may have blocked to reach here. */
\r
518 if( xReturn == pdPASS )
\r
520 ( pxMutex->uxRecursiveCallCount )++;
\r
524 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
531 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
532 /*-----------------------------------------------------------*/
\r
534 #if configUSE_COUNTING_SEMAPHORES == 1
\r
536 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
538 xQueueHandle pxHandle;
\r
540 pxHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
542 if( pxHandle != NULL )
\r
544 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
546 traceCREATE_COUNTING_SEMAPHORE();
\r
550 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
553 configASSERT( pxHandle );
\r
557 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
558 /*-----------------------------------------------------------*/
\r
560 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
562 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
563 xTimeOutType xTimeOut;
\r
565 configASSERT( pxQueue );
\r
566 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
568 /* This function relaxes the coding standard somewhat to allow return
\r
569 statements within the function itself. This is done in the interest
\r
570 of execution time efficiency. */
\r
573 taskENTER_CRITICAL();
\r
575 /* Is there room on the queue now? To be running we must be
\r
576 the highest priority task wanting to access the queue. */
\r
577 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
579 traceQUEUE_SEND( pxQueue );
\r
580 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
582 /* If there was a task waiting for data to arrive on the
\r
583 queue then unblock it now. */
\r
584 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
586 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
588 /* The unblocked task has a priority higher than
\r
589 our own so yield immediately. Yes it is ok to do
\r
590 this from within the critical section - the kernel
\r
591 takes care of that. */
\r
592 portYIELD_WITHIN_API();
\r
596 taskEXIT_CRITICAL();
\r
598 /* Return to the original privilege level before exiting the
\r
604 if( xTicksToWait == ( portTickType ) 0 )
\r
606 /* The queue was full and no block time is specified (or
\r
607 the block time has expired) so leave now. */
\r
608 taskEXIT_CRITICAL();
\r
610 /* Return to the original privilege level before exiting
\r
612 traceQUEUE_SEND_FAILED( pxQueue );
\r
613 return errQUEUE_FULL;
\r
615 else if( xEntryTimeSet == pdFALSE )
\r
617 /* The queue was full and a block time was specified so
\r
618 configure the timeout structure. */
\r
619 vTaskSetTimeOutState( &xTimeOut );
\r
620 xEntryTimeSet = pdTRUE;
\r
624 taskEXIT_CRITICAL();
\r
626 /* Interrupts and other tasks can send to and receive from the queue
\r
627 now the critical section has been exited. */
\r
630 prvLockQueue( pxQueue );
\r
632 /* Update the timeout state to see if it has expired yet. */
\r
633 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
635 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
637 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
638 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
640 /* Unlocking the queue means queue events can effect the
\r
641 event list. It is possible that interrupts occurring now
\r
642 remove this task from the event list again - but as the
\r
643 scheduler is suspended the task will go onto the pending
\r
644 ready last instead of the actual ready list. */
\r
645 prvUnlockQueue( pxQueue );
\r
647 /* Resuming the scheduler will move tasks from the pending
\r
648 ready list into the ready list - so it is feasible that this
\r
649 task is already in a ready list before it yields - in which
\r
650 case the yield will not cause a context switch unless there
\r
651 is also a higher priority task in the pending ready list. */
\r
652 if( xTaskResumeAll() == pdFALSE )
\r
654 portYIELD_WITHIN_API();
\r
660 prvUnlockQueue( pxQueue );
\r
661 ( void ) xTaskResumeAll();
\r
666 /* The timeout has expired. */
\r
667 prvUnlockQueue( pxQueue );
\r
668 ( void ) xTaskResumeAll();
\r
670 /* Return to the original privilege level before exiting the
\r
672 traceQUEUE_SEND_FAILED( pxQueue );
\r
673 return errQUEUE_FULL;
\r
677 /*-----------------------------------------------------------*/
\r
679 #if configUSE_ALTERNATIVE_API == 1
\r
681 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
683 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
684 xTimeOutType xTimeOut;
\r
686 configASSERT( pxQueue );
\r
687 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
691 taskENTER_CRITICAL();
\r
693 /* Is there room on the queue now? To be running we must be
\r
694 the highest priority task wanting to access the queue. */
\r
695 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
697 traceQUEUE_SEND( pxQueue );
\r
698 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
700 /* If there was a task waiting for data to arrive on the
\r
701 queue then unblock it now. */
\r
702 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
704 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
706 /* The unblocked task has a priority higher than
\r
707 our own so yield immediately. */
\r
708 portYIELD_WITHIN_API();
\r
712 taskEXIT_CRITICAL();
\r
717 if( xTicksToWait == ( portTickType ) 0 )
\r
719 taskEXIT_CRITICAL();
\r
720 return errQUEUE_FULL;
\r
722 else if( xEntryTimeSet == pdFALSE )
\r
724 vTaskSetTimeOutState( &xTimeOut );
\r
725 xEntryTimeSet = pdTRUE;
\r
729 taskEXIT_CRITICAL();
\r
731 taskENTER_CRITICAL();
\r
733 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
735 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
737 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
738 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
739 portYIELD_WITHIN_API();
\r
744 taskEXIT_CRITICAL();
\r
745 traceQUEUE_SEND_FAILED( pxQueue );
\r
746 return errQUEUE_FULL;
\r
749 taskEXIT_CRITICAL();
\r
753 #endif /* configUSE_ALTERNATIVE_API */
\r
754 /*-----------------------------------------------------------*/
\r
756 #if configUSE_ALTERNATIVE_API == 1
\r
758 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
760 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
761 xTimeOutType xTimeOut;
\r
762 signed char *pcOriginalReadPosition;
\r
764 configASSERT( pxQueue );
\r
765 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
769 taskENTER_CRITICAL();
\r
771 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
773 /* Remember our read position in case we are just peeking. */
\r
774 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
776 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
778 if( xJustPeeking == pdFALSE )
\r
780 traceQUEUE_RECEIVE( pxQueue );
\r
782 /* We are actually removing data. */
\r
783 --( pxQueue->uxMessagesWaiting );
\r
785 #if ( configUSE_MUTEXES == 1 )
\r
787 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
789 /* Record the information required to implement
\r
790 priority inheritance should it become necessary. */
\r
791 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
796 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
798 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
800 portYIELD_WITHIN_API();
\r
806 traceQUEUE_PEEK( pxQueue );
\r
808 /* We are not removing the data, so reset our read
\r
810 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
812 /* The data is being left in the queue, so see if there are
\r
813 any other tasks waiting for the data. */
\r
814 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
816 /* Tasks that are removed from the event list will get added to
\r
817 the pending ready list as the scheduler is still suspended. */
\r
818 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
820 /* The task waiting has a higher priority than this task. */
\r
821 portYIELD_WITHIN_API();
\r
827 taskEXIT_CRITICAL();
\r
832 if( xTicksToWait == ( portTickType ) 0 )
\r
834 taskEXIT_CRITICAL();
\r
835 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
836 return errQUEUE_EMPTY;
\r
838 else if( xEntryTimeSet == pdFALSE )
\r
840 vTaskSetTimeOutState( &xTimeOut );
\r
841 xEntryTimeSet = pdTRUE;
\r
845 taskEXIT_CRITICAL();
\r
847 taskENTER_CRITICAL();
\r
849 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
851 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
853 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
855 #if ( configUSE_MUTEXES == 1 )
\r
857 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
859 portENTER_CRITICAL();
\r
860 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
861 portEXIT_CRITICAL();
\r
866 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
867 portYIELD_WITHIN_API();
\r
872 taskEXIT_CRITICAL();
\r
873 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
874 return errQUEUE_EMPTY;
\r
877 taskEXIT_CRITICAL();
\r
882 #endif /* configUSE_ALTERNATIVE_API */
\r
883 /*-----------------------------------------------------------*/
\r
885 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
887 signed portBASE_TYPE xReturn;
\r
888 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
890 configASSERT( pxQueue );
\r
891 configASSERT( pxHigherPriorityTaskWoken );
\r
892 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
894 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
895 in the queue. Also we don't directly wake a task that was blocked on a
\r
896 queue read, instead we return a flag to say whether a context switch is
\r
897 required or not (i.e. has a task with a higher priority than us been woken
\r
899 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
901 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
903 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
905 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
907 /* If the queue is locked we do not alter the event list. This will
\r
908 be done when the queue is unlocked later. */
\r
909 if( pxQueue->xTxLock == queueUNLOCKED )
\r
911 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
913 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
915 /* The task waiting has a higher priority so record that a
\r
916 context switch is required. */
\r
917 *pxHigherPriorityTaskWoken = pdTRUE;
\r
923 /* Increment the lock count so the task that unlocks the queue
\r
924 knows that data was posted while it was locked. */
\r
925 ++( pxQueue->xTxLock );
\r
932 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
933 xReturn = errQUEUE_FULL;
\r
936 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
940 /*-----------------------------------------------------------*/
\r
942 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
944 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
945 xTimeOutType xTimeOut;
\r
946 signed char *pcOriginalReadPosition;
\r
948 configASSERT( pxQueue );
\r
949 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
951 /* This function relaxes the coding standard somewhat to allow return
\r
952 statements within the function itself. This is done in the interest
\r
953 of execution time efficiency. */
\r
957 taskENTER_CRITICAL();
\r
959 /* Is there data in the queue now? To be running we must be
\r
960 the highest priority task wanting to access the queue. */
\r
961 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
963 /* Remember our read position in case we are just peeking. */
\r
964 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
966 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
968 if( xJustPeeking == pdFALSE )
\r
970 traceQUEUE_RECEIVE( pxQueue );
\r
972 /* We are actually removing data. */
\r
973 --( pxQueue->uxMessagesWaiting );
\r
975 #if ( configUSE_MUTEXES == 1 )
\r
977 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
979 /* Record the information required to implement
\r
980 priority inheritance should it become necessary. */
\r
981 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
986 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
988 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
990 portYIELD_WITHIN_API();
\r
996 traceQUEUE_PEEK( pxQueue );
\r
998 /* We are not removing the data, so reset our read
\r
1000 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
1002 /* The data is being left in the queue, so see if there are
\r
1003 any other tasks waiting for the data. */
\r
1004 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1006 /* Tasks that are removed from the event list will get added to
\r
1007 the pending ready list as the scheduler is still suspended. */
\r
1008 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1010 /* The task waiting has a higher priority than this task. */
\r
1011 portYIELD_WITHIN_API();
\r
1017 taskEXIT_CRITICAL();
\r
1022 if( xTicksToWait == ( portTickType ) 0 )
\r
1024 /* The queue was empty and no block time is specified (or
\r
1025 the block time has expired) so leave now. */
\r
1026 taskEXIT_CRITICAL();
\r
1027 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1028 return errQUEUE_EMPTY;
\r
1030 else if( xEntryTimeSet == pdFALSE )
\r
1032 /* The queue was empty and a block time was specified so
\r
1033 configure the timeout structure. */
\r
1034 vTaskSetTimeOutState( &xTimeOut );
\r
1035 xEntryTimeSet = pdTRUE;
\r
1039 taskEXIT_CRITICAL();
\r
1041 /* Interrupts and other tasks can send to and receive from the queue
\r
1042 now the critical section has been exited. */
\r
1044 vTaskSuspendAll();
\r
1045 prvLockQueue( pxQueue );
\r
1047 /* Update the timeout state to see if it has expired yet. */
\r
1048 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1050 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1052 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1054 #if ( configUSE_MUTEXES == 1 )
\r
1056 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1058 portENTER_CRITICAL();
\r
1060 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
1062 portEXIT_CRITICAL();
\r
1067 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1068 prvUnlockQueue( pxQueue );
\r
1069 if( xTaskResumeAll() == pdFALSE )
\r
1071 portYIELD_WITHIN_API();
\r
1077 prvUnlockQueue( pxQueue );
\r
1078 ( void ) xTaskResumeAll();
\r
1083 prvUnlockQueue( pxQueue );
\r
1084 ( void ) xTaskResumeAll();
\r
1085 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1086 return errQUEUE_EMPTY;
\r
1090 /*-----------------------------------------------------------*/
\r
1092 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1094 signed portBASE_TYPE xReturn;
\r
1095 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1097 configASSERT( pxQueue );
\r
1098 configASSERT( pxTaskWoken );
\r
1099 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1101 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1103 /* We cannot block from an ISR, so check there is data available. */
\r
1104 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1106 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1108 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1109 --( pxQueue->uxMessagesWaiting );
\r
1111 /* If the queue is locked we will not modify the event list. Instead
\r
1112 we update the lock count so the task that unlocks the queue will know
\r
1113 that an ISR has removed data while the queue was locked. */
\r
1114 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1116 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1118 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1120 /* The task waiting has a higher priority than us so
\r
1121 force a context switch. */
\r
1122 *pxTaskWoken = pdTRUE;
\r
1128 /* Increment the lock count so the task that unlocks the queue
\r
1129 knows that data was removed while it was locked. */
\r
1130 ++( pxQueue->xRxLock );
\r
1138 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1141 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1145 /*-----------------------------------------------------------*/
\r
1147 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1149 unsigned portBASE_TYPE uxReturn;
\r
1151 configASSERT( pxQueue );
\r
1153 taskENTER_CRITICAL();
\r
1154 uxReturn = pxQueue->uxMessagesWaiting;
\r
1155 taskEXIT_CRITICAL();
\r
1159 /*-----------------------------------------------------------*/
\r
1161 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1163 unsigned portBASE_TYPE uxReturn;
\r
1165 configASSERT( pxQueue );
\r
1167 uxReturn = pxQueue->uxMessagesWaiting;
\r
1171 /*-----------------------------------------------------------*/
\r
1173 void vQueueDelete( xQueueHandle pxQueue )
\r
1175 configASSERT( pxQueue );
\r
1177 traceQUEUE_DELETE( pxQueue );
\r
1178 vQueueUnregisterQueue( pxQueue );
\r
1179 vPortFree( pxQueue->pcHead );
\r
1180 vPortFree( pxQueue );
\r
1182 /*-----------------------------------------------------------*/
\r
1184 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1186 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )
\r
1188 return pxQueue->ucQueueNumber;
\r
1192 /*-----------------------------------------------------------*/
\r
1194 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1196 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber )
\r
1198 pxQueue->ucQueueNumber = ucQueueNumber;
\r
1202 /*-----------------------------------------------------------*/
\r
1204 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1206 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )
\r
1208 return pxQueue->ucQueueType;
\r
1212 /*-----------------------------------------------------------*/
\r
1214 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1216 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1218 #if ( configUSE_MUTEXES == 1 )
\r
1220 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1222 /* The mutex is no longer being held. */
\r
1223 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1224 pxQueue->pxMutexHolder = NULL;
\r
1229 else if( xPosition == queueSEND_TO_BACK )
\r
1231 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1232 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1233 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1235 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1240 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1241 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1242 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1244 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1248 ++( pxQueue->uxMessagesWaiting );
\r
1250 /*-----------------------------------------------------------*/
\r
1252 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1254 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1256 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1257 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1259 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1261 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1264 /*-----------------------------------------------------------*/
\r
1266 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1268 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1270 /* The lock counts contains the number of extra data items placed or
\r
1271 removed from the queue while the queue was locked. When a queue is
\r
1272 locked items can be added or removed, but the event lists cannot be
\r
1274 taskENTER_CRITICAL();
\r
1276 /* See if data was added to the queue while it was locked. */
\r
1277 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1279 /* Data was posted while the queue was locked. Are any tasks
\r
1280 blocked waiting for data to become available? */
\r
1281 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1283 /* Tasks that are removed from the event list will get added to
\r
1284 the pending ready list as the scheduler is still suspended. */
\r
1285 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1287 /* The task waiting has a higher priority so record that a
\r
1288 context switch is required. */
\r
1289 vTaskMissedYield();
\r
1292 --( pxQueue->xTxLock );
\r
1300 pxQueue->xTxLock = queueUNLOCKED;
\r
1302 taskEXIT_CRITICAL();
\r
1304 /* Do the same for the Rx lock. */
\r
1305 taskENTER_CRITICAL();
\r
1307 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1309 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1311 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1313 vTaskMissedYield();
\r
1316 --( pxQueue->xRxLock );
\r
1324 pxQueue->xRxLock = queueUNLOCKED;
\r
1326 taskEXIT_CRITICAL();
\r
1328 /*-----------------------------------------------------------*/
\r
1330 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1332 signed portBASE_TYPE xReturn;
\r
1334 taskENTER_CRITICAL();
\r
1335 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1336 taskEXIT_CRITICAL();
\r
1340 /*-----------------------------------------------------------*/
\r
1342 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1344 signed portBASE_TYPE xReturn;
\r
1346 configASSERT( pxQueue );
\r
1347 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1351 /*-----------------------------------------------------------*/
\r
1353 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1355 signed portBASE_TYPE xReturn;
\r
1357 taskENTER_CRITICAL();
\r
1358 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1359 taskEXIT_CRITICAL();
\r
1363 /*-----------------------------------------------------------*/
\r
1365 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1367 signed portBASE_TYPE xReturn;
\r
1369 configASSERT( pxQueue );
\r
1370 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1374 /*-----------------------------------------------------------*/
\r
1376 #if configUSE_CO_ROUTINES == 1
\r
1377 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1379 signed portBASE_TYPE xReturn;
\r
1381 /* If the queue is already full we may have to block. A critical section
\r
1382 is required to prevent an interrupt removing something from the queue
\r
1383 between the check to see if the queue is full and blocking on the queue. */
\r
1384 portDISABLE_INTERRUPTS();
\r
1386 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1388 /* The queue is full - do we want to block or just leave without
\r
1390 if( xTicksToWait > ( portTickType ) 0 )
\r
1392 /* As this is called from a coroutine we cannot block directly, but
\r
1393 return indicating that we need to block. */
\r
1394 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1395 portENABLE_INTERRUPTS();
\r
1396 return errQUEUE_BLOCKED;
\r
1400 portENABLE_INTERRUPTS();
\r
1401 return errQUEUE_FULL;
\r
1405 portENABLE_INTERRUPTS();
\r
1409 portDISABLE_INTERRUPTS();
\r
1411 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1413 /* There is room in the queue, copy the data into the queue. */
\r
1414 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1417 /* Were any co-routines waiting for data to become available? */
\r
1418 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1420 /* In this instance the co-routine could be placed directly
\r
1421 into the ready list as we are within a critical section.
\r
1422 Instead the same pending ready list mechanism is used as if
\r
1423 the event were caused from within an interrupt. */
\r
1424 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1426 /* The co-routine waiting has a higher priority so record
\r
1427 that a yield might be appropriate. */
\r
1428 xReturn = errQUEUE_YIELD;
\r
1434 xReturn = errQUEUE_FULL;
\r
1437 portENABLE_INTERRUPTS();
\r
1442 /*-----------------------------------------------------------*/
\r
1444 #if configUSE_CO_ROUTINES == 1
\r
1445 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1447 signed portBASE_TYPE xReturn;
\r
1449 /* If the queue is already empty we may have to block. A critical section
\r
1450 is required to prevent an interrupt adding something to the queue
\r
1451 between the check to see if the queue is empty and blocking on the queue. */
\r
1452 portDISABLE_INTERRUPTS();
\r
1454 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1456 /* There are no messages in the queue, do we want to block or just
\r
1457 leave with nothing? */
\r
1458 if( xTicksToWait > ( portTickType ) 0 )
\r
1460 /* As this is a co-routine we cannot block directly, but return
\r
1461 indicating that we need to block. */
\r
1462 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1463 portENABLE_INTERRUPTS();
\r
1464 return errQUEUE_BLOCKED;
\r
1468 portENABLE_INTERRUPTS();
\r
1469 return errQUEUE_FULL;
\r
1473 portENABLE_INTERRUPTS();
\r
1477 portDISABLE_INTERRUPTS();
\r
1479 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1481 /* Data is available from the queue. */
\r
1482 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1483 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1485 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1487 --( pxQueue->uxMessagesWaiting );
\r
1488 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1492 /* Were any co-routines waiting for space to become available? */
\r
1493 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1495 /* In this instance the co-routine could be placed directly
\r
1496 into the ready list as we are within a critical section.
\r
1497 Instead the same pending ready list mechanism is used as if
\r
1498 the event were caused from within an interrupt. */
\r
1499 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1501 xReturn = errQUEUE_YIELD;
\r
1510 portENABLE_INTERRUPTS();
\r
1515 /*-----------------------------------------------------------*/
\r
1519 #if configUSE_CO_ROUTINES == 1
\r
1520 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1522 /* Cannot block within an ISR so if there is no space on the queue then
\r
1523 exit without doing anything. */
\r
1524 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1526 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1528 /* We only want to wake one co-routine per ISR, so check that a
\r
1529 co-routine has not already been woken. */
\r
1530 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1532 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1534 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1542 return xCoRoutinePreviouslyWoken;
\r
1545 /*-----------------------------------------------------------*/
\r
1547 #if configUSE_CO_ROUTINES == 1
\r
1548 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1550 signed portBASE_TYPE xReturn;
\r
1552 /* We cannot block from an ISR, so check there is data available. If
\r
1553 not then just leave without doing anything. */
\r
1554 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1556 /* Copy the data from the queue. */
\r
1557 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1558 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1560 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1562 --( pxQueue->uxMessagesWaiting );
\r
1563 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1565 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1567 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1569 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1571 *pxCoRoutineWoken = pdTRUE;
\r
1586 /*-----------------------------------------------------------*/
\r
1588 #if configQUEUE_REGISTRY_SIZE > 0
\r
1590 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1592 unsigned portBASE_TYPE ux;
\r
1594 /* See if there is an empty space in the registry. A NULL name denotes
\r
1596 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1598 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1600 /* Store the information on this queue. */
\r
1601 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1602 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1609 /*-----------------------------------------------------------*/
\r
1611 #if configQUEUE_REGISTRY_SIZE > 0
\r
1613 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1615 unsigned portBASE_TYPE ux;
\r
1617 /* See if the handle of the queue being unregistered in actually in the
\r
1619 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1621 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1623 /* Set the name to NULL to show that this slot if free again. */
\r
1624 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1632 /*-----------------------------------------------------------*/
\r
1634 #if configUSE_TIMERS == 1
\r
1636 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1638 /* This function should not be called by application code hence the
\r
1639 'Restricted' in its name. It is not part of the public API. It is
\r
1640 designed for use by kernel code, and has special calling requirements.
\r
1641 It can result in vListInsert() being called on a list that can only
\r
1642 possibly ever have one item in it, so the list will be fast, but even
\r
1643 so it should be called with the scheduler locked and not from a critical
\r
1646 /* Only do anything if there are no messages in the queue. This function
\r
1647 will not actually cause the task to block, just place it on a blocked
\r
1648 list. It will not block until the scheduler is unlocked - at which
\r
1649 time a yield will be performed. If an item is added to the queue while
\r
1650 the queue is locked, and the calling task blocks on the queue, then the
\r
1651 calling task will be immediately unblocked when the queue is unlocked. */
\r
1652 prvLockQueue( pxQueue );
\r
1653 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1655 /* There is nothing in the queue, block for the specified period. */
\r
1656 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1658 prvUnlockQueue( pxQueue );
\r