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
147 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
150 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
151 * an optional component.
\r
153 #if configUSE_CO_ROUTINES == 1
\r
154 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
155 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
156 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
157 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
161 * The queue registry is just a means for kernel aware debuggers to locate
\r
162 * queue structures. It has no other purpose so is an optional component.
\r
164 #if configQUEUE_REGISTRY_SIZE > 0
\r
166 /* The type stored within the queue registry array. This allows a name
\r
167 to be assigned to each queue making kernel aware debugging a little
\r
168 more user friendly. */
\r
169 typedef struct QUEUE_REGISTRY_ITEM
\r
171 signed char *pcQueueName;
\r
172 xQueueHandle xHandle;
\r
173 } xQueueRegistryItem;
\r
175 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
176 The pcQueueName member of a structure being NULL is indicative of the
\r
177 array position being vacant. */
\r
178 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
180 /* Removes a queue from the registry by simply setting the pcQueueName
\r
182 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
183 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
187 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
188 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
189 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
190 * queue is locked it will instead increment the appropriate queue lock count
\r
191 * to indicate that a task may require unblocking. When the queue in unlocked
\r
192 * these lock counts are inspected, and the appropriate action taken.
\r
194 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
197 * Uses a critical section to determine if there is any data in a queue.
\r
199 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
201 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
204 * Uses a critical section to determine if there is any space in a queue.
\r
206 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
208 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
211 * Copies an item into the queue, either at the front of the queue or the
\r
212 * back of the queue.
\r
214 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
217 * Copies an item out of a queue.
\r
219 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
220 /*-----------------------------------------------------------*/
\r
223 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
224 * accessing the queue event lists.
\r
226 #define prvLockQueue( pxQueue ) \
\r
227 taskENTER_CRITICAL(); \
\r
229 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
231 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
233 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
235 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
238 taskEXIT_CRITICAL()
\r
239 /*-----------------------------------------------------------*/
\r
242 /*-----------------------------------------------------------
\r
243 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
244 *----------------------------------------------------------*/
\r
246 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
248 xQUEUE *pxNewQueue;
\r
249 size_t xQueueSizeInBytes;
\r
250 xQueueHandle xReturn = NULL;
\r
252 /* Allocate the new queue structure. */
\r
253 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
255 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
256 if( pxNewQueue != NULL )
\r
258 /* Create the list of pointers to queue items. The queue is one byte
\r
259 longer than asked for to make wrap checking easier/faster. */
\r
260 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
262 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
263 if( pxNewQueue->pcHead != NULL )
\r
265 /* Initialise the queue members as described above where the
\r
266 queue type is defined. */
\r
267 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
268 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
269 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
270 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - ( unsigned portBASE_TYPE ) 1U ) * uxItemSize );
\r
271 pxNewQueue->uxLength = uxQueueLength;
\r
272 pxNewQueue->uxItemSize = uxItemSize;
\r
273 pxNewQueue->xRxLock = queueUNLOCKED;
\r
274 pxNewQueue->xTxLock = queueUNLOCKED;
\r
276 /* Likewise ensure the event queues start with the correct state. */
\r
277 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
278 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
280 traceQUEUE_CREATE( pxNewQueue );
\r
281 xReturn = pxNewQueue;
\r
285 traceQUEUE_CREATE_FAILED();
\r
286 vPortFree( pxNewQueue );
\r
291 configASSERT( xReturn );
\r
295 /*-----------------------------------------------------------*/
\r
297 #if ( configUSE_MUTEXES == 1 )
\r
299 xQueueHandle xQueueCreateMutex( void )
\r
301 xQUEUE *pxNewQueue;
\r
303 /* Allocate the new queue structure. */
\r
304 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
305 if( pxNewQueue != NULL )
\r
307 /* Information required for priority inheritance. */
\r
308 pxNewQueue->pxMutexHolder = NULL;
\r
309 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
311 /* Queues used as a mutex no data is actually copied into or out
\r
313 pxNewQueue->pcWriteTo = NULL;
\r
314 pxNewQueue->pcReadFrom = NULL;
\r
316 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
317 an item size of 0 as nothing is actually copied into or out
\r
319 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
320 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
321 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
322 pxNewQueue->xRxLock = queueUNLOCKED;
\r
323 pxNewQueue->xTxLock = queueUNLOCKED;
\r
325 /* Ensure the event queues start with the correct state. */
\r
326 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
327 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
329 /* Start with the semaphore in the expected state. */
\r
330 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
332 traceCREATE_MUTEX( pxNewQueue );
\r
336 traceCREATE_MUTEX_FAILED();
\r
339 configASSERT( pxNewQueue );
\r
343 #endif /* configUSE_MUTEXES */
\r
344 /*-----------------------------------------------------------*/
\r
346 #if configUSE_RECURSIVE_MUTEXES == 1
\r
348 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
350 portBASE_TYPE xReturn;
\r
352 configASSERT( pxMutex );
\r
354 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
355 change outside of this task. If this task does not hold the mutex then
\r
356 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
357 this is the only condition we are interested in it does not matter if
\r
358 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
359 mutual exclusion is required to test the pxMutexHolder variable. */
\r
360 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
362 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
364 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
365 the task handle, therefore no underflow check is required. Also,
\r
366 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
367 there can only be one, no mutual exclusion is required to modify the
\r
368 uxRecursiveCallCount member. */
\r
369 ( pxMutex->uxRecursiveCallCount )--;
\r
371 /* Have we unwound the call count? */
\r
372 if( pxMutex->uxRecursiveCallCount == 0 )
\r
374 /* Return the mutex. This will automatically unblock any other
\r
375 task that might be waiting to access the mutex. */
\r
376 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
383 /* We cannot give the mutex because we are not the holder. */
\r
386 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
392 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
393 /*-----------------------------------------------------------*/
\r
395 #if configUSE_RECURSIVE_MUTEXES == 1
\r
397 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
399 portBASE_TYPE xReturn;
\r
401 configASSERT( pxMutex );
\r
403 /* Comments regarding mutual exclusion as per those within
\r
404 xQueueGiveMutexRecursive(). */
\r
406 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
408 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
410 ( pxMutex->uxRecursiveCallCount )++;
\r
415 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
417 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
418 we may have blocked to reach here. */
\r
419 if( xReturn == pdPASS )
\r
421 ( pxMutex->uxRecursiveCallCount )++;
\r
425 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
432 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
433 /*-----------------------------------------------------------*/
\r
435 #if configUSE_COUNTING_SEMAPHORES == 1
\r
437 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
439 xQueueHandle pxHandle;
\r
441 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
443 if( pxHandle != NULL )
\r
445 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
447 traceCREATE_COUNTING_SEMAPHORE();
\r
451 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
454 configASSERT( pxHandle );
\r
458 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
459 /*-----------------------------------------------------------*/
\r
461 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
463 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
464 xTimeOutType xTimeOut;
\r
466 configASSERT( pxQueue );
\r
467 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
469 /* This function relaxes the coding standard somewhat to allow return
\r
470 statements within the function itself. This is done in the interest
\r
471 of execution time efficiency. */
\r
474 taskENTER_CRITICAL();
\r
476 /* Is there room on the queue now? To be running we must be
\r
477 the highest priority task wanting to access the queue. */
\r
478 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
480 traceQUEUE_SEND( pxQueue );
\r
481 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
483 /* If there was a task waiting for data to arrive on the
\r
484 queue then unblock it now. */
\r
485 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
487 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
489 /* The unblocked task has a priority higher than
\r
490 our own so yield immediately. Yes it is ok to do
\r
491 this from within the critical section - the kernel
\r
492 takes care of that. */
\r
493 portYIELD_WITHIN_API();
\r
497 taskEXIT_CRITICAL();
\r
499 /* Return to the original privilege level before exiting the
\r
505 if( xTicksToWait == ( portTickType ) 0 )
\r
507 /* The queue was full and no block time is specified (or
\r
508 the block time has expired) so leave now. */
\r
509 taskEXIT_CRITICAL();
\r
511 /* Return to the original privilege level before exiting
\r
513 traceQUEUE_SEND_FAILED( pxQueue );
\r
514 return errQUEUE_FULL;
\r
516 else if( xEntryTimeSet == pdFALSE )
\r
518 /* The queue was full and a block time was specified so
\r
519 configure the timeout structure. */
\r
520 vTaskSetTimeOutState( &xTimeOut );
\r
521 xEntryTimeSet = pdTRUE;
\r
525 taskEXIT_CRITICAL();
\r
527 /* Interrupts and other tasks can send to and receive from the queue
\r
528 now the critical section has been exited. */
\r
531 prvLockQueue( pxQueue );
\r
533 /* Update the timeout state to see if it has expired yet. */
\r
534 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
536 if( prvIsQueueFull( pxQueue ) )
\r
538 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
539 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
541 /* Unlocking the queue means queue events can effect the
\r
542 event list. It is possible that interrupts occurring now
\r
543 remove this task from the event list again - but as the
\r
544 scheduler is suspended the task will go onto the pending
\r
545 ready last instead of the actual ready list. */
\r
546 prvUnlockQueue( pxQueue );
\r
548 /* Resuming the scheduler will move tasks from the pending
\r
549 ready list into the ready list - so it is feasible that this
\r
550 task is already in a ready list before it yields - in which
\r
551 case the yield will not cause a context switch unless there
\r
552 is also a higher priority task in the pending ready list. */
\r
553 if( !xTaskResumeAll() )
\r
555 portYIELD_WITHIN_API();
\r
561 prvUnlockQueue( pxQueue );
\r
562 ( void ) xTaskResumeAll();
\r
567 /* The timeout has expired. */
\r
568 prvUnlockQueue( pxQueue );
\r
569 ( void ) xTaskResumeAll();
\r
571 /* Return to the original privilege level before exiting the
\r
573 traceQUEUE_SEND_FAILED( pxQueue );
\r
574 return errQUEUE_FULL;
\r
578 /*-----------------------------------------------------------*/
\r
580 #if configUSE_ALTERNATIVE_API == 1
\r
582 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
584 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
585 xTimeOutType xTimeOut;
\r
587 configASSERT( pxQueue );
\r
588 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
592 taskENTER_CRITICAL();
\r
594 /* Is there room on the queue now? To be running we must be
\r
595 the highest priority task wanting to access the queue. */
\r
596 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
598 traceQUEUE_SEND( pxQueue );
\r
599 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
601 /* If there was a task waiting for data to arrive on the
\r
602 queue then unblock it now. */
\r
603 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
605 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
607 /* The unblocked task has a priority higher than
\r
608 our own so yield immediately. */
\r
609 portYIELD_WITHIN_API();
\r
613 taskEXIT_CRITICAL();
\r
618 if( xTicksToWait == ( portTickType ) 0 )
\r
620 taskEXIT_CRITICAL();
\r
621 return errQUEUE_FULL;
\r
623 else if( xEntryTimeSet == pdFALSE )
\r
625 vTaskSetTimeOutState( &xTimeOut );
\r
626 xEntryTimeSet = pdTRUE;
\r
630 taskEXIT_CRITICAL();
\r
632 taskENTER_CRITICAL();
\r
634 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
636 if( prvIsQueueFull( pxQueue ) )
\r
638 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
639 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
640 portYIELD_WITHIN_API();
\r
645 taskEXIT_CRITICAL();
\r
646 traceQUEUE_SEND_FAILED( pxQueue );
\r
647 return errQUEUE_FULL;
\r
650 taskEXIT_CRITICAL();
\r
654 #endif /* configUSE_ALTERNATIVE_API */
\r
655 /*-----------------------------------------------------------*/
\r
657 #if configUSE_ALTERNATIVE_API == 1
\r
659 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
661 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
662 xTimeOutType xTimeOut;
\r
663 signed char *pcOriginalReadPosition;
\r
665 configASSERT( pxQueue );
\r
666 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
670 taskENTER_CRITICAL();
\r
672 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
674 /* Remember our read position in case we are just peeking. */
\r
675 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
677 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
679 if( xJustPeeking == pdFALSE )
\r
681 traceQUEUE_RECEIVE( pxQueue );
\r
683 /* We are actually removing data. */
\r
684 --( pxQueue->uxMessagesWaiting );
\r
686 #if ( configUSE_MUTEXES == 1 )
\r
688 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
690 /* Record the information required to implement
\r
691 priority inheritance should it become necessary. */
\r
692 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
697 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
699 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
701 portYIELD_WITHIN_API();
\r
707 traceQUEUE_PEEK( pxQueue );
\r
709 /* We are not removing the data, so reset our read
\r
711 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
713 /* The data is being left in the queue, so see if there are
\r
714 any other tasks waiting for the data. */
\r
715 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
717 /* Tasks that are removed from the event list will get added to
\r
718 the pending ready list as the scheduler is still suspended. */
\r
719 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
721 /* The task waiting has a higher priority than this task. */
\r
722 portYIELD_WITHIN_API();
\r
728 taskEXIT_CRITICAL();
\r
733 if( xTicksToWait == ( portTickType ) 0 )
\r
735 taskEXIT_CRITICAL();
\r
736 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
737 return errQUEUE_EMPTY;
\r
739 else if( xEntryTimeSet == pdFALSE )
\r
741 vTaskSetTimeOutState( &xTimeOut );
\r
742 xEntryTimeSet = pdTRUE;
\r
746 taskEXIT_CRITICAL();
\r
748 taskENTER_CRITICAL();
\r
750 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
752 if( prvIsQueueEmpty( pxQueue ) )
\r
754 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
756 #if ( configUSE_MUTEXES == 1 )
\r
758 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
760 portENTER_CRITICAL();
\r
761 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
762 portEXIT_CRITICAL();
\r
767 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
768 portYIELD_WITHIN_API();
\r
773 taskEXIT_CRITICAL();
\r
774 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
775 return errQUEUE_EMPTY;
\r
778 taskEXIT_CRITICAL();
\r
783 #endif /* configUSE_ALTERNATIVE_API */
\r
784 /*-----------------------------------------------------------*/
\r
786 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
788 signed portBASE_TYPE xReturn;
\r
789 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
791 configASSERT( pxQueue );
\r
792 configASSERT( pxHigherPriorityTaskWoken );
\r
793 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
795 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
796 in the queue. Also we don't directly wake a task that was blocked on a
\r
797 queue read, instead we return a flag to say whether a context switch is
\r
798 required or not (i.e. has a task with a higher priority than us been woken
\r
800 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
802 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
804 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
806 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
808 /* If the queue is locked we do not alter the event list. This will
\r
809 be done when the queue is unlocked later. */
\r
810 if( pxQueue->xTxLock == queueUNLOCKED )
\r
812 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
814 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
816 /* The task waiting has a higher priority so record that a
\r
817 context switch is required. */
\r
818 *pxHigherPriorityTaskWoken = pdTRUE;
\r
824 /* Increment the lock count so the task that unlocks the queue
\r
825 knows that data was posted while it was locked. */
\r
826 ++( pxQueue->xTxLock );
\r
833 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
834 xReturn = errQUEUE_FULL;
\r
837 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
841 /*-----------------------------------------------------------*/
\r
843 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
845 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
846 xTimeOutType xTimeOut;
\r
847 signed char *pcOriginalReadPosition;
\r
849 configASSERT( pxQueue );
\r
850 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
852 /* This function relaxes the coding standard somewhat to allow return
\r
853 statements within the function itself. This is done in the interest
\r
854 of execution time efficiency. */
\r
858 taskENTER_CRITICAL();
\r
860 /* Is there data in the queue now? To be running we must be
\r
861 the highest priority task wanting to access the queue. */
\r
862 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
864 /* Remember our read position in case we are just peeking. */
\r
865 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
867 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
869 if( xJustPeeking == pdFALSE )
\r
871 traceQUEUE_RECEIVE( pxQueue );
\r
873 /* We are actually removing data. */
\r
874 --( pxQueue->uxMessagesWaiting );
\r
876 #if ( configUSE_MUTEXES == 1 )
\r
878 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
880 /* Record the information required to implement
\r
881 priority inheritance should it become necessary. */
\r
882 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
887 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
889 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
891 portYIELD_WITHIN_API();
\r
897 traceQUEUE_PEEK( pxQueue );
\r
899 /* We are not removing the data, so reset our read
\r
901 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
903 /* The data is being left in the queue, so see if there are
\r
904 any other tasks waiting for the data. */
\r
905 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
907 /* Tasks that are removed from the event list will get added to
\r
908 the pending ready list as the scheduler is still suspended. */
\r
909 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
911 /* The task waiting has a higher priority than this task. */
\r
912 portYIELD_WITHIN_API();
\r
918 taskEXIT_CRITICAL();
\r
923 if( xTicksToWait == ( portTickType ) 0 )
\r
925 /* The queue was empty and no block time is specified (or
\r
926 the block time has expired) so leave now. */
\r
927 taskEXIT_CRITICAL();
\r
928 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
929 return errQUEUE_EMPTY;
\r
931 else if( xEntryTimeSet == pdFALSE )
\r
933 /* The queue was empty and a block time was specified so
\r
934 configure the timeout structure. */
\r
935 vTaskSetTimeOutState( &xTimeOut );
\r
936 xEntryTimeSet = pdTRUE;
\r
940 taskEXIT_CRITICAL();
\r
942 /* Interrupts and other tasks can send to and receive from the queue
\r
943 now the critical section has been exited. */
\r
946 prvLockQueue( pxQueue );
\r
948 /* Update the timeout state to see if it has expired yet. */
\r
949 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
951 if( prvIsQueueEmpty( pxQueue ) )
\r
953 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
955 #if ( configUSE_MUTEXES == 1 )
\r
957 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
959 portENTER_CRITICAL();
\r
961 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
963 portEXIT_CRITICAL();
\r
968 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
969 prvUnlockQueue( pxQueue );
\r
970 if( !xTaskResumeAll() )
\r
972 portYIELD_WITHIN_API();
\r
978 prvUnlockQueue( pxQueue );
\r
979 ( void ) xTaskResumeAll();
\r
984 prvUnlockQueue( pxQueue );
\r
985 ( void ) xTaskResumeAll();
\r
986 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
987 return errQUEUE_EMPTY;
\r
991 /*-----------------------------------------------------------*/
\r
993 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
995 signed portBASE_TYPE xReturn;
\r
996 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
998 configASSERT( pxQueue );
\r
999 configASSERT( pxTaskWoken );
\r
1000 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1002 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1004 /* We cannot block from an ISR, so check there is data available. */
\r
1005 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1007 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1009 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1010 --( pxQueue->uxMessagesWaiting );
\r
1012 /* If the queue is locked we will not modify the event list. Instead
\r
1013 we update the lock count so the task that unlocks the queue will know
\r
1014 that an ISR has removed data while the queue was locked. */
\r
1015 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1017 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1019 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1021 /* The task waiting has a higher priority than us so
\r
1022 force a context switch. */
\r
1023 *pxTaskWoken = pdTRUE;
\r
1029 /* Increment the lock count so the task that unlocks the queue
\r
1030 knows that data was removed while it was locked. */
\r
1031 ++( pxQueue->xRxLock );
\r
1039 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1042 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1046 /*-----------------------------------------------------------*/
\r
1048 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1050 unsigned portBASE_TYPE uxReturn;
\r
1052 configASSERT( pxQueue );
\r
1054 taskENTER_CRITICAL();
\r
1055 uxReturn = pxQueue->uxMessagesWaiting;
\r
1056 taskEXIT_CRITICAL();
\r
1060 /*-----------------------------------------------------------*/
\r
1062 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1064 unsigned portBASE_TYPE uxReturn;
\r
1066 configASSERT( pxQueue );
\r
1068 uxReturn = pxQueue->uxMessagesWaiting;
\r
1072 /*-----------------------------------------------------------*/
\r
1074 void vQueueDelete( xQueueHandle pxQueue )
\r
1076 configASSERT( pxQueue );
\r
1078 traceQUEUE_DELETE( pxQueue );
\r
1079 vQueueUnregisterQueue( pxQueue );
\r
1080 vPortFree( pxQueue->pcHead );
\r
1081 vPortFree( pxQueue );
\r
1083 /*-----------------------------------------------------------*/
\r
1085 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1087 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1089 #if ( configUSE_MUTEXES == 1 )
\r
1091 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1093 /* The mutex is no longer being held. */
\r
1094 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1095 pxQueue->pxMutexHolder = NULL;
\r
1100 else if( xPosition == queueSEND_TO_BACK )
\r
1102 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1103 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1104 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1106 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1111 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1112 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1113 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1115 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1119 ++( pxQueue->uxMessagesWaiting );
\r
1121 /*-----------------------------------------------------------*/
\r
1123 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1125 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1127 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1128 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1130 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1132 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1135 /*-----------------------------------------------------------*/
\r
1137 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1139 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1141 /* The lock counts contains the number of extra data items placed or
\r
1142 removed from the queue while the queue was locked. When a queue is
\r
1143 locked items can be added or removed, but the event lists cannot be
\r
1145 taskENTER_CRITICAL();
\r
1147 /* See if data was added to the queue while it was locked. */
\r
1148 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1150 /* Data was posted while the queue was locked. Are any tasks
\r
1151 blocked waiting for data to become available? */
\r
1152 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1154 /* Tasks that are removed from the event list will get added to
\r
1155 the pending ready list as the scheduler is still suspended. */
\r
1156 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1158 /* The task waiting has a higher priority so record that a
\r
1159 context switch is required. */
\r
1160 vTaskMissedYield();
\r
1163 --( pxQueue->xTxLock );
\r
1171 pxQueue->xTxLock = queueUNLOCKED;
\r
1173 taskEXIT_CRITICAL();
\r
1175 /* Do the same for the Rx lock. */
\r
1176 taskENTER_CRITICAL();
\r
1178 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1180 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1182 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1184 vTaskMissedYield();
\r
1187 --( pxQueue->xRxLock );
\r
1195 pxQueue->xRxLock = queueUNLOCKED;
\r
1197 taskEXIT_CRITICAL();
\r
1199 /*-----------------------------------------------------------*/
\r
1201 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1203 signed portBASE_TYPE xReturn;
\r
1205 taskENTER_CRITICAL();
\r
1206 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1207 taskEXIT_CRITICAL();
\r
1211 /*-----------------------------------------------------------*/
\r
1213 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1215 signed portBASE_TYPE xReturn;
\r
1217 configASSERT( pxQueue );
\r
1218 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1222 /*-----------------------------------------------------------*/
\r
1224 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1226 signed portBASE_TYPE xReturn;
\r
1228 taskENTER_CRITICAL();
\r
1229 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1230 taskEXIT_CRITICAL();
\r
1234 /*-----------------------------------------------------------*/
\r
1236 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1238 signed portBASE_TYPE xReturn;
\r
1240 configASSERT( pxQueue );
\r
1241 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1245 /*-----------------------------------------------------------*/
\r
1247 #if configUSE_CO_ROUTINES == 1
\r
1248 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1250 signed portBASE_TYPE xReturn;
\r
1252 /* If the queue is already full we may have to block. A critical section
\r
1253 is required to prevent an interrupt removing something from the queue
\r
1254 between the check to see if the queue is full and blocking on the queue. */
\r
1255 portDISABLE_INTERRUPTS();
\r
1257 if( prvIsQueueFull( pxQueue ) )
\r
1259 /* The queue is full - do we want to block or just leave without
\r
1261 if( xTicksToWait > ( portTickType ) 0 )
\r
1263 /* As this is called from a coroutine we cannot block directly, but
\r
1264 return indicating that we need to block. */
\r
1265 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1266 portENABLE_INTERRUPTS();
\r
1267 return errQUEUE_BLOCKED;
\r
1271 portENABLE_INTERRUPTS();
\r
1272 return errQUEUE_FULL;
\r
1276 portENABLE_INTERRUPTS();
\r
1280 portDISABLE_INTERRUPTS();
\r
1282 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1284 /* There is room in the queue, copy the data into the queue. */
\r
1285 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1288 /* Were any co-routines waiting for data to become available? */
\r
1289 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1291 /* In this instance the co-routine could be placed directly
\r
1292 into the ready list as we are within a critical section.
\r
1293 Instead the same pending ready list mechanism is used as if
\r
1294 the event were caused from within an interrupt. */
\r
1295 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1297 /* The co-routine waiting has a higher priority so record
\r
1298 that a yield might be appropriate. */
\r
1299 xReturn = errQUEUE_YIELD;
\r
1305 xReturn = errQUEUE_FULL;
\r
1308 portENABLE_INTERRUPTS();
\r
1313 /*-----------------------------------------------------------*/
\r
1315 #if configUSE_CO_ROUTINES == 1
\r
1316 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1318 signed portBASE_TYPE xReturn;
\r
1320 /* If the queue is already empty we may have to block. A critical section
\r
1321 is required to prevent an interrupt adding something to the queue
\r
1322 between the check to see if the queue is empty and blocking on the queue. */
\r
1323 portDISABLE_INTERRUPTS();
\r
1325 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1327 /* There are no messages in the queue, do we want to block or just
\r
1328 leave with nothing? */
\r
1329 if( xTicksToWait > ( portTickType ) 0 )
\r
1331 /* As this is a co-routine we cannot block directly, but return
\r
1332 indicating that we need to block. */
\r
1333 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1334 portENABLE_INTERRUPTS();
\r
1335 return errQUEUE_BLOCKED;
\r
1339 portENABLE_INTERRUPTS();
\r
1340 return errQUEUE_FULL;
\r
1344 portENABLE_INTERRUPTS();
\r
1348 portDISABLE_INTERRUPTS();
\r
1350 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1352 /* Data is available from the queue. */
\r
1353 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1354 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1356 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1358 --( pxQueue->uxMessagesWaiting );
\r
1359 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1363 /* Were any co-routines waiting for space to become available? */
\r
1364 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1366 /* In this instance the co-routine could be placed directly
\r
1367 into the ready list as we are within a critical section.
\r
1368 Instead the same pending ready list mechanism is used as if
\r
1369 the event were caused from within an interrupt. */
\r
1370 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1372 xReturn = errQUEUE_YIELD;
\r
1381 portENABLE_INTERRUPTS();
\r
1386 /*-----------------------------------------------------------*/
\r
1390 #if configUSE_CO_ROUTINES == 1
\r
1391 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1393 /* Cannot block within an ISR so if there is no space on the queue then
\r
1394 exit without doing anything. */
\r
1395 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1397 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1399 /* We only want to wake one co-routine per ISR, so check that a
\r
1400 co-routine has not already been woken. */
\r
1401 if( !xCoRoutinePreviouslyWoken )
\r
1403 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1405 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1413 return xCoRoutinePreviouslyWoken;
\r
1416 /*-----------------------------------------------------------*/
\r
1418 #if configUSE_CO_ROUTINES == 1
\r
1419 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1421 signed portBASE_TYPE xReturn;
\r
1423 /* We cannot block from an ISR, so check there is data available. If
\r
1424 not then just leave without doing anything. */
\r
1425 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1427 /* Copy the data from the queue. */
\r
1428 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1429 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1431 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1433 --( pxQueue->uxMessagesWaiting );
\r
1434 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1436 if( !( *pxCoRoutineWoken ) )
\r
1438 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1440 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1442 *pxCoRoutineWoken = pdTRUE;
\r
1457 /*-----------------------------------------------------------*/
\r
1459 #if configQUEUE_REGISTRY_SIZE > 0
\r
1461 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1463 unsigned portBASE_TYPE ux;
\r
1465 /* See if there is an empty space in the registry. A NULL name denotes
\r
1467 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1469 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1471 /* Store the information on this queue. */
\r
1472 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1473 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1480 /*-----------------------------------------------------------*/
\r
1482 #if configQUEUE_REGISTRY_SIZE > 0
\r
1484 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1486 unsigned portBASE_TYPE ux;
\r
1488 /* See if the handle of the queue being unregistered in actually in the
\r
1490 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1492 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1494 /* Set the name to NULL to show that this slot if free again. */
\r
1495 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1503 /*-----------------------------------------------------------*/
\r
1505 #if configUSE_TIMERS == 1
\r
1507 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1509 /* This function should not be called by application code hence the
\r
1510 'Restricted' in its name. It is not part of the public API. It is
\r
1511 designed for use by kernel code, and has special calling requirements.
\r
1512 It can result in vListInsert() being called on a list that can only
\r
1513 possibly ever have one item in it, so the list will be fast, but even
\r
1514 so it should be called with the scheduler locked and not from a critical
\r
1517 /* Only do anything if there are no messages in the queue. This function
\r
1518 will not actually cause the task to block, just place it on a blocked
\r
1519 list. It will not block until the scheduler is unlocked - at which
\r
1520 time a yield will be performed. If an item is added to the queue while
\r
1521 the queue is locked, and the calling task blocks on the queue, then the
\r
1522 calling task will be immediately unblocked when the queue is unlocked. */
\r
1523 prvLockQueue( pxQueue );
\r
1524 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1526 /* There is nothing in the queue, block for the specified period. */
\r
1527 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1529 prvUnlockQueue( pxQueue );
\r