2 FreeRTOS V7.2.0 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
71 all the API functions to use the MPU wrappers. That should only be done when
\r
72 task.h is included from an application file. */
\r
73 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
75 #include "FreeRTOS.h"
\r
78 #if ( configUSE_CO_ROUTINES == 1 )
\r
79 #include "croutine.h"
\r
82 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
84 /*-----------------------------------------------------------
\r
85 * PUBLIC LIST API documented in list.h
\r
86 *----------------------------------------------------------*/
\r
88 /* Constants used with the cRxLock and xTxLock structure members. */
\r
89 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
90 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
92 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
94 /* For internal use only. */
\r
95 #define queueSEND_TO_BACK ( 0 )
\r
96 #define queueSEND_TO_FRONT ( 1 )
\r
98 /* Effectively make a union out of the xQUEUE structure. */
\r
99 #define pxMutexHolder pcTail
\r
100 #define uxQueueType pcHead
\r
101 #define uxRecursiveCallCount pcReadFrom
\r
102 #define queueQUEUE_IS_MUTEX NULL
\r
104 /* Semaphores do not actually store or copy data, so have an items size of
\r
106 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )
\r
107 #define queueDONT_BLOCK ( ( portTickType ) 0U )
\r
108 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0U )
\r
110 /* These definitions *must* match those in queue.h. */
\r
111 #define queueQUEUE_TYPE_BASE ( 0U )
\r
112 #define queueQUEUE_TYPE_MUTEX ( 1U )
\r
113 #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( 2U )
\r
114 #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( 3U )
\r
115 #define queueQUEUE_TYPE_RECURSIVE_MUTEX ( 4U )
\r
118 * Definition of the queue used by the scheduler.
\r
119 * Items are queued by copy, not reference.
\r
121 typedef struct QueueDefinition
\r
123 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
124 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
126 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
127 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
129 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
130 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
132 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
133 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
134 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
136 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
137 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
139 #if ( configUSE_TRACE_FACILITY == 1 )
\r
140 unsigned char ucQueueNumber;
\r
141 unsigned char ucQueueType;
\r
145 /*-----------------------------------------------------------*/
\r
148 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
149 * To keep the definition private the API header file defines it as a
\r
152 typedef xQUEUE * xQueueHandle;
\r
155 * Prototypes for public functions are included here so we don't have to
\r
156 * include the API header file (as it defines xQueueHandle differently). These
\r
157 * functions are documented in the API header file.
\r
159 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
160 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
161 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
162 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
163 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
164 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
165 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION;
\r
166 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
167 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
168 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
169 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
170 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
171 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
172 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
173 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
174 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
175 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
176 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
177 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber ) PRIVILEGED_FUNCTION;
\r
178 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
179 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue ) PRIVILEGED_FUNCTION;
\r
180 xTaskHandle xQueueGetMutexHolder( xQueueHandle xSemaphore ) PRIVILEGED_FUNCTION;
\r
183 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
184 * an optional component.
\r
186 #if configUSE_CO_ROUTINES == 1
\r
187 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
188 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
189 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
190 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
194 * The queue registry is just a means for kernel aware debuggers to locate
\r
195 * queue structures. It has no other purpose so is an optional component.
\r
197 #if configQUEUE_REGISTRY_SIZE > 0
\r
199 /* The type stored within the queue registry array. This allows a name
\r
200 to be assigned to each queue making kernel aware debugging a little
\r
201 more user friendly. */
\r
202 typedef struct QUEUE_REGISTRY_ITEM
\r
204 signed char *pcQueueName;
\r
205 xQueueHandle xHandle;
\r
206 } xQueueRegistryItem;
\r
208 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
209 The pcQueueName member of a structure being NULL is indicative of the
\r
210 array position being vacant. */
\r
211 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
213 /* Removes a queue from the registry by simply setting the pcQueueName
\r
215 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
216 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
220 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
221 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
222 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
223 * queue is locked it will instead increment the appropriate queue lock count
\r
224 * to indicate that a task may require unblocking. When the queue in unlocked
\r
225 * these lock counts are inspected, and the appropriate action taken.
\r
227 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
230 * Uses a critical section to determine if there is any data in a queue.
\r
232 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
234 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
237 * Uses a critical section to determine if there is any space in a queue.
\r
239 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
241 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
244 * Copies an item into the queue, either at the front of the queue or the
\r
245 * back of the queue.
\r
247 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
250 * Copies an item out of a queue.
\r
252 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
253 /*-----------------------------------------------------------*/
\r
256 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
257 * accessing the queue event lists.
\r
259 #define prvLockQueue( pxQueue ) \
\r
260 taskENTER_CRITICAL(); \
\r
262 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
264 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
266 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
268 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
271 taskEXIT_CRITICAL()
\r
272 /*-----------------------------------------------------------*/
\r
275 /*-----------------------------------------------------------
\r
276 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
277 *----------------------------------------------------------*/
\r
279 portBASE_TYPE xQueueGenericReset( xQueueHandle pxQueue, portBASE_TYPE xNewQueue )
\r
281 configASSERT( pxQueue );
\r
283 taskENTER_CRITICAL();
\r
285 pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
\r
286 pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
287 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
288 pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );
\r
289 pxQueue->xRxLock = queueUNLOCKED;
\r
290 pxQueue->xTxLock = queueUNLOCKED;
\r
292 if( xNewQueue == pdFALSE )
\r
294 /* If there are tasks blocked waiting to read from the queue, then
\r
295 the tasks will remain blocked as after this function exits the queue
\r
296 will still be empty. If there are tasks blocked waiting to write to
\r
297 the queue, then one should be unblocked as after this function exits
\r
298 it will be possible to write to it. */
\r
299 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
301 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
303 portYIELD_WITHIN_API();
\r
309 /* Ensure the event queues start in the correct state. */
\r
310 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
\r
311 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
\r
314 taskEXIT_CRITICAL();
\r
316 /* A value is returned for calling semantic consistency with previous
\r
320 /*-----------------------------------------------------------*/
\r
322 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )
\r
324 xQUEUE *pxNewQueue;
\r
325 size_t xQueueSizeInBytes;
\r
326 xQueueHandle xReturn = NULL;
\r
328 /* Remove compiler warnings about unused parameters should
\r
329 configUSE_TRACE_FACILITY not be set to 1. */
\r
330 ( void ) ucQueueType;
\r
332 /* Allocate the new queue structure. */
\r
333 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
335 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
336 if( pxNewQueue != NULL )
\r
338 /* Create the list of pointers to queue items. The queue is one byte
\r
339 longer than asked for to make wrap checking easier/faster. */
\r
340 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
342 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
343 if( pxNewQueue->pcHead != NULL )
\r
345 /* Initialise the queue members as described above where the
\r
346 queue type is defined. */
\r
347 pxNewQueue->uxLength = uxQueueLength;
\r
348 pxNewQueue->uxItemSize = uxItemSize;
\r
349 xQueueGenericReset( pxNewQueue, pdTRUE );
\r
350 #if ( configUSE_TRACE_FACILITY == 1 )
\r
352 pxNewQueue->ucQueueType = ucQueueType;
\r
354 #endif /* configUSE_TRACE_FACILITY */
\r
356 traceQUEUE_CREATE( pxNewQueue );
\r
357 xReturn = pxNewQueue;
\r
361 traceQUEUE_CREATE_FAILED( ucQueueType );
\r
362 vPortFree( pxNewQueue );
\r
367 configASSERT( xReturn );
\r
371 /*-----------------------------------------------------------*/
\r
373 #if ( configUSE_MUTEXES == 1 )
\r
375 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )
\r
377 xQUEUE *pxNewQueue;
\r
379 /* Prevent compiler warnings about unused parameters if
\r
380 configUSE_TRACE_FACILITY does not equal 1. */
\r
381 ( void ) ucQueueType;
\r
383 /* Allocate the new queue structure. */
\r
384 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
385 if( pxNewQueue != NULL )
\r
387 /* Information required for priority inheritance. */
\r
388 pxNewQueue->pxMutexHolder = NULL;
\r
389 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
391 /* Queues used as a mutex no data is actually copied into or out
\r
393 pxNewQueue->pcWriteTo = NULL;
\r
394 pxNewQueue->pcReadFrom = NULL;
\r
396 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
397 an item size of 0 as nothing is actually copied into or out
\r
399 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
400 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
401 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
402 pxNewQueue->xRxLock = queueUNLOCKED;
\r
403 pxNewQueue->xTxLock = queueUNLOCKED;
\r
405 #if ( configUSE_TRACE_FACILITY == 1 )
\r
407 pxNewQueue->ucQueueType = ucQueueType;
\r
411 /* Ensure the event queues start with the correct state. */
\r
412 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
413 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
415 traceCREATE_MUTEX( pxNewQueue );
\r
417 /* Start with the semaphore in the expected state. */
\r
418 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
422 traceCREATE_MUTEX_FAILED();
\r
425 configASSERT( pxNewQueue );
\r
429 #endif /* configUSE_MUTEXES */
\r
430 /*-----------------------------------------------------------*/
\r
432 #if ( ( configUSE_MUTEXES == 1 ) && ( INCLUDE_xQueueGetMutexHolder == 1 ) )
\r
434 void* xQueueGetMutexHolder( xQueueHandle xSemaphore )
\r
438 /* This function is called by xSemaphoreGetMutexHolder(), and should not
\r
439 be called directly. Note: This is is a good way of determining if the
\r
440 calling task is the mutex holder, but not a good way of determining the
\r
441 identity of the mutex holder, as the holder may change between the
\r
442 following critical section exiting and the function returning. */
\r
443 taskENTER_CRITICAL();
\r
445 if( xSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
\r
447 pxReturn = ( void * ) xSemaphore->pxMutexHolder;
\r
454 taskEXIT_CRITICAL();
\r
460 /*-----------------------------------------------------------*/
\r
462 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
464 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
466 portBASE_TYPE xReturn;
\r
468 configASSERT( pxMutex );
\r
470 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
471 change outside of this task. If this task does not hold the mutex then
\r
472 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
473 this is the only condition we are interested in it does not matter if
\r
474 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
475 mutual exclusion is required to test the pxMutexHolder variable. */
\r
476 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
478 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
480 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
481 the task handle, therefore no underflow check is required. Also,
\r
482 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
483 there can only be one, no mutual exclusion is required to modify the
\r
484 uxRecursiveCallCount member. */
\r
485 ( pxMutex->uxRecursiveCallCount )--;
\r
487 /* Have we unwound the call count? */
\r
488 if( pxMutex->uxRecursiveCallCount == 0 )
\r
490 /* Return the mutex. This will automatically unblock any other
\r
491 task that might be waiting to access the mutex. */
\r
492 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
499 /* We cannot give the mutex because we are not the holder. */
\r
502 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
508 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
509 /*-----------------------------------------------------------*/
\r
511 #if configUSE_RECURSIVE_MUTEXES == 1
\r
513 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
515 portBASE_TYPE xReturn;
\r
517 configASSERT( pxMutex );
\r
519 /* Comments regarding mutual exclusion as per those within
\r
520 xQueueGiveMutexRecursive(). */
\r
522 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
524 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
526 ( pxMutex->uxRecursiveCallCount )++;
\r
531 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
533 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
534 we may have blocked to reach here. */
\r
535 if( xReturn == pdPASS )
\r
537 ( pxMutex->uxRecursiveCallCount )++;
\r
541 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
548 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
549 /*-----------------------------------------------------------*/
\r
551 #if configUSE_COUNTING_SEMAPHORES == 1
\r
553 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
555 xQueueHandle pxHandle;
\r
557 pxHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
559 if( pxHandle != NULL )
\r
561 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
563 traceCREATE_COUNTING_SEMAPHORE();
\r
567 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
570 configASSERT( pxHandle );
\r
574 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
575 /*-----------------------------------------------------------*/
\r
577 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
579 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
580 xTimeOutType xTimeOut;
\r
582 configASSERT( pxQueue );
\r
583 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
585 /* This function relaxes the coding standard somewhat to allow return
\r
586 statements within the function itself. This is done in the interest
\r
587 of execution time efficiency. */
\r
590 taskENTER_CRITICAL();
\r
592 /* Is there room on the queue now? To be running we must be
\r
593 the highest priority task wanting to access the queue. */
\r
594 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
596 traceQUEUE_SEND( pxQueue );
\r
597 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
599 /* If there was a task waiting for data to arrive on the
\r
600 queue then unblock it now. */
\r
601 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
603 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
605 /* The unblocked task has a priority higher than
\r
606 our own so yield immediately. Yes it is ok to do
\r
607 this from within the critical section - the kernel
\r
608 takes care of that. */
\r
609 portYIELD_WITHIN_API();
\r
613 taskEXIT_CRITICAL();
\r
615 /* Return to the original privilege level before exiting the
\r
621 if( xTicksToWait == ( portTickType ) 0 )
\r
623 /* The queue was full and no block time is specified (or
\r
624 the block time has expired) so leave now. */
\r
625 taskEXIT_CRITICAL();
\r
627 /* Return to the original privilege level before exiting
\r
629 traceQUEUE_SEND_FAILED( pxQueue );
\r
630 return errQUEUE_FULL;
\r
632 else if( xEntryTimeSet == pdFALSE )
\r
634 /* The queue was full and a block time was specified so
\r
635 configure the timeout structure. */
\r
636 vTaskSetTimeOutState( &xTimeOut );
\r
637 xEntryTimeSet = pdTRUE;
\r
641 taskEXIT_CRITICAL();
\r
643 /* Interrupts and other tasks can send to and receive from the queue
\r
644 now the critical section has been exited. */
\r
647 prvLockQueue( pxQueue );
\r
649 /* Update the timeout state to see if it has expired yet. */
\r
650 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
652 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
654 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
655 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
657 /* Unlocking the queue means queue events can effect the
\r
658 event list. It is possible that interrupts occurring now
\r
659 remove this task from the event list again - but as the
\r
660 scheduler is suspended the task will go onto the pending
\r
661 ready last instead of the actual ready list. */
\r
662 prvUnlockQueue( pxQueue );
\r
664 /* Resuming the scheduler will move tasks from the pending
\r
665 ready list into the ready list - so it is feasible that this
\r
666 task is already in a ready list before it yields - in which
\r
667 case the yield will not cause a context switch unless there
\r
668 is also a higher priority task in the pending ready list. */
\r
669 if( xTaskResumeAll() == pdFALSE )
\r
671 portYIELD_WITHIN_API();
\r
677 prvUnlockQueue( pxQueue );
\r
678 ( void ) xTaskResumeAll();
\r
683 /* The timeout has expired. */
\r
684 prvUnlockQueue( pxQueue );
\r
685 ( void ) xTaskResumeAll();
\r
687 /* Return to the original privilege level before exiting the
\r
689 traceQUEUE_SEND_FAILED( pxQueue );
\r
690 return errQUEUE_FULL;
\r
694 /*-----------------------------------------------------------*/
\r
696 #if configUSE_ALTERNATIVE_API == 1
\r
698 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
700 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
701 xTimeOutType xTimeOut;
\r
703 configASSERT( pxQueue );
\r
704 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
708 taskENTER_CRITICAL();
\r
710 /* Is there room on the queue now? To be running we must be
\r
711 the highest priority task wanting to access the queue. */
\r
712 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
714 traceQUEUE_SEND( pxQueue );
\r
715 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
717 /* If there was a task waiting for data to arrive on the
\r
718 queue then unblock it now. */
\r
719 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
721 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
723 /* The unblocked task has a priority higher than
\r
724 our own so yield immediately. */
\r
725 portYIELD_WITHIN_API();
\r
729 taskEXIT_CRITICAL();
\r
734 if( xTicksToWait == ( portTickType ) 0 )
\r
736 taskEXIT_CRITICAL();
\r
737 return errQUEUE_FULL;
\r
739 else if( xEntryTimeSet == pdFALSE )
\r
741 vTaskSetTimeOutState( &xTimeOut );
\r
742 xEntryTimeSet = pdTRUE;
\r
746 taskEXIT_CRITICAL();
\r
748 taskENTER_CRITICAL();
\r
750 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
752 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
754 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
755 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
756 portYIELD_WITHIN_API();
\r
761 taskEXIT_CRITICAL();
\r
762 traceQUEUE_SEND_FAILED( pxQueue );
\r
763 return errQUEUE_FULL;
\r
766 taskEXIT_CRITICAL();
\r
770 #endif /* configUSE_ALTERNATIVE_API */
\r
771 /*-----------------------------------------------------------*/
\r
773 #if configUSE_ALTERNATIVE_API == 1
\r
775 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
777 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
778 xTimeOutType xTimeOut;
\r
779 signed char *pcOriginalReadPosition;
\r
781 configASSERT( pxQueue );
\r
782 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
786 taskENTER_CRITICAL();
\r
788 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
790 /* Remember our read position in case we are just peeking. */
\r
791 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
793 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
795 if( xJustPeeking == pdFALSE )
\r
797 traceQUEUE_RECEIVE( pxQueue );
\r
799 /* We are actually removing data. */
\r
800 --( pxQueue->uxMessagesWaiting );
\r
802 #if ( configUSE_MUTEXES == 1 )
\r
804 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
806 /* Record the information required to implement
\r
807 priority inheritance should it become necessary. */
\r
808 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
813 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
815 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
817 portYIELD_WITHIN_API();
\r
823 traceQUEUE_PEEK( pxQueue );
\r
825 /* We are not removing the data, so reset our read
\r
827 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
829 /* The data is being left in the queue, so see if there are
\r
830 any other tasks waiting for the data. */
\r
831 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
833 /* Tasks that are removed from the event list will get added to
\r
834 the pending ready list as the scheduler is still suspended. */
\r
835 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
837 /* The task waiting has a higher priority than this task. */
\r
838 portYIELD_WITHIN_API();
\r
844 taskEXIT_CRITICAL();
\r
849 if( xTicksToWait == ( portTickType ) 0 )
\r
851 taskEXIT_CRITICAL();
\r
852 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
853 return errQUEUE_EMPTY;
\r
855 else if( xEntryTimeSet == pdFALSE )
\r
857 vTaskSetTimeOutState( &xTimeOut );
\r
858 xEntryTimeSet = pdTRUE;
\r
862 taskEXIT_CRITICAL();
\r
864 taskENTER_CRITICAL();
\r
866 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
868 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
870 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
872 #if ( configUSE_MUTEXES == 1 )
\r
874 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
876 portENTER_CRITICAL();
\r
878 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
880 portEXIT_CRITICAL();
\r
885 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
886 portYIELD_WITHIN_API();
\r
891 taskEXIT_CRITICAL();
\r
892 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
893 return errQUEUE_EMPTY;
\r
896 taskEXIT_CRITICAL();
\r
901 #endif /* configUSE_ALTERNATIVE_API */
\r
902 /*-----------------------------------------------------------*/
\r
904 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
906 signed portBASE_TYPE xReturn;
\r
907 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
909 configASSERT( pxQueue );
\r
910 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
912 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
913 in the queue. Also we don't directly wake a task that was blocked on a
\r
914 queue read, instead we return a flag to say whether a context switch is
\r
915 required or not (i.e. has a task with a higher priority than us been woken
\r
917 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
919 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
921 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
923 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
925 /* If the queue is locked we do not alter the event list. This will
\r
926 be done when the queue is unlocked later. */
\r
927 if( pxQueue->xTxLock == queueUNLOCKED )
\r
929 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
931 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
933 /* The task waiting has a higher priority so record that a
\r
934 context switch is required. */
\r
935 if( pxHigherPriorityTaskWoken != NULL )
\r
937 *pxHigherPriorityTaskWoken = pdTRUE;
\r
944 /* Increment the lock count so the task that unlocks the queue
\r
945 knows that data was posted while it was locked. */
\r
946 ++( pxQueue->xTxLock );
\r
953 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
954 xReturn = errQUEUE_FULL;
\r
957 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
961 /*-----------------------------------------------------------*/
\r
963 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
965 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
966 xTimeOutType xTimeOut;
\r
967 signed char *pcOriginalReadPosition;
\r
969 configASSERT( pxQueue );
\r
970 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
972 /* This function relaxes the coding standard somewhat to allow return
\r
973 statements within the function itself. This is done in the interest
\r
974 of execution time efficiency. */
\r
978 taskENTER_CRITICAL();
\r
980 /* Is there data in the queue now? To be running we must be
\r
981 the highest priority task wanting to access the queue. */
\r
982 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
984 /* Remember our read position in case we are just peeking. */
\r
985 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
987 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
989 if( xJustPeeking == pdFALSE )
\r
991 traceQUEUE_RECEIVE( pxQueue );
\r
993 /* We are actually removing data. */
\r
994 --( pxQueue->uxMessagesWaiting );
\r
996 #if ( configUSE_MUTEXES == 1 )
\r
998 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1000 /* Record the information required to implement
\r
1001 priority inheritance should it become necessary. */
\r
1002 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
1007 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1009 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
1011 portYIELD_WITHIN_API();
\r
1017 traceQUEUE_PEEK( pxQueue );
\r
1019 /* We are not removing the data, so reset our read
\r
1021 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
1023 /* The data is being left in the queue, so see if there are
\r
1024 any other tasks waiting for the data. */
\r
1025 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1027 /* Tasks that are removed from the event list will get added to
\r
1028 the pending ready list as the scheduler is still suspended. */
\r
1029 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1031 /* The task waiting has a higher priority than this task. */
\r
1032 portYIELD_WITHIN_API();
\r
1037 taskEXIT_CRITICAL();
\r
1042 if( xTicksToWait == ( portTickType ) 0 )
\r
1044 /* The queue was empty and no block time is specified (or
\r
1045 the block time has expired) so leave now. */
\r
1046 taskEXIT_CRITICAL();
\r
1047 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1048 return errQUEUE_EMPTY;
\r
1050 else if( xEntryTimeSet == pdFALSE )
\r
1052 /* The queue was empty and a block time was specified so
\r
1053 configure the timeout structure. */
\r
1054 vTaskSetTimeOutState( &xTimeOut );
\r
1055 xEntryTimeSet = pdTRUE;
\r
1059 taskEXIT_CRITICAL();
\r
1061 /* Interrupts and other tasks can send to and receive from the queue
\r
1062 now the critical section has been exited. */
\r
1064 vTaskSuspendAll();
\r
1065 prvLockQueue( pxQueue );
\r
1067 /* Update the timeout state to see if it has expired yet. */
\r
1068 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1070 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1072 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1074 #if ( configUSE_MUTEXES == 1 )
\r
1076 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1078 portENTER_CRITICAL();
\r
1080 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
1082 portEXIT_CRITICAL();
\r
1087 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1088 prvUnlockQueue( pxQueue );
\r
1089 if( xTaskResumeAll() == pdFALSE )
\r
1091 portYIELD_WITHIN_API();
\r
1097 prvUnlockQueue( pxQueue );
\r
1098 ( void ) xTaskResumeAll();
\r
1103 prvUnlockQueue( pxQueue );
\r
1104 ( void ) xTaskResumeAll();
\r
1105 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1106 return errQUEUE_EMPTY;
\r
1110 /*-----------------------------------------------------------*/
\r
1112 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken )
\r
1114 signed portBASE_TYPE xReturn;
\r
1115 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1117 configASSERT( pxQueue );
\r
1118 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1120 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1122 /* We cannot block from an ISR, so check there is data available. */
\r
1123 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1125 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1127 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1128 --( pxQueue->uxMessagesWaiting );
\r
1130 /* If the queue is locked we will not modify the event list. Instead
\r
1131 we update the lock count so the task that unlocks the queue will know
\r
1132 that an ISR has removed data while the queue was locked. */
\r
1133 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1135 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1137 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1139 /* The task waiting has a higher priority than us so
\r
1140 force a context switch. */
\r
1141 if( pxHigherPriorityTaskWoken != NULL )
\r
1143 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1150 /* Increment the lock count so the task that unlocks the queue
\r
1151 knows that data was removed while it was locked. */
\r
1152 ++( pxQueue->xRxLock );
\r
1160 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1163 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1167 /*-----------------------------------------------------------*/
\r
1169 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1171 unsigned portBASE_TYPE uxReturn;
\r
1173 configASSERT( pxQueue );
\r
1175 taskENTER_CRITICAL();
\r
1176 uxReturn = pxQueue->uxMessagesWaiting;
\r
1177 taskEXIT_CRITICAL();
\r
1181 /*-----------------------------------------------------------*/
\r
1183 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1185 unsigned portBASE_TYPE uxReturn;
\r
1187 configASSERT( pxQueue );
\r
1189 uxReturn = pxQueue->uxMessagesWaiting;
\r
1193 /*-----------------------------------------------------------*/
\r
1195 void vQueueDelete( xQueueHandle pxQueue )
\r
1197 configASSERT( pxQueue );
\r
1199 traceQUEUE_DELETE( pxQueue );
\r
1200 vQueueUnregisterQueue( pxQueue );
\r
1201 vPortFree( pxQueue->pcHead );
\r
1202 vPortFree( pxQueue );
\r
1204 /*-----------------------------------------------------------*/
\r
1206 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1208 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )
\r
1210 return pxQueue->ucQueueNumber;
\r
1214 /*-----------------------------------------------------------*/
\r
1216 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1218 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber )
\r
1220 pxQueue->ucQueueNumber = ucQueueNumber;
\r
1224 /*-----------------------------------------------------------*/
\r
1226 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1228 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )
\r
1230 return pxQueue->ucQueueType;
\r
1234 /*-----------------------------------------------------------*/
\r
1236 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1238 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1240 #if ( configUSE_MUTEXES == 1 )
\r
1242 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1244 /* The mutex is no longer being held. */
\r
1245 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1246 pxQueue->pxMutexHolder = NULL;
\r
1251 else if( xPosition == queueSEND_TO_BACK )
\r
1253 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1254 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1255 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1257 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1262 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1263 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1264 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1266 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1270 ++( pxQueue->uxMessagesWaiting );
\r
1272 /*-----------------------------------------------------------*/
\r
1274 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1276 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1278 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1279 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1281 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1283 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1286 /*-----------------------------------------------------------*/
\r
1288 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1290 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1292 /* The lock counts contains the number of extra data items placed or
\r
1293 removed from the queue while the queue was locked. When a queue is
\r
1294 locked items can be added or removed, but the event lists cannot be
\r
1296 taskENTER_CRITICAL();
\r
1298 /* See if data was added to the queue while it was locked. */
\r
1299 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1301 /* Data was posted while the queue was locked. Are any tasks
\r
1302 blocked waiting for data to become available? */
\r
1303 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1305 /* Tasks that are removed from the event list will get added to
\r
1306 the pending ready list as the scheduler is still suspended. */
\r
1307 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1309 /* The task waiting has a higher priority so record that a
\r
1310 context switch is required. */
\r
1311 vTaskMissedYield();
\r
1314 --( pxQueue->xTxLock );
\r
1322 pxQueue->xTxLock = queueUNLOCKED;
\r
1324 taskEXIT_CRITICAL();
\r
1326 /* Do the same for the Rx lock. */
\r
1327 taskENTER_CRITICAL();
\r
1329 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1331 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1333 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1335 vTaskMissedYield();
\r
1338 --( pxQueue->xRxLock );
\r
1346 pxQueue->xRxLock = queueUNLOCKED;
\r
1348 taskEXIT_CRITICAL();
\r
1350 /*-----------------------------------------------------------*/
\r
1352 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1354 signed portBASE_TYPE xReturn;
\r
1356 taskENTER_CRITICAL();
\r
1357 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1358 taskEXIT_CRITICAL();
\r
1362 /*-----------------------------------------------------------*/
\r
1364 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1366 signed portBASE_TYPE xReturn;
\r
1368 configASSERT( pxQueue );
\r
1369 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1373 /*-----------------------------------------------------------*/
\r
1375 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1377 signed portBASE_TYPE xReturn;
\r
1379 taskENTER_CRITICAL();
\r
1380 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1381 taskEXIT_CRITICAL();
\r
1385 /*-----------------------------------------------------------*/
\r
1387 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1389 signed portBASE_TYPE xReturn;
\r
1391 configASSERT( pxQueue );
\r
1392 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1396 /*-----------------------------------------------------------*/
\r
1398 #if configUSE_CO_ROUTINES == 1
\r
1399 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1401 signed portBASE_TYPE xReturn;
\r
1403 /* If the queue is already full we may have to block. A critical section
\r
1404 is required to prevent an interrupt removing something from the queue
\r
1405 between the check to see if the queue is full and blocking on the queue. */
\r
1406 portDISABLE_INTERRUPTS();
\r
1408 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1410 /* The queue is full - do we want to block or just leave without
\r
1412 if( xTicksToWait > ( portTickType ) 0 )
\r
1414 /* As this is called from a coroutine we cannot block directly, but
\r
1415 return indicating that we need to block. */
\r
1416 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1417 portENABLE_INTERRUPTS();
\r
1418 return errQUEUE_BLOCKED;
\r
1422 portENABLE_INTERRUPTS();
\r
1423 return errQUEUE_FULL;
\r
1427 portENABLE_INTERRUPTS();
\r
1431 portDISABLE_INTERRUPTS();
\r
1433 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1435 /* There is room in the queue, copy the data into the queue. */
\r
1436 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1439 /* Were any co-routines waiting for data to become available? */
\r
1440 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1442 /* In this instance the co-routine could be placed directly
\r
1443 into the ready list as we are within a critical section.
\r
1444 Instead the same pending ready list mechanism is used as if
\r
1445 the event were caused from within an interrupt. */
\r
1446 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1448 /* The co-routine waiting has a higher priority so record
\r
1449 that a yield might be appropriate. */
\r
1450 xReturn = errQUEUE_YIELD;
\r
1456 xReturn = errQUEUE_FULL;
\r
1459 portENABLE_INTERRUPTS();
\r
1464 /*-----------------------------------------------------------*/
\r
1466 #if configUSE_CO_ROUTINES == 1
\r
1467 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1469 signed portBASE_TYPE xReturn;
\r
1471 /* If the queue is already empty we may have to block. A critical section
\r
1472 is required to prevent an interrupt adding something to the queue
\r
1473 between the check to see if the queue is empty and blocking on the queue. */
\r
1474 portDISABLE_INTERRUPTS();
\r
1476 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1478 /* There are no messages in the queue, do we want to block or just
\r
1479 leave with nothing? */
\r
1480 if( xTicksToWait > ( portTickType ) 0 )
\r
1482 /* As this is a co-routine we cannot block directly, but return
\r
1483 indicating that we need to block. */
\r
1484 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1485 portENABLE_INTERRUPTS();
\r
1486 return errQUEUE_BLOCKED;
\r
1490 portENABLE_INTERRUPTS();
\r
1491 return errQUEUE_FULL;
\r
1495 portENABLE_INTERRUPTS();
\r
1499 portDISABLE_INTERRUPTS();
\r
1501 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1503 /* Data is available from the queue. */
\r
1504 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1505 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1507 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1509 --( pxQueue->uxMessagesWaiting );
\r
1510 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1514 /* Were any co-routines waiting for space to become available? */
\r
1515 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1517 /* In this instance the co-routine could be placed directly
\r
1518 into the ready list as we are within a critical section.
\r
1519 Instead the same pending ready list mechanism is used as if
\r
1520 the event were caused from within an interrupt. */
\r
1521 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1523 xReturn = errQUEUE_YIELD;
\r
1532 portENABLE_INTERRUPTS();
\r
1537 /*-----------------------------------------------------------*/
\r
1541 #if configUSE_CO_ROUTINES == 1
\r
1542 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1544 /* Cannot block within an ISR so if there is no space on the queue then
\r
1545 exit without doing anything. */
\r
1546 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1548 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1550 /* We only want to wake one co-routine per ISR, so check that a
\r
1551 co-routine has not already been woken. */
\r
1552 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1554 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1556 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1564 return xCoRoutinePreviouslyWoken;
\r
1567 /*-----------------------------------------------------------*/
\r
1569 #if configUSE_CO_ROUTINES == 1
\r
1570 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1572 signed portBASE_TYPE xReturn;
\r
1574 /* We cannot block from an ISR, so check there is data available. If
\r
1575 not then just leave without doing anything. */
\r
1576 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1578 /* Copy the data from the queue. */
\r
1579 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1580 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1582 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1584 --( pxQueue->uxMessagesWaiting );
\r
1585 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1587 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1589 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1591 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1593 *pxCoRoutineWoken = pdTRUE;
\r
1608 /*-----------------------------------------------------------*/
\r
1610 #if configQUEUE_REGISTRY_SIZE > 0
\r
1612 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1614 unsigned portBASE_TYPE ux;
\r
1616 /* See if there is an empty space in the registry. A NULL name denotes
\r
1618 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1620 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1622 /* Store the information on this queue. */
\r
1623 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1624 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1631 /*-----------------------------------------------------------*/
\r
1633 #if configQUEUE_REGISTRY_SIZE > 0
\r
1635 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1637 unsigned portBASE_TYPE ux;
\r
1639 /* See if the handle of the queue being unregistered in actually in the
\r
1641 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1643 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1645 /* Set the name to NULL to show that this slot if free again. */
\r
1646 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1654 /*-----------------------------------------------------------*/
\r
1656 #if configUSE_TIMERS == 1
\r
1658 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1660 /* This function should not be called by application code hence the
\r
1661 'Restricted' in its name. It is not part of the public API. It is
\r
1662 designed for use by kernel code, and has special calling requirements.
\r
1663 It can result in vListInsert() being called on a list that can only
\r
1664 possibly ever have one item in it, so the list will be fast, but even
\r
1665 so it should be called with the scheduler locked and not from a critical
\r
1668 /* Only do anything if there are no messages in the queue. This function
\r
1669 will not actually cause the task to block, just place it on a blocked
\r
1670 list. It will not block until the scheduler is unlocked - at which
\r
1671 time a yield will be performed. If an item is added to the queue while
\r
1672 the queue is locked, and the calling task blocks on the queue, then the
\r
1673 calling task will be immediately unblocked when the queue is unlocked. */
\r
1674 prvLockQueue( pxQueue );
\r
1675 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1677 /* There is nothing in the queue, block for the specified period. */
\r
1678 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1680 prvUnlockQueue( pxQueue );
\r