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 cTxLock 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 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 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 xQueue, 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 *pxTaskWoken ) 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 portBASE_TYPE xReturn = pdPASS;
\r
283 configASSERT( pxQueue );
\r
285 /* If the queue being reset has already been used (has not just been
\r
286 created), then only reset the queue if its event lists are empty. */
\r
287 if( xNewQueue != pdTRUE )
\r
289 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
294 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
300 if( xReturn == pdPASS )
\r
302 pxQueue->pcTail = pxQueue->pcHead + ( pxQueue->uxLength * pxQueue->uxItemSize );
\r
303 pxQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
304 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
305 pxQueue->pcReadFrom = pxQueue->pcHead + ( ( pxQueue->uxLength - ( unsigned portBASE_TYPE ) 1U ) * pxQueue->uxItemSize );
\r
306 pxQueue->xRxLock = queueUNLOCKED;
\r
307 pxQueue->xTxLock = queueUNLOCKED;
\r
309 /* Ensure the event queues start with the correct state. */
\r
310 vListInitialise( &( pxQueue->xTasksWaitingToSend ) );
\r
311 vListInitialise( &( pxQueue->xTasksWaitingToReceive ) );
\r
316 /*-----------------------------------------------------------*/
\r
318 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )
\r
320 xQUEUE *pxNewQueue;
\r
321 size_t xQueueSizeInBytes;
\r
322 xQueueHandle xReturn = NULL;
\r
324 /* Remove compiler warnings about unused parameters should
\r
325 configUSE_TRACE_FACILITY not be set to 1. */
\r
326 ( void ) ucQueueType;
\r
328 /* Allocate the new queue structure. */
\r
329 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
331 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
332 if( pxNewQueue != NULL )
\r
334 /* Create the list of pointers to queue items. The queue is one byte
\r
335 longer than asked for to make wrap checking easier/faster. */
\r
336 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
338 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
339 if( pxNewQueue->pcHead != NULL )
\r
341 /* Initialise the queue members as described above where the
\r
342 queue type is defined. */
\r
343 pxNewQueue->uxLength = uxQueueLength;
\r
344 pxNewQueue->uxItemSize = uxItemSize;
\r
345 xQueueGenericReset( pxNewQueue, pdTRUE );
\r
346 #if ( configUSE_TRACE_FACILITY == 1 )
\r
348 pxNewQueue->ucQueueType = ucQueueType;
\r
350 #endif /* configUSE_TRACE_FACILITY */
\r
352 traceQUEUE_CREATE( pxNewQueue );
\r
353 xReturn = pxNewQueue;
\r
357 traceQUEUE_CREATE_FAILED( ucQueueType );
\r
358 vPortFree( pxNewQueue );
\r
363 configASSERT( xReturn );
\r
367 /*-----------------------------------------------------------*/
\r
369 #if ( configUSE_MUTEXES == 1 )
\r
371 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )
\r
373 xQUEUE *pxNewQueue;
\r
375 /* Prevent compiler warnings about unused parameters if
\r
376 configUSE_TRACE_FACILITY does not equal 1. */
\r
377 ( void ) ucQueueType;
\r
379 /* Allocate the new queue structure. */
\r
380 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
381 if( pxNewQueue != NULL )
\r
383 /* Information required for priority inheritance. */
\r
384 pxNewQueue->pxMutexHolder = NULL;
\r
385 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
387 /* Queues used as a mutex no data is actually copied into or out
\r
389 pxNewQueue->pcWriteTo = NULL;
\r
390 pxNewQueue->pcReadFrom = NULL;
\r
392 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
393 an item size of 0 as nothing is actually copied into or out
\r
395 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
396 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
397 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
398 pxNewQueue->xRxLock = queueUNLOCKED;
\r
399 pxNewQueue->xTxLock = queueUNLOCKED;
\r
401 #if ( configUSE_TRACE_FACILITY == 1 )
\r
403 pxNewQueue->ucQueueType = ucQueueType;
\r
407 /* Ensure the event queues start with the correct state. */
\r
408 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
409 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
411 traceCREATE_MUTEX( pxNewQueue );
\r
413 /* Start with the semaphore in the expected state. */
\r
414 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
418 traceCREATE_MUTEX_FAILED();
\r
421 configASSERT( pxNewQueue );
\r
425 #endif /* configUSE_MUTEXES */
\r
426 /*-----------------------------------------------------------*/
\r
428 #if ( configUSE_MUTEXES == 1 )
\r
430 void* xQueueGetMutexHolder( xQueueHandle xSemaphore )
\r
434 /* This function is called by xSemaphoreGetMutexHolder(), and should not
\r
435 be called directly. Note: This is is a good way of determining if the
\r
436 calling task is the mutex holder, but not a good way of determining the
\r
437 identity of the mutex holder, as the holder may change between the
\r
438 following critical section exiting and the function returning. */
\r
439 taskENTER_CRITICAL();
\r
441 if( xSemaphore->uxQueueType == queueQUEUE_IS_MUTEX )
\r
443 pxReturn = ( void * ) xSemaphore->pxMutexHolder;
\r
450 taskEXIT_CRITICAL();
\r
456 /*-----------------------------------------------------------*/
\r
458 #if ( configUSE_RECURSIVE_MUTEXES == 1 )
\r
460 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
462 portBASE_TYPE xReturn;
\r
464 configASSERT( pxMutex );
\r
466 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
467 change outside of this task. If this task does not hold the mutex then
\r
468 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
469 this is the only condition we are interested in it does not matter if
\r
470 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
471 mutual exclusion is required to test the pxMutexHolder variable. */
\r
472 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
474 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
476 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
477 the task handle, therefore no underflow check is required. Also,
\r
478 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
479 there can only be one, no mutual exclusion is required to modify the
\r
480 uxRecursiveCallCount member. */
\r
481 ( pxMutex->uxRecursiveCallCount )--;
\r
483 /* Have we unwound the call count? */
\r
484 if( pxMutex->uxRecursiveCallCount == 0 )
\r
486 /* Return the mutex. This will automatically unblock any other
\r
487 task that might be waiting to access the mutex. */
\r
488 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
495 /* We cannot give the mutex because we are not the holder. */
\r
498 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
504 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
505 /*-----------------------------------------------------------*/
\r
507 #if configUSE_RECURSIVE_MUTEXES == 1
\r
509 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
511 portBASE_TYPE xReturn;
\r
513 configASSERT( pxMutex );
\r
515 /* Comments regarding mutual exclusion as per those within
\r
516 xQueueGiveMutexRecursive(). */
\r
518 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
520 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
522 ( pxMutex->uxRecursiveCallCount )++;
\r
527 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
529 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
530 we may have blocked to reach here. */
\r
531 if( xReturn == pdPASS )
\r
533 ( pxMutex->uxRecursiveCallCount )++;
\r
537 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
544 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
545 /*-----------------------------------------------------------*/
\r
547 #if configUSE_COUNTING_SEMAPHORES == 1
\r
549 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
551 xQueueHandle pxHandle;
\r
553 pxHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
555 if( pxHandle != NULL )
\r
557 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
559 traceCREATE_COUNTING_SEMAPHORE();
\r
563 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
566 configASSERT( pxHandle );
\r
570 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
571 /*-----------------------------------------------------------*/
\r
573 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
575 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
576 xTimeOutType xTimeOut;
\r
578 configASSERT( pxQueue );
\r
579 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
581 /* This function relaxes the coding standard somewhat to allow return
\r
582 statements within the function itself. This is done in the interest
\r
583 of execution time efficiency. */
\r
586 taskENTER_CRITICAL();
\r
588 /* Is there room on the queue now? To be running we must be
\r
589 the highest priority task wanting to access the queue. */
\r
590 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
592 traceQUEUE_SEND( pxQueue );
\r
593 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
595 /* If there was a task waiting for data to arrive on the
\r
596 queue then unblock it now. */
\r
597 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
599 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
601 /* The unblocked task has a priority higher than
\r
602 our own so yield immediately. Yes it is ok to do
\r
603 this from within the critical section - the kernel
\r
604 takes care of that. */
\r
605 portYIELD_WITHIN_API();
\r
609 taskEXIT_CRITICAL();
\r
611 /* Return to the original privilege level before exiting the
\r
617 if( xTicksToWait == ( portTickType ) 0 )
\r
619 /* The queue was full and no block time is specified (or
\r
620 the block time has expired) so leave now. */
\r
621 taskEXIT_CRITICAL();
\r
623 /* Return to the original privilege level before exiting
\r
625 traceQUEUE_SEND_FAILED( pxQueue );
\r
626 return errQUEUE_FULL;
\r
628 else if( xEntryTimeSet == pdFALSE )
\r
630 /* The queue was full and a block time was specified so
\r
631 configure the timeout structure. */
\r
632 vTaskSetTimeOutState( &xTimeOut );
\r
633 xEntryTimeSet = pdTRUE;
\r
637 taskEXIT_CRITICAL();
\r
639 /* Interrupts and other tasks can send to and receive from the queue
\r
640 now the critical section has been exited. */
\r
643 prvLockQueue( pxQueue );
\r
645 /* Update the timeout state to see if it has expired yet. */
\r
646 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
648 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
650 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
651 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
653 /* Unlocking the queue means queue events can effect the
\r
654 event list. It is possible that interrupts occurring now
\r
655 remove this task from the event list again - but as the
\r
656 scheduler is suspended the task will go onto the pending
\r
657 ready last instead of the actual ready list. */
\r
658 prvUnlockQueue( pxQueue );
\r
660 /* Resuming the scheduler will move tasks from the pending
\r
661 ready list into the ready list - so it is feasible that this
\r
662 task is already in a ready list before it yields - in which
\r
663 case the yield will not cause a context switch unless there
\r
664 is also a higher priority task in the pending ready list. */
\r
665 if( xTaskResumeAll() == pdFALSE )
\r
667 portYIELD_WITHIN_API();
\r
673 prvUnlockQueue( pxQueue );
\r
674 ( void ) xTaskResumeAll();
\r
679 /* The timeout has expired. */
\r
680 prvUnlockQueue( pxQueue );
\r
681 ( void ) xTaskResumeAll();
\r
683 /* Return to the original privilege level before exiting the
\r
685 traceQUEUE_SEND_FAILED( pxQueue );
\r
686 return errQUEUE_FULL;
\r
690 /*-----------------------------------------------------------*/
\r
692 #if configUSE_ALTERNATIVE_API == 1
\r
694 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
696 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
697 xTimeOutType xTimeOut;
\r
699 configASSERT( pxQueue );
\r
700 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
704 taskENTER_CRITICAL();
\r
706 /* Is there room on the queue now? To be running we must be
\r
707 the highest priority task wanting to access the queue. */
\r
708 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
710 traceQUEUE_SEND( pxQueue );
\r
711 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
713 /* If there was a task waiting for data to arrive on the
\r
714 queue then unblock it now. */
\r
715 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
717 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
719 /* The unblocked task has a priority higher than
\r
720 our own so yield immediately. */
\r
721 portYIELD_WITHIN_API();
\r
725 taskEXIT_CRITICAL();
\r
730 if( xTicksToWait == ( portTickType ) 0 )
\r
732 taskEXIT_CRITICAL();
\r
733 return errQUEUE_FULL;
\r
735 else if( xEntryTimeSet == pdFALSE )
\r
737 vTaskSetTimeOutState( &xTimeOut );
\r
738 xEntryTimeSet = pdTRUE;
\r
742 taskEXIT_CRITICAL();
\r
744 taskENTER_CRITICAL();
\r
746 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
748 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
750 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
751 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
752 portYIELD_WITHIN_API();
\r
757 taskEXIT_CRITICAL();
\r
758 traceQUEUE_SEND_FAILED( pxQueue );
\r
759 return errQUEUE_FULL;
\r
762 taskEXIT_CRITICAL();
\r
766 #endif /* configUSE_ALTERNATIVE_API */
\r
767 /*-----------------------------------------------------------*/
\r
769 #if configUSE_ALTERNATIVE_API == 1
\r
771 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
773 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
774 xTimeOutType xTimeOut;
\r
775 signed char *pcOriginalReadPosition;
\r
777 configASSERT( pxQueue );
\r
778 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
782 taskENTER_CRITICAL();
\r
784 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
786 /* Remember our read position in case we are just peeking. */
\r
787 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
789 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
791 if( xJustPeeking == pdFALSE )
\r
793 traceQUEUE_RECEIVE( pxQueue );
\r
795 /* We are actually removing data. */
\r
796 --( pxQueue->uxMessagesWaiting );
\r
798 #if ( configUSE_MUTEXES == 1 )
\r
800 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
802 /* Record the information required to implement
\r
803 priority inheritance should it become necessary. */
\r
804 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
809 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
811 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
813 portYIELD_WITHIN_API();
\r
819 traceQUEUE_PEEK( pxQueue );
\r
821 /* We are not removing the data, so reset our read
\r
823 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
825 /* The data is being left in the queue, so see if there are
\r
826 any other tasks waiting for the data. */
\r
827 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
829 /* Tasks that are removed from the event list will get added to
\r
830 the pending ready list as the scheduler is still suspended. */
\r
831 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
833 /* The task waiting has a higher priority than this task. */
\r
834 portYIELD_WITHIN_API();
\r
840 taskEXIT_CRITICAL();
\r
845 if( xTicksToWait == ( portTickType ) 0 )
\r
847 taskEXIT_CRITICAL();
\r
848 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
849 return errQUEUE_EMPTY;
\r
851 else if( xEntryTimeSet == pdFALSE )
\r
853 vTaskSetTimeOutState( &xTimeOut );
\r
854 xEntryTimeSet = pdTRUE;
\r
858 taskEXIT_CRITICAL();
\r
860 taskENTER_CRITICAL();
\r
862 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
864 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
866 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
868 #if ( configUSE_MUTEXES == 1 )
\r
870 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
872 portENTER_CRITICAL();
\r
873 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
874 portEXIT_CRITICAL();
\r
879 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
880 portYIELD_WITHIN_API();
\r
885 taskEXIT_CRITICAL();
\r
886 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
887 return errQUEUE_EMPTY;
\r
890 taskEXIT_CRITICAL();
\r
895 #endif /* configUSE_ALTERNATIVE_API */
\r
896 /*-----------------------------------------------------------*/
\r
898 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
900 signed portBASE_TYPE xReturn;
\r
901 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
903 configASSERT( pxQueue );
\r
904 configASSERT( pxHigherPriorityTaskWoken );
\r
905 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
907 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
908 in the queue. Also we don't directly wake a task that was blocked on a
\r
909 queue read, instead we return a flag to say whether a context switch is
\r
910 required or not (i.e. has a task with a higher priority than us been woken
\r
912 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
914 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
916 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
918 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
920 /* If the queue is locked we do not alter the event list. This will
\r
921 be done when the queue is unlocked later. */
\r
922 if( pxQueue->xTxLock == queueUNLOCKED )
\r
924 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
926 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
928 /* The task waiting has a higher priority so record that a
\r
929 context switch is required. */
\r
930 *pxHigherPriorityTaskWoken = pdTRUE;
\r
936 /* Increment the lock count so the task that unlocks the queue
\r
937 knows that data was posted while it was locked. */
\r
938 ++( pxQueue->xTxLock );
\r
945 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
946 xReturn = errQUEUE_FULL;
\r
949 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
953 /*-----------------------------------------------------------*/
\r
955 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
957 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
958 xTimeOutType xTimeOut;
\r
959 signed char *pcOriginalReadPosition;
\r
961 configASSERT( pxQueue );
\r
962 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
964 /* This function relaxes the coding standard somewhat to allow return
\r
965 statements within the function itself. This is done in the interest
\r
966 of execution time efficiency. */
\r
970 taskENTER_CRITICAL();
\r
972 /* Is there data in the queue now? To be running we must be
\r
973 the highest priority task wanting to access the queue. */
\r
974 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
976 /* Remember our read position in case we are just peeking. */
\r
977 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
979 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
981 if( xJustPeeking == pdFALSE )
\r
983 traceQUEUE_RECEIVE( pxQueue );
\r
985 /* We are actually removing data. */
\r
986 --( pxQueue->uxMessagesWaiting );
\r
988 #if ( configUSE_MUTEXES == 1 )
\r
990 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
992 /* Record the information required to implement
\r
993 priority inheritance should it become necessary. */
\r
994 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
999 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1001 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
1003 portYIELD_WITHIN_API();
\r
1009 traceQUEUE_PEEK( pxQueue );
\r
1011 /* We are not removing the data, so reset our read
\r
1013 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
1015 /* The data is being left in the queue, so see if there are
\r
1016 any other tasks waiting for the data. */
\r
1017 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1019 /* Tasks that are removed from the event list will get added to
\r
1020 the pending ready list as the scheduler is still suspended. */
\r
1021 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1023 /* The task waiting has a higher priority than this task. */
\r
1024 portYIELD_WITHIN_API();
\r
1030 taskEXIT_CRITICAL();
\r
1035 if( xTicksToWait == ( portTickType ) 0 )
\r
1037 /* The queue was empty and no block time is specified (or
\r
1038 the block time has expired) so leave now. */
\r
1039 taskEXIT_CRITICAL();
\r
1040 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1041 return errQUEUE_EMPTY;
\r
1043 else if( xEntryTimeSet == pdFALSE )
\r
1045 /* The queue was empty and a block time was specified so
\r
1046 configure the timeout structure. */
\r
1047 vTaskSetTimeOutState( &xTimeOut );
\r
1048 xEntryTimeSet = pdTRUE;
\r
1052 taskEXIT_CRITICAL();
\r
1054 /* Interrupts and other tasks can send to and receive from the queue
\r
1055 now the critical section has been exited. */
\r
1057 vTaskSuspendAll();
\r
1058 prvLockQueue( pxQueue );
\r
1060 /* Update the timeout state to see if it has expired yet. */
\r
1061 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
1063 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
1065 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
1067 #if ( configUSE_MUTEXES == 1 )
\r
1069 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1071 portENTER_CRITICAL();
\r
1073 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
1075 portEXIT_CRITICAL();
\r
1080 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1081 prvUnlockQueue( pxQueue );
\r
1082 if( xTaskResumeAll() == pdFALSE )
\r
1084 portYIELD_WITHIN_API();
\r
1090 prvUnlockQueue( pxQueue );
\r
1091 ( void ) xTaskResumeAll();
\r
1096 prvUnlockQueue( pxQueue );
\r
1097 ( void ) xTaskResumeAll();
\r
1098 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1099 return errQUEUE_EMPTY;
\r
1103 /*-----------------------------------------------------------*/
\r
1105 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1107 signed portBASE_TYPE xReturn;
\r
1108 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1110 configASSERT( pxQueue );
\r
1111 configASSERT( pxTaskWoken );
\r
1112 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1114 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1116 /* We cannot block from an ISR, so check there is data available. */
\r
1117 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1119 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1121 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1122 --( pxQueue->uxMessagesWaiting );
\r
1124 /* If the queue is locked we will not modify the event list. Instead
\r
1125 we update the lock count so the task that unlocks the queue will know
\r
1126 that an ISR has removed data while the queue was locked. */
\r
1127 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1129 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1131 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1133 /* The task waiting has a higher priority than us so
\r
1134 force a context switch. */
\r
1135 *pxTaskWoken = pdTRUE;
\r
1141 /* Increment the lock count so the task that unlocks the queue
\r
1142 knows that data was removed while it was locked. */
\r
1143 ++( pxQueue->xRxLock );
\r
1151 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1154 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1158 /*-----------------------------------------------------------*/
\r
1160 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1162 unsigned portBASE_TYPE uxReturn;
\r
1164 configASSERT( pxQueue );
\r
1166 taskENTER_CRITICAL();
\r
1167 uxReturn = pxQueue->uxMessagesWaiting;
\r
1168 taskEXIT_CRITICAL();
\r
1172 /*-----------------------------------------------------------*/
\r
1174 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1176 unsigned portBASE_TYPE uxReturn;
\r
1178 configASSERT( pxQueue );
\r
1180 uxReturn = pxQueue->uxMessagesWaiting;
\r
1184 /*-----------------------------------------------------------*/
\r
1186 void vQueueDelete( xQueueHandle pxQueue )
\r
1188 configASSERT( pxQueue );
\r
1190 traceQUEUE_DELETE( pxQueue );
\r
1191 vQueueUnregisterQueue( pxQueue );
\r
1192 vPortFree( pxQueue->pcHead );
\r
1193 vPortFree( pxQueue );
\r
1195 /*-----------------------------------------------------------*/
\r
1197 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1199 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )
\r
1201 return pxQueue->ucQueueNumber;
\r
1205 /*-----------------------------------------------------------*/
\r
1207 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1209 void vQueueSetQueueNumber( xQueueHandle pxQueue, unsigned char ucQueueNumber )
\r
1211 pxQueue->ucQueueNumber = ucQueueNumber;
\r
1215 /*-----------------------------------------------------------*/
\r
1217 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1219 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )
\r
1221 return pxQueue->ucQueueType;
\r
1225 /*-----------------------------------------------------------*/
\r
1227 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1229 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1231 #if ( configUSE_MUTEXES == 1 )
\r
1233 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1235 /* The mutex is no longer being held. */
\r
1236 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1237 pxQueue->pxMutexHolder = NULL;
\r
1242 else if( xPosition == queueSEND_TO_BACK )
\r
1244 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1245 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1246 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1248 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1253 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1254 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1255 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1257 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1261 ++( pxQueue->uxMessagesWaiting );
\r
1263 /*-----------------------------------------------------------*/
\r
1265 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1267 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1269 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1270 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1272 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1274 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1277 /*-----------------------------------------------------------*/
\r
1279 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1281 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1283 /* The lock counts contains the number of extra data items placed or
\r
1284 removed from the queue while the queue was locked. When a queue is
\r
1285 locked items can be added or removed, but the event lists cannot be
\r
1287 taskENTER_CRITICAL();
\r
1289 /* See if data was added to the queue while it was locked. */
\r
1290 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1292 /* Data was posted while the queue was locked. Are any tasks
\r
1293 blocked waiting for data to become available? */
\r
1294 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1296 /* Tasks that are removed from the event list will get added to
\r
1297 the pending ready list as the scheduler is still suspended. */
\r
1298 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1300 /* The task waiting has a higher priority so record that a
\r
1301 context switch is required. */
\r
1302 vTaskMissedYield();
\r
1305 --( pxQueue->xTxLock );
\r
1313 pxQueue->xTxLock = queueUNLOCKED;
\r
1315 taskEXIT_CRITICAL();
\r
1317 /* Do the same for the Rx lock. */
\r
1318 taskENTER_CRITICAL();
\r
1320 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1322 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1324 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1326 vTaskMissedYield();
\r
1329 --( pxQueue->xRxLock );
\r
1337 pxQueue->xRxLock = queueUNLOCKED;
\r
1339 taskEXIT_CRITICAL();
\r
1341 /*-----------------------------------------------------------*/
\r
1343 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1345 signed portBASE_TYPE xReturn;
\r
1347 taskENTER_CRITICAL();
\r
1348 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1349 taskEXIT_CRITICAL();
\r
1353 /*-----------------------------------------------------------*/
\r
1355 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1357 signed portBASE_TYPE xReturn;
\r
1359 configASSERT( pxQueue );
\r
1360 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1364 /*-----------------------------------------------------------*/
\r
1366 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1368 signed portBASE_TYPE xReturn;
\r
1370 taskENTER_CRITICAL();
\r
1371 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1372 taskEXIT_CRITICAL();
\r
1376 /*-----------------------------------------------------------*/
\r
1378 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1380 signed portBASE_TYPE xReturn;
\r
1382 configASSERT( pxQueue );
\r
1383 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1387 /*-----------------------------------------------------------*/
\r
1389 #if configUSE_CO_ROUTINES == 1
\r
1390 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1392 signed portBASE_TYPE xReturn;
\r
1394 /* If the queue is already full we may have to block. A critical section
\r
1395 is required to prevent an interrupt removing something from the queue
\r
1396 between the check to see if the queue is full and blocking on the queue. */
\r
1397 portDISABLE_INTERRUPTS();
\r
1399 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1401 /* The queue is full - do we want to block or just leave without
\r
1403 if( xTicksToWait > ( portTickType ) 0 )
\r
1405 /* As this is called from a coroutine we cannot block directly, but
\r
1406 return indicating that we need to block. */
\r
1407 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1408 portENABLE_INTERRUPTS();
\r
1409 return errQUEUE_BLOCKED;
\r
1413 portENABLE_INTERRUPTS();
\r
1414 return errQUEUE_FULL;
\r
1418 portENABLE_INTERRUPTS();
\r
1422 portDISABLE_INTERRUPTS();
\r
1424 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1426 /* There is room in the queue, copy the data into the queue. */
\r
1427 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1430 /* Were any co-routines waiting for data to become available? */
\r
1431 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1433 /* In this instance the co-routine could be placed directly
\r
1434 into the ready list as we are within a critical section.
\r
1435 Instead the same pending ready list mechanism is used as if
\r
1436 the event were caused from within an interrupt. */
\r
1437 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1439 /* The co-routine waiting has a higher priority so record
\r
1440 that a yield might be appropriate. */
\r
1441 xReturn = errQUEUE_YIELD;
\r
1447 xReturn = errQUEUE_FULL;
\r
1450 portENABLE_INTERRUPTS();
\r
1455 /*-----------------------------------------------------------*/
\r
1457 #if configUSE_CO_ROUTINES == 1
\r
1458 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1460 signed portBASE_TYPE xReturn;
\r
1462 /* If the queue is already empty we may have to block. A critical section
\r
1463 is required to prevent an interrupt adding something to the queue
\r
1464 between the check to see if the queue is empty and blocking on the queue. */
\r
1465 portDISABLE_INTERRUPTS();
\r
1467 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1469 /* There are no messages in the queue, do we want to block or just
\r
1470 leave with nothing? */
\r
1471 if( xTicksToWait > ( portTickType ) 0 )
\r
1473 /* As this is a co-routine we cannot block directly, but return
\r
1474 indicating that we need to block. */
\r
1475 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1476 portENABLE_INTERRUPTS();
\r
1477 return errQUEUE_BLOCKED;
\r
1481 portENABLE_INTERRUPTS();
\r
1482 return errQUEUE_FULL;
\r
1486 portENABLE_INTERRUPTS();
\r
1490 portDISABLE_INTERRUPTS();
\r
1492 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1494 /* Data is available from the queue. */
\r
1495 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1496 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1498 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1500 --( pxQueue->uxMessagesWaiting );
\r
1501 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1505 /* Were any co-routines waiting for space to become available? */
\r
1506 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1508 /* In this instance the co-routine could be placed directly
\r
1509 into the ready list as we are within a critical section.
\r
1510 Instead the same pending ready list mechanism is used as if
\r
1511 the event were caused from within an interrupt. */
\r
1512 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1514 xReturn = errQUEUE_YIELD;
\r
1523 portENABLE_INTERRUPTS();
\r
1528 /*-----------------------------------------------------------*/
\r
1532 #if configUSE_CO_ROUTINES == 1
\r
1533 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1535 /* Cannot block within an ISR so if there is no space on the queue then
\r
1536 exit without doing anything. */
\r
1537 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1539 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1541 /* We only want to wake one co-routine per ISR, so check that a
\r
1542 co-routine has not already been woken. */
\r
1543 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1545 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1547 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1555 return xCoRoutinePreviouslyWoken;
\r
1558 /*-----------------------------------------------------------*/
\r
1560 #if configUSE_CO_ROUTINES == 1
\r
1561 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1563 signed portBASE_TYPE xReturn;
\r
1565 /* We cannot block from an ISR, so check there is data available. If
\r
1566 not then just leave without doing anything. */
\r
1567 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1569 /* Copy the data from the queue. */
\r
1570 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1571 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1573 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1575 --( pxQueue->uxMessagesWaiting );
\r
1576 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1578 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1580 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1582 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1584 *pxCoRoutineWoken = pdTRUE;
\r
1599 /*-----------------------------------------------------------*/
\r
1601 #if configQUEUE_REGISTRY_SIZE > 0
\r
1603 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1605 unsigned portBASE_TYPE ux;
\r
1607 /* See if there is an empty space in the registry. A NULL name denotes
\r
1609 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1611 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1613 /* Store the information on this queue. */
\r
1614 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1615 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1622 /*-----------------------------------------------------------*/
\r
1624 #if configQUEUE_REGISTRY_SIZE > 0
\r
1626 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1628 unsigned portBASE_TYPE ux;
\r
1630 /* See if the handle of the queue being unregistered in actually in the
\r
1632 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1634 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1636 /* Set the name to NULL to show that this slot if free again. */
\r
1637 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1645 /*-----------------------------------------------------------*/
\r
1647 #if configUSE_TIMERS == 1
\r
1649 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1651 /* This function should not be called by application code hence the
\r
1652 'Restricted' in its name. It is not part of the public API. It is
\r
1653 designed for use by kernel code, and has special calling requirements.
\r
1654 It can result in vListInsert() being called on a list that can only
\r
1655 possibly ever have one item in it, so the list will be fast, but even
\r
1656 so it should be called with the scheduler locked and not from a critical
\r
1659 /* Only do anything if there are no messages in the queue. This function
\r
1660 will not actually cause the task to block, just place it on a blocked
\r
1661 list. It will not block until the scheduler is unlocked - at which
\r
1662 time a yield will be performed. If an item is added to the queue while
\r
1663 the queue is locked, and the calling task blocks on the queue, then the
\r
1664 calling task will be immediately unblocked when the queue is unlocked. */
\r
1665 prvLockQueue( pxQueue );
\r
1666 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1668 /* There is nothing in the queue, block for the specified period. */
\r
1669 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1671 prvUnlockQueue( pxQueue );
\r