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
80 #if ( configUSE_CO_ROUTINES == 1 )
\r
81 #include "croutine.h"
\r
84 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
86 /*-----------------------------------------------------------
\r
87 * PUBLIC LIST API documented in list.h
\r
88 *----------------------------------------------------------*/
\r
90 /* Constants used with the cRxLock and xTxLock structure members. */
\r
91 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
92 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
94 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
96 /* For internal use only. */
\r
97 #define queueSEND_TO_BACK ( 0 )
\r
98 #define queueSEND_TO_FRONT ( 1 )
\r
100 /* Effectively make a union out of the xQUEUE structure. */
\r
101 #define pxMutexHolder pcTail
\r
102 #define uxQueueType pcHead
\r
103 #define uxRecursiveCallCount pcReadFrom
\r
104 #define queueQUEUE_IS_MUTEX NULL
\r
106 /* Semaphores do not actually store or copy data, so have an items size of
\r
108 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )
\r
109 #define queueDONT_BLOCK ( ( portTickType ) 0U )
\r
110 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0U )
\r
112 /* These definitions *must* match those in queue.h. */
\r
113 #define queueQUEUE_TYPE_BASE ( 0U )
\r
114 #define queueQUEUE_TYPE_MUTEX ( 1U )
\r
115 #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( 2U )
\r
116 #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( 3U )
\r
117 #define queueQUEUE_TYPE_RECURSIVE_MUTEX ( 4U )
\r
120 * Definition of the queue used by the scheduler.
\r
121 * Items are queued by copy, not reference.
\r
123 typedef struct QueueDefinition
\r
125 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
126 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
128 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
129 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
131 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
132 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
134 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
135 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
136 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
138 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
139 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
141 #if ( configUSE_TRACE_FACILITY == 1 )
\r
142 unsigned char ucQueueNumber;
\r
143 unsigned char ucQueueType;
\r
147 /*-----------------------------------------------------------*/
\r
150 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
151 * To keep the definition private the API header file defines it as a
\r
154 typedef xQUEUE * xQueueHandle;
\r
157 * Prototypes for public functions are included here so we don't have to
\r
158 * include the API header file (as it defines xQueueHandle differently). These
\r
159 * functions are documented in the API header file.
\r
161 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
162 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
163 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
164 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
165 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
166 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
167 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
168 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
169 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
170 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
171 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
172 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
173 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
174 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
175 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
176 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
177 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
178 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
179 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber ) PRIVILEGED_FUNCTION;
\r
180 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
181 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue ) PRIVILEGED_FUNCTION;
\r
182 xTaskHandle xQueueGetMutexHolder( xQueueHandle xSemaphore ) PRIVILEGED_FUNCTION;
\r
185 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
186 * an optional component.
\r
188 #if configUSE_CO_ROUTINES == 1
\r
189 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
190 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
191 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
192 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
196 * The queue registry is just a means for kernel aware debuggers to locate
\r
197 * queue structures. It has no other purpose so is an optional component.
\r
199 #if configQUEUE_REGISTRY_SIZE > 0
\r
201 /* The type stored within the queue registry array. This allows a name
\r
202 to be assigned to each queue making kernel aware debugging a little
\r
203 more user friendly. */
\r
204 typedef struct QUEUE_REGISTRY_ITEM
\r
206 signed char *pcQueueName;
\r
207 xQueueHandle xHandle;
\r
208 } xQueueRegistryItem;
\r
210 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
211 The pcQueueName member of a structure being NULL is indicative of the
\r
212 array position being vacant. */
\r
213 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
215 /* Removes a queue from the registry by simply setting the pcQueueName
\r
217 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
218 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
222 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
223 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
224 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
225 * queue is locked it will instead increment the appropriate queue lock count
\r
226 * to indicate that a task may require unblocking. When the queue in unlocked
\r
227 * these lock counts are inspected, and the appropriate action taken.
\r
229 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
232 * Uses a critical section to determine if there is any data in a queue.
\r
234 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
236 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
239 * Uses a critical section to determine if there is any space in a queue.
\r
241 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
243 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
246 * Copies an item into the queue, either at the front of the queue or the
\r
247 * back of the queue.
\r
249 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
252 * Copies an item out of a queue.
\r
254 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
255 /*-----------------------------------------------------------*/
\r
258 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
259 * accessing the queue event lists.
\r
261 #define prvLockQueue( pxQueue ) \
\r
262 taskENTER_CRITICAL(); \
\r
264 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
266 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
268 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
270 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
273 taskEXIT_CRITICAL()
\r
274 /*-----------------------------------------------------------*/
\r
277 /*-----------------------------------------------------------
\r
278 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
279 *----------------------------------------------------------*/
\r
281 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue )
\r
283 configASSERT( pxQueue );
\r
285 taskENTER_CRITICAL();
\r
287 pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
\r
288 pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
289 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
290 pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );
\r
291 pxQueue->xRxLock = queueUNLOCKED;
\r
292 pxQueue->xTxLock = queueUNLOCKED;
\r
294 if( xNewQueue == pdFALSE )
\r
296 /* If there are tasks blocked waiting to read from the queue, then
\r
297 the tasks will remain blocked as after this function exits the queue
\r
298 will still be empty. If there are tasks blocked waiting to write to
\r
299 the queue, then one should be unblocked as after this function exits
\r
300 it will be possible to write to it. */
\r
301 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
303 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
305 portYIELD_WITHIN_API();
\r
311 /* Ensure the event queues start in the correct state. */
\r
312 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
\r
313 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
\r
316 taskEXIT_CRITICAL();
\r
318 /* A value is returned for calling semantic consistency with previous
\r
322 /*-----------------------------------------------------------*/
\r
324 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )
\r
326 xQUEUE *pxNewQueue;
\r
327 size_t xQueueSizeInBytes;
\r
328 xQueueHandle xReturn = NULL;
\r
330 /* Remove compiler warnings about unused parameters should
\r
331 configUSE_TRACE_FACILITY not be set to 1. */
\r
332 ( void ) ucQueueType;
\r
334 /* Allocate the new queue structure. */
\r
335 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
337 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
338 if( pxNewQueue != NULL )
\r
340 /* Create the list of pointers to queue items. The queue is one byte
\r
341 longer than asked for to make wrap checking easier/faster. */
\r
342 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
344 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
345 if( pxNewQueue->pcHead != NULL )
\r
347 /* Initialise the queue members as described above where the
\r
348 queue type is defined. */
\r
349 pxNewQueue->uxLength = uxQueueLength;
\r
350 pxNewQueue->uxItemSize = uxItemSize;
\r
351 xQueueGenericReset( pxNewQueue, pdTRUE );
\r
352 #if ( configUSE_TRACE_FACILITY == 1 )
\r
354 pxNewQueue->ucQueueType = ucQueueType;
\r
356 #endif /* configUSE_TRACE_FACILITY */
\r
358 traceQUEUE_CREATE( pxNewQueue );
\r
359 xReturn = pxNewQueue;
\r
363 traceQUEUE_CREATE_FAILED( ucQueueType );
\r
364 vPortFree( pxNewQueue );
\r
369 configASSERT( xReturn );
\r
373 /*-----------------------------------------------------------*/
\r
375 #if ( configUSE_MUTEXES == 1 )
\r
377 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )
\r
379 xQUEUE *pxNewQueue;
\r
381 /* Prevent compiler warnings about unused parameters if
\r
382 configUSE_TRACE_FACILITY does not equal 1. */
\r
383 ( void ) ucQueueType;
\r
385 /* Allocate the new queue structure. */
\r
386 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
387 if( pxNewQueue != NULL )
\r
389 /* Information required for priority inheritance. */
\r
390 pxNewQueue->pxMutexHolder = NULL;
\r
391 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
393 /* Queues used as a mutex no data is actually copied into or out
\r
395 pxNewQueue->pcWriteTo = NULL;
\r
396 pxNewQueue->pcReadFrom = NULL;
\r
398 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
399 an item size of 0 as nothing is actually copied into or out
\r
401 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
402 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
403 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
404 pxNewQueue->xRxLock = queueUNLOCKED;
\r
405 pxNewQueue->xTxLock = queueUNLOCKED;
\r
407 #if ( configUSE_TRACE_FACILITY == 1 )
\r
409 pxNewQueue->ucQueueType = ucQueueType;
\r
413 /* Ensure the event queues start with the correct state. */
\r
414 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
415 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
417 traceCREATE_MUTEX( pxNewQueue );
\r
419 /* Start with the semaphore in the expected state. */
\r
420 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
424 traceCREATE_MUTEX_FAILED();
\r
427 configASSERT( pxNewQueue );
\r
431 #endif /* configUSE_MUTEXES */
\r
432 /*-----------------------------------------------------------*/
\r
434 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xQueueGetMutexHolder == 1 ) )
\r
436 void* xQueueGetMutexHolder( xQueueHandle xSemaphore )
\r
440 /* This function is called by xSemaphoreGetMutexHolder(), and should not
\r
441 be called directly. Note: This is is a good way of determining if the
\r
442 calling task is the mutex holder, but not a good way of determining the
\r
443 identity of the mutex holder, as the holder may change between the
\r
444 following critical section exiting and the function returning. */
\r
445 taskENTER_CRITICAL();
\r
447 if( xSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
\r
449 pxReturn = ( void * ) xSemaphore->pxMutexHolder;
\r
456 taskEXIT_CRITICAL();
\r
462 /*-----------------------------------------------------------*/
\r
464 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
466 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
468 portBASE_TYPE xReturn;
\r
470 configASSERT( pxMutex );
\r
472 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
473 change outside of this task. If this task does not hold the mutex then
\r
474 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
475 this is the only condition we are interested in it does not matter if
\r
476 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
477 mutual exclusion is required to test the pxMutexHolder variable. */
\r
478 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
480 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
482 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
483 the task handle, therefore no underflow check is required. Also,
\r
484 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
485 there can only be one, no mutual exclusion is required to modify the
\r
486 uxRecursiveCallCount member. */
\r
487 ( pxMutex->uxRecursiveCallCount )--;
\r
489 /* Have we unwound the call count? */
\r
490 if( pxMutex->uxRecursiveCallCount == 0 )
\r
492 /* Return the mutex. This will automatically unblock any other
\r
493 task that might be waiting to access the mutex. */
\r
494 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
501 /* We cannot give the mutex because we are not the holder. */
\r
504 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
510 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
511 /*-----------------------------------------------------------*/
\r
513 #if configUSE_RECURSIVE_MUTEXES == 1
\r
515 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
517 portBASE_TYPE xReturn;
\r
519 configASSERT( pxMutex );
\r
521 /* Comments regarding mutual exclusion as per those within
\r
522 xQueueGiveMutexRecursive(). */
\r
524 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
526 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
528 ( pxMutex->uxRecursiveCallCount )++;
\r
533 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
535 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
536 we may have blocked to reach here. */
\r
537 if( xReturn == pdPASS )
\r
539 ( pxMutex->uxRecursiveCallCount )++;
\r
543 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
550 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
551 /*-----------------------------------------------------------*/
\r
553 #if configUSE_COUNTING_SEMAPHORES == 1
\r
555 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
557 xQueueHandle pxHandle;
\r
559 pxHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
561 if( pxHandle != NULL )
\r
563 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
565 traceCREATE_COUNTING_SEMAPHORE();
\r
569 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
572 configASSERT( pxHandle );
\r
576 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
577 /*-----------------------------------------------------------*/
\r
579 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
581 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
582 xTimeOutType xTimeOut;
\r
584 configASSERT( pxQueue );
\r
585 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
587 /* This function relaxes the coding standard somewhat to allow return
\r
588 statements within the function itself. This is done in the interest
\r
589 of execution time efficiency. */
\r
592 taskENTER_CRITICAL();
\r
594 /* Is there room on the queue now? To be running we must be
\r
595 the highest priority task wanting to access the queue. */
\r
596 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
598 traceQUEUE_SEND( pxQueue );
\r
599 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
601 /* If there was a task waiting for data to arrive on the
\r
602 queue then unblock it now. */
\r
603 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
605 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
607 /* The unblocked task has a priority higher than
\r
608 our own so yield immediately. Yes it is ok to do
\r
609 this from within the critical section - the kernel
\r
610 takes care of that. */
\r
611 portYIELD_WITHIN_API();
\r
615 taskEXIT_CRITICAL();
\r
617 /* Return to the original privilege level before exiting the
\r
623 if( xTicksToWait == ( portTickType ) 0 )
\r
625 /* The queue was full and no block time is specified (or
\r
626 the block time has expired) so leave now. */
\r
627 taskEXIT_CRITICAL();
\r
629 /* Return to the original privilege level before exiting
\r
631 traceQUEUE_SEND_FAILED( pxQueue );
\r
632 return errQUEUE_FULL;
\r
634 else if( xEntryTimeSet == pdFALSE )
\r
636 /* The queue was full and a block time was specified so
\r
637 configure the timeout structure. */
\r
638 vTaskSetTimeOutState( &xTimeOut );
\r
639 xEntryTimeSet = pdTRUE;
\r
643 taskEXIT_CRITICAL();
\r
645 /* Interrupts and other tasks can send to and receive from the queue
\r
646 now the critical section has been exited. */
\r
649 prvLockQueue( pxQueue );
\r
651 /* Update the timeout state to see if it has expired yet. */
\r
652 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
654 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
656 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
657 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
659 /* Unlocking the queue means queue events can effect the
\r
660 event list. It is possible that interrupts occurring now
\r
661 remove this task from the event list again - but as the
\r
662 scheduler is suspended the task will go onto the pending
\r
663 ready last instead of the actual ready list. */
\r
664 prvUnlockQueue( pxQueue );
\r
666 /* Resuming the scheduler will move tasks from the pending
\r
667 ready list into the ready list - so it is feasible that this
\r
668 task is already in a ready list before it yields - in which
\r
669 case the yield will not cause a context switch unless there
\r
670 is also a higher priority task in the pending ready list. */
\r
671 if( xTaskResumeAll() == pdFALSE )
\r
673 portYIELD_WITHIN_API();
\r
679 prvUnlockQueue( pxQueue );
\r
680 ( void ) xTaskResumeAll();
\r
685 /* The timeout has expired. */
\r
686 prvUnlockQueue( pxQueue );
\r
687 ( void ) xTaskResumeAll();
\r
689 /* Return to the original privilege level before exiting the
\r
691 traceQUEUE_SEND_FAILED( pxQueue );
\r
692 return errQUEUE_FULL;
\r
696 /*-----------------------------------------------------------*/
\r
698 #if configUSE_ALTERNATIVE_API == 1
\r
700 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
702 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
703 xTimeOutType xTimeOut;
\r
705 configASSERT( pxQueue );
\r
706 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
710 taskENTER_CRITICAL();
\r
712 /* Is there room on the queue now? To be running we must be
\r
713 the highest priority task wanting to access the queue. */
\r
714 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
716 traceQUEUE_SEND( pxQueue );
\r
717 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
719 /* If there was a task waiting for data to arrive on the
\r
720 queue then unblock it now. */
\r
721 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
723 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
725 /* The unblocked task has a priority higher than
\r
726 our own so yield immediately. */
\r
727 portYIELD_WITHIN_API();
\r
731 taskEXIT_CRITICAL();
\r
736 if( xTicksToWait == ( portTickType ) 0 )
\r
738 taskEXIT_CRITICAL();
\r
739 return errQUEUE_FULL;
\r
741 else if( xEntryTimeSet == pdFALSE )
\r
743 vTaskSetTimeOutState( &xTimeOut );
\r
744 xEntryTimeSet = pdTRUE;
\r
748 taskEXIT_CRITICAL();
\r
750 taskENTER_CRITICAL();
\r
752 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
754 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
756 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
757 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
758 portYIELD_WITHIN_API();
\r
763 taskEXIT_CRITICAL();
\r
764 traceQUEUE_SEND_FAILED( pxQueue );
\r
765 return errQUEUE_FULL;
\r
768 taskEXIT_CRITICAL();
\r
772 #endif /* configUSE_ALTERNATIVE_API */
\r
773 /*-----------------------------------------------------------*/
\r
775 #if configUSE_ALTERNATIVE_API == 1
\r
777 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
779 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
780 xTimeOutType xTimeOut;
\r
781 signed char *pcOriginalReadPosition;
\r
783 configASSERT( pxQueue );
\r
784 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
788 taskENTER_CRITICAL();
\r
790 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
792 /* Remember our read position in case we are just peeking. */
\r
793 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
795 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
797 if( xJustPeeking == pdFALSE )
\r
799 traceQUEUE_RECEIVE( pxQueue );
\r
801 /* We are actually removing data. */
\r
802 --( pxQueue->uxMessagesWaiting );
\r
804 #if ( configUSE_MUTEXES == 1 )
\r
806 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
808 /* Record the information required to implement
\r
809 priority inheritance should it become necessary. */
\r
810 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
815 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
817 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
819 portYIELD_WITHIN_API();
\r
825 traceQUEUE_PEEK( pxQueue );
\r
827 /* We are not removing the data, so reset our read
\r
829 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
831 /* The data is being left in the queue, so see if there are
\r
832 any other tasks waiting for the data. */
\r
833 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
835 /* Tasks that are removed from the event list will get added to
\r
836 the pending ready list as the scheduler is still suspended. */
\r
837 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
839 /* The task waiting has a higher priority than this task. */
\r
840 portYIELD_WITHIN_API();
\r
846 taskEXIT_CRITICAL();
\r
851 if( xTicksToWait == ( portTickType ) 0 )
\r
853 taskEXIT_CRITICAL();
\r
854 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
855 return errQUEUE_EMPTY;
\r
857 else if( xEntryTimeSet == pdFALSE )
\r
859 vTaskSetTimeOutState( &xTimeOut );
\r
860 xEntryTimeSet = pdTRUE;
\r
864 taskEXIT_CRITICAL();
\r
866 taskENTER_CRITICAL();
\r
868 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
870 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
872 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
874 #if ( configUSE_MUTEXES == 1 )
\r
876 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
878 portENTER_CRITICAL();
\r
880 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
882 portEXIT_CRITICAL();
\r
887 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
888 portYIELD_WITHIN_API();
\r
893 taskEXIT_CRITICAL();
\r
894 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
895 return errQUEUE_EMPTY;
\r
898 taskEXIT_CRITICAL();
\r
903 #endif /* configUSE_ALTERNATIVE_API */
\r
904 /*-----------------------------------------------------------*/
\r
906 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
908 signed portBASE_TYPE xReturn;
\r
909 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
911 configASSERT( pxQueue );
\r
912 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
914 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
915 in the queue. Also we don't directly wake a task that was blocked on a
\r
916 queue read, instead we return a flag to say whether a context switch is
\r
917 required or not (i.e. has a task with a higher priority than us been woken
\r
919 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
921 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
923 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
925 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
927 /* If the queue is locked we do not alter the event list. This will
\r
928 be done when the queue is unlocked later. */
\r
929 if( pxQueue->xTxLock == queueUNLOCKED )
\r
931 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
933 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
935 /* The task waiting has a higher priority so record that a
\r
936 context switch is required. */
\r
937 if( pxHigherPriorityTaskWoken != NULL )
\r
939 *pxHigherPriorityTaskWoken = pdTRUE;
\r
946 /* Increment the lock count so the task that unlocks the queue
\r
947 knows that data was posted while it was locked. */
\r
948 ++( pxQueue->xTxLock );
\r
955 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
956 xReturn = errQUEUE_FULL;
\r
959 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
963 /*-----------------------------------------------------------*/
\r
965 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
967 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
968 xTimeOutType xTimeOut;
\r
969 signed char *pcOriginalReadPosition;
\r
971 configASSERT( pxQueue );
\r
972 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
974 /* This function relaxes the coding standard somewhat to allow return
\r
975 statements within the function itself. This is done in the interest
\r
976 of execution time efficiency. */
\r
980 taskENTER_CRITICAL();
\r
982 /* Is there data in the queue now? To be running we must be
\r
983 the highest priority task wanting to access the queue. */
\r
984 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
986 /* Remember our read position in case we are just peeking. */
\r
987 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
989 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
991 if( xJustPeeking == pdFALSE )
\r
993 traceQUEUE_RECEIVE( pxQueue );
\r
995 /* We are actually removing data. */
\r
996 --( pxQueue->uxMessagesWaiting );
\r
998 #if ( configUSE_MUTEXES == 1 )
\r
1000 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1002 /* Record the information required to implement
\r
1003 priority inheritance should it become necessary. */
\r
1004 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
1009 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1011 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
1013 portYIELD_WITHIN_API();
\r
1019 traceQUEUE_PEEK( pxQueue );
\r
1021 /* We are not removing the data, so reset our read
\r
1023 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
1025 /* The data is being left in the queue, so see if there are
\r
1026 any other tasks waiting for the data. */
\r
1027 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1029 /* Tasks that are removed from the event list will get added to
\r
1030 the pending ready list as the scheduler is still suspended. */
\r
1031 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1033 /* The task waiting has a higher priority than this task. */
\r
1034 portYIELD_WITHIN_API();
\r
1039 taskEXIT_CRITICAL();
\r
1044 if( xTicksToWait == ( portTickType ) 0 )
\r
1046 /* The queue was empty and no block time is specified (or
\r
1047 the block time has expired) so leave now. */
\r
1048 taskEXIT_CRITICAL();
\r
1049 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1050 return errQUEUE_EMPTY;
\r
1052 else if( xEntryTimeSet == pdFALSE )
\r
1054 /* The queue was empty and a block time was specified so
\r
1055 configure the timeout structure. */
\r
1056 vTaskSetTimeOutState( &xTimeOut );
\r
1057 xEntryTimeSet = pdTRUE;
\r
1061 taskEXIT_CRITICAL();
\r
1063 /* Interrupts and other tasks can send to and receive from the queue
\r
1064 now the critical section has been exited. */
\r
1066 vTaskSuspendAll();
\r
1067 prvLockQueue( pxQueue );
\r
1069 /* Update the timeout state to see if it has expired yet. */
\r
1070 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1072 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1074 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1076 #if ( configUSE_MUTEXES == 1 )
\r
1078 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1080 portENTER_CRITICAL();
\r
1082 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
1084 portEXIT_CRITICAL();
\r
1089 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1090 prvUnlockQueue( pxQueue );
\r
1091 if( xTaskResumeAll() == pdFALSE )
\r
1093 portYIELD_WITHIN_API();
\r
1099 prvUnlockQueue( pxQueue );
\r
1100 ( void ) xTaskResumeAll();
\r
1105 prvUnlockQueue( pxQueue );
\r
1106 ( void ) xTaskResumeAll();
\r
1107 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1108 return errQUEUE_EMPTY;
\r
1112 /*-----------------------------------------------------------*/
\r
1114 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken )
\r
1116 signed portBASE_TYPE xReturn;
\r
1117 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1119 configASSERT( pxQueue );
\r
1120 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1122 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1124 /* We cannot block from an ISR, so check there is data available. */
\r
1125 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1127 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1129 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1130 --( pxQueue->uxMessagesWaiting );
\r
1132 /* If the queue is locked we will not modify the event list. Instead
\r
1133 we update the lock count so the task that unlocks the queue will know
\r
1134 that an ISR has removed data while the queue was locked. */
\r
1135 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1137 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1139 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1141 /* The task waiting has a higher priority than us so
\r
1142 force a context switch. */
\r
1143 if( pxHigherPriorityTaskWoken != NULL )
\r
1145 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1152 /* Increment the lock count so the task that unlocks the queue
\r
1153 knows that data was removed while it was locked. */
\r
1154 ++( pxQueue->xRxLock );
\r
1162 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1165 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1169 /*-----------------------------------------------------------*/
\r
1171 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1173 unsigned portBASE_TYPE uxReturn;
\r
1175 configASSERT( pxQueue );
\r
1177 taskENTER_CRITICAL();
\r
1178 uxReturn = pxQueue->uxMessagesWaiting;
\r
1179 taskEXIT_CRITICAL();
\r
1183 /*-----------------------------------------------------------*/
\r
1185 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1187 unsigned portBASE_TYPE uxReturn;
\r
1189 configASSERT( pxQueue );
\r
1191 uxReturn = pxQueue->uxMessagesWaiting;
\r
1195 /*-----------------------------------------------------------*/
\r
1197 void vQueueDelete( xQueueHandle pxQueue )
\r
1199 configASSERT( pxQueue );
\r
1201 traceQUEUE_DELETE( pxQueue );
\r
1202 vQueueUnregisterQueue( pxQueue );
\r
1203 vPortFree( pxQueue->pcHead );
\r
1204 vPortFree( pxQueue );
\r
1206 /*-----------------------------------------------------------*/
\r
1208 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1210 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )
\r
1212 return pxQueue->ucQueueNumber;
\r
1216 /*-----------------------------------------------------------*/
\r
1218 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1220 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber )
\r
1222 pxQueue->ucQueueNumber = ucQueueNumber;
\r
1226 /*-----------------------------------------------------------*/
\r
1228 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1230 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )
\r
1232 return pxQueue->ucQueueType;
\r
1236 /*-----------------------------------------------------------*/
\r
1238 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1240 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1242 #if ( configUSE_MUTEXES == 1 )
\r
1244 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1246 /* The mutex is no longer being held. */
\r
1247 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1248 pxQueue->pxMutexHolder = NULL;
\r
1253 else if( xPosition == queueSEND_TO_BACK )
\r
1255 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1256 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1257 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1259 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1264 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1265 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1266 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1268 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1272 ++( pxQueue->uxMessagesWaiting );
\r
1274 /*-----------------------------------------------------------*/
\r
1276 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1278 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1280 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1281 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1283 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1285 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1288 /*-----------------------------------------------------------*/
\r
1290 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1292 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1294 /* The lock counts contains the number of extra data items placed or
\r
1295 removed from the queue while the queue was locked. When a queue is
\r
1296 locked items can be added or removed, but the event lists cannot be
\r
1298 taskENTER_CRITICAL();
\r
1300 /* See if data was added to the queue while it was locked. */
\r
1301 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1303 /* Data was posted while the queue was locked. Are any tasks
\r
1304 blocked waiting for data to become available? */
\r
1305 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1307 /* Tasks that are removed from the event list will get added to
\r
1308 the pending ready list as the scheduler is still suspended. */
\r
1309 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1311 /* The task waiting has a higher priority so record that a
\r
1312 context switch is required. */
\r
1313 vTaskMissedYield();
\r
1316 --( pxQueue->xTxLock );
\r
1324 pxQueue->xTxLock = queueUNLOCKED;
\r
1326 taskEXIT_CRITICAL();
\r
1328 /* Do the same for the Rx lock. */
\r
1329 taskENTER_CRITICAL();
\r
1331 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1333 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1335 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1337 vTaskMissedYield();
\r
1340 --( pxQueue->xRxLock );
\r
1348 pxQueue->xRxLock = queueUNLOCKED;
\r
1350 taskEXIT_CRITICAL();
\r
1352 /*-----------------------------------------------------------*/
\r
1354 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1356 signed portBASE_TYPE xReturn;
\r
1358 taskENTER_CRITICAL();
\r
1359 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1360 taskEXIT_CRITICAL();
\r
1364 /*-----------------------------------------------------------*/
\r
1366 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1368 signed portBASE_TYPE xReturn;
\r
1370 configASSERT( pxQueue );
\r
1371 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1375 /*-----------------------------------------------------------*/
\r
1377 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1379 signed portBASE_TYPE xReturn;
\r
1381 taskENTER_CRITICAL();
\r
1382 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1383 taskEXIT_CRITICAL();
\r
1387 /*-----------------------------------------------------------*/
\r
1389 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1391 signed portBASE_TYPE xReturn;
\r
1393 configASSERT( pxQueue );
\r
1394 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1398 /*-----------------------------------------------------------*/
\r
1400 #if configUSE_CO_ROUTINES == 1
\r
1401 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1403 signed portBASE_TYPE xReturn;
\r
1405 /* If the queue is already full we may have to block. A critical section
\r
1406 is required to prevent an interrupt removing something from the queue
\r
1407 between the check to see if the queue is full and blocking on the queue. */
\r
1408 portDISABLE_INTERRUPTS();
\r
1410 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1412 /* The queue is full - do we want to block or just leave without
\r
1414 if( xTicksToWait > ( portTickType ) 0 )
\r
1416 /* As this is called from a coroutine we cannot block directly, but
\r
1417 return indicating that we need to block. */
\r
1418 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1419 portENABLE_INTERRUPTS();
\r
1420 return errQUEUE_BLOCKED;
\r
1424 portENABLE_INTERRUPTS();
\r
1425 return errQUEUE_FULL;
\r
1429 portENABLE_INTERRUPTS();
\r
1433 portDISABLE_INTERRUPTS();
\r
1435 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1437 /* There is room in the queue, copy the data into the queue. */
\r
1438 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1441 /* Were any co-routines waiting for data to become available? */
\r
1442 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1444 /* In this instance the co-routine could be placed directly
\r
1445 into the ready list as we are within a critical section.
\r
1446 Instead the same pending ready list mechanism is used as if
\r
1447 the event were caused from within an interrupt. */
\r
1448 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1450 /* The co-routine waiting has a higher priority so record
\r
1451 that a yield might be appropriate. */
\r
1452 xReturn = errQUEUE_YIELD;
\r
1458 xReturn = errQUEUE_FULL;
\r
1461 portENABLE_INTERRUPTS();
\r
1466 /*-----------------------------------------------------------*/
\r
1468 #if configUSE_CO_ROUTINES == 1
\r
1469 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1471 signed portBASE_TYPE xReturn;
\r
1473 /* If the queue is already empty we may have to block. A critical section
\r
1474 is required to prevent an interrupt adding something to the queue
\r
1475 between the check to see if the queue is empty and blocking on the queue. */
\r
1476 portDISABLE_INTERRUPTS();
\r
1478 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1480 /* There are no messages in the queue, do we want to block or just
\r
1481 leave with nothing? */
\r
1482 if( xTicksToWait > ( portTickType ) 0 )
\r
1484 /* As this is a co-routine we cannot block directly, but return
\r
1485 indicating that we need to block. */
\r
1486 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1487 portENABLE_INTERRUPTS();
\r
1488 return errQUEUE_BLOCKED;
\r
1492 portENABLE_INTERRUPTS();
\r
1493 return errQUEUE_FULL;
\r
1497 portENABLE_INTERRUPTS();
\r
1501 portDISABLE_INTERRUPTS();
\r
1503 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1505 /* Data is available from the queue. */
\r
1506 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1507 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1509 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1511 --( pxQueue->uxMessagesWaiting );
\r
1512 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1516 /* Were any co-routines waiting for space to become available? */
\r
1517 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1519 /* In this instance the co-routine could be placed directly
\r
1520 into the ready list as we are within a critical section.
\r
1521 Instead the same pending ready list mechanism is used as if
\r
1522 the event were caused from within an interrupt. */
\r
1523 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1525 xReturn = errQUEUE_YIELD;
\r
1534 portENABLE_INTERRUPTS();
\r
1539 /*-----------------------------------------------------------*/
\r
1543 #if configUSE_CO_ROUTINES == 1
\r
1544 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1546 /* Cannot block within an ISR so if there is no space on the queue then
\r
1547 exit without doing anything. */
\r
1548 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1550 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1552 /* We only want to wake one co-routine per ISR, so check that a
\r
1553 co-routine has not already been woken. */
\r
1554 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1556 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1558 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1566 return xCoRoutinePreviouslyWoken;
\r
1569 /*-----------------------------------------------------------*/
\r
1571 #if configUSE_CO_ROUTINES == 1
\r
1572 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1574 signed portBASE_TYPE xReturn;
\r
1576 /* We cannot block from an ISR, so check there is data available. If
\r
1577 not then just leave without doing anything. */
\r
1578 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1580 /* Copy the data from the queue. */
\r
1581 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1582 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1584 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1586 --( pxQueue->uxMessagesWaiting );
\r
1587 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1589 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1591 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1593 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1595 *pxCoRoutineWoken = pdTRUE;
\r
1610 /*-----------------------------------------------------------*/
\r
1612 #if configQUEUE_REGISTRY_SIZE > 0
\r
1614 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1616 unsigned portBASE_TYPE ux;
\r
1618 /* See if there is an empty space in the registry. A NULL name denotes
\r
1620 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1622 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1624 /* Store the information on this queue. */
\r
1625 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1626 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1633 /*-----------------------------------------------------------*/
\r
1635 #if configQUEUE_REGISTRY_SIZE > 0
\r
1637 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1639 unsigned portBASE_TYPE ux;
\r
1641 /* See if the handle of the queue being unregistered in actually in the
\r
1643 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1645 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1647 /* Set the name to NULL to show that this slot if free again. */
\r
1648 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1656 /*-----------------------------------------------------------*/
\r
1658 #if configUSE_TIMERS == 1
\r
1660 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1662 /* This function should not be called by application code hence the
\r
1663 'Restricted' in its name. It is not part of the public API. It is
\r
1664 designed for use by kernel code, and has special calling requirements.
\r
1665 It can result in vListInsert() being called on a list that can only
\r
1666 possibly ever have one item in it, so the list will be fast, but even
\r
1667 so it should be called with the scheduler locked and not from a critical
\r
1670 /* Only do anything if there are no messages in the queue. This function
\r
1671 will not actually cause the task to block, just place it on a blocked
\r
1672 list. It will not block until the scheduler is unlocked - at which
\r
1673 time a yield will be performed. If an item is added to the queue while
\r
1674 the queue is locked, and the calling task blocks on the queue, then the
\r
1675 calling task will be immediately unblocked when the queue is unlocked. */
\r
1676 prvLockQueue( pxQueue );
\r
1677 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1679 /* There is nothing in the queue, block for the specified period. */
\r
1680 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1682 prvUnlockQueue( pxQueue );
\r