2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\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 exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 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
64 #include "croutine.h"
\r
66 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
68 /*-----------------------------------------------------------
\r
69 * PUBLIC LIST API documented in list.h
\r
70 *----------------------------------------------------------*/
\r
72 /* Constants used with the cRxLock and cTxLock structure members. */
\r
73 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
74 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
76 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
78 /* For internal use only. */
\r
79 #define queueSEND_TO_BACK ( 0 )
\r
80 #define queueSEND_TO_FRONT ( 1 )
\r
82 /* Effectively make a union out of the xQUEUE structure. */
\r
83 #define pxMutexHolder pcTail
\r
84 #define uxQueueType pcHead
\r
85 #define uxRecursiveCallCount pcReadFrom
\r
86 #define queueQUEUE_IS_MUTEX NULL
\r
88 /* Semaphores do not actually store or copy data, so have an items size of
\r
90 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
91 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
92 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
95 * Definition of the queue used by the scheduler.
\r
96 * Items are queued by copy, not reference.
\r
98 typedef struct QueueDefinition
\r
100 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
101 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
103 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
104 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
106 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
107 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
109 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
110 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
111 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
113 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
114 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
117 /*-----------------------------------------------------------*/
\r
120 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
121 * To keep the definition private the API header file defines it as a
\r
124 typedef xQUEUE * xQueueHandle;
\r
127 * Prototypes for public functions are included here so we don't have to
\r
128 * include the API header file (as it defines xQueueHandle differently). These
\r
129 * functions are documented in the API header file.
\r
131 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize ) PRIVILEGED_FUNCTION;
\r
132 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
133 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
134 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
135 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
136 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
137 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
138 xQueueHandle xQueueCreateMutex( void ) PRIVILEGED_FUNCTION;
\r
139 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
140 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
141 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
142 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
143 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
144 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
145 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
146 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
149 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
150 * an optional component.
\r
152 #if configUSE_CO_ROUTINES == 1
\r
153 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
154 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
155 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
156 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
160 * The queue registry is just a means for kernel aware debuggers to locate
\r
161 * queue structures. It has no other purpose so is an optional component.
\r
163 #if configQUEUE_REGISTRY_SIZE > 0
\r
165 /* The type stored within the queue registry array. This allows a name
\r
166 to be assigned to each queue making kernel aware debugging a little
\r
167 more user friendly. */
\r
168 typedef struct QUEUE_REGISTRY_ITEM
\r
170 signed char *pcQueueName;
\r
171 xQueueHandle xHandle;
\r
172 } xQueueRegistryItem;
\r
174 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
175 The pcQueueName member of a structure being NULL is indicative of the
\r
176 array position being vacant. */
\r
177 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
179 /* Removes a queue from the registry by simply setting the pcQueueName
\r
181 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
182 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
186 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
187 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
188 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
189 * queue is locked it will instead increment the appropriate queue lock count
\r
190 * to indicate that a task may require unblocking. When the queue in unlocked
\r
191 * these lock counts are inspected, and the appropriate action taken.
\r
193 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
196 * Uses a critical section to determine if there is any data in a queue.
\r
198 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
200 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
203 * Uses a critical section to determine if there is any space in a queue.
\r
205 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
207 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
210 * Copies an item into the queue, either at the front of the queue or the
\r
211 * back of the queue.
\r
213 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
216 * Copies an item out of a queue.
\r
218 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
219 /*-----------------------------------------------------------*/
\r
222 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
223 * accessing the queue event lists.
\r
225 #define prvLockQueue( pxQueue ) \
\r
226 taskENTER_CRITICAL(); \
\r
228 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
230 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
232 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
234 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
237 taskEXIT_CRITICAL()
\r
238 /*-----------------------------------------------------------*/
\r
241 /*-----------------------------------------------------------
\r
242 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
243 *----------------------------------------------------------*/
\r
245 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
247 xQUEUE *pxNewQueue;
\r
248 size_t xQueueSizeInBytes;
\r
249 xQueueHandle xReturn = NULL;
\r
251 /* Allocate the new queue structure. */
\r
252 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
254 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
255 if( pxNewQueue != NULL )
\r
257 /* Create the list of pointers to queue items. The queue is one byte
\r
258 longer than asked for to make wrap checking easier/faster. */
\r
259 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
261 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
262 if( pxNewQueue->pcHead != NULL )
\r
264 /* Initialise the queue members as described above where the
\r
265 queue type is defined. */
\r
266 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
267 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
268 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
269 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - ( unsigned portBASE_TYPE ) 1U ) * uxItemSize );
\r
270 pxNewQueue->uxLength = uxQueueLength;
\r
271 pxNewQueue->uxItemSize = uxItemSize;
\r
272 pxNewQueue->xRxLock = queueUNLOCKED;
\r
273 pxNewQueue->xTxLock = queueUNLOCKED;
\r
275 /* Likewise ensure the event queues start with the correct state. */
\r
276 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
277 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
279 traceQUEUE_CREATE( pxNewQueue );
\r
280 xReturn = pxNewQueue;
\r
284 traceQUEUE_CREATE_FAILED();
\r
285 vPortFree( pxNewQueue );
\r
290 configASSERT( xReturn );
\r
294 /*-----------------------------------------------------------*/
\r
296 #if ( configUSE_MUTEXES == 1 )
\r
298 xQueueHandle xQueueCreateMutex( void )
\r
300 xQUEUE *pxNewQueue;
\r
302 /* Allocate the new queue structure. */
\r
303 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
304 if( pxNewQueue != NULL )
\r
306 /* Information required for priority inheritance. */
\r
307 pxNewQueue->pxMutexHolder = NULL;
\r
308 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
310 /* Queues used as a mutex no data is actually copied into or out
\r
312 pxNewQueue->pcWriteTo = NULL;
\r
313 pxNewQueue->pcReadFrom = NULL;
\r
315 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
316 an item size of 0 as nothing is actually copied into or out
\r
318 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
319 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
320 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
321 pxNewQueue->xRxLock = queueUNLOCKED;
\r
322 pxNewQueue->xTxLock = queueUNLOCKED;
\r
324 /* Ensure the event queues start with the correct state. */
\r
325 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
326 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
328 /* Start with the semaphore in the expected state. */
\r
329 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
331 traceCREATE_MUTEX( pxNewQueue );
\r
335 traceCREATE_MUTEX_FAILED();
\r
338 configASSERT( pxNewQueue );
\r
342 #endif /* configUSE_MUTEXES */
\r
343 /*-----------------------------------------------------------*/
\r
345 #if configUSE_RECURSIVE_MUTEXES == 1
\r
347 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
349 portBASE_TYPE xReturn;
\r
351 configASSERT( pxMutex );
\r
353 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
354 change outside of this task. If this task does not hold the mutex then
\r
355 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
356 this is the only condition we are interested in it does not matter if
\r
357 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
358 mutual exclusion is required to test the pxMutexHolder variable. */
\r
359 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
361 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
363 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
364 the task handle, therefore no underflow check is required. Also,
\r
365 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
366 there can only be one, no mutual exclusion is required to modify the
\r
367 uxRecursiveCallCount member. */
\r
368 ( pxMutex->uxRecursiveCallCount )--;
\r
370 /* Have we unwound the call count? */
\r
371 if( pxMutex->uxRecursiveCallCount == 0 )
\r
373 /* Return the mutex. This will automatically unblock any other
\r
374 task that might be waiting to access the mutex. */
\r
375 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
382 /* We cannot give the mutex because we are not the holder. */
\r
385 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
391 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
392 /*-----------------------------------------------------------*/
\r
394 #if configUSE_RECURSIVE_MUTEXES == 1
\r
396 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
398 portBASE_TYPE xReturn;
\r
400 configASSERT( pxMutex );
\r
402 /* Comments regarding mutual exclusion as per those within
\r
403 xQueueGiveMutexRecursive(). */
\r
405 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
407 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
409 ( pxMutex->uxRecursiveCallCount )++;
\r
414 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
416 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
417 we may have blocked to reach here. */
\r
418 if( xReturn == pdPASS )
\r
420 ( pxMutex->uxRecursiveCallCount )++;
\r
424 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
431 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
432 /*-----------------------------------------------------------*/
\r
434 #if configUSE_COUNTING_SEMAPHORES == 1
\r
436 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
438 xQueueHandle pxHandle;
\r
440 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
442 if( pxHandle != NULL )
\r
444 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
446 traceCREATE_COUNTING_SEMAPHORE();
\r
450 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
453 configASSERT( pxHandle );
\r
457 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
458 /*-----------------------------------------------------------*/
\r
460 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
462 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
463 xTimeOutType xTimeOut;
\r
465 configASSERT( pxQueue );
\r
466 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
468 /* This function relaxes the coding standard somewhat to allow return
\r
469 statements within the function itself. This is done in the interest
\r
470 of execution time efficiency. */
\r
473 taskENTER_CRITICAL();
\r
475 /* Is there room on the queue now? To be running we must be
\r
476 the highest priority task wanting to access the queue. */
\r
477 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
479 traceQUEUE_SEND( pxQueue );
\r
480 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
482 /* If there was a task waiting for data to arrive on the
\r
483 queue then unblock it now. */
\r
484 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
486 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
488 /* The unblocked task has a priority higher than
\r
489 our own so yield immediately. Yes it is ok to do
\r
490 this from within the critical section - the kernel
\r
491 takes care of that. */
\r
492 portYIELD_WITHIN_API();
\r
496 taskEXIT_CRITICAL();
\r
498 /* Return to the original privilege level before exiting the
\r
504 if( xTicksToWait == ( portTickType ) 0 )
\r
506 /* The queue was full and no block time is specified (or
\r
507 the block time has expired) so leave now. */
\r
508 taskEXIT_CRITICAL();
\r
510 /* Return to the original privilege level before exiting
\r
512 traceQUEUE_SEND_FAILED( pxQueue );
\r
513 return errQUEUE_FULL;
\r
515 else if( xEntryTimeSet == pdFALSE )
\r
517 /* The queue was full and a block time was specified so
\r
518 configure the timeout structure. */
\r
519 vTaskSetTimeOutState( &xTimeOut );
\r
520 xEntryTimeSet = pdTRUE;
\r
524 taskEXIT_CRITICAL();
\r
526 /* Interrupts and other tasks can send to and receive from the queue
\r
527 now the critical section has been exited. */
\r
530 prvLockQueue( pxQueue );
\r
532 /* Update the timeout state to see if it has expired yet. */
\r
533 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
535 if( prvIsQueueFull( pxQueue ) )
\r
537 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
538 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
540 /* Unlocking the queue means queue events can effect the
\r
541 event list. It is possible that interrupts occurring now
\r
542 remove this task from the event list again - but as the
\r
543 scheduler is suspended the task will go onto the pending
\r
544 ready last instead of the actual ready list. */
\r
545 prvUnlockQueue( pxQueue );
\r
547 /* Resuming the scheduler will move tasks from the pending
\r
548 ready list into the ready list - so it is feasible that this
\r
549 task is already in a ready list before it yields - in which
\r
550 case the yield will not cause a context switch unless there
\r
551 is also a higher priority task in the pending ready list. */
\r
552 if( !xTaskResumeAll() )
\r
554 portYIELD_WITHIN_API();
\r
560 prvUnlockQueue( pxQueue );
\r
561 ( void ) xTaskResumeAll();
\r
566 /* The timeout has expired. */
\r
567 prvUnlockQueue( pxQueue );
\r
568 ( void ) xTaskResumeAll();
\r
570 /* Return to the original privilege level before exiting the
\r
572 traceQUEUE_SEND_FAILED( pxQueue );
\r
573 return errQUEUE_FULL;
\r
577 /*-----------------------------------------------------------*/
\r
579 #if configUSE_ALTERNATIVE_API == 1
\r
581 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
583 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
584 xTimeOutType xTimeOut;
\r
586 configASSERT( pxQueue );
\r
587 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
591 taskENTER_CRITICAL();
\r
593 /* Is there room on the queue now? To be running we must be
\r
594 the highest priority task wanting to access the queue. */
\r
595 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
597 traceQUEUE_SEND( pxQueue );
\r
598 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
600 /* If there was a task waiting for data to arrive on the
\r
601 queue then unblock it now. */
\r
602 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
604 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
606 /* The unblocked task has a priority higher than
\r
607 our own so yield immediately. */
\r
608 portYIELD_WITHIN_API();
\r
612 taskEXIT_CRITICAL();
\r
617 if( xTicksToWait == ( portTickType ) 0 )
\r
619 taskEXIT_CRITICAL();
\r
620 return errQUEUE_FULL;
\r
622 else if( xEntryTimeSet == pdFALSE )
\r
624 vTaskSetTimeOutState( &xTimeOut );
\r
625 xEntryTimeSet = pdTRUE;
\r
629 taskEXIT_CRITICAL();
\r
631 taskENTER_CRITICAL();
\r
633 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
635 if( prvIsQueueFull( pxQueue ) )
\r
637 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
638 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
639 portYIELD_WITHIN_API();
\r
644 taskEXIT_CRITICAL();
\r
645 traceQUEUE_SEND_FAILED( pxQueue );
\r
646 return errQUEUE_FULL;
\r
649 taskEXIT_CRITICAL();
\r
653 #endif /* configUSE_ALTERNATIVE_API */
\r
654 /*-----------------------------------------------------------*/
\r
656 #if configUSE_ALTERNATIVE_API == 1
\r
658 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
660 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
661 xTimeOutType xTimeOut;
\r
662 signed char *pcOriginalReadPosition;
\r
664 configASSERT( pxQueue );
\r
665 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
669 taskENTER_CRITICAL();
\r
671 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
673 /* Remember our read position in case we are just peeking. */
\r
674 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
676 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
678 if( xJustPeeking == pdFALSE )
\r
680 traceQUEUE_RECEIVE( pxQueue );
\r
682 /* We are actually removing data. */
\r
683 --( pxQueue->uxMessagesWaiting );
\r
685 #if ( configUSE_MUTEXES == 1 )
\r
687 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
689 /* Record the information required to implement
\r
690 priority inheritance should it become necessary. */
\r
691 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
696 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
698 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
700 portYIELD_WITHIN_API();
\r
706 traceQUEUE_PEEK( pxQueue );
\r
708 /* We are not removing the data, so reset our read
\r
710 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
712 /* The data is being left in the queue, so see if there are
\r
713 any other tasks waiting for the data. */
\r
714 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
716 /* Tasks that are removed from the event list will get added to
\r
717 the pending ready list as the scheduler is still suspended. */
\r
718 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
720 /* The task waiting has a higher priority than this task. */
\r
721 portYIELD_WITHIN_API();
\r
727 taskEXIT_CRITICAL();
\r
732 if( xTicksToWait == ( portTickType ) 0 )
\r
734 taskEXIT_CRITICAL();
\r
735 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
736 return errQUEUE_EMPTY;
\r
738 else if( xEntryTimeSet == pdFALSE )
\r
740 vTaskSetTimeOutState( &xTimeOut );
\r
741 xEntryTimeSet = pdTRUE;
\r
745 taskEXIT_CRITICAL();
\r
747 taskENTER_CRITICAL();
\r
749 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
751 if( prvIsQueueEmpty( pxQueue ) )
\r
753 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
755 #if ( configUSE_MUTEXES == 1 )
\r
757 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
759 portENTER_CRITICAL();
\r
760 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
761 portEXIT_CRITICAL();
\r
766 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
767 portYIELD_WITHIN_API();
\r
772 taskEXIT_CRITICAL();
\r
773 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
774 return errQUEUE_EMPTY;
\r
777 taskEXIT_CRITICAL();
\r
782 #endif /* configUSE_ALTERNATIVE_API */
\r
783 /*-----------------------------------------------------------*/
\r
785 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
787 signed portBASE_TYPE xReturn;
\r
788 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
790 configASSERT( pxQueue );
\r
791 configASSERT( pxHigherPriorityTaskWoken );
\r
792 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
794 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
795 in the queue. Also we don't directly wake a task that was blocked on a
\r
796 queue read, instead we return a flag to say whether a context switch is
\r
797 required or not (i.e. has a task with a higher priority than us been woken
\r
799 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
801 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
803 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
805 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
807 /* If the queue is locked we do not alter the event list. This will
\r
808 be done when the queue is unlocked later. */
\r
809 if( pxQueue->xTxLock == queueUNLOCKED )
\r
811 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
813 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
815 /* The task waiting has a higher priority so record that a
\r
816 context switch is required. */
\r
817 *pxHigherPriorityTaskWoken = pdTRUE;
\r
823 /* Increment the lock count so the task that unlocks the queue
\r
824 knows that data was posted while it was locked. */
\r
825 ++( pxQueue->xTxLock );
\r
832 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
833 xReturn = errQUEUE_FULL;
\r
836 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
840 /*-----------------------------------------------------------*/
\r
842 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
844 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
845 xTimeOutType xTimeOut;
\r
846 signed char *pcOriginalReadPosition;
\r
848 configASSERT( pxQueue );
\r
849 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
851 /* This function relaxes the coding standard somewhat to allow return
\r
852 statements within the function itself. This is done in the interest
\r
853 of execution time efficiency. */
\r
857 taskENTER_CRITICAL();
\r
859 /* Is there data in the queue now? To be running we must be
\r
860 the highest priority task wanting to access the queue. */
\r
861 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
863 /* Remember our read position in case we are just peeking. */
\r
864 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
866 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
868 if( xJustPeeking == pdFALSE )
\r
870 traceQUEUE_RECEIVE( pxQueue );
\r
872 /* We are actually removing data. */
\r
873 --( pxQueue->uxMessagesWaiting );
\r
875 #if ( configUSE_MUTEXES == 1 )
\r
877 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
879 /* Record the information required to implement
\r
880 priority inheritance should it become necessary. */
\r
881 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
886 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
888 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
890 portYIELD_WITHIN_API();
\r
896 traceQUEUE_PEEK( pxQueue );
\r
898 /* We are not removing the data, so reset our read
\r
900 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
902 /* The data is being left in the queue, so see if there are
\r
903 any other tasks waiting for the data. */
\r
904 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
906 /* Tasks that are removed from the event list will get added to
\r
907 the pending ready list as the scheduler is still suspended. */
\r
908 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
910 /* The task waiting has a higher priority than this task. */
\r
911 portYIELD_WITHIN_API();
\r
917 taskEXIT_CRITICAL();
\r
922 if( xTicksToWait == ( portTickType ) 0 )
\r
924 /* The queue was empty and no block time is specified (or
\r
925 the block time has expired) so leave now. */
\r
926 taskEXIT_CRITICAL();
\r
927 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
928 return errQUEUE_EMPTY;
\r
930 else if( xEntryTimeSet == pdFALSE )
\r
932 /* The queue was empty and a block time was specified so
\r
933 configure the timeout structure. */
\r
934 vTaskSetTimeOutState( &xTimeOut );
\r
935 xEntryTimeSet = pdTRUE;
\r
939 taskEXIT_CRITICAL();
\r
941 /* Interrupts and other tasks can send to and receive from the queue
\r
942 now the critical section has been exited. */
\r
945 prvLockQueue( pxQueue );
\r
947 /* Update the timeout state to see if it has expired yet. */
\r
948 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
950 if( prvIsQueueEmpty( pxQueue ) )
\r
952 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
954 #if ( configUSE_MUTEXES == 1 )
\r
956 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
958 portENTER_CRITICAL();
\r
960 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
962 portEXIT_CRITICAL();
\r
967 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
968 prvUnlockQueue( pxQueue );
\r
969 if( !xTaskResumeAll() )
\r
971 portYIELD_WITHIN_API();
\r
977 prvUnlockQueue( pxQueue );
\r
978 ( void ) xTaskResumeAll();
\r
983 prvUnlockQueue( pxQueue );
\r
984 ( void ) xTaskResumeAll();
\r
985 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
986 return errQUEUE_EMPTY;
\r
990 /*-----------------------------------------------------------*/
\r
992 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
994 signed portBASE_TYPE xReturn;
\r
995 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
997 configASSERT( pxQueue );
\r
998 configASSERT( pxTaskWoken );
\r
999 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1001 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1003 /* We cannot block from an ISR, so check there is data available. */
\r
1004 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1006 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1008 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1009 --( pxQueue->uxMessagesWaiting );
\r
1011 /* If the queue is locked we will not modify the event list. Instead
\r
1012 we update the lock count so the task that unlocks the queue will know
\r
1013 that an ISR has removed data while the queue was locked. */
\r
1014 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1016 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1018 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1020 /* The task waiting has a higher priority than us so
\r
1021 force a context switch. */
\r
1022 *pxTaskWoken = pdTRUE;
\r
1028 /* Increment the lock count so the task that unlocks the queue
\r
1029 knows that data was removed while it was locked. */
\r
1030 ++( pxQueue->xRxLock );
\r
1038 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1041 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1045 /*-----------------------------------------------------------*/
\r
1047 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1049 unsigned portBASE_TYPE uxReturn;
\r
1051 configASSERT( pxQueue );
\r
1053 taskENTER_CRITICAL();
\r
1054 uxReturn = pxQueue->uxMessagesWaiting;
\r
1055 taskEXIT_CRITICAL();
\r
1059 /*-----------------------------------------------------------*/
\r
1061 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1063 unsigned portBASE_TYPE uxReturn;
\r
1065 configASSERT( pxQueue );
\r
1067 uxReturn = pxQueue->uxMessagesWaiting;
\r
1071 /*-----------------------------------------------------------*/
\r
1073 void vQueueDelete( xQueueHandle pxQueue )
\r
1075 configASSERT( pxQueue );
\r
1077 traceQUEUE_DELETE( pxQueue );
\r
1078 vQueueUnregisterQueue( pxQueue );
\r
1079 vPortFree( pxQueue->pcHead );
\r
1080 vPortFree( pxQueue );
\r
1082 /*-----------------------------------------------------------*/
\r
1084 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1086 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1088 #if ( configUSE_MUTEXES == 1 )
\r
1090 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1092 /* The mutex is no longer being held. */
\r
1093 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1094 pxQueue->pxMutexHolder = NULL;
\r
1099 else if( xPosition == queueSEND_TO_BACK )
\r
1101 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1102 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1103 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1105 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1110 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1111 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1112 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1114 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1118 ++( pxQueue->uxMessagesWaiting );
\r
1120 /*-----------------------------------------------------------*/
\r
1122 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1124 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1126 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1127 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1129 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1131 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1134 /*-----------------------------------------------------------*/
\r
1136 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1138 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1140 /* The lock counts contains the number of extra data items placed or
\r
1141 removed from the queue while the queue was locked. When a queue is
\r
1142 locked items can be added or removed, but the event lists cannot be
\r
1144 taskENTER_CRITICAL();
\r
1146 /* See if data was added to the queue while it was locked. */
\r
1147 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1149 /* Data was posted while the queue was locked. Are any tasks
\r
1150 blocked waiting for data to become available? */
\r
1151 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1153 /* Tasks that are removed from the event list will get added to
\r
1154 the pending ready list as the scheduler is still suspended. */
\r
1155 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1157 /* The task waiting has a higher priority so record that a
\r
1158 context switch is required. */
\r
1159 vTaskMissedYield();
\r
1162 --( pxQueue->xTxLock );
\r
1170 pxQueue->xTxLock = queueUNLOCKED;
\r
1172 taskEXIT_CRITICAL();
\r
1174 /* Do the same for the Rx lock. */
\r
1175 taskENTER_CRITICAL();
\r
1177 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1179 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1181 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1183 vTaskMissedYield();
\r
1186 --( pxQueue->xRxLock );
\r
1194 pxQueue->xRxLock = queueUNLOCKED;
\r
1196 taskEXIT_CRITICAL();
\r
1198 /*-----------------------------------------------------------*/
\r
1200 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1202 signed portBASE_TYPE xReturn;
\r
1204 taskENTER_CRITICAL();
\r
1205 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1206 taskEXIT_CRITICAL();
\r
1210 /*-----------------------------------------------------------*/
\r
1212 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1214 signed portBASE_TYPE xReturn;
\r
1216 configASSERT( pxQueue );
\r
1217 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1221 /*-----------------------------------------------------------*/
\r
1223 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1225 signed portBASE_TYPE xReturn;
\r
1227 taskENTER_CRITICAL();
\r
1228 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1229 taskEXIT_CRITICAL();
\r
1233 /*-----------------------------------------------------------*/
\r
1235 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1237 signed portBASE_TYPE xReturn;
\r
1239 configASSERT( pxQueue );
\r
1240 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1244 /*-----------------------------------------------------------*/
\r
1246 #if configUSE_CO_ROUTINES == 1
\r
1247 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1249 signed portBASE_TYPE xReturn;
\r
1251 /* If the queue is already full we may have to block. A critical section
\r
1252 is required to prevent an interrupt removing something from the queue
\r
1253 between the check to see if the queue is full and blocking on the queue. */
\r
1254 portDISABLE_INTERRUPTS();
\r
1256 if( prvIsQueueFull( pxQueue ) )
\r
1258 /* The queue is full - do we want to block or just leave without
\r
1260 if( xTicksToWait > ( portTickType ) 0 )
\r
1262 /* As this is called from a coroutine we cannot block directly, but
\r
1263 return indicating that we need to block. */
\r
1264 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1265 portENABLE_INTERRUPTS();
\r
1266 return errQUEUE_BLOCKED;
\r
1270 portENABLE_INTERRUPTS();
\r
1271 return errQUEUE_FULL;
\r
1275 portENABLE_INTERRUPTS();
\r
1279 portDISABLE_INTERRUPTS();
\r
1281 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1283 /* There is room in the queue, copy the data into the queue. */
\r
1284 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1287 /* Were any co-routines waiting for data to become available? */
\r
1288 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1290 /* In this instance the co-routine could be placed directly
\r
1291 into the ready list as we are within a critical section.
\r
1292 Instead the same pending ready list mechanism is used as if
\r
1293 the event were caused from within an interrupt. */
\r
1294 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1296 /* The co-routine waiting has a higher priority so record
\r
1297 that a yield might be appropriate. */
\r
1298 xReturn = errQUEUE_YIELD;
\r
1304 xReturn = errQUEUE_FULL;
\r
1307 portENABLE_INTERRUPTS();
\r
1312 /*-----------------------------------------------------------*/
\r
1314 #if configUSE_CO_ROUTINES == 1
\r
1315 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1317 signed portBASE_TYPE xReturn;
\r
1319 /* If the queue is already empty we may have to block. A critical section
\r
1320 is required to prevent an interrupt adding something to the queue
\r
1321 between the check to see if the queue is empty and blocking on the queue. */
\r
1322 portDISABLE_INTERRUPTS();
\r
1324 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1326 /* There are no messages in the queue, do we want to block or just
\r
1327 leave with nothing? */
\r
1328 if( xTicksToWait > ( portTickType ) 0 )
\r
1330 /* As this is a co-routine we cannot block directly, but return
\r
1331 indicating that we need to block. */
\r
1332 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1333 portENABLE_INTERRUPTS();
\r
1334 return errQUEUE_BLOCKED;
\r
1338 portENABLE_INTERRUPTS();
\r
1339 return errQUEUE_FULL;
\r
1343 portENABLE_INTERRUPTS();
\r
1347 portDISABLE_INTERRUPTS();
\r
1349 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1351 /* Data is available from the queue. */
\r
1352 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1353 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1355 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1357 --( pxQueue->uxMessagesWaiting );
\r
1358 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1362 /* Were any co-routines waiting for space to become available? */
\r
1363 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1365 /* In this instance the co-routine could be placed directly
\r
1366 into the ready list as we are within a critical section.
\r
1367 Instead the same pending ready list mechanism is used as if
\r
1368 the event were caused from within an interrupt. */
\r
1369 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1371 xReturn = errQUEUE_YIELD;
\r
1380 portENABLE_INTERRUPTS();
\r
1385 /*-----------------------------------------------------------*/
\r
1389 #if configUSE_CO_ROUTINES == 1
\r
1390 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1392 /* Cannot block within an ISR so if there is no space on the queue then
\r
1393 exit without doing anything. */
\r
1394 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1396 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1398 /* We only want to wake one co-routine per ISR, so check that a
\r
1399 co-routine has not already been woken. */
\r
1400 if( !xCoRoutinePreviouslyWoken )
\r
1402 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1404 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1412 return xCoRoutinePreviouslyWoken;
\r
1415 /*-----------------------------------------------------------*/
\r
1417 #if configUSE_CO_ROUTINES == 1
\r
1418 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1420 signed portBASE_TYPE xReturn;
\r
1422 /* We cannot block from an ISR, so check there is data available. If
\r
1423 not then just leave without doing anything. */
\r
1424 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1426 /* Copy the data from the queue. */
\r
1427 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1428 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1430 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1432 --( pxQueue->uxMessagesWaiting );
\r
1433 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1435 if( !( *pxCoRoutineWoken ) )
\r
1437 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1439 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1441 *pxCoRoutineWoken = pdTRUE;
\r
1456 /*-----------------------------------------------------------*/
\r
1458 #if configQUEUE_REGISTRY_SIZE > 0
\r
1460 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1462 unsigned portBASE_TYPE ux;
\r
1464 /* See if there is an empty space in the registry. A NULL name denotes
\r
1466 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1468 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1470 /* Store the information on this queue. */
\r
1471 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1472 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1479 /*-----------------------------------------------------------*/
\r
1481 #if configQUEUE_REGISTRY_SIZE > 0
\r
1483 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1485 unsigned portBASE_TYPE ux;
\r
1487 /* See if the handle of the queue being unregistered in actually in the
\r
1489 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1491 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1493 /* Set the name to NULL to show that this slot if free again. */
\r
1494 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1502 /*-----------------------------------------------------------*/
\r
1504 #if configUSE_TIMERS == 1
\r
1506 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1508 /* This function should not be called by application code hence the
\r
1509 'Restricted' in its name. It is not part of the public API. It is
\r
1510 designed for use by kernel code, and has special calling requirements.
\r
1511 It can result in vListInsert() being called on a list that can only
\r
1512 possibly ever have one item in it, so the list will be fast, but even
\r
1513 so it should be called with the scheduler locked and not from a critical
\r
1516 /* Only do anything if there are no messages in the queue. This function
\r
1517 will not actually cause the task to block, just place it on a blocked
\r
1518 list. It will not block until the scheduler is unlocked - at which
\r
1519 time a yield will be performed. If an item is added to the queue while
\r
1520 the queue is locked, and the calling task blocks on the queue, then the
\r
1521 calling task will be immediately unblocked when the queue is unlocked. */
\r
1522 prvLockQueue( pxQueue );
\r
1523 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1525 /* There is nothing in the queue, block for the specified period. */
\r
1526 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1528 prvUnlockQueue( pxQueue );
\r