2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
57 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
\r
58 all the API functions to use the MPU wrappers. That should only be done when
\r
59 task.h is included from an application file. */
\r
60 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
62 #include "FreeRTOS.h"
\r
65 #if ( configUSE_CO_ROUTINES == 1 )
\r
66 #include "croutine.h"
\r
69 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
\r
71 /*-----------------------------------------------------------
\r
72 * PUBLIC LIST API documented in list.h
\r
73 *----------------------------------------------------------*/
\r
75 /* Constants used with the cRxLock and cTxLock structure members. */
\r
76 #define queueUNLOCKED ( ( signed portBASE_TYPE ) -1 )
\r
77 #define queueLOCKED_UNMODIFIED ( ( signed portBASE_TYPE ) 0 )
\r
79 #define queueERRONEOUS_UNBLOCK ( -1 )
\r
81 /* For internal use only. */
\r
82 #define queueSEND_TO_BACK ( 0 )
\r
83 #define queueSEND_TO_FRONT ( 1 )
\r
85 /* Effectively make a union out of the xQUEUE structure. */
\r
86 #define pxMutexHolder pcTail
\r
87 #define uxQueueType pcHead
\r
88 #define uxRecursiveCallCount pcReadFrom
\r
89 #define queueQUEUE_IS_MUTEX NULL
\r
91 /* Semaphores do not actually store or copy data, so have an items size of
\r
93 #define queueSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned portBASE_TYPE ) 0 )
\r
94 #define queueDONT_BLOCK ( ( portTickType ) 0U )
\r
95 #define queueMUTEX_GIVE_BLOCK_TIME ( ( portTickType ) 0U )
\r
98 * Definition of the queue used by the scheduler.
\r
99 * Items are queued by copy, not reference.
\r
101 typedef struct QueueDefinition
\r
103 signed char *pcHead; /*< Points to the beginning of the queue storage area. */
\r
104 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
106 signed char *pcWriteTo; /*< Points to the free next place in the storage area. */
\r
107 signed char *pcReadFrom; /*< Points to the last place that a queued item was read from. */
\r
109 xList xTasksWaitingToSend; /*< List of tasks that are blocked waiting to post onto this queue. Stored in priority order. */
\r
110 xList xTasksWaitingToReceive; /*< List of tasks that are blocked waiting to read from this queue. Stored in priority order. */
\r
112 volatile unsigned portBASE_TYPE uxMessagesWaiting;/*< The number of items currently in the queue. */
\r
113 unsigned portBASE_TYPE uxLength; /*< The length of the queue defined as the number of items it will hold, not the number of bytes. */
\r
114 unsigned portBASE_TYPE uxItemSize; /*< The size of each items that the queue will hold. */
\r
116 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
117 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
120 /*-----------------------------------------------------------*/
\r
123 * Inside this file xQueueHandle is a pointer to a xQUEUE structure.
\r
124 * To keep the definition private the API header file defines it as a
\r
127 typedef xQUEUE * xQueueHandle;
\r
130 * Prototypes for public functions are included here so we don't have to
\r
131 * include the API header file (as it defines xQueueHandle differently). These
\r
132 * functions are documented in the API header file.
\r
134 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize ) PRIVILEGED_FUNCTION;
\r
135 signed portBASE_TYPE xQueueGenericSend( xQueueHandle xQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
136 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
137 void vQueueDelete( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
138 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
139 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
140 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
141 xQueueHandle xQueueCreateMutex( void ) PRIVILEGED_FUNCTION;
\r
142 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount ) PRIVILEGED_FUNCTION;
\r
143 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle xMutex, portTickType xBlockTime ) PRIVILEGED_FUNCTION;
\r
144 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle xMutex ) PRIVILEGED_FUNCTION;
\r
145 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition ) PRIVILEGED_FUNCTION;
\r
146 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking ) PRIVILEGED_FUNCTION;
\r
147 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
148 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
149 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
150 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
153 * Co-routine queue functions differ from task queue functions. Co-routines are
\r
154 * an optional component.
\r
156 #if configUSE_CO_ROUTINES == 1
\r
157 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken ) PRIVILEGED_FUNCTION;
\r
158 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken ) PRIVILEGED_FUNCTION;
\r
159 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
160 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait ) PRIVILEGED_FUNCTION;
\r
164 * The queue registry is just a means for kernel aware debuggers to locate
\r
165 * queue structures. It has no other purpose so is an optional component.
\r
167 #if configQUEUE_REGISTRY_SIZE > 0
\r
169 /* The type stored within the queue registry array. This allows a name
\r
170 to be assigned to each queue making kernel aware debugging a little
\r
171 more user friendly. */
\r
172 typedef struct QUEUE_REGISTRY_ITEM
\r
174 signed char *pcQueueName;
\r
175 xQueueHandle xHandle;
\r
176 } xQueueRegistryItem;
\r
178 /* The queue registry is simply an array of xQueueRegistryItem structures.
\r
179 The pcQueueName member of a structure being NULL is indicative of the
\r
180 array position being vacant. */
\r
181 xQueueRegistryItem xQueueRegistry[ configQUEUE_REGISTRY_SIZE ];
\r
183 /* Removes a queue from the registry by simply setting the pcQueueName
\r
185 static void vQueueUnregisterQueue( xQueueHandle xQueue ) PRIVILEGED_FUNCTION;
\r
186 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName ) PRIVILEGED_FUNCTION;
\r
190 * Unlocks a queue locked by a call to prvLockQueue. Locking a queue does not
\r
191 * prevent an ISR from adding or removing items to the queue, but does prevent
\r
192 * an ISR from removing tasks from the queue event lists. If an ISR finds a
\r
193 * queue is locked it will instead increment the appropriate queue lock count
\r
194 * to indicate that a task may require unblocking. When the queue in unlocked
\r
195 * these lock counts are inspected, and the appropriate action taken.
\r
197 static void prvUnlockQueue( xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
200 * Uses a critical section to determine if there is any data in a queue.
\r
202 * @return pdTRUE if the queue contains no items, otherwise pdFALSE.
\r
204 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
207 * Uses a critical section to determine if there is any space in a queue.
\r
209 * @return pdTRUE if there is no space, otherwise pdFALSE;
\r
211 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue ) PRIVILEGED_FUNCTION;
\r
214 * Copies an item into the queue, either at the front of the queue or the
\r
215 * back of the queue.
\r
217 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition ) PRIVILEGED_FUNCTION;
\r
220 * Copies an item out of a queue.
\r
222 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer ) PRIVILEGED_FUNCTION;
\r
223 /*-----------------------------------------------------------*/
\r
226 * Macro to mark a queue as locked. Locking a queue prevents an ISR from
\r
227 * accessing the queue event lists.
\r
229 #define prvLockQueue( pxQueue ) \
\r
230 taskENTER_CRITICAL(); \
\r
232 if( ( pxQueue )->xRxLock == queueUNLOCKED ) \
\r
234 ( pxQueue )->xRxLock = queueLOCKED_UNMODIFIED; \
\r
236 if( ( pxQueue )->xTxLock == queueUNLOCKED ) \
\r
238 ( pxQueue )->xTxLock = queueLOCKED_UNMODIFIED; \
\r
241 taskEXIT_CRITICAL()
\r
242 /*-----------------------------------------------------------*/
\r
245 /*-----------------------------------------------------------
\r
246 * PUBLIC QUEUE MANAGEMENT API documented in queue.h
\r
247 *----------------------------------------------------------*/
\r
249 xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize )
\r
251 xQUEUE *pxNewQueue;
\r
252 size_t xQueueSizeInBytes;
\r
253 xQueueHandle xReturn = NULL;
\r
255 /* Allocate the new queue structure. */
\r
256 if( uxQueueLength > ( unsigned portBASE_TYPE ) 0 )
\r
258 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
259 if( pxNewQueue != NULL )
\r
261 /* Create the list of pointers to queue items. The queue is one byte
\r
262 longer than asked for to make wrap checking easier/faster. */
\r
263 xQueueSizeInBytes = ( size_t ) ( uxQueueLength * uxItemSize ) + ( size_t ) 1;
\r
265 pxNewQueue->pcHead = ( signed char * ) pvPortMalloc( xQueueSizeInBytes );
\r
266 if( pxNewQueue->pcHead != NULL )
\r
268 /* Initialise the queue members as described above where the
\r
269 queue type is defined. */
\r
270 pxNewQueue->pcTail = pxNewQueue->pcHead + ( uxQueueLength * uxItemSize );
\r
271 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
272 pxNewQueue->pcWriteTo = pxNewQueue->pcHead;
\r
273 pxNewQueue->pcReadFrom = pxNewQueue->pcHead + ( ( uxQueueLength - ( unsigned portBASE_TYPE ) 1U ) * uxItemSize );
\r
274 pxNewQueue->uxLength = uxQueueLength;
\r
275 pxNewQueue->uxItemSize = uxItemSize;
\r
276 pxNewQueue->xRxLock = queueUNLOCKED;
\r
277 pxNewQueue->xTxLock = queueUNLOCKED;
\r
279 /* Likewise ensure the event queues start with the correct state. */
\r
280 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
281 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
283 traceQUEUE_CREATE( pxNewQueue );
\r
284 xReturn = pxNewQueue;
\r
288 traceQUEUE_CREATE_FAILED();
\r
289 vPortFree( pxNewQueue );
\r
294 configASSERT( xReturn );
\r
298 /*-----------------------------------------------------------*/
\r
300 #if ( configUSE_MUTEXES == 1 )
\r
302 xQueueHandle xQueueCreateMutex( void )
\r
304 xQUEUE *pxNewQueue;
\r
306 /* Allocate the new queue structure. */
\r
307 pxNewQueue = ( xQUEUE * ) pvPortMalloc( sizeof( xQUEUE ) );
\r
308 if( pxNewQueue != NULL )
\r
310 /* Information required for priority inheritance. */
\r
311 pxNewQueue->pxMutexHolder = NULL;
\r
312 pxNewQueue->uxQueueType = queueQUEUE_IS_MUTEX;
\r
314 /* Queues used as a mutex no data is actually copied into or out
\r
316 pxNewQueue->pcWriteTo = NULL;
\r
317 pxNewQueue->pcReadFrom = NULL;
\r
319 /* Each mutex has a length of 1 (like a binary semaphore) and
\r
320 an item size of 0 as nothing is actually copied into or out
\r
322 pxNewQueue->uxMessagesWaiting = ( unsigned portBASE_TYPE ) 0U;
\r
323 pxNewQueue->uxLength = ( unsigned portBASE_TYPE ) 1U;
\r
324 pxNewQueue->uxItemSize = ( unsigned portBASE_TYPE ) 0U;
\r
325 pxNewQueue->xRxLock = queueUNLOCKED;
\r
326 pxNewQueue->xTxLock = queueUNLOCKED;
\r
328 /* Ensure the event queues start with the correct state. */
\r
329 vListInitialise( &( pxNewQueue->xTasksWaitingToSend ) );
\r
330 vListInitialise( &( pxNewQueue->xTasksWaitingToReceive ) );
\r
332 /* Start with the semaphore in the expected state. */
\r
333 xQueueGenericSend( pxNewQueue, NULL, ( portTickType ) 0U, queueSEND_TO_BACK );
\r
335 traceCREATE_MUTEX( pxNewQueue );
\r
339 traceCREATE_MUTEX_FAILED();
\r
342 configASSERT( pxNewQueue );
\r
346 #endif /* configUSE_MUTEXES */
\r
347 /*-----------------------------------------------------------*/
\r
349 #if configUSE_RECURSIVE_MUTEXES == 1
\r
351 portBASE_TYPE xQueueGiveMutexRecursive( xQueueHandle pxMutex )
\r
353 portBASE_TYPE xReturn;
\r
355 configASSERT( pxMutex );
\r
357 /* If this is the task that holds the mutex then pxMutexHolder will not
\r
358 change outside of this task. If this task does not hold the mutex then
\r
359 pxMutexHolder can never coincidentally equal the tasks handle, and as
\r
360 this is the only condition we are interested in it does not matter if
\r
361 pxMutexHolder is accessed simultaneously by another task. Therefore no
\r
362 mutual exclusion is required to test the pxMutexHolder variable. */
\r
363 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
365 traceGIVE_MUTEX_RECURSIVE( pxMutex );
\r
367 /* uxRecursiveCallCount cannot be zero if pxMutexHolder is equal to
\r
368 the task handle, therefore no underflow check is required. Also,
\r
369 uxRecursiveCallCount is only modified by the mutex holder, and as
\r
370 there can only be one, no mutual exclusion is required to modify the
\r
371 uxRecursiveCallCount member. */
\r
372 ( pxMutex->uxRecursiveCallCount )--;
\r
374 /* Have we unwound the call count? */
\r
375 if( pxMutex->uxRecursiveCallCount == 0 )
\r
377 /* Return the mutex. This will automatically unblock any other
\r
378 task that might be waiting to access the mutex. */
\r
379 xQueueGenericSend( pxMutex, NULL, queueMUTEX_GIVE_BLOCK_TIME, queueSEND_TO_BACK );
\r
386 /* We cannot give the mutex because we are not the holder. */
\r
389 traceGIVE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
395 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
396 /*-----------------------------------------------------------*/
\r
398 #if configUSE_RECURSIVE_MUTEXES == 1
\r
400 portBASE_TYPE xQueueTakeMutexRecursive( xQueueHandle pxMutex, portTickType xBlockTime )
\r
402 portBASE_TYPE xReturn;
\r
404 configASSERT( pxMutex );
\r
406 /* Comments regarding mutual exclusion as per those within
\r
407 xQueueGiveMutexRecursive(). */
\r
409 traceTAKE_MUTEX_RECURSIVE( pxMutex );
\r
411 if( pxMutex->pxMutexHolder == xTaskGetCurrentTaskHandle() )
\r
413 ( pxMutex->uxRecursiveCallCount )++;
\r
418 xReturn = xQueueGenericReceive( pxMutex, NULL, xBlockTime, pdFALSE );
\r
420 /* pdPASS will only be returned if we successfully obtained the mutex,
\r
421 we may have blocked to reach here. */
\r
422 if( xReturn == pdPASS )
\r
424 ( pxMutex->uxRecursiveCallCount )++;
\r
428 traceTAKE_MUTEX_RECURSIVE_FAILED( pxMutex );
\r
435 #endif /* configUSE_RECURSIVE_MUTEXES */
\r
436 /*-----------------------------------------------------------*/
\r
438 #if configUSE_COUNTING_SEMAPHORES == 1
\r
440 xQueueHandle xQueueCreateCountingSemaphore( unsigned portBASE_TYPE uxCountValue, unsigned portBASE_TYPE uxInitialCount )
\r
442 xQueueHandle pxHandle;
\r
444 pxHandle = xQueueCreate( ( unsigned portBASE_TYPE ) uxCountValue, queueSEMAPHORE_QUEUE_ITEM_LENGTH );
\r
446 if( pxHandle != NULL )
\r
448 pxHandle->uxMessagesWaiting = uxInitialCount;
\r
450 traceCREATE_COUNTING_SEMAPHORE();
\r
454 traceCREATE_COUNTING_SEMAPHORE_FAILED();
\r
457 configASSERT( pxHandle );
\r
461 #endif /* configUSE_COUNTING_SEMAPHORES */
\r
462 /*-----------------------------------------------------------*/
\r
464 signed portBASE_TYPE xQueueGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
466 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
467 xTimeOutType xTimeOut;
\r
469 configASSERT( pxQueue );
\r
470 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
472 /* This function relaxes the coding standard somewhat to allow return
\r
473 statements within the function itself. This is done in the interest
\r
474 of execution time efficiency. */
\r
477 taskENTER_CRITICAL();
\r
479 /* Is there room on the queue now? To be running we must be
\r
480 the highest priority task wanting to access the queue. */
\r
481 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
483 traceQUEUE_SEND( pxQueue );
\r
484 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
486 /* If there was a task waiting for data to arrive on the
\r
487 queue then unblock it now. */
\r
488 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
490 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
492 /* The unblocked task has a priority higher than
\r
493 our own so yield immediately. Yes it is ok to do
\r
494 this from within the critical section - the kernel
\r
495 takes care of that. */
\r
496 portYIELD_WITHIN_API();
\r
500 taskEXIT_CRITICAL();
\r
502 /* Return to the original privilege level before exiting the
\r
508 if( xTicksToWait == ( portTickType ) 0 )
\r
510 /* The queue was full and no block time is specified (or
\r
511 the block time has expired) so leave now. */
\r
512 taskEXIT_CRITICAL();
\r
514 /* Return to the original privilege level before exiting
\r
516 traceQUEUE_SEND_FAILED( pxQueue );
\r
517 return errQUEUE_FULL;
\r
519 else if( xEntryTimeSet == pdFALSE )
\r
521 /* The queue was full and a block time was specified so
\r
522 configure the timeout structure. */
\r
523 vTaskSetTimeOutState( &xTimeOut );
\r
524 xEntryTimeSet = pdTRUE;
\r
528 taskEXIT_CRITICAL();
\r
530 /* Interrupts and other tasks can send to and receive from the queue
\r
531 now the critical section has been exited. */
\r
534 prvLockQueue( pxQueue );
\r
536 /* Update the timeout state to see if it has expired yet. */
\r
537 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
539 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
541 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
542 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
544 /* Unlocking the queue means queue events can effect the
\r
545 event list. It is possible that interrupts occurring now
\r
546 remove this task from the event list again - but as the
\r
547 scheduler is suspended the task will go onto the pending
\r
548 ready last instead of the actual ready list. */
\r
549 prvUnlockQueue( pxQueue );
\r
551 /* Resuming the scheduler will move tasks from the pending
\r
552 ready list into the ready list - so it is feasible that this
\r
553 task is already in a ready list before it yields - in which
\r
554 case the yield will not cause a context switch unless there
\r
555 is also a higher priority task in the pending ready list. */
\r
556 if( xTaskResumeAll() == pdFALSE )
\r
558 portYIELD_WITHIN_API();
\r
564 prvUnlockQueue( pxQueue );
\r
565 ( void ) xTaskResumeAll();
\r
570 /* The timeout has expired. */
\r
571 prvUnlockQueue( pxQueue );
\r
572 ( void ) xTaskResumeAll();
\r
574 /* Return to the original privilege level before exiting the
\r
576 traceQUEUE_SEND_FAILED( pxQueue );
\r
577 return errQUEUE_FULL;
\r
581 /*-----------------------------------------------------------*/
\r
583 #if configUSE_ALTERNATIVE_API == 1
\r
585 signed portBASE_TYPE xQueueAltGenericSend( xQueueHandle pxQueue, const void * const pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE xCopyPosition )
\r
587 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
588 xTimeOutType xTimeOut;
\r
590 configASSERT( pxQueue );
\r
591 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
595 taskENTER_CRITICAL();
\r
597 /* Is there room on the queue now? To be running we must be
\r
598 the highest priority task wanting to access the queue. */
\r
599 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
601 traceQUEUE_SEND( pxQueue );
\r
602 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
604 /* If there was a task waiting for data to arrive on the
\r
605 queue then unblock it now. */
\r
606 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
608 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) == pdTRUE )
\r
610 /* The unblocked task has a priority higher than
\r
611 our own so yield immediately. */
\r
612 portYIELD_WITHIN_API();
\r
616 taskEXIT_CRITICAL();
\r
621 if( xTicksToWait == ( portTickType ) 0 )
\r
623 taskEXIT_CRITICAL();
\r
624 return errQUEUE_FULL;
\r
626 else if( xEntryTimeSet == pdFALSE )
\r
628 vTaskSetTimeOutState( &xTimeOut );
\r
629 xEntryTimeSet = pdTRUE;
\r
633 taskEXIT_CRITICAL();
\r
635 taskENTER_CRITICAL();
\r
637 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
639 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
641 traceBLOCKING_ON_QUEUE_SEND( pxQueue );
\r
642 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToSend ), xTicksToWait );
\r
643 portYIELD_WITHIN_API();
\r
648 taskEXIT_CRITICAL();
\r
649 traceQUEUE_SEND_FAILED( pxQueue );
\r
650 return errQUEUE_FULL;
\r
653 taskEXIT_CRITICAL();
\r
657 #endif /* configUSE_ALTERNATIVE_API */
\r
658 /*-----------------------------------------------------------*/
\r
660 #if configUSE_ALTERNATIVE_API == 1
\r
662 signed portBASE_TYPE xQueueAltGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
664 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
665 xTimeOutType xTimeOut;
\r
666 signed char *pcOriginalReadPosition;
\r
668 configASSERT( pxQueue );
\r
669 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
673 taskENTER_CRITICAL();
\r
675 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
677 /* Remember our read position in case we are just peeking. */
\r
678 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
680 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
682 if( xJustPeeking == pdFALSE )
\r
684 traceQUEUE_RECEIVE( pxQueue );
\r
686 /* We are actually removing data. */
\r
687 --( pxQueue->uxMessagesWaiting );
\r
689 #if ( configUSE_MUTEXES == 1 )
\r
691 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
693 /* Record the information required to implement
\r
694 priority inheritance should it become necessary. */
\r
695 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
700 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
702 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
704 portYIELD_WITHIN_API();
\r
710 traceQUEUE_PEEK( pxQueue );
\r
712 /* We are not removing the data, so reset our read
\r
714 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
716 /* The data is being left in the queue, so see if there are
\r
717 any other tasks waiting for the data. */
\r
718 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
720 /* Tasks that are removed from the event list will get added to
\r
721 the pending ready list as the scheduler is still suspended. */
\r
722 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
724 /* The task waiting has a higher priority than this task. */
\r
725 portYIELD_WITHIN_API();
\r
731 taskEXIT_CRITICAL();
\r
736 if( xTicksToWait == ( portTickType ) 0 )
\r
738 taskEXIT_CRITICAL();
\r
739 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
740 return errQUEUE_EMPTY;
\r
742 else if( xEntryTimeSet == pdFALSE )
\r
744 vTaskSetTimeOutState( &xTimeOut );
\r
745 xEntryTimeSet = pdTRUE;
\r
749 taskEXIT_CRITICAL();
\r
751 taskENTER_CRITICAL();
\r
753 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
755 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
757 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
759 #if ( configUSE_MUTEXES == 1 )
\r
761 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
763 portENTER_CRITICAL();
\r
764 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
765 portEXIT_CRITICAL();
\r
770 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
771 portYIELD_WITHIN_API();
\r
776 taskEXIT_CRITICAL();
\r
777 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
778 return errQUEUE_EMPTY;
\r
781 taskEXIT_CRITICAL();
\r
786 #endif /* configUSE_ALTERNATIVE_API */
\r
787 /*-----------------------------------------------------------*/
\r
789 signed portBASE_TYPE xQueueGenericSendFromISR( xQueueHandle pxQueue, const void * const pvItemToQueue, signed portBASE_TYPE *pxHigherPriorityTaskWoken, portBASE_TYPE xCopyPosition )
\r
791 signed portBASE_TYPE xReturn;
\r
792 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
794 configASSERT( pxQueue );
\r
795 configASSERT( pxHigherPriorityTaskWoken );
\r
796 configASSERT( !( ( pvItemToQueue == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
798 /* Similar to xQueueGenericSend, except we don't block if there is no room
\r
799 in the queue. Also we don't directly wake a task that was blocked on a
\r
800 queue read, instead we return a flag to say whether a context switch is
\r
801 required or not (i.e. has a task with a higher priority than us been woken
\r
803 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
805 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
807 traceQUEUE_SEND_FROM_ISR( pxQueue );
\r
809 prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition );
\r
811 /* If the queue is locked we do not alter the event list. This will
\r
812 be done when the queue is unlocked later. */
\r
813 if( pxQueue->xTxLock == queueUNLOCKED )
\r
815 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
817 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
819 /* The task waiting has a higher priority so record that a
\r
820 context switch is required. */
\r
821 *pxHigherPriorityTaskWoken = pdTRUE;
\r
827 /* Increment the lock count so the task that unlocks the queue
\r
828 knows that data was posted while it was locked. */
\r
829 ++( pxQueue->xTxLock );
\r
836 traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue );
\r
837 xReturn = errQUEUE_FULL;
\r
840 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
844 /*-----------------------------------------------------------*/
\r
846 signed portBASE_TYPE xQueueGenericReceive( xQueueHandle pxQueue, void * const pvBuffer, portTickType xTicksToWait, portBASE_TYPE xJustPeeking )
\r
848 signed portBASE_TYPE xEntryTimeSet = pdFALSE;
\r
849 xTimeOutType xTimeOut;
\r
850 signed char *pcOriginalReadPosition;
\r
852 configASSERT( pxQueue );
\r
853 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
855 /* This function relaxes the coding standard somewhat to allow return
\r
856 statements within the function itself. This is done in the interest
\r
857 of execution time efficiency. */
\r
861 taskENTER_CRITICAL();
\r
863 /* Is there data in the queue now? To be running we must be
\r
864 the highest priority task wanting to access the queue. */
\r
865 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
867 /* Remember our read position in case we are just peeking. */
\r
868 pcOriginalReadPosition = pxQueue->pcReadFrom;
\r
870 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
872 if( xJustPeeking == pdFALSE )
\r
874 traceQUEUE_RECEIVE( pxQueue );
\r
876 /* We are actually removing data. */
\r
877 --( pxQueue->uxMessagesWaiting );
\r
879 #if ( configUSE_MUTEXES == 1 )
\r
881 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
883 /* Record the information required to implement
\r
884 priority inheritance should it become necessary. */
\r
885 pxQueue->pxMutexHolder = xTaskGetCurrentTaskHandle();
\r
890 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
892 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) == pdTRUE )
\r
894 portYIELD_WITHIN_API();
\r
900 traceQUEUE_PEEK( pxQueue );
\r
902 /* We are not removing the data, so reset our read
\r
904 pxQueue->pcReadFrom = pcOriginalReadPosition;
\r
906 /* The data is being left in the queue, so see if there are
\r
907 any other tasks waiting for the data. */
\r
908 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
910 /* Tasks that are removed from the event list will get added to
\r
911 the pending ready list as the scheduler is still suspended. */
\r
912 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
914 /* The task waiting has a higher priority than this task. */
\r
915 portYIELD_WITHIN_API();
\r
921 taskEXIT_CRITICAL();
\r
926 if( xTicksToWait == ( portTickType ) 0 )
\r
928 /* The queue was empty and no block time is specified (or
\r
929 the block time has expired) so leave now. */
\r
930 taskEXIT_CRITICAL();
\r
931 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
932 return errQUEUE_EMPTY;
\r
934 else if( xEntryTimeSet == pdFALSE )
\r
936 /* The queue was empty and a block time was specified so
\r
937 configure the timeout structure. */
\r
938 vTaskSetTimeOutState( &xTimeOut );
\r
939 xEntryTimeSet = pdTRUE;
\r
943 taskEXIT_CRITICAL();
\r
945 /* Interrupts and other tasks can send to and receive from the queue
\r
946 now the critical section has been exited. */
\r
949 prvLockQueue( pxQueue );
\r
951 /* Update the timeout state to see if it has expired yet. */
\r
952 if( xTaskCheckForTimeOut( &xTimeOut, &xTicksToWait ) == pdFALSE )
\r
954 if( prvIsQueueEmpty( pxQueue ) != pdFALSE )
\r
956 traceBLOCKING_ON_QUEUE_RECEIVE( pxQueue );
\r
958 #if ( configUSE_MUTEXES == 1 )
\r
960 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
962 portENTER_CRITICAL();
\r
964 vTaskPriorityInherit( ( void * ) pxQueue->pxMutexHolder );
\r
966 portEXIT_CRITICAL();
\r
971 vTaskPlaceOnEventList( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
972 prvUnlockQueue( pxQueue );
\r
973 if( xTaskResumeAll() == pdFALSE )
\r
975 portYIELD_WITHIN_API();
\r
981 prvUnlockQueue( pxQueue );
\r
982 ( void ) xTaskResumeAll();
\r
987 prvUnlockQueue( pxQueue );
\r
988 ( void ) xTaskResumeAll();
\r
989 traceQUEUE_RECEIVE_FAILED( pxQueue );
\r
990 return errQUEUE_EMPTY;
\r
994 /*-----------------------------------------------------------*/
\r
996 signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void * const pvBuffer, signed portBASE_TYPE *pxTaskWoken )
\r
998 signed portBASE_TYPE xReturn;
\r
999 unsigned portBASE_TYPE uxSavedInterruptStatus;
\r
1001 configASSERT( pxQueue );
\r
1002 configASSERT( pxTaskWoken );
\r
1003 configASSERT( !( ( pvBuffer == NULL ) && ( pxQueue->uxItemSize != ( unsigned portBASE_TYPE ) 0U ) ) );
\r
1005 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
1007 /* We cannot block from an ISR, so check there is data available. */
\r
1008 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1010 traceQUEUE_RECEIVE_FROM_ISR( pxQueue );
\r
1012 prvCopyDataFromQueue( pxQueue, pvBuffer );
\r
1013 --( pxQueue->uxMessagesWaiting );
\r
1015 /* If the queue is locked we will not modify the event list. Instead
\r
1016 we update the lock count so the task that unlocks the queue will know
\r
1017 that an ISR has removed data while the queue was locked. */
\r
1018 if( pxQueue->xRxLock == queueUNLOCKED )
\r
1020 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1022 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1024 /* The task waiting has a higher priority than us so
\r
1025 force a context switch. */
\r
1026 *pxTaskWoken = pdTRUE;
\r
1032 /* Increment the lock count so the task that unlocks the queue
\r
1033 knows that data was removed while it was locked. */
\r
1034 ++( pxQueue->xRxLock );
\r
1042 traceQUEUE_RECEIVE_FROM_ISR_FAILED( pxQueue );
\r
1045 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
1049 /*-----------------------------------------------------------*/
\r
1051 unsigned portBASE_TYPE uxQueueMessagesWaiting( const xQueueHandle pxQueue )
\r
1053 unsigned portBASE_TYPE uxReturn;
\r
1055 configASSERT( pxQueue );
\r
1057 taskENTER_CRITICAL();
\r
1058 uxReturn = pxQueue->uxMessagesWaiting;
\r
1059 taskEXIT_CRITICAL();
\r
1063 /*-----------------------------------------------------------*/
\r
1065 unsigned portBASE_TYPE uxQueueMessagesWaitingFromISR( const xQueueHandle pxQueue )
\r
1067 unsigned portBASE_TYPE uxReturn;
\r
1069 configASSERT( pxQueue );
\r
1071 uxReturn = pxQueue->uxMessagesWaiting;
\r
1075 /*-----------------------------------------------------------*/
\r
1077 void vQueueDelete( xQueueHandle pxQueue )
\r
1079 configASSERT( pxQueue );
\r
1081 traceQUEUE_DELETE( pxQueue );
\r
1082 vQueueUnregisterQueue( pxQueue );
\r
1083 vPortFree( pxQueue->pcHead );
\r
1084 vPortFree( pxQueue );
\r
1086 /*-----------------------------------------------------------*/
\r
1088 static void prvCopyDataToQueue( xQUEUE *pxQueue, const void *pvItemToQueue, portBASE_TYPE xPosition )
\r
1090 if( pxQueue->uxItemSize == ( unsigned portBASE_TYPE ) 0 )
\r
1092 #if ( configUSE_MUTEXES == 1 )
\r
1094 if( pxQueue->uxQueueType == queueQUEUE_IS_MUTEX )
\r
1096 /* The mutex is no longer being held. */
\r
1097 vTaskPriorityDisinherit( ( void * ) pxQueue->pxMutexHolder );
\r
1098 pxQueue->pxMutexHolder = NULL;
\r
1103 else if( xPosition == queueSEND_TO_BACK )
\r
1105 memcpy( ( void * ) pxQueue->pcWriteTo, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1106 pxQueue->pcWriteTo += pxQueue->uxItemSize;
\r
1107 if( pxQueue->pcWriteTo >= pxQueue->pcTail )
\r
1109 pxQueue->pcWriteTo = pxQueue->pcHead;
\r
1114 memcpy( ( void * ) pxQueue->pcReadFrom, pvItemToQueue, ( unsigned ) pxQueue->uxItemSize );
\r
1115 pxQueue->pcReadFrom -= pxQueue->uxItemSize;
\r
1116 if( pxQueue->pcReadFrom < pxQueue->pcHead )
\r
1118 pxQueue->pcReadFrom = ( pxQueue->pcTail - pxQueue->uxItemSize );
\r
1122 ++( pxQueue->uxMessagesWaiting );
\r
1124 /*-----------------------------------------------------------*/
\r
1126 static void prvCopyDataFromQueue( xQUEUE * const pxQueue, const void *pvBuffer )
\r
1128 if( pxQueue->uxQueueType != queueQUEUE_IS_MUTEX )
\r
1130 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1131 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1133 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1135 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1138 /*-----------------------------------------------------------*/
\r
1140 static void prvUnlockQueue( xQueueHandle pxQueue )
\r
1142 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. */
\r
1144 /* The lock counts contains the number of extra data items placed or
\r
1145 removed from the queue while the queue was locked. When a queue is
\r
1146 locked items can be added or removed, but the event lists cannot be
\r
1148 taskENTER_CRITICAL();
\r
1150 /* See if data was added to the queue while it was locked. */
\r
1151 while( pxQueue->xTxLock > queueLOCKED_UNMODIFIED )
\r
1153 /* Data was posted while the queue was locked. Are any tasks
\r
1154 blocked waiting for data to become available? */
\r
1155 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1157 /* Tasks that are removed from the event list will get added to
\r
1158 the pending ready list as the scheduler is still suspended. */
\r
1159 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1161 /* The task waiting has a higher priority so record that a
\r
1162 context switch is required. */
\r
1163 vTaskMissedYield();
\r
1166 --( pxQueue->xTxLock );
\r
1174 pxQueue->xTxLock = queueUNLOCKED;
\r
1176 taskEXIT_CRITICAL();
\r
1178 /* Do the same for the Rx lock. */
\r
1179 taskENTER_CRITICAL();
\r
1181 while( pxQueue->xRxLock > queueLOCKED_UNMODIFIED )
\r
1183 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1185 if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1187 vTaskMissedYield();
\r
1190 --( pxQueue->xRxLock );
\r
1198 pxQueue->xRxLock = queueUNLOCKED;
\r
1200 taskEXIT_CRITICAL();
\r
1202 /*-----------------------------------------------------------*/
\r
1204 static signed portBASE_TYPE prvIsQueueEmpty( const xQueueHandle pxQueue )
\r
1206 signed portBASE_TYPE xReturn;
\r
1208 taskENTER_CRITICAL();
\r
1209 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1210 taskEXIT_CRITICAL();
\r
1214 /*-----------------------------------------------------------*/
\r
1216 signed portBASE_TYPE xQueueIsQueueEmptyFromISR( const xQueueHandle pxQueue )
\r
1218 signed portBASE_TYPE xReturn;
\r
1220 configASSERT( pxQueue );
\r
1221 xReturn = ( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 );
\r
1225 /*-----------------------------------------------------------*/
\r
1227 static signed portBASE_TYPE prvIsQueueFull( const xQueueHandle pxQueue )
\r
1229 signed portBASE_TYPE xReturn;
\r
1231 taskENTER_CRITICAL();
\r
1232 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1233 taskEXIT_CRITICAL();
\r
1237 /*-----------------------------------------------------------*/
\r
1239 signed portBASE_TYPE xQueueIsQueueFullFromISR( const xQueueHandle pxQueue )
\r
1241 signed portBASE_TYPE xReturn;
\r
1243 configASSERT( pxQueue );
\r
1244 xReturn = ( pxQueue->uxMessagesWaiting == pxQueue->uxLength );
\r
1248 /*-----------------------------------------------------------*/
\r
1250 #if configUSE_CO_ROUTINES == 1
\r
1251 signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait )
\r
1253 signed portBASE_TYPE xReturn;
\r
1255 /* If the queue is already full we may have to block. A critical section
\r
1256 is required to prevent an interrupt removing something from the queue
\r
1257 between the check to see if the queue is full and blocking on the queue. */
\r
1258 portDISABLE_INTERRUPTS();
\r
1260 if( prvIsQueueFull( pxQueue ) != pdFALSE )
\r
1262 /* The queue is full - do we want to block or just leave without
\r
1264 if( xTicksToWait > ( portTickType ) 0 )
\r
1266 /* As this is called from a coroutine we cannot block directly, but
\r
1267 return indicating that we need to block. */
\r
1268 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToSend ) );
\r
1269 portENABLE_INTERRUPTS();
\r
1270 return errQUEUE_BLOCKED;
\r
1274 portENABLE_INTERRUPTS();
\r
1275 return errQUEUE_FULL;
\r
1279 portENABLE_INTERRUPTS();
\r
1283 portDISABLE_INTERRUPTS();
\r
1285 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1287 /* There is room in the queue, copy the data into the queue. */
\r
1288 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1291 /* Were any co-routines waiting for data to become available? */
\r
1292 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1294 /* In this instance the co-routine could be placed directly
\r
1295 into the ready list as we are within a critical section.
\r
1296 Instead the same pending ready list mechanism is used as if
\r
1297 the event were caused from within an interrupt. */
\r
1298 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1300 /* The co-routine waiting has a higher priority so record
\r
1301 that a yield might be appropriate. */
\r
1302 xReturn = errQUEUE_YIELD;
\r
1308 xReturn = errQUEUE_FULL;
\r
1311 portENABLE_INTERRUPTS();
\r
1316 /*-----------------------------------------------------------*/
\r
1318 #if configUSE_CO_ROUTINES == 1
\r
1319 signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait )
\r
1321 signed portBASE_TYPE xReturn;
\r
1323 /* If the queue is already empty we may have to block. A critical section
\r
1324 is required to prevent an interrupt adding something to the queue
\r
1325 between the check to see if the queue is empty and blocking on the queue. */
\r
1326 portDISABLE_INTERRUPTS();
\r
1328 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0 )
\r
1330 /* There are no messages in the queue, do we want to block or just
\r
1331 leave with nothing? */
\r
1332 if( xTicksToWait > ( portTickType ) 0 )
\r
1334 /* As this is a co-routine we cannot block directly, but return
\r
1335 indicating that we need to block. */
\r
1336 vCoRoutineAddToDelayedList( xTicksToWait, &( pxQueue->xTasksWaitingToReceive ) );
\r
1337 portENABLE_INTERRUPTS();
\r
1338 return errQUEUE_BLOCKED;
\r
1342 portENABLE_INTERRUPTS();
\r
1343 return errQUEUE_FULL;
\r
1347 portENABLE_INTERRUPTS();
\r
1351 portDISABLE_INTERRUPTS();
\r
1353 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1355 /* Data is available from the queue. */
\r
1356 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1357 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1359 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1361 --( pxQueue->uxMessagesWaiting );
\r
1362 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1366 /* Were any co-routines waiting for space to become available? */
\r
1367 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1369 /* In this instance the co-routine could be placed directly
\r
1370 into the ready list as we are within a critical section.
\r
1371 Instead the same pending ready list mechanism is used as if
\r
1372 the event were caused from within an interrupt. */
\r
1373 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1375 xReturn = errQUEUE_YIELD;
\r
1384 portENABLE_INTERRUPTS();
\r
1389 /*-----------------------------------------------------------*/
\r
1393 #if configUSE_CO_ROUTINES == 1
\r
1394 signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken )
\r
1396 /* Cannot block within an ISR so if there is no space on the queue then
\r
1397 exit without doing anything. */
\r
1398 if( pxQueue->uxMessagesWaiting < pxQueue->uxLength )
\r
1400 prvCopyDataToQueue( pxQueue, pvItemToQueue, queueSEND_TO_BACK );
\r
1402 /* We only want to wake one co-routine per ISR, so check that a
\r
1403 co-routine has not already been woken. */
\r
1404 if( xCoRoutinePreviouslyWoken == pdFALSE )
\r
1406 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE )
\r
1408 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE )
\r
1416 return xCoRoutinePreviouslyWoken;
\r
1419 /*-----------------------------------------------------------*/
\r
1421 #if configUSE_CO_ROUTINES == 1
\r
1422 signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxCoRoutineWoken )
\r
1424 signed portBASE_TYPE xReturn;
\r
1426 /* We cannot block from an ISR, so check there is data available. If
\r
1427 not then just leave without doing anything. */
\r
1428 if( pxQueue->uxMessagesWaiting > ( unsigned portBASE_TYPE ) 0 )
\r
1430 /* Copy the data from the queue. */
\r
1431 pxQueue->pcReadFrom += pxQueue->uxItemSize;
\r
1432 if( pxQueue->pcReadFrom >= pxQueue->pcTail )
\r
1434 pxQueue->pcReadFrom = pxQueue->pcHead;
\r
1436 --( pxQueue->uxMessagesWaiting );
\r
1437 memcpy( ( void * ) pvBuffer, ( void * ) pxQueue->pcReadFrom, ( unsigned ) pxQueue->uxItemSize );
\r
1439 if( ( *pxCoRoutineWoken ) == pdFALSE )
\r
1441 if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToSend ) ) == pdFALSE )
\r
1443 if( xCoRoutineRemoveFromEventList( &( pxQueue->xTasksWaitingToSend ) ) != pdFALSE )
\r
1445 *pxCoRoutineWoken = pdTRUE;
\r
1460 /*-----------------------------------------------------------*/
\r
1462 #if configQUEUE_REGISTRY_SIZE > 0
\r
1464 void vQueueAddToRegistry( xQueueHandle xQueue, signed char *pcQueueName )
\r
1466 unsigned portBASE_TYPE ux;
\r
1468 /* See if there is an empty space in the registry. A NULL name denotes
\r
1470 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1472 if( xQueueRegistry[ ux ].pcQueueName == NULL )
\r
1474 /* Store the information on this queue. */
\r
1475 xQueueRegistry[ ux ].pcQueueName = pcQueueName;
\r
1476 xQueueRegistry[ ux ].xHandle = xQueue;
\r
1483 /*-----------------------------------------------------------*/
\r
1485 #if configQUEUE_REGISTRY_SIZE > 0
\r
1487 static void vQueueUnregisterQueue( xQueueHandle xQueue )
\r
1489 unsigned portBASE_TYPE ux;
\r
1491 /* See if the handle of the queue being unregistered in actually in the
\r
1493 for( ux = ( unsigned portBASE_TYPE ) 0U; ux < ( unsigned portBASE_TYPE ) configQUEUE_REGISTRY_SIZE; ux++ )
\r
1495 if( xQueueRegistry[ ux ].xHandle == xQueue )
\r
1497 /* Set the name to NULL to show that this slot if free again. */
\r
1498 xQueueRegistry[ ux ].pcQueueName = NULL;
\r
1506 /*-----------------------------------------------------------*/
\r
1508 #if configUSE_TIMERS == 1
\r
1510 void vQueueWaitForMessageRestricted( xQueueHandle pxQueue, portTickType xTicksToWait )
\r
1512 /* This function should not be called by application code hence the
\r
1513 'Restricted' in its name. It is not part of the public API. It is
\r
1514 designed for use by kernel code, and has special calling requirements.
\r
1515 It can result in vListInsert() being called on a list that can only
\r
1516 possibly ever have one item in it, so the list will be fast, but even
\r
1517 so it should be called with the scheduler locked and not from a critical
\r
1520 /* Only do anything if there are no messages in the queue. This function
\r
1521 will not actually cause the task to block, just place it on a blocked
\r
1522 list. It will not block until the scheduler is unlocked - at which
\r
1523 time a yield will be performed. If an item is added to the queue while
\r
1524 the queue is locked, and the calling task blocks on the queue, then the
\r
1525 calling task will be immediately unblocked when the queue is unlocked. */
\r
1526 prvLockQueue( pxQueue );
\r
1527 if( pxQueue->uxMessagesWaiting == ( unsigned portBASE_TYPE ) 0U )
\r
1529 /* There is nothing in the queue, block for the specified period. */
\r
1530 vTaskPlaceOnEventListRestricted( &( pxQueue->xTasksWaitingToReceive ), xTicksToWait );
\r
1532 prvUnlockQueue( pxQueue );
\r