2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
58 all the API functions to use the MPU wrappers. That should only be done when
\r
59 task.h is included from an application file. */
\r
60 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
62 #include "FreeRTOS.h"
\r
64 #include "croutine.h"
\r
66 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
68 /*-----------------------------------------------------------
\r
69 * PUBLIC LIST API documented in list.h
\r
70 *----------------------------------------------------------*/
\r
72 /* Constants used with the cRxLock and cTxLock structure members. */
\r
73 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
74 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
76 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
78 /* For internal use only. */
\r
79 #define queueSEND_TO_BACK ( 0 )
\r
80 #define queueSEND_TO_FRONT ( 1 )
\r
82 /* Effectively make a union out of the xQUEUE structure. */
\r
83 #define pxMutexHolder pcTail
\r
84 #define uxQueueType pcHead
\r
85 #define uxRecursiveCallCount pcReadFrom
\r
86 #define queueQUEUE_IS_MUTEX NULL
\r
88 /* Semaphores do not actually store or copy data, so have an items size of
\r
90 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( 0 )
\r
91 #define queueDONT_BLOCK ( ( portTickType ) 0 )
\r
92 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0 )
\r
95 * Definition of the queue used by the scheduler.
\r
96 * Items are queued by copy, not reference.
\r
98 typedef struct QueueDefinition
\r
100 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
101 signed char *pcTail; /*< Points to the byte at the end of the queue storage area. Once more byte is allocated than necessary to store the queue items, this is used as a marker. */
\r
103 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
104 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
106 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
107 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
109 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
110 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
111 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
113 signed portBASE_TYPE xRxLock; /*< Stores the number of items received from the queue (removed from the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
114 signed portBASE_TYPE xTxLock; /*< Stores the number of items transmitted to the queue (added to the queue) while the queue was locked. Set to queueUNLOCKED when the queue is not locked. */
\r
117 /*-----------------------------------------------------------*/
\r
120 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
121 * To keep the definition private the API header file defines it as a
\r
124 typedef xQUEUE * xQueueHandle;
\r
127 * Prototypes for public functions are included here so we don't have to
\r
128 * include the API header file (as it defines xQueueHandle differently). These
\r
129 * functions are documented in the API header file.
\r
131 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize ) PRIVILEGED_FUNCTION;
\r
132 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
133 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
134 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
135 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
136 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
137 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
138 xQueueHandle xQueueCreateMutex( void ) PRIVILEGED_FUNCTION;
\r
139 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
140 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
141 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
142 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
143 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
144 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
145 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
146 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
149 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
150 * an optional component.
\r
152 #if configUSE_CO_ROUTINES == 1
\r
153 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
154 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
155 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
156 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
160 * The queue registry is just a means for kernel aware debuggers to locate
\r
161 * queue structures. It has no other purpose so is an optional component.
\r
163 #if configQUEUE_REGISTRY_SIZE > 0
\r
165 /* The type stored within the queue registry array. This allows a name
\r
166 to be assigned to each queue making kernel aware debugging a little
\r
167 more user friendly. */
\r
168 typedef struct QUEUE_REGISTRY_ITEM
\r
170 signed char *pcQueueName;
\r
171 xQueueHandle xHandle;
\r
172 } xQueueRegistryItem;
\r
174 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
175 The pcQueueName member of a structure being NULL is indicative of the
\r
176 array position being vacant. */
\r
177 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
179 /* Removes a queue from the registry by simply setting the pcQueueName
\r
181 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
182 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
186 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
187 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
188 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
189 * queue is locked it will instead increment the appropriate queue lock count
\r
190 * to indicate that a task may require unblocking. When the queue in unlocked
\r
191 * these lock counts are inspected, and the appropriate action taken.
\r
193 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
196 * Uses a critical section to determine if there is any data in a queue.
\r
198 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
200 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
203 * Uses a critical section to determine if there is any space in a queue.
\r
205 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
207 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
210 * Copies an item into the queue, either at the front of the queue or the
\r
211 * back of the queue.
\r
213 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
216 * Copies an item out of a queue.
\r
218 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
219 /*-----------------------------------------------------------*/
\r
222 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
223 * accessing the queue event lists.
\r
225 #define prvLockQueue( pxQueue ) \
\r
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
240 /*-----------------------------------------------------------*/
\r
243 /*-----------------------------------------------------------
\r
244 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
245 *----------------------------------------------------------*/
\r
247 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
249 xQUEUE *pxNewQueue;
\r
250 size_t xQueueSizeInBytes;
\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 = 0;
\r
269 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
270 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - 1 ) * 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
285 traceQUEUE_CREATE_FAILED();
\r
286 vPortFree( pxNewQueue );
\r
291 /* Will only reach here if we could not allocate enough memory or no memory
\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 = 0;
\r
320 pxNewQueue->uxLength = 1;
\r
321 pxNewQueue->uxItemSize = 0;
\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, 0, queueSEND_TO_BACK );
\r
332 traceCREATE_MUTEX( pxNewQueue );
\r
336 traceCREATE_MUTEX_FAILED();
\r
342 #endif /* configUSE_MUTEXES */
\r
343 /*-----------------------------------------------------------*/
\r
345 #if configUSE_RECURSIVE_MUTEXES == 1
\r
347 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
349 portBASE_TYPE xReturn;
\r
351 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
352 change outside of this task. If this task does not hold the mutex then
\r
353 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
354 this is the only condition we are interested in it does not matter if
\r
355 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
356 mutual exclusion is required to test the pxMutexHolder variable. */
\r
357 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
359 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
361 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
362 the task handle, therefore no underflow check is required. Also,
\r
363 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
364 there can only be one, no mutual exclusion is required to modify the
\r
365 uxRecursiveCallCount member. */
\r
366 ( pxMutex->uxRecursiveCallCount )--;
\r
368 /* Have we unwound the call count? */
\r
369 if( pxMutex->uxRecursiveCallCount == 0 )
\r
371 /* Return the mutex. This will automatically unblock any other
\r
372 task that might be waiting to access the mutex. */
\r
373 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
380 /* We cannot give the mutex because we are not the holder. */
\r
383 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
389 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
390 /*-----------------------------------------------------------*/
\r
392 #if configUSE_RECURSIVE_MUTEXES == 1
\r
394 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
396 portBASE_TYPE xReturn;
\r
398 /* Comments regarding mutual exclusion as per those within
\r
399 xQueueGiveMutexRecursive(). */
\r
401 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
403 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
405 ( pxMutex->uxRecursiveCallCount )++;
\r
410 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
412 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
413 we may have blocked to reach here. */
\r
414 if( xReturn == pdPASS )
\r
416 ( pxMutex->uxRecursiveCallCount )++;
\r
420 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
427 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
428 /*-----------------------------------------------------------*/
\r
430 #if configUSE_COUNTING_SEMAPHORES == 1
\r
432 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
434 xQueueHandle pxHandle;
\r
436 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
438 if( pxHandle != NULL )
\r
440 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
442 traceCREATE_COUNTING_SEMAPHORE();
\r
446 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
452 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
453 /*-----------------------------------------------------------*/
\r
455 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
457 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
458 xTimeOutType xTimeOut;
\r
460 /* This function relaxes the coding standard somewhat to allow return
\r
461 statements within the function itself. This is done in the interest
\r
462 of execution time efficiency. */
\r
465 taskENTER_CRITICAL();
\r
467 /* Is there room on the queue now? To be running we must be
\r
468 the highest priority task wanting to access the queue. */
\r
469 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
471 traceQUEUE_SEND( pxQueue );
\r
472 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
474 /* If there was a task waiting for data to arrive on the
\r
475 queue then unblock it now. */
\r
476 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
478 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
480 /* The unblocked task has a priority higher than
\r
481 our own so yield immediately. Yes it is ok to do
\r
482 this from within the critical section - the kernel
\r
483 takes care of that. */
\r
484 portYIELD_WITHIN_API();
\r
488 taskEXIT_CRITICAL();
\r
490 /* Return to the original privilege level before exiting the
\r
496 if( xTicksToWait == ( portTickType ) 0 )
\r
498 /* The queue was full and no block time is specified (or
\r
499 the block time has expired) so leave now. */
\r
500 taskEXIT_CRITICAL();
\r
502 /* Return to the original privilege level before exiting
\r
504 traceQUEUE_SEND_FAILED( pxQueue );
\r
505 return errQUEUE_FULL;
\r
507 else if( xEntryTimeSet == pdFALSE )
\r
509 /* The queue was full and a block time was specified so
\r
510 configure the timeout structure. */
\r
511 vTaskSetTimeOutState( &xTimeOut );
\r
512 xEntryTimeSet = pdTRUE;
\r
516 taskEXIT_CRITICAL();
\r
518 /* Interrupts and other tasks can send to and receive from the queue
\r
519 now the critical section has been exited. */
\r
522 prvLockQueue( pxQueue );
\r
524 /* Update the timeout state to see if it has expired yet. */
\r
525 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
527 if( prvIsQueueFull( pxQueue ) )
\r
529 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
530 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
532 /* Unlocking the queue means queue events can effect the
\r
533 event list. It is possible that interrupts occurring now
\r
534 remove this task from the event list again - but as the
\r
535 scheduler is suspended the task will go onto the pending
\r
536 ready last instead of the actual ready list. */
\r
537 prvUnlockQueue( pxQueue );
\r
539 /* Resuming the scheduler will move tasks from the pending
\r
540 ready list into the ready list - so it is feasible that this
\r
541 task is already in a ready list before it yields - in which
\r
542 case the yield will not cause a context switch unless there
\r
543 is also a higher priority task in the pending ready list. */
\r
544 if( !xTaskResumeAll() )
\r
546 portYIELD_WITHIN_API();
\r
552 prvUnlockQueue( pxQueue );
\r
553 ( void ) xTaskResumeAll();
\r
558 /* The timeout has expired. */
\r
559 prvUnlockQueue( pxQueue );
\r
560 ( void ) xTaskResumeAll();
\r
562 /* Return to the original privilege level before exiting the
\r
564 traceQUEUE_SEND_FAILED( pxQueue );
\r
565 return errQUEUE_FULL;
\r
569 /*-----------------------------------------------------------*/
\r
571 #if configUSE_ALTERNATIVE_API == 1
\r
573 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
575 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
576 xTimeOutType xTimeOut;
\r
580 taskENTER_CRITICAL();
\r
582 /* Is there room on the queue now? To be running we must be
\r
583 the highest priority task wanting to access the queue. */
\r
584 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
586 traceQUEUE_SEND( pxQueue );
\r
587 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
589 /* If there was a task waiting for data to arrive on the
\r
590 queue then unblock it now. */
\r
591 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
593 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
595 /* The unblocked task has a priority higher than
\r
596 our own so yield immediately. */
\r
597 portYIELD_WITHIN_API();
\r
601 taskEXIT_CRITICAL();
\r
606 if( xTicksToWait == ( portTickType ) 0 )
\r
608 taskEXIT_CRITICAL();
\r
609 return errQUEUE_FULL;
\r
611 else if( xEntryTimeSet == pdFALSE )
\r
613 vTaskSetTimeOutState( &xTimeOut );
\r
614 xEntryTimeSet = pdTRUE;
\r
618 taskEXIT_CRITICAL();
\r
620 taskENTER_CRITICAL();
\r
622 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
624 if( prvIsQueueFull( pxQueue ) )
\r
626 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
627 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
628 portYIELD_WITHIN_API();
\r
633 taskEXIT_CRITICAL();
\r
634 traceQUEUE_SEND_FAILED( pxQueue );
\r
635 return errQUEUE_FULL;
\r
638 taskEXIT_CRITICAL();
\r
642 #endif /* configUSE_ALTERNATIVE_API */
\r
643 /*-----------------------------------------------------------*/
\r
645 #if configUSE_ALTERNATIVE_API == 1
\r
647 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
649 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
650 xTimeOutType xTimeOut;
\r
651 signed char *pcOriginalReadPosition;
\r
655 taskENTER_CRITICAL();
\r
657 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
659 /* Remember our read position in case we are just peeking. */
\r
660 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
662 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
664 if( xJustPeeking == pdFALSE )
\r
666 traceQUEUE_RECEIVE( pxQueue );
\r
668 /* We are actually removing data. */
\r
669 --( pxQueue->uxMessagesWaiting );
\r
671 #if ( configUSE_MUTEXES == 1 )
\r
673 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
675 /* Record the information required to implement
\r
676 priority inheritance should it become necessary. */
\r
677 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
682 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
684 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
686 portYIELD_WITHIN_API();
\r
692 traceQUEUE_PEEK( pxQueue );
\r
694 /* We are not removing the data, so reset our read
\r
696 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
698 /* The data is being left in the queue, so see if there are
\r
699 any other tasks waiting for the data. */
\r
700 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
702 /* Tasks that are removed from the event list will get added to
\r
703 the pending ready list as the scheduler is still suspended. */
\r
704 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
706 /* The task waiting has a higher priority than this task. */
\r
707 portYIELD_WITHIN_API();
\r
713 taskEXIT_CRITICAL();
\r
718 if( xTicksToWait == ( portTickType ) 0 )
\r
720 taskEXIT_CRITICAL();
\r
721 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
722 return errQUEUE_EMPTY;
\r
724 else if( xEntryTimeSet == pdFALSE )
\r
726 vTaskSetTimeOutState( &xTimeOut );
\r
727 xEntryTimeSet = pdTRUE;
\r
731 taskEXIT_CRITICAL();
\r
733 taskENTER_CRITICAL();
\r
735 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
737 if( prvIsQueueEmpty( pxQueue ) )
\r
739 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
741 #if ( configUSE_MUTEXES == 1 )
\r
743 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
745 portENTER_CRITICAL();
\r
746 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
747 portEXIT_CRITICAL();
\r
752 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
753 portYIELD_WITHIN_API();
\r
758 taskEXIT_CRITICAL();
\r
759 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
760 return errQUEUE_EMPTY;
\r
763 taskEXIT_CRITICAL();
\r
768 #endif /* configUSE_ALTERNATIVE_API */
\r
769 /*-----------------------------------------------------------*/
\r
771 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
773 signed portBASE_TYPE xReturn;
\r
774 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
776 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
777 in the queue. Also we don't directly wake a task that was blocked on a
\r
778 queue read, instead we return a flag to say whether a context switch is
\r
779 required or not (i.e. has a task with a higher priority than us been woken
\r
781 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
783 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
785 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
787 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
789 /* If the queue is locked we do not alter the event list. This will
\r
790 be done when the queue is unlocked later. */
\r
791 if( pxQueue->xTxLock == queueUNLOCKED )
\r
793 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
795 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
797 /* The task waiting has a higher priority so record that a
\r
798 context switch is required. */
\r
799 *pxHigherPriorityTaskWoken = pdTRUE;
\r
805 /* Increment the lock count so the task that unlocks the queue
\r
806 knows that data was posted while it was locked. */
\r
807 ++( pxQueue->xTxLock );
\r
814 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
815 xReturn = errQUEUE_FULL;
\r
818 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
822 /*-----------------------------------------------------------*/
\r
824 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
826 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
827 xTimeOutType xTimeOut;
\r
828 signed char *pcOriginalReadPosition;
\r
830 /* This function relaxes the coding standard somewhat to allow return
\r
831 statements within the function itself. This is done in the interest
\r
832 of execution time efficiency. */
\r
836 taskENTER_CRITICAL();
\r
838 /* Is there data in the queue now? To be running we must be
\r
839 the highest priority task wanting to access the queue. */
\r
840 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
842 /* Remember our read position in case we are just peeking. */
\r
843 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
845 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
847 if( xJustPeeking == pdFALSE )
\r
849 traceQUEUE_RECEIVE( pxQueue );
\r
851 /* We are actually removing data. */
\r
852 --( pxQueue->uxMessagesWaiting );
\r
854 #if ( configUSE_MUTEXES == 1 )
\r
856 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
858 /* Record the information required to implement
\r
859 priority inheritance should it become necessary. */
\r
860 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
865 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
867 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
869 portYIELD_WITHIN_API();
\r
875 traceQUEUE_PEEK( pxQueue );
\r
877 /* We are not removing the data, so reset our read
\r
879 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
881 /* The data is being left in the queue, so see if there are
\r
882 any other tasks waiting for the data. */
\r
883 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
885 /* Tasks that are removed from the event list will get added to
\r
886 the pending ready list as the scheduler is still suspended. */
\r
887 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
889 /* The task waiting has a higher priority than this task. */
\r
890 portYIELD_WITHIN_API();
\r
896 taskEXIT_CRITICAL();
\r
901 if( xTicksToWait == ( portTickType ) 0 )
\r
903 /* The queue was empty and no block time is specified (or
\r
904 the block time has expired) so leave now. */
\r
905 taskEXIT_CRITICAL();
\r
906 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
907 return errQUEUE_EMPTY;
\r
909 else if( xEntryTimeSet == pdFALSE )
\r
911 /* The queue was empty and a block time was specified so
\r
912 configure the timeout structure. */
\r
913 vTaskSetTimeOutState( &xTimeOut );
\r
914 xEntryTimeSet = pdTRUE;
\r
918 taskEXIT_CRITICAL();
\r
920 /* Interrupts and other tasks can send to and receive from the queue
\r
921 now the critical section has been exited. */
\r
924 prvLockQueue( pxQueue );
\r
926 /* Update the timeout state to see if it has expired yet. */
\r
927 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
929 if( prvIsQueueEmpty( pxQueue ) )
\r
931 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
933 #if ( configUSE_MUTEXES == 1 )
\r
935 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
937 portENTER_CRITICAL();
\r
939 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
941 portEXIT_CRITICAL();
\r
946 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
947 prvUnlockQueue( pxQueue );
\r
948 if( !xTaskResumeAll() )
\r
950 portYIELD_WITHIN_API();
\r
956 prvUnlockQueue( pxQueue );
\r
957 ( void ) xTaskResumeAll();
\r
962 prvUnlockQueue( pxQueue );
\r
963 ( void ) xTaskResumeAll();
\r
964 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
965 return errQUEUE_EMPTY;
\r
969 /*-----------------------------------------------------------*/
\r
971 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
973 signed portBASE_TYPE xReturn;
\r
974 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
976 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
978 /* We cannot block from an ISR, so check there is data available. */
\r
979 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
981 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
983 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
984 --( pxQueue->uxMessagesWaiting );
\r
986 /* If the queue is locked we will not modify the event list. Instead
\r
987 we update the lock count so the task that unlocks the queue will know
\r
988 that an ISR has removed data while the queue was locked. */
\r
989 if( pxQueue->xRxLock == queueUNLOCKED )
\r
991 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
993 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
995 /* The task waiting has a higher priority than us so
\r
996 force a context switch. */
\r
997 *pxTaskWoken = pdTRUE;
\r
1003 /* Increment the lock count so the task that unlocks the queue
\r
1004 knows that data was removed while it was locked. */
\r
1005 ++( pxQueue->xRxLock );
\r
1013 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1016 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1020 /*-----------------------------------------------------------*/
\r
1022 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1024 unsigned portBASE_TYPE uxReturn;
\r
1026 taskENTER_CRITICAL();
\r
1027 uxReturn = pxQueue->uxMessagesWaiting;
\r
1028 taskEXIT_CRITICAL();
\r
1032 /*-----------------------------------------------------------*/
\r
1034 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1036 unsigned portBASE_TYPE uxReturn;
\r
1038 uxReturn = pxQueue->uxMessagesWaiting;
\r
1042 /*-----------------------------------------------------------*/
\r
1044 void vQueueDelete( xQueueHandle pxQueue )
\r
1046 traceQUEUE_DELETE( pxQueue );
\r
1047 vQueueUnregisterQueue( pxQueue );
\r
1048 vPortFree( pxQueue->pcHead );
\r
1049 vPortFree( pxQueue );
\r
1051 /*-----------------------------------------------------------*/
\r
1053 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1055 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1057 #if ( configUSE_MUTEXES == 1 )
\r
1059 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1061 /* The mutex is no longer being held. */
\r
1062 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1063 pxQueue->pxMutexHolder = NULL;
\r
1068 else if( xPosition == queueSEND_TO_BACK )
\r
1070 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1071 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1072 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1074 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1079 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1080 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1081 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1083 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1087 ++( pxQueue->uxMessagesWaiting );
\r
1089 /*-----------------------------------------------------------*/
\r
1091 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1093 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1095 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1096 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1098 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1100 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1103 /*-----------------------------------------------------------*/
\r
1105 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1107 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1109 /* The lock counts contains the number of extra data items placed or
\r
1110 removed from the queue while the queue was locked. When a queue is
\r
1111 locked items can be added or removed, but the event lists cannot be
\r
1113 taskENTER_CRITICAL();
\r
1115 /* See if data was added to the queue while it was locked. */
\r
1116 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1118 /* Data was posted while the queue was locked. Are any tasks
\r
1119 blocked waiting for data to become available? */
\r
1120 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1122 /* Tasks that are removed from the event list will get added to
\r
1123 the pending ready list as the scheduler is still suspended. */
\r
1124 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1126 /* The task waiting has a higher priority so record that a
\r
1127 context switch is required. */
\r
1128 vTaskMissedYield();
\r
1131 --( pxQueue->xTxLock );
\r
1139 pxQueue->xTxLock = queueUNLOCKED;
\r
1141 taskEXIT_CRITICAL();
\r
1143 /* Do the same for the Rx lock. */
\r
1144 taskENTER_CRITICAL();
\r
1146 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1148 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1150 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1152 vTaskMissedYield();
\r
1155 --( pxQueue->xRxLock );
\r
1163 pxQueue->xRxLock = queueUNLOCKED;
\r
1165 taskEXIT_CRITICAL();
\r
1167 /*-----------------------------------------------------------*/
\r
1169 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1171 signed portBASE_TYPE xReturn;
\r
1173 taskENTER_CRITICAL();
\r
1174 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1175 taskEXIT_CRITICAL();
\r
1179 /*-----------------------------------------------------------*/
\r
1181 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1183 signed portBASE_TYPE xReturn;
\r
1185 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1189 /*-----------------------------------------------------------*/
\r
1191 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1193 signed portBASE_TYPE xReturn;
\r
1195 taskENTER_CRITICAL();
\r
1196 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1197 taskEXIT_CRITICAL();
\r
1201 /*-----------------------------------------------------------*/
\r
1203 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1205 signed portBASE_TYPE xReturn;
\r
1207 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1211 /*-----------------------------------------------------------*/
\r
1213 #if configUSE_CO_ROUTINES == 1
\r
1214 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1216 signed portBASE_TYPE xReturn;
\r
1218 /* If the queue is already full we may have to block. A critical section
\r
1219 is required to prevent an interrupt removing something from the queue
\r
1220 between the check to see if the queue is full and blocking on the queue. */
\r
1221 portDISABLE_INTERRUPTS();
\r
1223 if( prvIsQueueFull( pxQueue ) )
\r
1225 /* The queue is full - do we want to block or just leave without
\r
1227 if( xTicksToWait > ( portTickType ) 0 )
\r
1229 /* As this is called from a coroutine we cannot block directly, but
\r
1230 return indicating that we need to block. */
\r
1231 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1232 portENABLE_INTERRUPTS();
\r
1233 return errQUEUE_BLOCKED;
\r
1237 portENABLE_INTERRUPTS();
\r
1238 return errQUEUE_FULL;
\r
1242 portENABLE_INTERRUPTS();
\r
1246 portDISABLE_INTERRUPTS();
\r
1248 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1250 /* There is room in the queue, copy the data into the queue. */
\r
1251 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1254 /* Were any co-routines waiting for data to become available? */
\r
1255 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1257 /* In this instance the co-routine could be placed directly
\r
1258 into the ready list as we are within a critical section.
\r
1259 Instead the same pending ready list mechanism is used as if
\r
1260 the event were caused from within an interrupt. */
\r
1261 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1263 /* The co-routine waiting has a higher priority so record
\r
1264 that a yield might be appropriate. */
\r
1265 xReturn = errQUEUE_YIELD;
\r
1271 xReturn = errQUEUE_FULL;
\r
1274 portENABLE_INTERRUPTS();
\r
1279 /*-----------------------------------------------------------*/
\r
1281 #if configUSE_CO_ROUTINES == 1
\r
1282 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1284 signed portBASE_TYPE xReturn;
\r
1286 /* If the queue is already empty we may have to block. A critical section
\r
1287 is required to prevent an interrupt adding something to the queue
\r
1288 between the check to see if the queue is empty and blocking on the queue. */
\r
1289 portDISABLE_INTERRUPTS();
\r
1291 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1293 /* There are no messages in the queue, do we want to block or just
\r
1294 leave with nothing? */
\r
1295 if( xTicksToWait > ( portTickType ) 0 )
\r
1297 /* As this is a co-routine we cannot block directly, but return
\r
1298 indicating that we need to block. */
\r
1299 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1300 portENABLE_INTERRUPTS();
\r
1301 return errQUEUE_BLOCKED;
\r
1305 portENABLE_INTERRUPTS();
\r
1306 return errQUEUE_FULL;
\r
1310 portENABLE_INTERRUPTS();
\r
1314 portDISABLE_INTERRUPTS();
\r
1316 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1318 /* Data is available from the queue. */
\r
1319 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1320 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1322 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1324 --( pxQueue->uxMessagesWaiting );
\r
1325 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1329 /* Were any co-routines waiting for space to become available? */
\r
1330 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1332 /* In this instance the co-routine could be placed directly
\r
1333 into the ready list as we are within a critical section.
\r
1334 Instead the same pending ready list mechanism is used as if
\r
1335 the event were caused from within an interrupt. */
\r
1336 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1338 xReturn = errQUEUE_YIELD;
\r
1347 portENABLE_INTERRUPTS();
\r
1352 /*-----------------------------------------------------------*/
\r
1356 #if configUSE_CO_ROUTINES == 1
\r
1357 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1359 /* Cannot block within an ISR so if there is no space on the queue then
\r
1360 exit without doing anything. */
\r
1361 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1363 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1365 /* We only want to wake one co-routine per ISR, so check that a
\r
1366 co-routine has not already been woken. */
\r
1367 if( !xCoRoutinePreviouslyWoken )
\r
1369 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) )
\r
1371 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1379 return xCoRoutinePreviouslyWoken;
\r
1382 /*-----------------------------------------------------------*/
\r
1384 #if configUSE_CO_ROUTINES == 1
\r
1385 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1387 signed portBASE_TYPE xReturn;
\r
1389 /* We cannot block from an ISR, so check there is data available. If
\r
1390 not then just leave without doing anything. */
\r
1391 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1393 /* Copy the data from the queue. */
\r
1394 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1395 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1397 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1399 --( pxQueue->uxMessagesWaiting );
\r
1400 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1402 if( !( *pxCoRoutineWoken ) )
\r
1404 if( !listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) )
\r
1406 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1408 *pxCoRoutineWoken = pdTRUE;
\r
1423 /*-----------------------------------------------------------*/
\r
1425 #if configQUEUE_REGISTRY_SIZE > 0
\r
1427 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1429 unsigned portBASE_TYPE ux;
\r
1431 /* See if there is an empty space in the registry. A NULL name denotes
\r
1433 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1435 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1437 /* Store the information on this queue. */
\r
1438 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1439 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1446 /*-----------------------------------------------------------*/
\r
1448 #if configQUEUE_REGISTRY_SIZE > 0
\r
1450 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1452 unsigned portBASE_TYPE ux;
\r
1454 /* See if the handle of the queue being unregistered in actually in the
\r
1456 for( ux = 0; ux < configQUEUE_REGISTRY_SIZE; ux++ )
\r
1458 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1460 /* Set the name to NULL to show that this slot if free again. */
\r
1461 xQueueRegistry[ ux ].pcQueueName = NULL;
\r