2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
58 all the API functions to use the MPU wrappers. That should only be done when
\r
59 task.h is included from an application file. */
\r
60 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
62 #include "FreeRTOS.h"
\r
65 #if ( configUSE_CO_ROUTINES == 1 )
\r
66 #include "croutine.h"
\r
69 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
71 /*-----------------------------------------------------------
\r
72 * PUBLIC LIST API documented in list.h
\r
73 *----------------------------------------------------------*/
\r
75 /* Constants used with the cRxLock and cTxLock structure members. */
\r
76 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
77 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
79 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
81 /* For internal use only. */
\r
82 #define queueSEND_TO_BACK ( 0 )
\r
83 #define queueSEND_TO_FRONT ( 1 )
\r
85 /* Effectively make a union out of the xQUEUE structure. */
\r
86 #define pxMutexHolder pcTail
\r
87 #define uxQueueType pcHead
\r
88 #define uxRecursiveCallCount pcReadFrom
\r
89 #define queueQUEUE_IS_MUTEX NULL
\r
91 /* Semaphores do not actually store or copy data, so have an items size of
\r
93 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )
\r
94 #define queueDONT_BLOCK ( ( portTickType ) 0U )
\r
95 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0U )
\r
97 /* These definitions *must* match those in queue.h. */
\r
98 #define queueQUEUE_TYPE_BASE ( 0U )
\r
99 #define queueQUEUE_TYPE_MUTEX ( 1U )
\r
100 #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ( 2U )
\r
101 #define queueQUEUE_TYPE_BINARY_SEMAPHORE ( 3U )
\r
102 #define queueQUEUE_TYPE_RECURSIVE_MUTEX ( 4U )
\r
105 * Definition of the queue used by the scheduler.
\r
106 * Items are queued by copy, not reference.
\r
108 typedef struct QueueDefinition
\r
110 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
111 signed char *pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
\r
113 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
114 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
116 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
117 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
119 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
120 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
121 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
123 signed portBASE_TYPE xRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
124 signed portBASE_TYPE xTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
126 #if ( configUSE_TRACE_FACILITY == 1 )
\r
127 unsigned char ucQueueNumber;
\r
128 unsigned char ucQueueType;
\r
132 /*-----------------------------------------------------------*/
\r
135 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
136 * To keep the definition private the API header file defines it as a
\r
139 typedef xQUEUE * xQueueHandle;
\r
142 * Prototypes for public functions are included here so we don't have to
\r
143 * include the API header file (as it defines xQueueHandle differently). These
\r
144 * functions are documented in the API header file.
\r
146 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
147 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
148 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
149 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
150 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
151 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
152 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
153 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType ) PRIVILEGED_FUNCTION;
\r
154 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
155 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
156 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
157 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
158 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
159 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
160 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
161 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
162 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
163 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
164 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
167 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
168 * an optional component.
\r
170 #if configUSE_CO_ROUTINES == 1
\r
171 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
172 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
173 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
174 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
178 * The queue registry is just a means for kernel aware debuggers to locate
\r
179 * queue structures. It has no other purpose so is an optional component.
\r
181 #if configQUEUE_REGISTRY_SIZE > 0
\r
183 /* The type stored within the queue registry array. This allows a name
\r
184 to be assigned to each queue making kernel aware debugging a little
\r
185 more user friendly. */
\r
186 typedef struct QUEUE_REGISTRY_ITEM
\r
188 signed char *pcQueueName;
\r
189 xQueueHandle xHandle;
\r
190 } xQueueRegistryItem;
\r
192 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
193 The pcQueueName member of a structure being NULL is indicative of the
\r
194 array position being vacant. */
\r
195 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
197 /* Removes a queue from the registry by simply setting the pcQueueName
\r
199 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
200 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
204 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
205 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
206 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
207 * queue is locked it will instead increment the appropriate queue lock count
\r
208 * to indicate that a task may require unblocking. When the queue in unlocked
\r
209 * these lock counts are inspected, and the appropriate action taken.
\r
211 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
214 * Uses a critical section to determine if there is any data in a queue.
\r
216 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
218 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
221 * Uses a critical section to determine if there is any space in a queue.
\r
223 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
225 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
228 * Copies an item into the queue, either at the front of the queue or the
\r
229 * back of the queue.
\r
231 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
234 * Copies an item out of a queue.
\r
236 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
237 /*-----------------------------------------------------------*/
\r
240 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
241 * accessing the queue event lists.
\r
243 #define prvLockQueue( pxQueue ) \
\r
244 taskENTER_CRITICAL(); \
\r
246 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
248 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
250 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
252 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
255 taskEXIT_CRITICAL()
\r
256 /*-----------------------------------------------------------*/
\r
259 /*-----------------------------------------------------------
\r
260 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
261 *----------------------------------------------------------*/
\r
263 xQueueHandle xQueueGenericCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize, unsigned char ucQueueType )
\r
265 xQUEUE *pxNewQueue;
\r
266 size_t xQueueSizeInBytes;
\r
267 xQueueHandle xReturn = NULL;
\r
269 /* Remove compiler warnings about unused parameters should
\r
270 configUSE_TRACE_FACILITY not be set to 1. */
\r
271 ( void ) ucQueueType;
\r
273 /* Allocate the new queue structure. */
\r
274 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
276 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
277 if( pxNewQueue != NULL )
\r
279 /* Create the list of pointers to queue items. The queue is one byte
\r
280 longer than asked for to make wrap checking easier/faster. */
\r
281 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
283 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
284 if( pxNewQueue->pcHead != NULL )
\r
286 /* Initialise the queue members as described above where the
\r
287 queue type is defined. */
\r
288 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
289 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
290 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
291 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - ( unsigned portBASE_TYPE ) 1U ) * uxItemSize );
\r
292 pxNewQueue->uxLength = uxQueueLength;
\r
293 pxNewQueue->uxItemSize = uxItemSize;
\r
294 pxNewQueue->xRxLock = queueUNLOCKED;
\r
295 pxNewQueue->xTxLock = queueUNLOCKED;
\r
296 #if ( configUSE_TRACE_FACILITY == 1 )
\r
298 pxNewQueue->ucQueueType = ucQueueType;
\r
300 #endif /* configUSE_TRACE_FACILITY */
\r
302 /* Likewise ensure the event queues start with the correct state. */
\r
303 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
304 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
306 traceQUEUE_CREATE( pxNewQueue );
\r
307 xReturn = pxNewQueue;
\r
311 traceQUEUE_CREATE_FAILED( ucQueueType );
\r
312 vPortFree( pxNewQueue );
\r
317 configASSERT( xReturn );
\r
321 /*-----------------------------------------------------------*/
\r
323 #if ( configUSE_MUTEXES == 1 )
\r
325 xQueueHandle xQueueCreateMutex( unsigned char ucQueueType )
\r
327 xQUEUE *pxNewQueue;
\r
329 /* Prevent compiler warnings about unused parameters if
\r
330 configUSE_TRACE_FACILITY does not equal 1. */
\r
331 ( void ) ucQueueType;
\r
333 /* Allocate the new queue structure. */
\r
334 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
335 if( pxNewQueue != NULL )
\r
337 /* Information required for priority inheritance. */
\r
338 pxNewQueue->pxMutexHolder = NULL;
\r
339 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
341 /* Queues used as a mutex no data is actually copied into or out
\r
343 pxNewQueue->pcWriteTo = NULL;
\r
344 pxNewQueue->pcReadFrom = NULL;
\r
346 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
347 an item size of 0 as nothing is actually copied into or out
\r
349 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
350 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
351 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
352 pxNewQueue->xRxLock = queueUNLOCKED;
\r
353 pxNewQueue->xTxLock = queueUNLOCKED;
\r
355 #if ( configUSE_TRACE_FACILITY == 1 )
\r
357 pxNewQueue->ucQueueType = ucQueueType;
\r
361 /* Ensure the event queues start with the correct state. */
\r
362 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
363 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
365 traceCREATE_MUTEX( pxNewQueue );
\r
367 /* Start with the semaphore in the expected state. */
\r
368 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
372 traceCREATE_MUTEX_FAILED();
\r
375 configASSERT( pxNewQueue );
\r
379 #endif /* configUSE_MUTEXES */
\r
380 /*-----------------------------------------------------------*/
\r
382 #if configUSE_RECURSIVE_MUTEXES == 1
\r
384 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
386 portBASE_TYPE xReturn;
\r
388 configASSERT( pxMutex );
\r
390 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
391 change outside of this task. If this task does not hold the mutex then
\r
392 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
393 this is the only condition we are interested in it does not matter if
\r
394 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
395 mutual exclusion is required to test the pxMutexHolder variable. */
\r
396 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
398 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
400 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
401 the task handle, therefore no underflow check is required. Also,
\r
402 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
403 there can only be one, no mutual exclusion is required to modify the
\r
404 uxRecursiveCallCount member. */
\r
405 ( pxMutex->uxRecursiveCallCount )--;
\r
407 /* Have we unwound the call count? */
\r
408 if( pxMutex->uxRecursiveCallCount == 0 )
\r
410 /* Return the mutex. This will automatically unblock any other
\r
411 task that might be waiting to access the mutex. */
\r
412 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
419 /* We cannot give the mutex because we are not the holder. */
\r
422 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
428 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
429 /*-----------------------------------------------------------*/
\r
431 #if configUSE_RECURSIVE_MUTEXES == 1
\r
433 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
435 portBASE_TYPE xReturn;
\r
437 configASSERT( pxMutex );
\r
439 /* Comments regarding mutual exclusion as per those within
\r
440 xQueueGiveMutexRecursive(). */
\r
442 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
444 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
446 ( pxMutex->uxRecursiveCallCount )++;
\r
451 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
453 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
454 we may have blocked to reach here. */
\r
455 if( xReturn == pdPASS )
\r
457 ( pxMutex->uxRecursiveCallCount )++;
\r
461 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
468 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
469 /*-----------------------------------------------------------*/
\r
471 #if configUSE_COUNTING_SEMAPHORES == 1
\r
473 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
475 xQueueHandle pxHandle;
\r
477 pxHandle = xQueueGenericCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_COUNTING_SEMAPHORE );
\r
479 if( pxHandle != NULL )
\r
481 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
483 traceCREATE_COUNTING_SEMAPHORE();
\r
487 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
490 configASSERT( pxHandle );
\r
494 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
495 /*-----------------------------------------------------------*/
\r
497 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
499 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
500 xTimeOutType xTimeOut;
\r
502 configASSERT( pxQueue );
\r
503 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
505 /* This function relaxes the coding standard somewhat to allow return
\r
506 statements within the function itself. This is done in the interest
\r
507 of execution time efficiency. */
\r
510 taskENTER_CRITICAL();
\r
512 /* Is there room on the queue now? To be running we must be
\r
513 the highest priority task wanting to access the queue. */
\r
514 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
516 traceQUEUE_SEND( pxQueue );
\r
517 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
519 /* If there was a task waiting for data to arrive on the
\r
520 queue then unblock it now. */
\r
521 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
523 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
525 /* The unblocked task has a priority higher than
\r
526 our own so yield immediately. Yes it is ok to do
\r
527 this from within the critical section - the kernel
\r
528 takes care of that. */
\r
529 portYIELD_WITHIN_API();
\r
533 taskEXIT_CRITICAL();
\r
535 /* Return to the original privilege level before exiting the
\r
541 if( xTicksToWait == ( portTickType ) 0 )
\r
543 /* The queue was full and no block time is specified (or
\r
544 the block time has expired) so leave now. */
\r
545 taskEXIT_CRITICAL();
\r
547 /* Return to the original privilege level before exiting
\r
549 traceQUEUE_SEND_FAILED( pxQueue );
\r
550 return errQUEUE_FULL;
\r
552 else if( xEntryTimeSet == pdFALSE )
\r
554 /* The queue was full and a block time was specified so
\r
555 configure the timeout structure. */
\r
556 vTaskSetTimeOutState( &xTimeOut );
\r
557 xEntryTimeSet = pdTRUE;
\r
561 taskEXIT_CRITICAL();
\r
563 /* Interrupts and other tasks can send to and receive from the queue
\r
564 now the critical section has been exited. */
\r
567 prvLockQueue( pxQueue );
\r
569 /* Update the timeout state to see if it has expired yet. */
\r
570 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
572 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
574 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
575 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
577 /* Unlocking the queue means queue events can effect the
\r
578 event list. It is possible that interrupts occurring now
\r
579 remove this task from the event list again - but as the
\r
580 scheduler is suspended the task will go onto the pending
\r
581 ready last instead of the actual ready list. */
\r
582 prvUnlockQueue( pxQueue );
\r
584 /* Resuming the scheduler will move tasks from the pending
\r
585 ready list into the ready list - so it is feasible that this
\r
586 task is already in a ready list before it yields - in which
\r
587 case the yield will not cause a context switch unless there
\r
588 is also a higher priority task in the pending ready list. */
\r
589 if( xTaskResumeAll() == pdFALSE )
\r
591 portYIELD_WITHIN_API();
\r
597 prvUnlockQueue( pxQueue );
\r
598 ( void ) xTaskResumeAll();
\r
603 /* The timeout has expired. */
\r
604 prvUnlockQueue( pxQueue );
\r
605 ( void ) xTaskResumeAll();
\r
607 /* Return to the original privilege level before exiting the
\r
609 traceQUEUE_SEND_FAILED( pxQueue );
\r
610 return errQUEUE_FULL;
\r
614 /*-----------------------------------------------------------*/
\r
616 #if configUSE_ALTERNATIVE_API == 1
\r
618 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
620 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
621 xTimeOutType xTimeOut;
\r
623 configASSERT( pxQueue );
\r
624 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
628 taskENTER_CRITICAL();
\r
630 /* Is there room on the queue now? To be running we must be
\r
631 the highest priority task wanting to access the queue. */
\r
632 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
634 traceQUEUE_SEND( pxQueue );
\r
635 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
637 /* If there was a task waiting for data to arrive on the
\r
638 queue then unblock it now. */
\r
639 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
641 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
643 /* The unblocked task has a priority higher than
\r
644 our own so yield immediately. */
\r
645 portYIELD_WITHIN_API();
\r
649 taskEXIT_CRITICAL();
\r
654 if( xTicksToWait == ( portTickType ) 0 )
\r
656 taskEXIT_CRITICAL();
\r
657 return errQUEUE_FULL;
\r
659 else if( xEntryTimeSet == pdFALSE )
\r
661 vTaskSetTimeOutState( &xTimeOut );
\r
662 xEntryTimeSet = pdTRUE;
\r
666 taskEXIT_CRITICAL();
\r
668 taskENTER_CRITICAL();
\r
670 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
672 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
674 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
675 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
676 portYIELD_WITHIN_API();
\r
681 taskEXIT_CRITICAL();
\r
682 traceQUEUE_SEND_FAILED( pxQueue );
\r
683 return errQUEUE_FULL;
\r
686 taskEXIT_CRITICAL();
\r
690 #endif /* configUSE_ALTERNATIVE_API */
\r
691 /*-----------------------------------------------------------*/
\r
693 #if configUSE_ALTERNATIVE_API == 1
\r
695 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
697 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
698 xTimeOutType xTimeOut;
\r
699 signed char *pcOriginalReadPosition;
\r
701 configASSERT( pxQueue );
\r
702 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
706 taskENTER_CRITICAL();
\r
708 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
710 /* Remember our read position in case we are just peeking. */
\r
711 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
713 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
715 if( xJustPeeking == pdFALSE )
\r
717 traceQUEUE_RECEIVE( pxQueue );
\r
719 /* We are actually removing data. */
\r
720 --( pxQueue->uxMessagesWaiting );
\r
722 #if ( configUSE_MUTEXES == 1 )
\r
724 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
726 /* Record the information required to implement
\r
727 priority inheritance should it become necessary. */
\r
728 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
733 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
735 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
737 portYIELD_WITHIN_API();
\r
743 traceQUEUE_PEEK( pxQueue );
\r
745 /* We are not removing the data, so reset our read
\r
747 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
749 /* The data is being left in the queue, so see if there are
\r
750 any other tasks waiting for the data. */
\r
751 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
753 /* Tasks that are removed from the event list will get added to
\r
754 the pending ready list as the scheduler is still suspended. */
\r
755 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
757 /* The task waiting has a higher priority than this task. */
\r
758 portYIELD_WITHIN_API();
\r
764 taskEXIT_CRITICAL();
\r
769 if( xTicksToWait == ( portTickType ) 0 )
\r
771 taskEXIT_CRITICAL();
\r
772 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
773 return errQUEUE_EMPTY;
\r
775 else if( xEntryTimeSet == pdFALSE )
\r
777 vTaskSetTimeOutState( &xTimeOut );
\r
778 xEntryTimeSet = pdTRUE;
\r
782 taskEXIT_CRITICAL();
\r
784 taskENTER_CRITICAL();
\r
786 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
788 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
790 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
792 #if ( configUSE_MUTEXES == 1 )
\r
794 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
796 portENTER_CRITICAL();
\r
797 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
798 portEXIT_CRITICAL();
\r
803 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
804 portYIELD_WITHIN_API();
\r
809 taskEXIT_CRITICAL();
\r
810 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
811 return errQUEUE_EMPTY;
\r
814 taskEXIT_CRITICAL();
\r
819 #endif /* configUSE_ALTERNATIVE_API */
\r
820 /*-----------------------------------------------------------*/
\r
822 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
824 signed portBASE_TYPE xReturn;
\r
825 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
827 configASSERT( pxQueue );
\r
828 configASSERT( pxHigherPriorityTaskWoken );
\r
829 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
831 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
832 in the queue. Also we don't directly wake a task that was blocked on a
\r
833 queue read, instead we return a flag to say whether a context switch is
\r
834 required or not (i.e. has a task with a higher priority than us been woken
\r
836 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
838 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
840 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
842 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
844 /* If the queue is locked we do not alter the event list. This will
\r
845 be done when the queue is unlocked later. */
\r
846 if( pxQueue->xTxLock == queueUNLOCKED )
\r
848 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
850 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
852 /* The task waiting has a higher priority so record that a
\r
853 context switch is required. */
\r
854 *pxHigherPriorityTaskWoken = pdTRUE;
\r
860 /* Increment the lock count so the task that unlocks the queue
\r
861 knows that data was posted while it was locked. */
\r
862 ++( pxQueue->xTxLock );
\r
869 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
870 xReturn = errQUEUE_FULL;
\r
873 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
877 /*-----------------------------------------------------------*/
\r
879 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
881 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
882 xTimeOutType xTimeOut;
\r
883 signed char *pcOriginalReadPosition;
\r
885 configASSERT( pxQueue );
\r
886 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
888 /* This function relaxes the coding standard somewhat to allow return
\r
889 statements within the function itself. This is done in the interest
\r
890 of execution time efficiency. */
\r
894 taskENTER_CRITICAL();
\r
896 /* Is there data in the queue now? To be running we must be
\r
897 the highest priority task wanting to access the queue. */
\r
898 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
900 /* Remember our read position in case we are just peeking. */
\r
901 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
903 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
905 if( xJustPeeking == pdFALSE )
\r
907 traceQUEUE_RECEIVE( pxQueue );
\r
909 /* We are actually removing data. */
\r
910 --( pxQueue->uxMessagesWaiting );
\r
912 #if ( configUSE_MUTEXES == 1 )
\r
914 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
916 /* Record the information required to implement
\r
917 priority inheritance should it become necessary. */
\r
918 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
923 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
925 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
927 portYIELD_WITHIN_API();
\r
933 traceQUEUE_PEEK( pxQueue );
\r
935 /* We are not removing the data, so reset our read
\r
937 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
939 /* The data is being left in the queue, so see if there are
\r
940 any other tasks waiting for the data. */
\r
941 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
943 /* Tasks that are removed from the event list will get added to
\r
944 the pending ready list as the scheduler is still suspended. */
\r
945 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
947 /* The task waiting has a higher priority than this task. */
\r
948 portYIELD_WITHIN_API();
\r
954 taskEXIT_CRITICAL();
\r
959 if( xTicksToWait == ( portTickType ) 0 )
\r
961 /* The queue was empty and no block time is specified (or
\r
962 the block time has expired) so leave now. */
\r
963 taskEXIT_CRITICAL();
\r
964 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
965 return errQUEUE_EMPTY;
\r
967 else if( xEntryTimeSet == pdFALSE )
\r
969 /* The queue was empty and a block time was specified so
\r
970 configure the timeout structure. */
\r
971 vTaskSetTimeOutState( &xTimeOut );
\r
972 xEntryTimeSet = pdTRUE;
\r
976 taskEXIT_CRITICAL();
\r
978 /* Interrupts and other tasks can send to and receive from the queue
\r
979 now the critical section has been exited. */
\r
982 prvLockQueue( pxQueue );
\r
984 /* Update the timeout state to see if it has expired yet. */
\r
985 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
987 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
989 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
991 #if ( configUSE_MUTEXES == 1 )
\r
993 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
995 portENTER_CRITICAL();
\r
997 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
999 portEXIT_CRITICAL();
\r
1004 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1005 prvUnlockQueue( pxQueue );
\r
1006 if( xTaskResumeAll() == pdFALSE )
\r
1008 portYIELD_WITHIN_API();
\r
1014 prvUnlockQueue( pxQueue );
\r
1015 ( void ) xTaskResumeAll();
\r
1020 prvUnlockQueue( pxQueue );
\r
1021 ( void ) xTaskResumeAll();
\r
1022 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
1023 return errQUEUE_EMPTY;
\r
1027 /*-----------------------------------------------------------*/
\r
1029 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
1031 signed portBASE_TYPE xReturn;
\r
1032 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1034 configASSERT( pxQueue );
\r
1035 configASSERT( pxTaskWoken );
\r
1036 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1038 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1040 /* We cannot block from an ISR, so check there is data available. */
\r
1041 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1043 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1045 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1046 --( pxQueue->uxMessagesWaiting );
\r
1048 /* If the queue is locked we will not modify the event list. Instead
\r
1049 we update the lock count so the task that unlocks the queue will know
\r
1050 that an ISR has removed data while the queue was locked. */
\r
1051 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1053 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1055 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1057 /* The task waiting has a higher priority than us so
\r
1058 force a context switch. */
\r
1059 *pxTaskWoken = pdTRUE;
\r
1065 /* Increment the lock count so the task that unlocks the queue
\r
1066 knows that data was removed while it was locked. */
\r
1067 ++( pxQueue->xRxLock );
\r
1075 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1078 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1082 /*-----------------------------------------------------------*/
\r
1084 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1086 unsigned portBASE_TYPE uxReturn;
\r
1088 configASSERT( pxQueue );
\r
1090 taskENTER_CRITICAL();
\r
1091 uxReturn = pxQueue->uxMessagesWaiting;
\r
1092 taskEXIT_CRITICAL();
\r
1096 /*-----------------------------------------------------------*/
\r
1098 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1100 unsigned portBASE_TYPE uxReturn;
\r
1102 configASSERT( pxQueue );
\r
1104 uxReturn = pxQueue->uxMessagesWaiting;
\r
1108 /*-----------------------------------------------------------*/
\r
1110 void vQueueDelete( xQueueHandle pxQueue )
\r
1112 configASSERT( pxQueue );
\r
1114 traceQUEUE_DELETE( pxQueue );
\r
1115 vQueueUnregisterQueue( pxQueue );
\r
1116 vPortFree( pxQueue->pcHead );
\r
1117 vPortFree( pxQueue );
\r
1119 /*-----------------------------------------------------------*/
\r
1121 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1123 unsigned char ucQueueGetQueueNumber( xQueueHandle pxQueue )
\r
1125 return pxQueue->ucQueueNumber;
\r
1129 /*-----------------------------------------------------------*/
\r
1131 #if ( configUSE_TRACE_FACILITY == 1 )
\r
1133 unsigned char ucQueueGetQueueType( xQueueHandle pxQueue )
\r
1135 return pxQueue->ucQueueType;
\r
1139 /*-----------------------------------------------------------*/
\r
1141 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1143 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1145 #if ( configUSE_MUTEXES == 1 )
\r
1147 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1149 /* The mutex is no longer being held. */
\r
1150 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1151 pxQueue->pxMutexHolder = NULL;
\r
1156 else if( xPosition == queueSEND_TO_BACK )
\r
1158 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1159 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1160 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1162 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1167 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1168 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1169 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1171 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1175 ++( pxQueue->uxMessagesWaiting );
\r
1177 /*-----------------------------------------------------------*/
\r
1179 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1181 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1183 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1184 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1186 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1188 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1191 /*-----------------------------------------------------------*/
\r
1193 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1195 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1197 /* The lock counts contains the number of extra data items placed or
\r
1198 removed from the queue while the queue was locked. When a queue is
\r
1199 locked items can be added or removed, but the event lists cannot be
\r
1201 taskENTER_CRITICAL();
\r
1203 /* See if data was added to the queue while it was locked. */
\r
1204 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1206 /* Data was posted while the queue was locked. Are any tasks
\r
1207 blocked waiting for data to become available? */
\r
1208 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1210 /* Tasks that are removed from the event list will get added to
\r
1211 the pending ready list as the scheduler is still suspended. */
\r
1212 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1214 /* The task waiting has a higher priority so record that a
\r
1215 context switch is required. */
\r
1216 vTaskMissedYield();
\r
1219 --( pxQueue->xTxLock );
\r
1227 pxQueue->xTxLock = queueUNLOCKED;
\r
1229 taskEXIT_CRITICAL();
\r
1231 /* Do the same for the Rx lock. */
\r
1232 taskENTER_CRITICAL();
\r
1234 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1236 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1238 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1240 vTaskMissedYield();
\r
1243 --( pxQueue->xRxLock );
\r
1251 pxQueue->xRxLock = queueUNLOCKED;
\r
1253 taskEXIT_CRITICAL();
\r
1255 /*-----------------------------------------------------------*/
\r
1257 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1259 signed portBASE_TYPE xReturn;
\r
1261 taskENTER_CRITICAL();
\r
1262 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1263 taskEXIT_CRITICAL();
\r
1267 /*-----------------------------------------------------------*/
\r
1269 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1271 signed portBASE_TYPE xReturn;
\r
1273 configASSERT( pxQueue );
\r
1274 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1278 /*-----------------------------------------------------------*/
\r
1280 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1282 signed portBASE_TYPE xReturn;
\r
1284 taskENTER_CRITICAL();
\r
1285 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1286 taskEXIT_CRITICAL();
\r
1290 /*-----------------------------------------------------------*/
\r
1292 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1294 signed portBASE_TYPE xReturn;
\r
1296 configASSERT( pxQueue );
\r
1297 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1301 /*-----------------------------------------------------------*/
\r
1303 #if configUSE_CO_ROUTINES == 1
\r
1304 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1306 signed portBASE_TYPE xReturn;
\r
1308 /* If the queue is already full we may have to block. A critical section
\r
1309 is required to prevent an interrupt removing something from the queue
\r
1310 between the check to see if the queue is full and blocking on the queue. */
\r
1311 portDISABLE_INTERRUPTS();
\r
1313 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1315 /* The queue is full - do we want to block or just leave without
\r
1317 if( xTicksToWait > ( portTickType ) 0 )
\r
1319 /* As this is called from a coroutine we cannot block directly, but
\r
1320 return indicating that we need to block. */
\r
1321 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1322 portENABLE_INTERRUPTS();
\r
1323 return errQUEUE_BLOCKED;
\r
1327 portENABLE_INTERRUPTS();
\r
1328 return errQUEUE_FULL;
\r
1332 portENABLE_INTERRUPTS();
\r
1336 portDISABLE_INTERRUPTS();
\r
1338 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1340 /* There is room in the queue, copy the data into the queue. */
\r
1341 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1344 /* Were any co-routines waiting for data to become available? */
\r
1345 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1347 /* In this instance the co-routine could be placed directly
\r
1348 into the ready list as we are within a critical section.
\r
1349 Instead the same pending ready list mechanism is used as if
\r
1350 the event were caused from within an interrupt. */
\r
1351 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1353 /* The co-routine waiting has a higher priority so record
\r
1354 that a yield might be appropriate. */
\r
1355 xReturn = errQUEUE_YIELD;
\r
1361 xReturn = errQUEUE_FULL;
\r
1364 portENABLE_INTERRUPTS();
\r
1369 /*-----------------------------------------------------------*/
\r
1371 #if configUSE_CO_ROUTINES == 1
\r
1372 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1374 signed portBASE_TYPE xReturn;
\r
1376 /* If the queue is already empty we may have to block. A critical section
\r
1377 is required to prevent an interrupt adding something to the queue
\r
1378 between the check to see if the queue is empty and blocking on the queue. */
\r
1379 portDISABLE_INTERRUPTS();
\r
1381 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1383 /* There are no messages in the queue, do we want to block or just
\r
1384 leave with nothing? */
\r
1385 if( xTicksToWait > ( portTickType ) 0 )
\r
1387 /* As this is a co-routine we cannot block directly, but return
\r
1388 indicating that we need to block. */
\r
1389 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1390 portENABLE_INTERRUPTS();
\r
1391 return errQUEUE_BLOCKED;
\r
1395 portENABLE_INTERRUPTS();
\r
1396 return errQUEUE_FULL;
\r
1400 portENABLE_INTERRUPTS();
\r
1404 portDISABLE_INTERRUPTS();
\r
1406 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1408 /* Data is available from the queue. */
\r
1409 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1410 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1412 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1414 --( pxQueue->uxMessagesWaiting );
\r
1415 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1419 /* Were any co-routines waiting for space to become available? */
\r
1420 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1422 /* In this instance the co-routine could be placed directly
\r
1423 into the ready list as we are within a critical section.
\r
1424 Instead the same pending ready list mechanism is used as if
\r
1425 the event were caused from within an interrupt. */
\r
1426 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1428 xReturn = errQUEUE_YIELD;
\r
1437 portENABLE_INTERRUPTS();
\r
1442 /*-----------------------------------------------------------*/
\r
1446 #if configUSE_CO_ROUTINES == 1
\r
1447 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1449 /* Cannot block within an ISR so if there is no space on the queue then
\r
1450 exit without doing anything. */
\r
1451 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1453 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1455 /* We only want to wake one co-routine per ISR, so check that a
\r
1456 co-routine has not already been woken. */
\r
1457 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1459 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1461 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1469 return xCoRoutinePreviouslyWoken;
\r
1472 /*-----------------------------------------------------------*/
\r
1474 #if configUSE_CO_ROUTINES == 1
\r
1475 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1477 signed portBASE_TYPE xReturn;
\r
1479 /* We cannot block from an ISR, so check there is data available. If
\r
1480 not then just leave without doing anything. */
\r
1481 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1483 /* Copy the data from the queue. */
\r
1484 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1485 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1487 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1489 --( pxQueue->uxMessagesWaiting );
\r
1490 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1492 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1494 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1496 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1498 *pxCoRoutineWoken = pdTRUE;
\r
1513 /*-----------------------------------------------------------*/
\r
1515 #if configQUEUE_REGISTRY_SIZE > 0
\r
1517 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1519 unsigned portBASE_TYPE ux;
\r
1521 /* See if there is an empty space in the registry. A NULL name denotes
\r
1523 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1525 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1527 /* Store the information on this queue. */
\r
1528 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1529 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1536 /*-----------------------------------------------------------*/
\r
1538 #if configQUEUE_REGISTRY_SIZE > 0
\r
1540 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1542 unsigned portBASE_TYPE ux;
\r
1544 /* See if the handle of the queue being unregistered in actually in the
\r
1546 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1548 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1550 /* Set the name to NULL to show that this slot if free again. */
\r
1551 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1559 /*-----------------------------------------------------------*/
\r
1561 #if configUSE_TIMERS == 1
\r
1563 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1565 /* This function should not be called by application code hence the
\r
1566 'Restricted' in its name. It is not part of the public API. It is
\r
1567 designed for use by kernel code, and has special calling requirements.
\r
1568 It can result in vListInsert() being called on a list that can only
\r
1569 possibly ever have one item in it, so the list will be fast, but even
\r
1570 so it should be called with the scheduler locked and not from a critical
\r
1573 /* Only do anything if there are no messages in the queue. This function
\r
1574 will not actually cause the task to block, just place it on a blocked
\r
1575 list. It will not block until the scheduler is unlocked - at which
\r
1576 time a yield will be performed. If an item is added to the queue while
\r
1577 the queue is locked, and the calling task blocks on the queue, then the
\r
1578 calling task will be immediately unblocked when the queue is unlocked. */
\r
1579 prvLockQueue( pxQueue );
\r
1580 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1582 /* There is nothing in the queue, block for the specified period. */
\r
1583 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1585 prvUnlockQueue( pxQueue );
\r