2 FreeRTOS V7.1.1 - 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
877 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
878 portEXIT_CRITICAL();
\r
883 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
884 portYIELD_WITHIN_API();
\r
889 taskEXIT_CRITICAL();
\r
890 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
891 return errQUEUE_EMPTY;
\r
894 taskEXIT_CRITICAL();
\r
899 #endif /* configUSE_ALTERNATIVE_API */
\r
900 /*-----------------------------------------------------------*/
\r
902 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
904 signed portBASE_TYPE xReturn;
\r
905 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
907 configASSERT( pxQueue );
\r
908 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
910 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
911 in the queue. Also we don't directly wake a task that was blocked on a
\r
912 queue read, instead we return a flag to say whether a context switch is
\r
913 required or not (i.e. has a task with a higher priority than us been woken
\r
915 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
917 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
919 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
921 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
923 /* If the queue is locked we do not alter the event list. This will
\r
924 be done when the queue is unlocked later. */
\r
925 if( pxQueue->xTxLock == queueUNLOCKED )
\r
927 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
929 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
931 /* The task waiting has a higher priority so record that a
\r
932 context switch is required. */
\r
933 if( pxHigherPriorityTaskWoken != NULL )
\r
935 *pxHigherPriorityTaskWoken = pdTRUE;
\r
942 /* Increment the lock count so the task that unlocks the queue
\r
943 knows that data was posted while it was locked. */
\r
944 ++( pxQueue->xTxLock );
\r
951 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
952 xReturn = errQUEUE_FULL;
\r
955 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
959 /*-----------------------------------------------------------*/
\r
961 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
963 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
964 xTimeOutType xTimeOut;
\r
965 signed char *pcOriginalReadPosition;
\r
967 configASSERT( pxQueue );
\r
968 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
970 /* This function relaxes the coding standard somewhat to allow return
\r
971 statements within the function itself. This is done in the interest
\r
972 of execution time efficiency. */
\r
976 taskENTER_CRITICAL();
\r
978 /* Is there data in the queue now? To be running we must be
\r
979 the highest priority task wanting to access the queue. */
\r
980 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
982 /* Remember our read position in case we are just peeking. */
\r
983 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
985 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
987 if( xJustPeeking == pdFALSE )
\r
989 traceQUEUE_RECEIVE( pxQueue );
\r
991 /* We are actually removing data. */
\r
992 --( pxQueue->uxMessagesWaiting );
\r
994 #if ( configUSE_MUTEXES == 1 )
\r
996 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
998 /* Record the information required to implement
\r
999 priority inheritance should it become necessary. */
\r
1000 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
1005 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1007 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
1009 portYIELD_WITHIN_API();
\r
1015 traceQUEUE_PEEK( pxQueue );
\r
1017 /* We are not removing the data, so reset our read
\r
1019 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
1021 /* The data is being left in the queue, so see if there are
\r
1022 any other tasks waiting for the data. */
\r
1023 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1025 /* Tasks that are removed from the event list will get added to
\r
1026 the pending ready list as the scheduler is still suspended. */
\r
1027 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1029 /* The task waiting has a higher priority than this task. */
\r
1030 portYIELD_WITHIN_API();
\r
1035 taskEXIT_CRITICAL();
\r
1040 if( xTicksToWait == ( portTickType ) 0 )
\r
1042 /* The queue was empty and no block time is specified (or
\r
1043 the block time has expired) so leave now. */
\r
1044 taskEXIT_CRITICAL();
\r
1045 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1046 return errQUEUE_EMPTY;
\r
1048 else if( xEntryTimeSet == pdFALSE )
\r
1050 /* The queue was empty and a block time was specified so
\r
1051 configure the timeout structure. */
\r
1052 vTaskSetTimeOutState( &xTimeOut );
\r
1053 xEntryTimeSet = pdTRUE;
\r
1057 taskEXIT_CRITICAL();
\r
1059 /* Interrupts and other tasks can send to and receive from the queue
\r
1060 now the critical section has been exited. */
\r
1062 vTaskSuspendAll();
\r
1063 prvLockQueue( pxQueue );
\r
1065 /* Update the timeout state to see if it has expired yet. */
\r
1066 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1068 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1070 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1072 #if ( configUSE_MUTEXES == 1 )
\r
1074 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1076 portENTER_CRITICAL();
\r
1078 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
1080 portEXIT_CRITICAL();
\r
1085 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1086 prvUnlockQueue( pxQueue );
\r
1087 if( xTaskResumeAll() == pdFALSE )
\r
1089 portYIELD_WITHIN_API();
\r
1095 prvUnlockQueue( pxQueue );
\r
1096 ( void ) xTaskResumeAll();
\r
1101 prvUnlockQueue( pxQueue );
\r
1102 ( void ) xTaskResumeAll();
\r
1103 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1104 return errQUEUE_EMPTY;
\r
1108 /*-----------------------------------------------------------*/
\r
1110 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxHigherPriorityTaskWoken )
\r
1112 signed portBASE_TYPE xReturn;
\r
1113 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1115 configASSERT( pxQueue );
\r
1116 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1118 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1120 /* We cannot block from an ISR, so check there is data available. */
\r
1121 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1123 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1125 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1126 --( pxQueue->uxMessagesWaiting );
\r
1128 /* If the queue is locked we will not modify the event list. Instead
\r
1129 we update the lock count so the task that unlocks the queue will know
\r
1130 that an ISR has removed data while the queue was locked. */
\r
1131 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1133 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1135 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1137 /* The task waiting has a higher priority than us so
\r
1138 force a context switch. */
\r
1139 if( pxHigherPriorityTaskWoken != NULL )
\r
1141 *pxHigherPriorityTaskWoken = pdTRUE;
\r
1148 /* Increment the lock count so the task that unlocks the queue
\r
1149 knows that data was removed while it was locked. */
\r
1150 ++( pxQueue->xRxLock );
\r
1158 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1161 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1165 /*-----------------------------------------------------------*/
\r
1167 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1169 unsigned portBASE_TYPE uxReturn;
\r
1171 configASSERT( pxQueue );
\r
1173 taskENTER_CRITICAL();
\r
1174 uxReturn = pxQueue->uxMessagesWaiting;
\r
1175 taskEXIT_CRITICAL();
\r
1179 /*-----------------------------------------------------------*/
\r
1181 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1183 unsigned portBASE_TYPE uxReturn;
\r
1185 configASSERT( pxQueue );
\r
1187 uxReturn = pxQueue->uxMessagesWaiting;
\r
1191 /*-----------------------------------------------------------*/
\r
1193 void vQueueDelete( xQueueHandle pxQueue )
\r
1195 configASSERT( pxQueue );
\r
1197 traceQUEUE_DELETE( pxQueue );
\r
1198 vQueueUnregisterQueue( pxQueue );
\r
1199 vPortFree( pxQueue->pcHead );
\r
1200 vPortFree( pxQueue );
\r
1202 /*-----------------------------------------------------------*/
\r
1204 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1206 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )
\r
1208 return pxQueue->ucQueueNumber;
\r
1212 /*-----------------------------------------------------------*/
\r
1214 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1216 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber )
\r
1218 pxQueue->ucQueueNumber = ucQueueNumber;
\r
1222 /*-----------------------------------------------------------*/
\r
1224 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1226 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )
\r
1228 return pxQueue->ucQueueType;
\r
1232 /*-----------------------------------------------------------*/
\r
1234 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1236 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1238 #if ( configUSE_MUTEXES == 1 )
\r
1240 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1242 /* The mutex is no longer being held. */
\r
1243 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1244 pxQueue->pxMutexHolder = NULL;
\r
1249 else if( xPosition == queueSEND_TO_BACK )
\r
1251 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1252 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1253 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1255 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1260 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1261 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1262 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1264 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1268 ++( pxQueue->uxMessagesWaiting );
\r
1270 /*-----------------------------------------------------------*/
\r
1272 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1274 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1276 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1277 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1279 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1281 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1284 /*-----------------------------------------------------------*/
\r
1286 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1288 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1290 /* The lock counts contains the number of extra data items placed or
\r
1291 removed from the queue while the queue was locked. When a queue is
\r
1292 locked items can be added or removed, but the event lists cannot be
\r
1294 taskENTER_CRITICAL();
\r
1296 /* See if data was added to the queue while it was locked. */
\r
1297 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1299 /* Data was posted while the queue was locked. Are any tasks
\r
1300 blocked waiting for data to become available? */
\r
1301 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1303 /* Tasks that are removed from the event list will get added to
\r
1304 the pending ready list as the scheduler is still suspended. */
\r
1305 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1307 /* The task waiting has a higher priority so record that a
\r
1308 context switch is required. */
\r
1309 vTaskMissedYield();
\r
1312 --( pxQueue->xTxLock );
\r
1320 pxQueue->xTxLock = queueUNLOCKED;
\r
1322 taskEXIT_CRITICAL();
\r
1324 /* Do the same for the Rx lock. */
\r
1325 taskENTER_CRITICAL();
\r
1327 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1329 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1331 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1333 vTaskMissedYield();
\r
1336 --( pxQueue->xRxLock );
\r
1344 pxQueue->xRxLock = queueUNLOCKED;
\r
1346 taskEXIT_CRITICAL();
\r
1348 /*-----------------------------------------------------------*/
\r
1350 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1352 signed portBASE_TYPE xReturn;
\r
1354 taskENTER_CRITICAL();
\r
1355 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1356 taskEXIT_CRITICAL();
\r
1360 /*-----------------------------------------------------------*/
\r
1362 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1364 signed portBASE_TYPE xReturn;
\r
1366 configASSERT( pxQueue );
\r
1367 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1371 /*-----------------------------------------------------------*/
\r
1373 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1375 signed portBASE_TYPE xReturn;
\r
1377 taskENTER_CRITICAL();
\r
1378 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1379 taskEXIT_CRITICAL();
\r
1383 /*-----------------------------------------------------------*/
\r
1385 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1387 signed portBASE_TYPE xReturn;
\r
1389 configASSERT( pxQueue );
\r
1390 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1394 /*-----------------------------------------------------------*/
\r
1396 #if configUSE_CO_ROUTINES == 1
\r
1397 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1399 signed portBASE_TYPE xReturn;
\r
1401 /* If the queue is already full we may have to block. A critical section
\r
1402 is required to prevent an interrupt removing something from the queue
\r
1403 between the check to see if the queue is full and blocking on the queue. */
\r
1404 portDISABLE_INTERRUPTS();
\r
1406 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1408 /* The queue is full - do we want to block or just leave without
\r
1410 if( xTicksToWait > ( portTickType ) 0 )
\r
1412 /* As this is called from a coroutine we cannot block directly, but
\r
1413 return indicating that we need to block. */
\r
1414 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1415 portENABLE_INTERRUPTS();
\r
1416 return errQUEUE_BLOCKED;
\r
1420 portENABLE_INTERRUPTS();
\r
1421 return errQUEUE_FULL;
\r
1425 portENABLE_INTERRUPTS();
\r
1429 portDISABLE_INTERRUPTS();
\r
1431 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1433 /* There is room in the queue, copy the data into the queue. */
\r
1434 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1437 /* Were any co-routines waiting for data to become available? */
\r
1438 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1440 /* In this instance the co-routine could be placed directly
\r
1441 into the ready list as we are within a critical section.
\r
1442 Instead the same pending ready list mechanism is used as if
\r
1443 the event were caused from within an interrupt. */
\r
1444 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1446 /* The co-routine waiting has a higher priority so record
\r
1447 that a yield might be appropriate. */
\r
1448 xReturn = errQUEUE_YIELD;
\r
1454 xReturn = errQUEUE_FULL;
\r
1457 portENABLE_INTERRUPTS();
\r
1462 /*-----------------------------------------------------------*/
\r
1464 #if configUSE_CO_ROUTINES == 1
\r
1465 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1467 signed portBASE_TYPE xReturn;
\r
1469 /* If the queue is already empty we may have to block. A critical section
\r
1470 is required to prevent an interrupt adding something to the queue
\r
1471 between the check to see if the queue is empty and blocking on the queue. */
\r
1472 portDISABLE_INTERRUPTS();
\r
1474 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1476 /* There are no messages in the queue, do we want to block or just
\r
1477 leave with nothing? */
\r
1478 if( xTicksToWait > ( portTickType ) 0 )
\r
1480 /* As this is a co-routine we cannot block directly, but return
\r
1481 indicating that we need to block. */
\r
1482 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1483 portENABLE_INTERRUPTS();
\r
1484 return errQUEUE_BLOCKED;
\r
1488 portENABLE_INTERRUPTS();
\r
1489 return errQUEUE_FULL;
\r
1493 portENABLE_INTERRUPTS();
\r
1497 portDISABLE_INTERRUPTS();
\r
1499 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1501 /* Data is available from the queue. */
\r
1502 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1503 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1505 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1507 --( pxQueue->uxMessagesWaiting );
\r
1508 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1512 /* Were any co-routines waiting for space to become available? */
\r
1513 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1515 /* In this instance the co-routine could be placed directly
\r
1516 into the ready list as we are within a critical section.
\r
1517 Instead the same pending ready list mechanism is used as if
\r
1518 the event were caused from within an interrupt. */
\r
1519 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1521 xReturn = errQUEUE_YIELD;
\r
1530 portENABLE_INTERRUPTS();
\r
1535 /*-----------------------------------------------------------*/
\r
1539 #if configUSE_CO_ROUTINES == 1
\r
1540 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1542 /* Cannot block within an ISR so if there is no space on the queue then
\r
1543 exit without doing anything. */
\r
1544 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1546 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1548 /* We only want to wake one co-routine per ISR, so check that a
\r
1549 co-routine has not already been woken. */
\r
1550 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1552 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1554 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1562 return xCoRoutinePreviouslyWoken;
\r
1565 /*-----------------------------------------------------------*/
\r
1567 #if configUSE_CO_ROUTINES == 1
\r
1568 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1570 signed portBASE_TYPE xReturn;
\r
1572 /* We cannot block from an ISR, so check there is data available. If
\r
1573 not then just leave without doing anything. */
\r
1574 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1576 /* Copy the data from the queue. */
\r
1577 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1578 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1580 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1582 --( pxQueue->uxMessagesWaiting );
\r
1583 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1585 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1587 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1589 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1591 *pxCoRoutineWoken = pdTRUE;
\r
1606 /*-----------------------------------------------------------*/
\r
1608 #if configQUEUE_REGISTRY_SIZE > 0
\r
1610 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1612 unsigned portBASE_TYPE ux;
\r
1614 /* See if there is an empty space in the registry. A NULL name denotes
\r
1616 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1618 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1620 /* Store the information on this queue. */
\r
1621 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1622 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1629 /*-----------------------------------------------------------*/
\r
1631 #if configQUEUE_REGISTRY_SIZE > 0
\r
1633 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1635 unsigned portBASE_TYPE ux;
\r
1637 /* See if the handle of the queue being unregistered in actually in the
\r
1639 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1641 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1643 /* Set the name to NULL to show that this slot if free again. */
\r
1644 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1652 /*-----------------------------------------------------------*/
\r
1654 #if configUSE_TIMERS == 1
\r
1656 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1658 /* This function should not be called by application code hence the
\r
1659 'Restricted' in its name. It is not part of the public API. It is
\r
1660 designed for use by kernel code, and has special calling requirements.
\r
1661 It can result in vListInsert() being called on a list that can only
\r
1662 possibly ever have one item in it, so the list will be fast, but even
\r
1663 so it should be called with the scheduler locked and not from a critical
\r
1666 /* Only do anything if there are no messages in the queue. This function
\r
1667 will not actually cause the task to block, just place it on a blocked
\r
1668 list. It will not block until the scheduler is unlocked - at which
\r
1669 time a yield will be performed. If an item is added to the queue while
\r
1670 the queue is locked, and the calling task blocks on the queue, then the
\r
1671 calling task will be immediately unblocked when the queue is unlocked. */
\r
1672 prvLockQueue( pxQueue );
\r
1673 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1675 /* There is nothing in the queue, block for the specified period. */
\r
1676 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1678 prvUnlockQueue( pxQueue );
\r