2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 * Tests the use of queue sets.
\r
31 * A receive task creates a number of queues and adds them to a queue set before
\r
32 * blocking on the queue set receive. A transmit task and (optionally) an
\r
33 * interrupt repeatedly unblocks the receive task by sending messages to the
\r
34 * queues in a pseudo random order. The receive task removes the messages from
\r
35 * the queues and flags an error if the received message does not match that
\r
36 * expected. The task sends values in the range 0 to
\r
37 * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range
\r
38 * queuesetINITIAL_ISR_TX_VALUE to ULONG_MAX.
\r
42 /* Standard includes. */
\r
46 /* Kernel includes. */
\r
47 #include "FreeRTOS.h"
\r
51 /* Demo includes. */
\r
52 #include "QueueSet.h"
\r
55 #if( configUSE_QUEUE_SETS == 1 ) /* Remove the tests if queue sets are not defined. */
\r
58 /* The number of queues that are created and added to the queue set. */
\r
59 #define queuesetNUM_QUEUES_IN_SET 3
\r
61 /* The length of each created queue. */
\r
62 #define queuesetQUEUE_LENGTH 3
\r
64 /* Block times used in this demo. A block time or 0 means "don't block". */
\r
65 #define queuesetSHORT_DELAY 200
\r
66 #define queuesetDONT_BLOCK 0
\r
68 /* Messages are sent in incrementing order from both a task and an interrupt.
\r
69 The task sends values in the range 0 to 0xfffe, and the interrupt sends values
\r
70 in the range of 0xffff to ULONG_MAX. */
\r
71 #define queuesetINITIAL_ISR_TX_VALUE 0xffffUL
\r
73 /* The priorities used in this demo. */
\r
74 #define queuesetLOW_PRIORITY ( tskIDLE_PRIORITY )
\r
75 #define queuesetMEDIUM_PRIORITY ( queuesetLOW_PRIORITY + 1 )
\r
77 /* For test purposes the priority of the sending task is changed after every
\r
78 queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */
\r
79 #define queuesetPRIORITY_CHANGE_LOOPS ( ( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH ) * 2 )
\r
81 /* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */
\r
82 #define queuesetISR_TX_PERIOD ( 100UL )
\r
84 /* A delay inserted when the Tx task changes its priority to be above the idle
\r
85 task priority to ensure the idle priority tasks get some CPU time before the
\r
86 next iteration of the queue set Tx task. */
\r
87 #define queuesetTX_LOOP_DELAY pdMS_TO_TICKS( ( TickType_t ) 200 )
\r
89 /* The allowable maximum deviation between a received value and the expected
\r
90 received value. A deviation will occur when data is received from a queue
\r
91 inside an ISR in between a task receiving from a queue and the task checking
\r
92 the received value. */
\r
93 #define queuesetALLOWABLE_RX_DEVIATION 3
\r
95 /* Ignore values that are at the boundaries of allowable values to make the
\r
96 testing of limits easier (don't have to deal with wrapping values). */
\r
97 #define queuesetIGNORED_BOUNDARY ( queuesetALLOWABLE_RX_DEVIATION * 2 )
\r
101 eEqualPriority = 0, /* Tx and Rx tasks have the same priority. */
\r
102 eTxHigherPriority, /* The priority of the Tx task is above that of the Rx task. */
\r
103 eTxLowerPriority /* The priority of the Tx task is below that of the Rx task. */
\r
104 } eRelativePriorities;
\r
107 * The task that periodically sends to the queue set.
\r
109 static void prvQueueSetSendingTask( void *pvParameters );
\r
112 * The task that reads from the queue set.
\r
114 static void prvQueueSetReceivingTask( void *pvParameters );
\r
117 * Check the value received from a queue is the expected value. Some values
\r
118 * originate from the send task, some values originate from the ISR, with the
\r
119 * range of the value being used to distinguish between the two message
\r
122 static void prvCheckReceivedValue( uint32_t ulReceived );
\r
125 * For purposes of test coverage, functions that read from and write to a
\r
126 * queue set from an ISR respectively.
\r
128 static void prvReceiveFromQueueInSetFromISR( void );
\r
129 static void prvSendToQueueInSetFromISR( void );
\r
132 * Create the queues and add them to a queue set before resuming the Tx
\r
135 static void prvSetupTest( void );
\r
138 * Checks a value received from a queue falls within the range of expected
\r
141 static BaseType_t prvCheckReceivedValueWithinExpectedRange( uint32_t ulReceived, uint32_t ulExpectedReceived );
\r
144 * Increase test coverage by occasionally change the priorities of the two tasks
\r
145 * relative to each other.
\r
147 static void prvChangeRelativePriorities( void );
\r
150 * Queue overwrites can only be performed on queues of length of one, requiring
\r
151 * a special test function so a queue of length 1 can temporarily be added to a
\r
154 static void prvTestQueueOverwriteWithQueueSet( void );
\r
157 * Test the case where two queues within a set are written to with
\r
158 * xQueueOverwrite().
\r
160 static void prvTestQueueOverwriteOnTwoQueusInQueueSet( void );
\r
161 static void prvTestQueueOverwriteFromISROnTwoQueusInQueueSet( void );
\r
164 * Local pseudo random number seed and return functions. Used to avoid calls
\r
165 * to the standard library.
\r
167 static size_t prvRand( void );
\r
168 static void prvSRand( size_t uxSeed );
\r
170 /*-----------------------------------------------------------*/
\r
172 /* The queues that are added to the set. */
\r
173 static QueueHandle_t xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
175 /* Counts how many times each queue in the set is used to ensure all the
\r
176 queues are used. */
\r
177 static uint32_t ulQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
179 /* The handle of the queue set to which the queues are added. */
\r
180 static QueueSetHandle_t xQueueSet;
\r
182 /* If the prvQueueSetReceivingTask() task has not detected any errors then
\r
183 it increments ulCycleCounter on each iteration.
\r
184 xAreQueueSetTasksStillRunning() returns pdPASS if the value of
\r
185 ulCycleCounter has changed between consecutive calls, and pdFALSE if
\r
186 ulCycleCounter has stopped incrementing (indicating an error condition). */
\r
187 static volatile uint32_t ulCycleCounter = 0UL;
\r
189 /* Set to pdFAIL if an error is detected by any queue set task.
\r
190 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
\r
191 static volatile BaseType_t xQueueSetTasksStatus = pdPASS;
\r
193 /* Just a flag to let the function that writes to a queue from an ISR know that
\r
194 the queues are setup and can be used. */
\r
195 static volatile BaseType_t xSetupComplete = pdFALSE;
\r
197 /* The value sent to the queue from the ISR is file scope so the
\r
198 xAreQueeuSetTasksStillRunning() function can check it is incrementing as
\r
200 static volatile uint32_t ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
202 /* Used by the pseudo random number generator. */
\r
203 static size_t uxNextRand = 0;
\r
205 /* The task handles are stored so their priorities can be changed. */
\r
206 TaskHandle_t xQueueSetSendingTask, xQueueSetReceivingTask;
\r
208 /*-----------------------------------------------------------*/
\r
210 void vStartQueueSetTasks( void )
\r
212 /* Create the tasks. */
\r
213 xTaskCreate( prvQueueSetSendingTask, "SetTx", configMINIMAL_STACK_SIZE, NULL, queuesetMEDIUM_PRIORITY, &xQueueSetSendingTask );
\r
215 if( xQueueSetSendingTask != NULL )
\r
217 xTaskCreate( prvQueueSetReceivingTask, "SetRx", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, queuesetMEDIUM_PRIORITY, &xQueueSetReceivingTask );
\r
219 /* It is important that the sending task does not attempt to write to a
\r
220 queue before the queue has been created. It is therefore placed into
\r
221 the suspended state before the scheduler has started. It is resumed by
\r
222 the receiving task after the receiving task has created the queues and
\r
223 added the queues to the queue set. */
\r
224 vTaskSuspend( xQueueSetSendingTask );
\r
227 /*-----------------------------------------------------------*/
\r
229 BaseType_t xAreQueueSetTasksStillRunning( void )
\r
231 static uint32_t ulLastCycleCounter, ulLastISRTxValue = 0;
\r
232 static uint32_t ulLastQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
233 BaseType_t xReturn = pdPASS, x;
\r
235 if( ulLastCycleCounter == ulCycleCounter )
\r
237 /* The cycle counter is no longer being incremented. Either one of the
\r
238 tasks is stalled or an error has been detected. */
\r
242 ulLastCycleCounter = ulCycleCounter;
\r
244 /* Ensure that all the queues in the set have been used. This ensures the
\r
245 test is working as intended and guards against the rand() in the Tx task
\r
246 missing some values. */
\r
247 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
249 if( ulLastQueueUsedCounter[ x ] == ulQueueUsedCounter[ x ] )
\r
254 ulLastQueueUsedCounter[ x ] = ulQueueUsedCounter[ x ];
\r
257 /* Check the global status flag. */
\r
258 if( xQueueSetTasksStatus != pdPASS )
\r
263 /* Check that the ISR is still sending values to the queues too. */
\r
264 if( ulISRTxValue == ulLastISRTxValue )
\r
270 ulLastISRTxValue = ulISRTxValue;
\r
275 /*-----------------------------------------------------------*/
\r
277 static void prvQueueSetSendingTask( void *pvParameters )
\r
279 uint32_t ulTaskTxValue = 0;
\r
280 size_t uxQueueToWriteTo;
\r
281 QueueHandle_t xQueueInUse;
\r
283 /* Remove compiler warning about the unused parameter. */
\r
284 ( void ) pvParameters;
\r
286 /* Seed mini pseudo random number generator. */
\r
287 prvSRand( ( size_t ) &ulTaskTxValue );
\r
291 /* Generate the index for the queue to which a value is to be sent. */
\r
292 uxQueueToWriteTo = prvRand() % queuesetNUM_QUEUES_IN_SET;
\r
293 xQueueInUse = xQueues[ uxQueueToWriteTo ];
\r
295 /* Note which index is being written to to ensure all the queues are
\r
297 ( ulQueueUsedCounter[ uxQueueToWriteTo ] )++;
\r
299 /* Send to the queue to unblock the task that is waiting for data to
\r
300 arrive on a queue within the queue set to which this queue belongs. */
\r
301 if( xQueueSendToBack( xQueueInUse, &ulTaskTxValue, portMAX_DELAY ) != pdPASS )
\r
303 /* The send should always pass as an infinite block time was
\r
305 xQueueSetTasksStatus = pdFAIL;
\r
308 #if( configUSE_PREEMPTION == 0 )
\r
314 /* If the Tx value has reached the range used by the ISR then set it
\r
316 if( ulTaskTxValue == queuesetINITIAL_ISR_TX_VALUE )
\r
321 /* Increase test coverage by occasionally change the priorities of the
\r
322 two tasks relative to each other. */
\r
323 prvChangeRelativePriorities();
\r
326 /*-----------------------------------------------------------*/
\r
328 static void prvChangeRelativePriorities( void )
\r
330 static UBaseType_t ulLoops = 0;
\r
331 static eRelativePriorities ePriorities = eEqualPriority;
\r
333 /* Occasionally change the task priority relative to the priority of
\r
334 the receiving task. */
\r
336 if( ulLoops >= queuesetPRIORITY_CHANGE_LOOPS )
\r
340 switch( ePriorities )
\r
342 case eEqualPriority:
\r
343 /* Both tasks are running with medium priority. Now lower the
\r
344 priority of the receiving task so the Tx task has the higher
\r
345 relative priority. */
\r
346 vTaskPrioritySet( xQueueSetReceivingTask, queuesetLOW_PRIORITY );
\r
347 ePriorities = eTxHigherPriority;
\r
350 case eTxHigherPriority:
\r
351 /* The Tx task is running with a higher priority than the Rx
\r
352 task. Switch the priorities around so the Rx task has the
\r
353 higher relative priority. */
\r
354 vTaskPrioritySet( xQueueSetReceivingTask, queuesetMEDIUM_PRIORITY );
\r
355 vTaskPrioritySet( xQueueSetSendingTask, queuesetLOW_PRIORITY );
\r
356 ePriorities = eTxLowerPriority;
\r
359 case eTxLowerPriority:
\r
360 /* The Tx task is running with a lower priority than the Rx
\r
361 task. Make the priorities equal again. */
\r
362 vTaskPrioritySet( xQueueSetSendingTask, queuesetMEDIUM_PRIORITY );
\r
363 ePriorities = eEqualPriority;
\r
365 /* When both tasks are using a non-idle priority the queue set
\r
366 tasks will starve idle priority tasks of execution time - so
\r
367 relax a bit before the next iteration to minimise the impact. */
\r
368 vTaskDelay( queuesetTX_LOOP_DELAY );
\r
374 /*-----------------------------------------------------------*/
\r
376 static void prvQueueSetReceivingTask( void *pvParameters )
\r
378 uint32_t ulReceived;
\r
379 QueueHandle_t xActivatedQueue;
\r
380 TickType_t xBlockTime;
\r
382 /* Remove compiler warnings. */
\r
383 ( void ) pvParameters;
\r
385 /* Create the queues and add them to the queue set before resuming the Tx
\r
391 /* For test coverage reasons, the block time is dependent on the
\r
392 priority of this task - which changes during the test. When the task
\r
393 is at the idle priority it polls the queue set. */
\r
394 if( uxTaskPriorityGet( NULL ) == tskIDLE_PRIORITY )
\r
400 xBlockTime = portMAX_DELAY;
\r
403 /* Wait for a message to arrive on one of the queues in the set. */
\r
404 xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );
\r
406 if( xActivatedQueue == NULL )
\r
408 if( xBlockTime != 0 )
\r
410 /* This should not happen as an infinite delay was used. */
\r
411 xQueueSetTasksStatus = pdFAIL;
\r
416 /* Reading from the queue should pass with a zero block time as
\r
417 this task will only run when something has been posted to a task
\r
418 in the queue set. */
\r
419 if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )
\r
421 xQueueSetTasksStatus = pdFAIL;
\r
424 /* Ensure the value received was the value expected. This function
\r
425 manipulates file scope data and is also called from an ISR, hence
\r
426 the critical section. */
\r
427 taskENTER_CRITICAL();
\r
429 prvCheckReceivedValue( ulReceived );
\r
431 taskEXIT_CRITICAL();
\r
433 if( xQueueSetTasksStatus == pdPASS )
\r
440 /*-----------------------------------------------------------*/
\r
442 void vQueueSetAccessQueueSetFromISR( void )
\r
444 static uint32_t ulCallCount = 0;
\r
446 /* xSetupComplete is set to pdTRUE when the queues have been created and
\r
447 are available for use. */
\r
448 if( xSetupComplete == pdTRUE )
\r
450 /* It is intended that this function is called from the tick hook
\r
451 function, so each call is one tick period apart. */
\r
453 if( ulCallCount > queuesetISR_TX_PERIOD )
\r
457 /* First attempt to read from the queue set. */
\r
458 prvReceiveFromQueueInSetFromISR();
\r
460 /* Then write to the queue set. */
\r
461 prvSendToQueueInSetFromISR();
\r
465 /*-----------------------------------------------------------*/
\r
467 static void prvCheckReceivedValue( uint32_t ulReceived )
\r
469 static uint32_t ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
471 /* Values are received in tasks and interrupts. It is likely that the
\r
472 receiving task will sometimes get preempted by the receiving interrupt
\r
473 between reading a value from the queue and calling this function. When
\r
474 that happens, if the receiving interrupt calls this function the values
\r
475 will get passed into this function slightly out of order. For that
\r
476 reason the value passed in is tested against a small range of expected
\r
477 values, rather than a single absolute value. To make the range testing
\r
478 easier values in the range limits are ignored. */
\r
480 /* If the received value is equal to or greater than
\r
481 queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */
\r
482 if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )
\r
484 /* The value was sent from the ISR. */
\r
485 if( ( ulReceived - queuesetINITIAL_ISR_TX_VALUE ) < queuesetIGNORED_BOUNDARY )
\r
487 /* The value received is at the lower limit of the expected range.
\r
488 Don't test it and expect to receive one higher next time. */
\r
490 else if( ( ULONG_MAX - ulReceived ) <= queuesetIGNORED_BOUNDARY )
\r
492 /* The value received is at the higher limit of the expected range.
\r
493 Don't test it and expect to wrap soon. */
\r
497 /* Check the value against its expected value range. */
\r
498 if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromISR ) != pdPASS )
\r
500 xQueueSetTasksStatus = pdFAIL;
\r
504 configASSERT( xQueueSetTasksStatus );
\r
506 /* It is expected to receive an incrementing number. */
\r
507 ulExpectedReceivedFromISR++;
\r
508 if( ulExpectedReceivedFromISR == 0 )
\r
510 ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
515 /* The value was sent from the Tx task. */
\r
516 if( ulReceived < queuesetIGNORED_BOUNDARY )
\r
518 /* The value received is at the lower limit of the expected range.
\r
519 Don't test it, and expect to receive one higher next time. */
\r
521 else if( ( ( queuesetINITIAL_ISR_TX_VALUE - 1 ) - ulReceived ) <= queuesetIGNORED_BOUNDARY )
\r
523 /* The value received is at the higher limit of the expected range.
\r
524 Don't test it and expect to wrap soon. */
\r
528 /* Check the value against its expected value range. */
\r
529 if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromTask ) != pdPASS )
\r
531 xQueueSetTasksStatus = pdFAIL;
\r
535 configASSERT( xQueueSetTasksStatus );
\r
537 /* It is expected to receive an incrementing number. */
\r
538 ulExpectedReceivedFromTask++;
\r
539 if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )
\r
541 ulExpectedReceivedFromTask = 0;
\r
545 /*-----------------------------------------------------------*/
\r
547 static BaseType_t prvCheckReceivedValueWithinExpectedRange( uint32_t ulReceived, uint32_t ulExpectedReceived )
\r
549 BaseType_t xReturn = pdPASS;
\r
551 if( ulReceived > ulExpectedReceived )
\r
553 configASSERT( ( ulReceived - ulExpectedReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
\r
554 if( ( ulReceived - ulExpectedReceived ) > queuesetALLOWABLE_RX_DEVIATION )
\r
561 configASSERT( ( ulExpectedReceived - ulReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
\r
562 if( ( ulExpectedReceived - ulReceived ) > queuesetALLOWABLE_RX_DEVIATION )
\r
570 /*-----------------------------------------------------------*/
\r
572 static void prvReceiveFromQueueInSetFromISR( void )
\r
574 QueueSetMemberHandle_t xActivatedQueue;
\r
575 uint32_t ulReceived;
\r
577 /* See if any of the queues in the set contain data. */
\r
578 xActivatedQueue = xQueueSelectFromSetFromISR( xQueueSet );
\r
580 if( xActivatedQueue != NULL )
\r
582 /* Reading from the queue for test purposes only. */
\r
583 if( xQueueReceiveFromISR( xActivatedQueue, &ulReceived, NULL ) != pdPASS )
\r
585 /* Data should have been available as the handle was returned from
\r
586 xQueueSelectFromSetFromISR(). */
\r
587 xQueueSetTasksStatus = pdFAIL;
\r
590 /* Ensure the value received was the value expected. */
\r
591 prvCheckReceivedValue( ulReceived );
\r
594 /*-----------------------------------------------------------*/
\r
596 static void prvSendToQueueInSetFromISR( void )
\r
598 static BaseType_t xQueueToWriteTo = 0;
\r
599 uint32_t ulTxValueSnapshot = ulISRTxValue;
\r
601 if( xQueueSendFromISR( xQueues[ xQueueToWriteTo ], ( void * ) &ulTxValueSnapshot, NULL ) == pdPASS )
\r
605 /* If the Tx value has wrapped then set it back to its initial value. */
\r
606 if( ulISRTxValue == 0UL )
\r
608 ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
611 /* Use a different queue next time. */
\r
613 if( xQueueToWriteTo >= queuesetNUM_QUEUES_IN_SET )
\r
615 xQueueToWriteTo = 0;
\r
619 /*-----------------------------------------------------------*/
\r
621 static void prvTestQueueOverwriteWithQueueSet( void )
\r
623 uint32_t ulValueToSend = 0, ulValueReceived = 0;
\r
624 QueueHandle_t xQueueHandle = NULL, xReceivedHandle = NULL;
\r
625 const UBaseType_t xLengthOfOne = ( UBaseType_t ) 1;
\r
627 /* Create a queue that has a length of one - a requirement in order to call
\r
628 xQueueOverwrite. This will get deleted again when this test completes. */
\r
629 xQueueHandle = xQueueCreate( xLengthOfOne, sizeof( uint32_t ) );
\r
630 configASSERT( xQueueHandle );
\r
632 if( xQueueHandle != NULL )
\r
634 xQueueAddToSet( xQueueHandle, xQueueSet );
\r
636 /* Add an item to the queue then ensure the queue set correctly
\r
637 indicates that one item is available, and that item is indeed the
\r
638 queue written to. */
\r
639 xQueueOverwrite( xQueueHandle, ( void * ) &ulValueToSend );
\r
640 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 1 )
\r
642 /* Expected one item in the queue set. */
\r
643 xQueueSetTasksStatus = pdFAIL;
\r
645 xQueuePeek( xQueueSet, &xReceivedHandle, queuesetDONT_BLOCK );
\r
646 if( xReceivedHandle != xQueueHandle )
\r
648 /* Wrote to xQueueHandle so expected xQueueHandle to be the handle
\r
649 held in the queue set. */
\r
650 xQueueSetTasksStatus = pdFAIL;
\r
653 /* Now overwrite the value in the queue and ensure the queue set state
\r
654 doesn't change as the number of items in the queues within the set have
\r
657 xQueueOverwrite( xQueueHandle, ( void * ) &ulValueToSend );
\r
658 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 1 )
\r
660 /* Still expected one item in the queue set. */
\r
661 xQueueSetTasksStatus = pdFAIL;
\r
663 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
664 if( xReceivedHandle != xQueueHandle )
\r
666 /* Wrote to xQueueHandle so expected xQueueHandle to be the handle
\r
667 held in the queue set. */
\r
668 xQueueSetTasksStatus = pdFAIL;
\r
671 /* Also ensure the value received from the queue is the overwritten
\r
672 value, not the value originally written. */
\r
673 xQueueReceive( xQueueHandle, &ulValueReceived, queuesetDONT_BLOCK );
\r
674 if( ulValueReceived != ulValueToSend )
\r
676 /* Unexpected value received from the queue. */
\r
677 xQueueSetTasksStatus = pdFAIL;
\r
680 /* Should be anything in the queue set now. */
\r
681 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 0 )
\r
683 xQueueSetTasksStatus = pdFAIL;
\r
685 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
686 if( xReceivedHandle != NULL )
\r
688 xQueueSetTasksStatus = pdFAIL;
\r
692 xQueueRemoveFromSet( xQueueHandle, xQueueSet );
\r
693 vQueueDelete( xQueueHandle );
\r
696 /*-----------------------------------------------------------*/
\r
698 static void prvTestQueueOverwriteOnTwoQueusInQueueSet( void )
\r
700 uint32_t ulValueToSend1 = 1, ulValueToSend2 = 2UL, ulValueReceived = 0;
\r
701 QueueHandle_t xQueueHandle1 = NULL, xQueueHandle2 = NULL, xReceivedHandle = NULL;
\r
702 const UBaseType_t xLengthOfOne = ( UBaseType_t ) 1;
\r
704 /* Create two queues that have a length of one - a requirement in order to call
\r
705 xQueueOverwrite. These will get deleted again when this test completes. */
\r
706 xQueueHandle1 = xQueueCreate( xLengthOfOne, sizeof( uint32_t ) );
\r
707 configASSERT( xQueueHandle1 );
\r
708 xQueueHandle2 = xQueueCreate( xLengthOfOne, sizeof( uint32_t ) );
\r
709 configASSERT( xQueueHandle2 );
\r
711 if( ( xQueueHandle1 != NULL ) && ( xQueueHandle2 != NULL ) )
\r
713 /* Add both queues to the queue set. */
\r
714 xQueueAddToSet( xQueueHandle1, xQueueSet );
\r
715 xQueueAddToSet( xQueueHandle2, xQueueSet );
\r
717 /* Add an item using the first queue. */
\r
718 xQueueOverwrite( xQueueHandle1, ( void * ) &ulValueToSend1 );
\r
719 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 1 )
\r
721 /* Expected one item in the queue set. */
\r
722 xQueueSetTasksStatus = pdFAIL;
\r
725 xQueuePeek( xQueueSet, &xReceivedHandle, queuesetDONT_BLOCK );
\r
726 if( xReceivedHandle != xQueueHandle1 )
\r
728 /* Wrote to xQueueHandle so expected xQueueHandle to be the handle
\r
729 held in the queue set. */
\r
730 xQueueSetTasksStatus = pdFAIL;
\r
734 /* Next add an item to the second queue. */
\r
735 xQueueOverwrite( xQueueHandle2, ( void * ) &ulValueToSend2 );
\r
736 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
738 /* Expected two items in the queue set. */
\r
739 xQueueSetTasksStatus = pdFAIL;
\r
742 /* The head of the queue set should not have changed though. */
\r
743 xQueuePeek( xQueueSet, &xReceivedHandle, queuesetDONT_BLOCK );
\r
744 if( xReceivedHandle != xQueueHandle1 )
\r
746 /* Wrote to xQueueHandle so expected xQueueHandle to be the handle
\r
747 held in the queue set. */
\r
748 xQueueSetTasksStatus = pdFAIL;
\r
754 /* Now overwrite the value in the queue and ensure the queue set state
\r
755 doesn't change as the number of items in the queues within the set have
\r
756 not changed. NOTE: after this queue 1 should hold ulValueToSend2 and queue
\r
757 2 should hold the value ulValueToSend1. */
\r
758 xQueueOverwrite( xQueueHandle1, ( void * ) &ulValueToSend2 );
\r
759 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
761 /* Still expected two items in the queue set. */
\r
762 xQueueSetTasksStatus = pdFAIL;
\r
764 xQueueOverwrite( xQueueHandle2, ( void * ) &ulValueToSend1 );
\r
765 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
767 /* Still expected two items in the queue set. */
\r
768 xQueueSetTasksStatus = pdFAIL;
\r
772 /* Repeat the above to ensure the queue set state doesn't change. */
\r
773 xQueueOverwrite( xQueueHandle1, ( void * ) &ulValueToSend2 );
\r
774 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
776 /* Still expected two items in the queue set. */
\r
777 xQueueSetTasksStatus = pdFAIL;
\r
779 xQueueOverwrite( xQueueHandle2, ( void * ) &ulValueToSend1 );
\r
780 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
782 /* Still expected two items in the queue set. */
\r
783 xQueueSetTasksStatus = pdFAIL;
\r
787 /* Now when reading from the queue set we expect the handle to the first
\r
788 queue to be received first, and for that queue to hold ulValueToSend2 as the
\r
789 originally written value was overwritten. Likewise the second handle received
\r
790 from the set should be that of the second queue, and that queue should hold
\r
791 ulValueToSend1 as the originally written value was overwritten. */
\r
792 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
793 if( xReceivedHandle != xQueueHandle1 )
\r
795 /* Wrote to xQueueHandle1 first so expected that handle to be read from
\r
797 xQueueSetTasksStatus = pdFAIL;
\r
799 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 1 )
\r
801 /* One value was read from the set, so now only expect a single value
\r
803 xQueueSetTasksStatus = pdFAIL;
\r
805 xQueueReceive( xReceivedHandle, &ulValueReceived, queuesetDONT_BLOCK );
\r
806 if( ulValueReceived != ulValueToSend2 )
\r
808 /* Unexpected value received from the queue. ulValueToSend1 was written
\r
809 first, but then overwritten with ulValueToSend2; */
\r
810 xQueueSetTasksStatus = pdFAIL;
\r
813 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
814 if( xReceivedHandle != xQueueHandle2 )
\r
816 /* xQueueHandle1 has already been removed from the set so expect only
\r
817 xQueueHandle2 to be left. */
\r
818 xQueueSetTasksStatus = pdFAIL;
\r
820 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 0 )
\r
822 /* The last value was read from the set so don't expect any more. */
\r
823 xQueueSetTasksStatus = pdFAIL;
\r
825 xQueueReceive( xReceivedHandle, &ulValueReceived, queuesetDONT_BLOCK );
\r
826 if( ulValueReceived != ulValueToSend1 )
\r
828 /* Unexpected value received from the queue. ulValueToSend2 was written
\r
829 first, but then overwritten with ulValueToSend1. */
\r
830 xQueueSetTasksStatus = pdFAIL;
\r
836 /* Should be anything in the queue set now. */
\r
837 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
838 if( xReceivedHandle != NULL )
\r
840 xQueueSetTasksStatus = pdFAIL;
\r
844 xQueueRemoveFromSet( xQueueHandle1, xQueueSet );
\r
845 xQueueRemoveFromSet( xQueueHandle2, xQueueSet );
\r
846 vQueueDelete( xQueueHandle1 );
\r
847 vQueueDelete( xQueueHandle2 );
\r
850 /*-----------------------------------------------------------*/
\r
852 static void prvTestQueueOverwriteFromISROnTwoQueusInQueueSet( void )
\r
854 uint32_t ulValueToSend1 = 1, ulValueToSend2 = 2UL, ulValueReceived = 0;
\r
855 QueueHandle_t xQueueHandle1 = NULL, xQueueHandle2 = NULL, xReceivedHandle = NULL;
\r
856 const UBaseType_t xLengthOfOne = ( UBaseType_t ) 1;
\r
858 /* Create two queues that have a length of one - a requirement in order to call
\r
859 xQueueOverwrite. These will get deleted again when this test completes. */
\r
860 xQueueHandle1 = xQueueCreate( xLengthOfOne, sizeof( uint32_t ) );
\r
861 configASSERT( xQueueHandle1 );
\r
862 xQueueHandle2 = xQueueCreate( xLengthOfOne, sizeof( uint32_t ) );
\r
863 configASSERT( xQueueHandle2 );
\r
865 if( ( xQueueHandle1 != NULL ) && ( xQueueHandle2 != NULL ) )
\r
867 /* Add both queues to the queue set. */
\r
868 xQueueAddToSet( xQueueHandle1, xQueueSet );
\r
869 xQueueAddToSet( xQueueHandle2, xQueueSet );
\r
871 /* Add an item using the first queue using the 'FromISR' version of the
\r
872 overwrite function. */
\r
873 xQueueOverwriteFromISR( xQueueHandle1, ( void * ) &ulValueToSend1, NULL );
\r
874 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 1 )
\r
876 /* Expected one item in the queue set. */
\r
877 xQueueSetTasksStatus = pdFAIL;
\r
880 xQueuePeek( xQueueSet, &xReceivedHandle, queuesetDONT_BLOCK );
\r
881 if( xReceivedHandle != xQueueHandle1 )
\r
883 /* Wrote to xQueueHandle so expected xQueueHandle to be the handle
\r
884 held in the queue set. */
\r
885 xQueueSetTasksStatus = pdFAIL;
\r
889 /* Next add an item to the second queue using the 'FromISR' version of the
\r
890 overwrite function. */
\r
891 xQueueOverwriteFromISR( xQueueHandle2, ( void * ) &ulValueToSend2, NULL );
\r
892 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
894 /* Expected two items in the queue set. */
\r
895 xQueueSetTasksStatus = pdFAIL;
\r
898 /* The head of the queue set should not have changed though. */
\r
899 xQueuePeek( xQueueSet, &xReceivedHandle, queuesetDONT_BLOCK );
\r
900 if( xReceivedHandle != xQueueHandle1 )
\r
902 /* Wrote to xQueueHandle so expected xQueueHandle to be the handle
\r
903 held in the queue set. */
\r
904 xQueueSetTasksStatus = pdFAIL;
\r
910 /* Now overwrite the value in the queue and ensure the queue set state
\r
911 doesn't change as the number of items in the queues within the set have
\r
912 not changed. NOTE: after this queue 1 should hold ulValueToSend2 and queue
\r
913 2 should hold the value ulValueToSend1. */
\r
914 xQueueOverwriteFromISR( xQueueHandle1, ( void * ) &ulValueToSend2, NULL );
\r
915 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
917 /* Still expected two items in the queue set. */
\r
918 xQueueSetTasksStatus = pdFAIL;
\r
920 xQueueOverwriteFromISR( xQueueHandle2, ( void * ) &ulValueToSend1, NULL );
\r
921 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
923 /* Still expected two items in the queue set. */
\r
924 xQueueSetTasksStatus = pdFAIL;
\r
928 /* Repeat the above to ensure the queue set state doesn't change. */
\r
929 xQueueOverwriteFromISR( xQueueHandle1, ( void * ) &ulValueToSend2, NULL );
\r
930 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
932 /* Still expected two items in the queue set. */
\r
933 xQueueSetTasksStatus = pdFAIL;
\r
935 xQueueOverwriteFromISR( xQueueHandle2, ( void * ) &ulValueToSend1, NULL );
\r
936 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 2 )
\r
938 /* Still expected two items in the queue set. */
\r
939 xQueueSetTasksStatus = pdFAIL;
\r
943 /* Now when reading from the queue set we expect the handle to the first
\r
944 queue to be received first, and for that queue to hold ulValueToSend2 as the
\r
945 originally written value was overwritten. Likewise the second handle received
\r
946 from the set should be that of the second queue, and that queue should hold
\r
947 ulValueToSend1 as the originally written value was overwritten. */
\r
948 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
949 if( xReceivedHandle != xQueueHandle1 )
\r
951 /* Wrote to xQueueHandle1 first so expected that handle to be read from
\r
953 xQueueSetTasksStatus = pdFAIL;
\r
955 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 1 )
\r
957 /* One value was read from the set, so now only expect a single value
\r
959 xQueueSetTasksStatus = pdFAIL;
\r
961 xQueueReceive( xReceivedHandle, &ulValueReceived, queuesetDONT_BLOCK );
\r
962 if( ulValueReceived != ulValueToSend2 )
\r
964 /* Unexpected value received from the queue. ulValueToSend1 was written
\r
965 first, but then overwritten with ulValueToSend2; */
\r
966 xQueueSetTasksStatus = pdFAIL;
\r
969 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
970 if( xReceivedHandle != xQueueHandle2 )
\r
972 /* xQueueHandle1 has already been removed from the set so expect only
\r
973 xQueueHandle2 to be left. */
\r
974 xQueueSetTasksStatus = pdFAIL;
\r
976 if( uxQueueMessagesWaiting( xQueueSet ) != ( UBaseType_t ) 0 )
\r
978 /* The last value was read from the set so don't expect any more. */
\r
979 xQueueSetTasksStatus = pdFAIL;
\r
981 xQueueReceive( xReceivedHandle, &ulValueReceived, queuesetDONT_BLOCK );
\r
982 if( ulValueReceived != ulValueToSend1 )
\r
984 /* Unexpected value received from the queue. ulValueToSend2 was written
\r
985 first, but then overwritten with ulValueToSend1. */
\r
986 xQueueSetTasksStatus = pdFAIL;
\r
992 /* Should be anything in the queue set now. */
\r
993 xReceivedHandle = xQueueSelectFromSet( xQueueSet, queuesetDONT_BLOCK );
\r
994 if( xReceivedHandle != NULL )
\r
996 xQueueSetTasksStatus = pdFAIL;
\r
1000 xQueueRemoveFromSet( xQueueHandle1, xQueueSet );
\r
1001 xQueueRemoveFromSet( xQueueHandle2, xQueueSet );
\r
1002 vQueueDelete( xQueueHandle1 );
\r
1003 vQueueDelete( xQueueHandle2 );
\r
1006 /*-----------------------------------------------------------*/
\r
1008 static void prvSetupTest( void )
\r
1011 uint32_t ulValueToSend = 0;
\r
1013 /* Ensure the queues are created and the queue set configured before the
\r
1014 sending task is unsuspended.
\r
1016 First Create the queue set such that it will be able to hold a message for
\r
1017 every space in every queue in the set. */
\r
1018 xQueueSet = xQueueCreateSet( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );
\r
1020 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
1022 /* Create the queue and add it to the set. The queue is just holding
\r
1023 uint32_t value. */
\r
1024 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( uint32_t ) );
\r
1025 configASSERT( xQueues[ x ] );
\r
1026 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdPASS )
\r
1028 xQueueSetTasksStatus = pdFAIL;
\r
1032 /* The queue has now been added to the queue set and cannot be added to
\r
1034 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdFAIL )
\r
1036 xQueueSetTasksStatus = pdFAIL;
\r
1041 /* Attempt to remove a queue from a queue set it does not belong
\r
1042 to (NULL being passed as the queue set in this case). */
\r
1043 if( xQueueRemoveFromSet( xQueues[ 0 ], NULL ) != pdFAIL )
\r
1045 /* It is not possible to successfully remove a queue from a queue
\r
1046 set it does not belong to. */
\r
1047 xQueueSetTasksStatus = pdFAIL;
\r
1050 /* Attempt to remove a queue from the queue set it does belong to. */
\r
1051 if( xQueueRemoveFromSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
\r
1053 /* It should be possible to remove the queue from the queue set it
\r
1054 does belong to. */
\r
1055 xQueueSetTasksStatus = pdFAIL;
\r
1058 /* Add an item to the queue before attempting to add it back into the
\r
1060 xQueueSend( xQueues[ 0 ], ( void * ) &ulValueToSend, 0 );
\r
1061 if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdFAIL )
\r
1063 /* Should not be able to add a non-empty queue to a set. */
\r
1064 xQueueSetTasksStatus = pdFAIL;
\r
1067 /* Remove the item from the queue before adding the queue back into the
\r
1068 set so the dynamic tests can begin. */
\r
1069 xQueueReceive( xQueues[ 0 ], &ulValueToSend, 0 );
\r
1070 if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
\r
1072 /* If the queue was successfully removed from the queue set then it
\r
1073 should be possible to add it back in again. */
\r
1074 xQueueSetTasksStatus = pdFAIL;
\r
1077 /* The task that sends to the queues is not running yet, so attempting to
\r
1078 read from the queue set should fail. */
\r
1079 if( xQueueSelectFromSet( xQueueSet, queuesetSHORT_DELAY ) != NULL )
\r
1081 xQueueSetTasksStatus = pdFAIL;
\r
1084 /* Testing the behaviour of queue sets when a queue overwrite operation is
\r
1085 performed on a set member requires a special test as overwrites can only
\r
1086 be performed on queues that have a length of 1. */
\r
1087 prvTestQueueOverwriteWithQueueSet();
\r
1089 /* Test the case where two queues within a set are written to with
\r
1090 xQueueOverwrite(). */
\r
1091 prvTestQueueOverwriteOnTwoQueusInQueueSet();
\r
1092 prvTestQueueOverwriteFromISROnTwoQueusInQueueSet();
\r
1094 /* In case any of the above have already indicated a failure. */
\r
1095 configASSERT( xQueueSetTasksStatus != pdFAIL );
\r
1097 /* Resume the task that writes to the queues. */
\r
1098 vTaskResume( xQueueSetSendingTask );
\r
1100 /* Let the ISR access the queues also. */
\r
1101 xSetupComplete = pdTRUE;
\r
1103 /*-----------------------------------------------------------*/
\r
1105 static size_t prvRand( void )
\r
1107 uxNextRand = ( uxNextRand * ( size_t ) 1103515245 ) + ( size_t ) 12345;
\r
1108 return ( uxNextRand / ( size_t ) 65536 ) % ( size_t ) 32768;
\r
1110 /*-----------------------------------------------------------*/
\r
1112 static void prvSRand( size_t uxSeed )
\r
1114 uxNextRand = uxSeed;
\r
1117 #endif /* ( configUSE_QUEUE_SETS == 1 ) */
\r