2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
52 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
53 all the API functions to use the MPU wrappers. That should only be done when
\r
54 task.h is included from an application file. */
\r
55 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
57 #include "FreeRTOS.h"
\r
59 #include "croutine.h"
\r
61 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
63 /*-----------------------------------------------------------
\r
64 * PUBLIC LIST API documented in list.h
\r
65 *----------------------------------------------------------*/
\r
67 /* Constants used with the cRxLock and cTxLock structure members. */
\r
68 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
69 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
71 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
73 /* For internal use only. */
\r
74 #define queueSEND_TO_BACK ( 0 )
\r
75 #define queueSEND_TO_FRONT ( 1 )
\r
77 /* Effectively make a union out of the xQUEUE structure. */
\r
78 #define pxMutexHolder pcTail
\r
79 #define uxQueueType pcHead
\r
80 #define uxRecursiveCallCount pcReadFrom
\r
81 #define queueQUEUE_IS_MUTEX NULL
\r
83 /* Semaphores do not actually store or copy data, so have an items size of
\r
85 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
86 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
87 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
90 * Definition of the queue used by the scheduler.
\r
91 * Items are queued by copy, not reference.
\r
93 typedef struct QueueDefinition
\r
95 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
96 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
98 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
99 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
101 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
102 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
104 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
105 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
106 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
108 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
109 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
112 /*-----------------------------------------------------------*/
\r
115 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
116 * To keep the definition private the API header file defines it as a
\r
119 typedef xQUEUE * xQueueHandle;
\r
122 * Prototypes for public functions are included here so we don't have to
\r
123 * include the API header file (as it defines xQueueHandle differently). These
\r
124 * functions are documented in the API header file.
\r
126 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize ) PRIVILEGED_FUNCTION;
\r
127 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
128 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
129 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
130 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
131 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
132 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
133 xQueueHandle xQueueCreateMutex( void ) PRIVILEGED_FUNCTION;
\r
134 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
135 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
136 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
137 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
138 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
139 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
140 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
141 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
144 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
145 * an optional component.
\r
147 #if configUSE_CO_ROUTINES == 1
\r
148 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
149 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
150 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
151 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
155 * The queue registry is just a means for kernel aware debuggers to locate
\r
156 * queue structures. It has no other purpose so is an optional component.
\r
158 #if configQUEUE_REGISTRY_SIZE > 0
\r
160 /* The type stored within the queue registry array. This allows a name
\r
161 to be assigned to each queue making kernel aware debugging a little
\r
162 more user friendly. */
\r
163 typedef struct QUEUE_REGISTRY_ITEM
\r
165 signed char *pcQueueName;
\r
166 xQueueHandle xHandle;
\r
167 } xQueueRegistryItem;
\r
169 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
170 The pcQueueName member of a structure being NULL is indicative of the
\r
171 array position being vacant. */
\r
172 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
174 /* Removes a queue from the registry by simply setting the pcQueueName
\r
176 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
177 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
181 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
182 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
183 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
184 * queue is locked it will instead increment the appropriate queue lock count
\r
185 * to indicate that a task may require unblocking. When the queue in unlocked
\r
186 * these lock counts are inspected, and the appropriate action taken.
\r
188 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
191 * Uses a critical section to determine if there is any data in a queue.
\r
193 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
195 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
198 * Uses a critical section to determine if there is any space in a queue.
\r
200 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
202 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
205 * Copies an item into the queue, either at the front of the queue or the
\r
206 * back of the queue.
\r
208 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
211 * Copies an item out of a queue.
\r
213 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
214 /*-----------------------------------------------------------*/
\r
217 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
218 * accessing the queue event lists.
\r
220 #define prvLockQueue( pxQueue ) \
\r
222 taskENTER_CRITICAL(); \
\r
224 if( pxQueue->xRxLock == queueUNLOCKED ) \
\r
226 pxQueue->xRxLock = queueLOCKED_UNMODIFIED; \
\r
228 if( pxQueue->xTxLock == queueUNLOCKED ) \
\r
230 pxQueue->xTxLock = queueLOCKED_UNMODIFIED; \
\r
233 taskEXIT_CRITICAL(); \
\r
235 /*-----------------------------------------------------------*/
\r
238 /*-----------------------------------------------------------
\r
239 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
240 *----------------------------------------------------------*/
\r
242 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
244 xQUEUE *pxNewQueue;
\r
245 size_t xQueueSizeInBytes;
\r
247 /* Allocate the new queue structure. */
\r
248 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
250 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
251 if( pxNewQueue != NULL )
\r
253 /* Create the list of pointers to queue items. The queue is one byte
\r
254 longer than asked for to make wrap checking easier/faster. */
\r
255 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
257 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
258 if( pxNewQueue->pcHead != NULL )
\r
260 /* Initialise the queue members as described above where the
\r
261 queue type is defined. */
\r
262 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
263 pxNewQueue->uxMessagesWaiting = 0;
\r
264 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
265 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * uxItemSize );
\r
266 pxNewQueue->uxLength = uxQueueLength;
\r
267 pxNewQueue->uxItemSize = uxItemSize;
\r
268 pxNewQueue->xRxLock = queueUNLOCKED;
\r
269 pxNewQueue->xTxLock = queueUNLOCKED;
\r
271 /* Likewise ensure the event queues start with the correct state. */
\r
272 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
273 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
275 traceQUEUE_CREATE( pxNewQueue );
\r
280 traceQUEUE_CREATE_FAILED();
\r
281 vPortFree( pxNewQueue );
\r
286 /* Will only reach here if we could not allocate enough memory or no memory
\r
290 /*-----------------------------------------------------------*/
\r
292 #if ( configUSE_MUTEXES == 1 )
\r
294 xQueueHandle xQueueCreateMutex( void )
\r
296 xQUEUE *pxNewQueue;
\r
298 /* Allocate the new queue structure. */
\r
299 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
300 if( pxNewQueue != NULL )
\r
302 /* Information required for priority inheritance. */
\r
303 pxNewQueue->pxMutexHolder = NULL;
\r
304 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
306 /* Queues used as a mutex no data is actually copied into or out
\r
308 pxNewQueue->pcWriteTo = NULL;
\r
309 pxNewQueue->pcReadFrom = NULL;
\r
311 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
312 an item size of 0 as nothing is actually copied into or out
\r
314 pxNewQueue->uxMessagesWaiting = 0;
\r
315 pxNewQueue->uxLength = 1;
\r
316 pxNewQueue->uxItemSize = 0;
\r
317 pxNewQueue->xRxLock = queueUNLOCKED;
\r
318 pxNewQueue->xTxLock = queueUNLOCKED;
\r
320 /* Ensure the event queues start with the correct state. */
\r
321 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
322 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
324 /* Start with the semaphore in the expected state. */
\r
325 xQueueGenericSend( pxNewQueue, NULL, 0, queueSEND_TO_BACK );
\r
327 traceCREATE_MUTEX( pxNewQueue );
\r
331 traceCREATE_MUTEX_FAILED();
\r
337 #endif /* configUSE_MUTEXES */
\r
338 /*-----------------------------------------------------------*/
\r
340 #if configUSE_RECURSIVE_MUTEXES == 1
\r
342 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
344 portBASE_TYPE xReturn;
\r
346 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
347 change outside of this task. If this task does not hold the mutex then
\r
348 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
349 this is the only condition we are interested in it does not matter if
\r
350 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
351 mutual exclusion is required to test the pxMutexHolder variable. */
\r
352 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
354 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
356 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
357 the task handle, therefore no underflow check is required. Also,
\r
358 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
359 there can only be one, no mutual exclusion is required to modify the
\r
360 uxRecursiveCallCount member. */
\r
361 ( pxMutex->uxRecursiveCallCount )--;
\r
363 /* Have we unwound the call count? */
\r
364 if( pxMutex->uxRecursiveCallCount == 0 )
\r
366 /* Return the mutex. This will automatically unblock any other
\r
367 task that might be waiting to access the mutex. */
\r
368 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
375 /* We cannot give the mutex because we are not the holder. */
\r
378 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
384 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
385 /*-----------------------------------------------------------*/
\r
387 #if configUSE_RECURSIVE_MUTEXES == 1
\r
389 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
391 portBASE_TYPE xReturn;
\r
393 /* Comments regarding mutual exclusion as per those within
\r
394 xQueueGiveMutexRecursive(). */
\r
396 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
398 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
400 ( pxMutex->uxRecursiveCallCount )++;
\r
405 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
407 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
408 we may have blocked to reach here. */
\r
409 if( xReturn == pdPASS )
\r
411 ( pxMutex->uxRecursiveCallCount )++;
\r
418 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
419 /*-----------------------------------------------------------*/
\r
421 #if configUSE_COUNTING_SEMAPHORES == 1
\r
423 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
425 xQueueHandle pxHandle;
\r
427 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
429 if( pxHandle != NULL )
\r
431 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
433 traceCREATE_COUNTING_SEMAPHORE();
\r
437 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
443 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
444 /*-----------------------------------------------------------*/
\r
446 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
448 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
449 xTimeOutType xTimeOut;
\r
451 /* This function relaxes the coding standard somewhat to allow return
\r
452 statements within the function itself. This is done in the interest
\r
453 of execution time efficiency. */
\r
456 taskENTER_CRITICAL();
\r
458 /* Is there room on the queue now? To be running we must be
\r
459 the highest priority task wanting to access the queue. */
\r
460 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
462 traceQUEUE_SEND( pxQueue );
\r
463 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
465 /* If there was a task waiting for data to arrive on the
\r
466 queue then unblock it now. */
\r
467 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
469 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
471 /* The unblocked task has a priority higher than
\r
472 our own so yield immediately. Yes it is ok to do
\r
473 this from within the critical section - the kernel
\r
474 takes care of that. */
\r
475 portYIELD_WITHIN_API();
\r
479 taskEXIT_CRITICAL();
\r
481 /* Return to the original privilege level before exiting the
\r
487 if( xTicksToWait == ( portTickType ) 0 )
\r
489 /* The queue was full and no block time is specified (or
\r
490 the block time has expired) so leave now. */
\r
491 taskEXIT_CRITICAL();
\r
493 /* Return to the original privilege level before exiting
\r
495 traceQUEUE_SEND_FAILED( pxQueue );
\r
496 return errQUEUE_FULL;
\r
498 else if( xEntryTimeSet == pdFALSE )
\r
500 /* The queue was full and a block time was specified so
\r
501 configure the timeout structure. */
\r
502 vTaskSetTimeOutState( &xTimeOut );
\r
503 xEntryTimeSet = pdTRUE;
\r
507 taskEXIT_CRITICAL();
\r
509 /* Interrupts and other tasks can send to and receive from the queue
\r
510 now the critical section has been exited. */
\r
513 prvLockQueue( pxQueue );
\r
515 /* Update the timeout state to see if it has expired yet. */
\r
516 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
518 if( prvIsQueueFull( pxQueue ) )
\r
520 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
521 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
523 /* Unlocking the queue means queue events can effect the
\r
524 event list. It is possible that interrupts occurring now
\r
525 remove this task from the event list again - but as the
\r
526 scheduler is suspended the task will go onto the pending
\r
527 ready last instead of the actual ready list. */
\r
528 prvUnlockQueue( pxQueue );
\r
530 /* Resuming the scheduler will move tasks from the pending
\r
531 ready list into the ready list - so it is feasible that this
\r
532 task is already in a ready list before it yields - in which
\r
533 case the yield will not cause a context switch unless there
\r
534 is also a higher priority task in the pending ready list. */
\r
535 if( !xTaskResumeAll() )
\r
537 portYIELD_WITHIN_API();
\r
543 prvUnlockQueue( pxQueue );
\r
544 ( void ) xTaskResumeAll();
\r
549 /* The timeout has expired. */
\r
550 prvUnlockQueue( pxQueue );
\r
551 ( void ) xTaskResumeAll();
\r
553 /* Return to the original privilege level before exiting the
\r
555 traceQUEUE_SEND_FAILED( pxQueue );
\r
556 return errQUEUE_FULL;
\r
560 /*-----------------------------------------------------------*/
\r
562 #if configUSE_ALTERNATIVE_API == 1
\r
564 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
566 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
567 xTimeOutType xTimeOut;
\r
571 taskENTER_CRITICAL();
\r
573 /* Is there room on the queue now? To be running we must be
\r
574 the highest priority task wanting to access the queue. */
\r
575 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
577 traceQUEUE_SEND( pxQueue );
\r
578 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
580 /* If there was a task waiting for data to arrive on the
\r
581 queue then unblock it now. */
\r
582 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
584 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
586 /* The unblocked task has a priority higher than
\r
587 our own so yield immediately. */
\r
588 portYIELD_WITHIN_API();
\r
592 taskEXIT_CRITICAL();
\r
597 if( xTicksToWait == ( portTickType ) 0 )
\r
599 taskEXIT_CRITICAL();
\r
600 return errQUEUE_FULL;
\r
602 else if( xEntryTimeSet == pdFALSE )
\r
604 vTaskSetTimeOutState( &xTimeOut );
\r
605 xEntryTimeSet = pdTRUE;
\r
609 taskEXIT_CRITICAL();
\r
611 taskENTER_CRITICAL();
\r
613 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
615 if( prvIsQueueFull( pxQueue ) )
\r
617 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
618 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
619 portYIELD_WITHIN_API();
\r
624 taskEXIT_CRITICAL();
\r
625 traceQUEUE_SEND_FAILED( pxQueue );
\r
626 return errQUEUE_FULL;
\r
629 taskEXIT_CRITICAL();
\r
633 #endif /* configUSE_ALTERNATIVE_API */
\r
634 /*-----------------------------------------------------------*/
\r
636 #if configUSE_ALTERNATIVE_API == 1
\r
638 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
640 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
641 xTimeOutType xTimeOut;
\r
642 signed char *pcOriginalReadPosition;
\r
646 taskENTER_CRITICAL();
\r
648 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
650 /* Remember our read position in case we are just peeking. */
\r
651 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
653 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
655 if( xJustPeeking == pdFALSE )
\r
657 traceQUEUE_RECEIVE( pxQueue );
\r
659 /* We are actually removing data. */
\r
660 --( pxQueue->uxMessagesWaiting );
\r
662 #if ( configUSE_MUTEXES == 1 )
\r
664 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
666 /* Record the information required to implement
\r
667 priority inheritance should it become necessary. */
\r
668 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
673 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
675 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
677 portYIELD_WITHIN_API();
\r
683 traceQUEUE_PEEK( pxQueue );
\r
685 /* We are not removing the data, so reset our read
\r
687 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
689 /* The data is being left in the queue, so see if there are
\r
690 any other tasks waiting for the data. */
\r
691 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
693 /* Tasks that are removed from the event list will get added to
\r
694 the pending ready list as the scheduler is still suspended. */
\r
695 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
697 /* The task waiting has a higher priority than this task. */
\r
698 portYIELD_WITHIN_API();
\r
704 taskEXIT_CRITICAL();
\r
709 if( xTicksToWait == ( portTickType ) 0 )
\r
711 taskEXIT_CRITICAL();
\r
712 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
713 return errQUEUE_EMPTY;
\r
715 else if( xEntryTimeSet == pdFALSE )
\r
717 vTaskSetTimeOutState( &xTimeOut );
\r
718 xEntryTimeSet = pdTRUE;
\r
722 taskEXIT_CRITICAL();
\r
724 taskENTER_CRITICAL();
\r
726 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
728 if( prvIsQueueEmpty( pxQueue ) )
\r
730 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
732 #if ( configUSE_MUTEXES == 1 )
\r
734 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
736 portENTER_CRITICAL();
\r
737 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
738 portEXIT_CRITICAL();
\r
743 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
744 portYIELD_WITHIN_API();
\r
749 taskEXIT_CRITICAL();
\r
750 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
751 return errQUEUE_EMPTY;
\r
754 taskEXIT_CRITICAL();
\r
759 #endif /* configUSE_ALTERNATIVE_API */
\r
760 /*-----------------------------------------------------------*/
\r
762 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
764 signed portBASE_TYPE xReturn;
\r
765 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
767 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
768 in the queue. Also we don't directly wake a task that was blocked on a
\r
769 queue read, instead we return a flag to say whether a context switch is
\r
770 required or not (i.e. has a task with a higher priority than us been woken
\r
772 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
774 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
776 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
778 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
780 /* If the queue is locked we do not alter the event list. This will
\r
781 be done when the queue is unlocked later. */
\r
782 if( pxQueue->xTxLock == queueUNLOCKED )
\r
784 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
786 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
788 /* The task waiting has a higher priority so record that a
\r
789 context switch is required. */
\r
790 *pxHigherPriorityTaskWoken = pdTRUE;
\r
796 /* Increment the lock count so the task that unlocks the queue
\r
797 knows that data was posted while it was locked. */
\r
798 ++( pxQueue->xTxLock );
\r
805 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
806 xReturn = errQUEUE_FULL;
\r
809 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
813 /*-----------------------------------------------------------*/
\r
815 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
817 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
818 xTimeOutType xTimeOut;
\r
819 signed char *pcOriginalReadPosition;
\r
821 /* This function relaxes the coding standard somewhat to allow return
\r
822 statements within the function itself. This is done in the interest
\r
823 of execution time efficiency. */
\r
827 taskENTER_CRITICAL();
\r
829 /* Is there data in the queue now? To be running we must be
\r
830 the highest priority task wanting to access the queue. */
\r
831 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
833 /* Remember our read position in case we are just peeking. */
\r
834 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
836 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
838 if( xJustPeeking == pdFALSE )
\r
840 traceQUEUE_RECEIVE( pxQueue );
\r
842 /* We are actually removing data. */
\r
843 --( pxQueue->uxMessagesWaiting );
\r
845 #if ( configUSE_MUTEXES == 1 )
\r
847 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
849 /* Record the information required to implement
\r
850 priority inheritance should it become necessary. */
\r
851 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
856 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
858 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
860 portYIELD_WITHIN_API();
\r
866 traceQUEUE_PEEK( pxQueue );
\r
868 /* We are not removing the data, so reset our read
\r
870 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
872 /* The data is being left in the queue, so see if there are
\r
873 any other tasks waiting for the data. */
\r
874 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
876 /* Tasks that are removed from the event list will get added to
\r
877 the pending ready list as the scheduler is still suspended. */
\r
878 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
880 /* The task waiting has a higher priority than this task. */
\r
881 portYIELD_WITHIN_API();
\r
887 taskEXIT_CRITICAL();
\r
892 if( xTicksToWait == ( portTickType ) 0 )
\r
894 /* The queue was empty and no block time is specified (or
\r
895 the block time has expired) so leave now. */
\r
896 taskEXIT_CRITICAL();
\r
897 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
898 return errQUEUE_EMPTY;
\r
900 else if( xEntryTimeSet == pdFALSE )
\r
902 /* The queue was empty and a block time was specified so
\r
903 configure the timeout structure. */
\r
904 vTaskSetTimeOutState( &xTimeOut );
\r
905 xEntryTimeSet = pdTRUE;
\r
909 taskEXIT_CRITICAL();
\r
911 /* Interrupts and other tasks can send to and receive from the queue
\r
912 now the critical section has been exited. */
\r
915 prvLockQueue( pxQueue );
\r
917 /* Update the timeout state to see if it has expired yet. */
\r
918 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
920 if( prvIsQueueEmpty( pxQueue ) )
\r
922 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
924 #if ( configUSE_MUTEXES == 1 )
\r
926 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
928 portENTER_CRITICAL();
\r
930 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
932 portEXIT_CRITICAL();
\r
937 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
938 prvUnlockQueue( pxQueue );
\r
939 if( !xTaskResumeAll() )
\r
941 portYIELD_WITHIN_API();
\r
947 prvUnlockQueue( pxQueue );
\r
948 ( void ) xTaskResumeAll();
\r
953 prvUnlockQueue( pxQueue );
\r
954 ( void ) xTaskResumeAll();
\r
955 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
956 return errQUEUE_EMPTY;
\r
960 /*-----------------------------------------------------------*/
\r
962 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
964 signed portBASE_TYPE xReturn;
\r
965 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
967 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
969 /* We cannot block from an ISR, so check there is data available. */
\r
970 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
972 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
974 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
975 --( pxQueue->uxMessagesWaiting );
\r
977 /* If the queue is locked we will not modify the event list. Instead
\r
978 we update the lock count so the task that unlocks the queue will know
\r
979 that an ISR has removed data while the queue was locked. */
\r
980 if( pxQueue->xRxLock == queueUNLOCKED )
\r
982 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
984 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
986 /* The task waiting has a higher priority than us so
\r
987 force a context switch. */
\r
988 *pxTaskWoken = pdTRUE;
\r
994 /* Increment the lock count so the task that unlocks the queue
\r
995 knows that data was removed while it was locked. */
\r
996 ++( pxQueue->xRxLock );
\r
1004 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1007 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1011 /*-----------------------------------------------------------*/
\r
1013 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1015 unsigned portBASE_TYPE uxReturn;
\r
1017 taskENTER_CRITICAL();
\r
1018 uxReturn = pxQueue->uxMessagesWaiting;
\r
1019 taskEXIT_CRITICAL();
\r
1023 /*-----------------------------------------------------------*/
\r
1025 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1027 unsigned portBASE_TYPE uxReturn;
\r
1029 uxReturn = pxQueue->uxMessagesWaiting;
\r
1033 /*-----------------------------------------------------------*/
\r
1035 void vQueueDelete( xQueueHandle pxQueue )
\r
1037 traceQUEUE_DELETE( pxQueue );
\r
1038 vQueueUnregisterQueue( pxQueue );
\r
1039 vPortFree( pxQueue->pcHead );
\r
1040 vPortFree( pxQueue );
\r
1042 /*-----------------------------------------------------------*/
\r
1044 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1046 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1048 #if ( configUSE_MUTEXES == 1 )
\r
1050 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1052 /* The mutex is no longer being held. */
\r
1053 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1054 pxQueue->pxMutexHolder = NULL;
\r
1059 else if( xPosition == queueSEND_TO_BACK )
\r
1061 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1062 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1063 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1065 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1070 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1071 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1072 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1074 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1078 ++( pxQueue->uxMessagesWaiting );
\r
1080 /*-----------------------------------------------------------*/
\r
1082 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1084 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1086 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1087 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1089 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1091 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1094 /*-----------------------------------------------------------*/
\r
1096 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1098 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1100 /* The lock counts contains the number of extra data items placed or
\r
1101 removed from the queue while the queue was locked. When a queue is
\r
1102 locked items can be added or removed, but the event lists cannot be
\r
1104 taskENTER_CRITICAL();
\r
1106 /* See if data was added to the queue while it was locked. */
\r
1107 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1109 /* Data was posted while the queue was locked. Are any tasks
\r
1110 blocked waiting for data to become available? */
\r
1111 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1113 /* Tasks that are removed from the event list will get added to
\r
1114 the pending ready list as the scheduler is still suspended. */
\r
1115 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1117 /* The task waiting has a higher priority so record that a
\r
1118 context switch is required. */
\r
1119 vTaskMissedYield();
\r
1122 --( pxQueue->xTxLock );
\r
1130 pxQueue->xTxLock = queueUNLOCKED;
\r
1132 taskEXIT_CRITICAL();
\r
1134 /* Do the same for the Rx lock. */
\r
1135 taskENTER_CRITICAL();
\r
1137 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1139 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1141 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1143 vTaskMissedYield();
\r
1146 --( pxQueue->xRxLock );
\r
1154 pxQueue->xRxLock = queueUNLOCKED;
\r
1156 taskEXIT_CRITICAL();
\r
1158 /*-----------------------------------------------------------*/
\r
1160 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1162 signed portBASE_TYPE xReturn;
\r
1164 taskENTER_CRITICAL();
\r
1165 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1166 taskEXIT_CRITICAL();
\r
1170 /*-----------------------------------------------------------*/
\r
1172 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1174 signed portBASE_TYPE xReturn;
\r
1176 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1180 /*-----------------------------------------------------------*/
\r
1182 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1184 signed portBASE_TYPE xReturn;
\r
1186 taskENTER_CRITICAL();
\r
1187 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1188 taskEXIT_CRITICAL();
\r
1192 /*-----------------------------------------------------------*/
\r
1194 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1196 signed portBASE_TYPE xReturn;
\r
1198 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1202 /*-----------------------------------------------------------*/
\r
1204 #if configUSE_CO_ROUTINES == 1
\r
1205 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1207 signed portBASE_TYPE xReturn;
\r
1209 /* If the queue is already full we may have to block. A critical section
\r
1210 is required to prevent an interrupt removing something from the queue
\r
1211 between the check to see if the queue is full and blocking on the queue. */
\r
1212 portDISABLE_INTERRUPTS();
\r
1214 if( prvIsQueueFull( pxQueue ) )
\r
1216 /* The queue is full - do we want to block or just leave without
\r
1218 if( xTicksToWait > ( portTickType ) 0 )
\r
1220 /* As this is called from a coroutine we cannot block directly, but
\r
1221 return indicating that we need to block. */
\r
1222 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1223 portENABLE_INTERRUPTS();
\r
1224 return errQUEUE_BLOCKED;
\r
1228 portENABLE_INTERRUPTS();
\r
1229 return errQUEUE_FULL;
\r
1233 portENABLE_INTERRUPTS();
\r
1237 portDISABLE_INTERRUPTS();
\r
1239 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1241 /* There is room in the queue, copy the data into the queue. */
\r
1242 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1245 /* Were any co-routines waiting for data to become available? */
\r
1246 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1248 /* In this instance the co-routine could be placed directly
\r
1249 into the ready list as we are within a critical section.
\r
1250 Instead the same pending ready list mechanism is used as if
\r
1251 the event were caused from within an interrupt. */
\r
1252 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1254 /* The co-routine waiting has a higher priority so record
\r
1255 that a yield might be appropriate. */
\r
1256 xReturn = errQUEUE_YIELD;
\r
1262 xReturn = errQUEUE_FULL;
\r
1265 portENABLE_INTERRUPTS();
\r
1270 /*-----------------------------------------------------------*/
\r
1272 #if configUSE_CO_ROUTINES == 1
\r
1273 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1275 signed portBASE_TYPE xReturn;
\r
1277 /* If the queue is already empty we may have to block. A critical section
\r
1278 is required to prevent an interrupt adding something to the queue
\r
1279 between the check to see if the queue is empty and blocking on the queue. */
\r
1280 portDISABLE_INTERRUPTS();
\r
1282 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1284 /* There are no messages in the queue, do we want to block or just
\r
1285 leave with nothing? */
\r
1286 if( xTicksToWait > ( portTickType ) 0 )
\r
1288 /* As this is a co-routine we cannot block directly, but return
\r
1289 indicating that we need to block. */
\r
1290 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1291 portENABLE_INTERRUPTS();
\r
1292 return errQUEUE_BLOCKED;
\r
1296 portENABLE_INTERRUPTS();
\r
1297 return errQUEUE_FULL;
\r
1301 portENABLE_INTERRUPTS();
\r
1305 portDISABLE_INTERRUPTS();
\r
1307 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1309 /* Data is available from the queue. */
\r
1310 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1311 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1313 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1315 --( pxQueue->uxMessagesWaiting );
\r
1316 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1320 /* Were any co-routines waiting for space to become available? */
\r
1321 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1323 /* In this instance the co-routine could be placed directly
\r
1324 into the ready list as we are within a critical section.
\r
1325 Instead the same pending ready list mechanism is used as if
\r
1326 the event were caused from within an interrupt. */
\r
1327 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1329 xReturn = errQUEUE_YIELD;
\r
1338 portENABLE_INTERRUPTS();
\r
1343 /*-----------------------------------------------------------*/
\r
1347 #if configUSE_CO_ROUTINES == 1
\r
1348 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1350 /* Cannot block within an ISR so if there is no space on the queue then
\r
1351 exit without doing anything. */
\r
1352 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1354 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1356 /* We only want to wake one co-routine per ISR, so check that a
\r
1357 co-routine has not already been woken. */
\r
1358 if( !xCoRoutinePreviouslyWoken )
\r
1360 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1362 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1370 return xCoRoutinePreviouslyWoken;
\r
1373 /*-----------------------------------------------------------*/
\r
1375 #if configUSE_CO_ROUTINES == 1
\r
1376 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1378 signed portBASE_TYPE xReturn;
\r
1380 /* We cannot block from an ISR, so check there is data available. If
\r
1381 not then just leave without doing anything. */
\r
1382 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1384 /* Copy the data from the queue. */
\r
1385 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1386 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1388 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1390 --( pxQueue->uxMessagesWaiting );
\r
1391 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1393 if( !( *pxCoRoutineWoken ) )
\r
1395 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1397 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1399 *pxCoRoutineWoken = pdTRUE;
\r
1414 /*-----------------------------------------------------------*/
\r
1416 #if configQUEUE_REGISTRY_SIZE > 0
\r
1418 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1420 unsigned portBASE_TYPE ux;
\r
1422 /* See if there is an empty space in the registry. A NULL name denotes
\r
1424 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1426 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1428 /* Store the information on this queue. */
\r
1429 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1430 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1437 /*-----------------------------------------------------------*/
\r
1439 #if configQUEUE_REGISTRY_SIZE > 0
\r
1441 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1443 unsigned portBASE_TYPE ux;
\r
1445 /* See if the handle of the queue being unregistered in actually in the
\r
1447 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1449 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1451 /* Set the name to NULL to show that this slot if free again. */
\r
1452 xQueueRegistry[ ux ].pcQueueName = NULL;
\r