]> begriffs open source - cmsis-freertos/blob - Demo/Common/Minimal/QueueSet.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / Common / Minimal / QueueSet.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /*
71  * Tests the use of queue sets.
72  *
73  * A receive task creates a number of queues and adds them to a queue set before
74  * blocking on the queue set receive.  A transmit task and (optionally) an
75  * interrupt repeatedly unblocks the receive task by sending messages to the
76  * queues in a pseudo random order.  The receive task removes the messages from
77  * the queues and flags an error if the received message does not match that
78  * expected.  The task sends values in the range 0 to
79  * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range
80  * queuesetINITIAL_ISR_TX_VALUE to ULONG_MAX.
81  */
82
83
84 /* Standard includes. */
85 #include <stdlib.h>
86 #include <limits.h>
87
88 /* Kernel includes. */
89 #include "FreeRTOS.h"
90 #include "task.h"
91 #include "queue.h"
92
93 /* Demo includes. */
94 #include "QueueSet.h"
95
96 /* The number of queues that are created and added to the queue set. */
97 #define queuesetNUM_QUEUES_IN_SET 3
98
99 /* The length of each created queue. */
100 #define queuesetQUEUE_LENGTH    3
101
102 /* Block times used in this demo.  A block time or 0 means "don't block". */
103 #define queuesetSHORT_DELAY     200
104 #define queuesetDONT_BLOCK 0
105
106 /* Messages are sent in incrementing order from both a task and an interrupt.
107 The task sends values in the range 0 to 0xfffe, and the interrupt sends values
108 in the range of 0xffff to ULONG_MAX. */
109 #define queuesetINITIAL_ISR_TX_VALUE 0xffffUL
110
111 /* The priorities used in this demo. */
112 #define queuesetLOW_PRIORITY    ( tskIDLE_PRIORITY )
113 #define queuesetMEDIUM_PRIORITY ( queuesetLOW_PRIORITY + 1 )
114
115 /* For test purposes the priority of the sending task is changed after every
116 queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */
117 #define queuesetPRIORITY_CHANGE_LOOPS   ( ( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH ) * 2 )
118
119 /* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */
120 #define queuesetISR_TX_PERIOD   ( 100UL )
121
122 /* A delay inserted when the Tx task changes its priority to be above the idle
123 task priority to ensure the idle priority tasks get some CPU time before the
124 next iteration of the queue set Tx task. */
125 #define queuesetTX_LOOP_DELAY   pdMS_TO_TICKS( ( TickType_t ) 200 )
126
127 /* The allowable maximum deviation between a received value and the expected
128 received value.  A deviation will occur when data is received from a queue
129 inside an ISR in between a task receiving from a queue and the task checking
130 the received value. */
131 #define queuesetALLOWABLE_RX_DEVIATION 3
132
133 /* Ignore values that are at the boundaries of allowable values to make the
134 testing of limits easier (don't have to deal with wrapping values). */
135 #define queuesetIGNORED_BOUNDARY        ( queuesetALLOWABLE_RX_DEVIATION * 2 )
136
137 typedef enum
138 {
139         eEqualPriority = 0,     /* Tx and Rx tasks have the same priority. */
140         eTxHigherPriority,      /* The priority of the Tx task is above that of the Rx task. */
141         eTxLowerPriority        /* The priority of the Tx task is below that of the Rx task. */
142 } eRelativePriorities;
143
144 /*
145  * The task that periodically sends to the queue set.
146  */
147 static void prvQueueSetSendingTask( void *pvParameters );
148
149 /*
150  * The task that reads from the queue set.
151  */
152 static void prvQueueSetReceivingTask( void *pvParameters );
153
154 /*
155  * Check the value received from a queue is the expected value.  Some values
156  * originate from the send task, some values originate from the ISR, with the
157  * range of the value being used to distinguish between the two message
158  * sources.
159  */
160 static void prvCheckReceivedValue( uint32_t ulReceived );
161
162 /*
163  * For purposes of test coverage, functions that read from and write to a
164  * queue set from an ISR respectively.
165  */
166 static void prvReceiveFromQueueInSetFromISR( void );
167 static void prvSendToQueueInSetFromISR( void );
168
169 /*
170  * Create the queues and add them to a queue set before resuming the Tx
171  * task.
172  */
173 static void prvSetupTest( void );
174
175 /*
176  * Checks a value received from a queue falls within the range of expected
177  * values.
178  */
179 static BaseType_t prvCheckReceivedValueWithinExpectedRange( uint32_t ulReceived, uint32_t ulExpectedReceived );
180
181 /*
182  * Increase test coverage by occasionally change the priorities of the two tasks
183  * relative to each other. */
184 static void prvChangeRelativePriorities( void );
185
186 /*
187  * Local pseudo random number seed and return functions.  Used to avoid calls
188  * to the standard library.
189  */
190 static size_t prvRand( void );
191 static void prvSRand( size_t uxSeed );
192
193 /*-----------------------------------------------------------*/
194
195 /* The queues that are added to the set. */
196 static QueueHandle_t xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
197
198 /* Counts how many times each queue in the set is used to ensure all the
199 queues are used. */
200 static uint32_t ulQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
201
202 /* The handle of the queue set to which the queues are added. */
203 static QueueSetHandle_t xQueueSet;
204
205 /* If the prvQueueSetReceivingTask() task has not detected any errors then
206 it increments ulCycleCounter on each iteration.
207 xAreQueueSetTasksStillRunning() returns pdPASS if the value of
208 ulCycleCounter has changed between consecutive calls, and pdFALSE if
209 ulCycleCounter has stopped incrementing (indicating an error condition). */
210 static volatile uint32_t ulCycleCounter = 0UL;
211
212 /* Set to pdFAIL if an error is detected by any queue set task.
213 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
214 static volatile BaseType_t xQueueSetTasksStatus = pdPASS;
215
216 /* Just a flag to let the function that writes to a queue from an ISR know that
217 the queues are setup and can be used. */
218 static volatile BaseType_t xSetupComplete = pdFALSE;
219
220 /* The value sent to the queue from the ISR is file scope so the
221 xAreQueeuSetTasksStillRunning() function can check it is incrementing as
222 expected. */
223 static volatile uint32_t ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
224
225 /* Used by the pseudo random number generator. */
226 static size_t uxNextRand = 0;
227
228 /* The task handles are stored so their priorities can be changed. */
229 TaskHandle_t xQueueSetSendingTask, xQueueSetReceivingTask;
230
231 /*-----------------------------------------------------------*/
232
233 void vStartQueueSetTasks( void )
234 {
235         /* Create the tasks. */
236         xTaskCreate( prvQueueSetSendingTask, "SetTx", configMINIMAL_STACK_SIZE, NULL, queuesetMEDIUM_PRIORITY, &xQueueSetSendingTask );
237
238         if( xQueueSetSendingTask != NULL )
239         {
240                 xTaskCreate( prvQueueSetReceivingTask, "SetRx", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, queuesetMEDIUM_PRIORITY, &xQueueSetReceivingTask );
241
242                 /* It is important that the sending task does not attempt to write to a
243                 queue before the queue has been created.  It is therefore placed into
244                 the suspended state before the scheduler has started.  It is resumed by
245                 the receiving task after the receiving task has created the queues and
246                 added the queues to the queue set. */
247                 vTaskSuspend( xQueueSetSendingTask );
248         }
249 }
250 /*-----------------------------------------------------------*/
251
252 BaseType_t xAreQueueSetTasksStillRunning( void )
253 {
254 static uint32_t ulLastCycleCounter, ulLastISRTxValue = 0;
255 static uint32_t ulLastQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
256 BaseType_t xReturn = pdPASS, x;
257
258         if( ulLastCycleCounter == ulCycleCounter )
259         {
260                 /* The cycle counter is no longer being incremented.  Either one of the
261                 tasks is stalled or an error has been detected. */
262                 xReturn = pdFAIL;
263         }
264
265         ulLastCycleCounter = ulCycleCounter;
266
267         /* Ensure that all the queues in the set have been used.  This ensures the
268         test is working as intended and guards against the rand() in the Tx task
269         missing some values. */
270         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
271         {
272                 if( ulLastQueueUsedCounter[ x ] == ulQueueUsedCounter[ x ] )
273                 {
274                         xReturn = pdFAIL;
275                 }
276
277                 ulLastQueueUsedCounter[ x ] = ulQueueUsedCounter[ x ];
278         }
279
280         /* Check the global status flag. */
281         if( xQueueSetTasksStatus != pdPASS )
282         {
283                 xReturn = pdFAIL;
284         }
285
286         /* Check that the ISR is still sending values to the queues too. */
287         if( ulISRTxValue == ulLastISRTxValue )
288         {
289                 xReturn = pdFAIL;
290         }
291         else
292         {
293                 ulLastISRTxValue = ulISRTxValue;
294         }
295
296         return xReturn;
297 }
298 /*-----------------------------------------------------------*/
299
300 static void prvQueueSetSendingTask( void *pvParameters )
301 {
302 uint32_t ulTaskTxValue = 0;
303 size_t uxQueueToWriteTo;
304 QueueHandle_t xQueueInUse;
305
306         /* Remove compiler warning about the unused parameter. */
307         ( void ) pvParameters;
308
309         /* Seed mini pseudo random number generator. */
310         prvSRand( ( size_t ) &ulTaskTxValue );
311
312         for( ;; )
313         {
314                 /* Generate the index for the queue to which a value is to be sent. */
315                 uxQueueToWriteTo = prvRand() % queuesetNUM_QUEUES_IN_SET;
316                 xQueueInUse = xQueues[ uxQueueToWriteTo ];
317
318                 /* Note which index is being written to to ensure all the queues are
319                 used. */
320                 ( ulQueueUsedCounter[ uxQueueToWriteTo ] )++;
321
322                 /* Send to the queue to unblock the task that is waiting for data to
323                 arrive on a queue within the queue set to which this queue belongs. */
324                 if( xQueueSendToBack( xQueueInUse, &ulTaskTxValue, portMAX_DELAY ) != pdPASS )
325                 {
326                         /* The send should always pass as an infinite block time was
327                         used. */
328                         xQueueSetTasksStatus = pdFAIL;
329                 }
330
331                 #if( configUSE_PREEMPTION == 0 )
332                         taskYIELD();
333                 #endif
334
335                 ulTaskTxValue++;
336
337                 /* If the Tx value has reached the range used by the ISR then set it
338                 back to 0. */
339                 if( ulTaskTxValue == queuesetINITIAL_ISR_TX_VALUE )
340                 {
341                         ulTaskTxValue = 0;
342                 }
343
344                 /* Increase test coverage by occasionally change the priorities of the
345                 two tasks relative to each other. */
346                 prvChangeRelativePriorities();
347         }
348 }
349 /*-----------------------------------------------------------*/
350
351 static void prvChangeRelativePriorities( void )
352 {
353 static UBaseType_t ulLoops = 0;
354 static eRelativePriorities ePriorities = eEqualPriority;
355
356         /* Occasionally change the task priority relative to the priority of
357         the receiving task. */
358         ulLoops++;
359         if( ulLoops >= queuesetPRIORITY_CHANGE_LOOPS )
360         {
361                 ulLoops = 0;
362
363                 switch( ePriorities )
364                 {
365                         case eEqualPriority:
366                                 /* Both tasks are running with medium priority.  Now lower the
367                                 priority of the receiving task so the Tx task has the higher
368                                 relative priority. */
369                                 vTaskPrioritySet( xQueueSetReceivingTask, queuesetLOW_PRIORITY );
370                                 ePriorities = eTxHigherPriority;
371                                 break;
372
373                         case eTxHigherPriority:
374                                 /* The Tx task is running with a higher priority than the Rx
375                                 task.  Switch the priorities around so the Rx task has the
376                                 higher relative priority. */
377                                 vTaskPrioritySet( xQueueSetReceivingTask, queuesetMEDIUM_PRIORITY );
378                                 vTaskPrioritySet( xQueueSetSendingTask, queuesetLOW_PRIORITY );
379                                 ePriorities = eTxLowerPriority;
380                                 break;
381
382                         case eTxLowerPriority:
383                                 /* The Tx task is running with a lower priority than the Rx
384                                 task.  Make the priorities equal again. */
385                                 vTaskPrioritySet( xQueueSetSendingTask, queuesetMEDIUM_PRIORITY );
386                                 ePriorities = eEqualPriority;
387
388                                 /* When both tasks are using a non-idle priority the queue set
389                                 tasks will starve idle priority tasks of execution time - so
390                                 relax a bit before the next iteration to minimise the impact. */
391                                 vTaskDelay( queuesetTX_LOOP_DELAY );
392
393                                 break;
394                 }
395         }
396 }
397 /*-----------------------------------------------------------*/
398
399 static void prvQueueSetReceivingTask( void *pvParameters )
400 {
401 uint32_t ulReceived;
402 QueueHandle_t xActivatedQueue;
403 TickType_t xBlockTime;
404
405         /* Remove compiler warnings. */
406         ( void ) pvParameters;
407
408         /* Create the queues and add them to the queue set before resuming the Tx
409         task. */
410         prvSetupTest();
411
412         for( ;; )
413         {
414                 /* For test coverage reasons, the block time is dependent on the
415                 priority of this task - which changes during the test.  When the task
416                 is at the idle priority it polls the queue set. */
417                 if( uxTaskPriorityGet( NULL ) == tskIDLE_PRIORITY )
418                 {
419                         xBlockTime = 0;
420                 }
421                 else
422                 {
423                         xBlockTime = portMAX_DELAY;
424                 }
425
426                 /* Wait for a message to arrive on one of the queues in the set. */
427                 xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );
428
429                 if( xActivatedQueue == NULL )
430                 {
431                         if( xBlockTime != 0 )
432                         {
433                                 /* This should not happen as an infinite delay was used. */
434                                 xQueueSetTasksStatus = pdFAIL;
435                         }
436                 }
437                 else
438                 {
439                         /* Reading from the queue should pass with a zero block time as
440                         this task will only run when something has been posted to a task
441                         in the queue set. */
442                         if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )
443                         {
444                                 xQueueSetTasksStatus = pdFAIL;
445                         }
446
447                         /* Ensure the value received was the value expected.  This function
448                         manipulates file scope data and is also called from an ISR, hence
449                         the critical section. */
450                         taskENTER_CRITICAL();
451                         {
452                                 prvCheckReceivedValue( ulReceived );
453                         }
454                         taskEXIT_CRITICAL();
455
456                         if( xQueueSetTasksStatus == pdPASS )
457                         {
458                                 ulCycleCounter++;
459                         }
460                 }
461         }
462 }
463 /*-----------------------------------------------------------*/
464
465 void vQueueSetAccessQueueSetFromISR( void )
466 {
467 static uint32_t ulCallCount = 0;
468
469         /* xSetupComplete is set to pdTRUE when the queues have been created and
470         are available for use. */
471         if( xSetupComplete == pdTRUE )
472         {
473                 /* It is intended that this function is called from the tick hook
474                 function, so each call is one tick period apart. */
475                 ulCallCount++;
476                 if( ulCallCount > queuesetISR_TX_PERIOD )
477                 {
478                         ulCallCount = 0;
479
480                         /* First attempt to read from the queue set. */
481                         prvReceiveFromQueueInSetFromISR();
482
483                         /* Then write to the queue set. */
484                         prvSendToQueueInSetFromISR();
485                 }
486         }
487 }
488 /*-----------------------------------------------------------*/
489
490 static void prvCheckReceivedValue( uint32_t ulReceived )
491 {
492 static uint32_t ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
493
494         /* Values are received in tasks and interrupts.  It is likely that the
495         receiving task will sometimes get preempted by the receiving interrupt
496         between reading a value from the queue and calling this function.  When
497         that happens, if the receiving interrupt calls this function the values
498         will get passed into this function slightly out of order.  For that
499         reason the value passed in is tested against a small range of expected
500         values, rather than a single absolute value.  To make the range testing
501         easier values in the range limits are ignored. */
502
503         /* If the received value is equal to or greater than
504         queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */
505         if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )
506         {
507                 /* The value was sent from the ISR. */
508                 if( ( ulReceived - queuesetINITIAL_ISR_TX_VALUE ) < queuesetIGNORED_BOUNDARY )
509                 {
510                         /* The value received is at the lower limit of the expected range.
511                         Don't test it and expect to receive one higher next time. */
512                 }
513                 else if( ( ULONG_MAX - ulReceived ) <= queuesetIGNORED_BOUNDARY )
514                 {
515                         /* The value received is at the higher limit of the expected range.
516                         Don't test it and expect to wrap soon. */
517                 }
518                 else
519                 {
520                         /* Check the value against its expected value range. */
521                         if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromISR ) != pdPASS )
522                         {
523                                 xQueueSetTasksStatus = pdFAIL;
524                         }
525                 }
526
527                 configASSERT( xQueueSetTasksStatus );
528
529                 /* It is expected to receive an incrementing number. */
530                 ulExpectedReceivedFromISR++;
531                 if( ulExpectedReceivedFromISR == 0 )
532                 {
533                         ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
534                 }
535         }
536         else
537         {
538                 /* The value was sent from the Tx task. */
539                 if( ulReceived < queuesetIGNORED_BOUNDARY )
540                 {
541                         /* The value received is at the lower limit of the expected range.
542                         Don't test it, and expect to receive one higher next time. */
543                 }
544                 else if( ( ( queuesetINITIAL_ISR_TX_VALUE - 1 ) - ulReceived ) <= queuesetIGNORED_BOUNDARY )
545                 {
546                         /* The value received is at the higher limit of the expected range.
547                         Don't test it and expect to wrap soon. */
548                 }
549                 else
550                 {
551                         /* Check the value against its expected value range. */
552                         if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromTask ) != pdPASS )
553                         {
554                                 xQueueSetTasksStatus = pdFAIL;
555                         }
556                 }
557
558                 configASSERT( xQueueSetTasksStatus );
559
560                 /* It is expected to receive an incrementing number. */
561                 ulExpectedReceivedFromTask++;
562                 if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )
563                 {
564                         ulExpectedReceivedFromTask = 0;
565                 }
566         }
567 }
568 /*-----------------------------------------------------------*/
569
570 static BaseType_t prvCheckReceivedValueWithinExpectedRange( uint32_t ulReceived, uint32_t ulExpectedReceived )
571 {
572 BaseType_t xReturn = pdPASS;
573
574         if( ulReceived > ulExpectedReceived )
575         {
576                 configASSERT( ( ulReceived - ulExpectedReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
577                 if( ( ulReceived - ulExpectedReceived ) > queuesetALLOWABLE_RX_DEVIATION )
578                 {
579                         xReturn = pdFALSE;
580                 }
581         }
582         else
583         {
584                 configASSERT( ( ulExpectedReceived - ulReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
585                 if( ( ulExpectedReceived - ulReceived ) > queuesetALLOWABLE_RX_DEVIATION )
586                 {
587                         xReturn = pdFALSE;
588                 }
589         }
590
591         return xReturn;
592 }
593 /*-----------------------------------------------------------*/
594
595 static void prvReceiveFromQueueInSetFromISR( void )
596 {
597 QueueSetMemberHandle_t xActivatedQueue;
598 uint32_t ulReceived;
599
600         /* See if any of the queues in the set contain data. */
601         xActivatedQueue = xQueueSelectFromSetFromISR( xQueueSet );
602
603         if( xActivatedQueue != NULL )
604         {
605                 /* Reading from the queue for test purposes only. */
606                 if( xQueueReceiveFromISR( xActivatedQueue, &ulReceived, NULL ) != pdPASS )
607                 {
608                         /* Data should have been available as the handle was returned from
609                         xQueueSelectFromSetFromISR(). */
610                         xQueueSetTasksStatus = pdFAIL;
611                 }
612
613                 /* Ensure the value received was the value expected. */
614                 prvCheckReceivedValue( ulReceived );
615         }
616 }
617 /*-----------------------------------------------------------*/
618
619 static void prvSendToQueueInSetFromISR( void )
620 {
621 static BaseType_t xQueueToWriteTo = 0;
622
623         if( xQueueSendFromISR( xQueues[ xQueueToWriteTo ], ( void * ) &ulISRTxValue, NULL ) == pdPASS )
624         {
625                 ulISRTxValue++;
626
627                 /* If the Tx value has wrapped then set it back to its initial value. */
628                 if( ulISRTxValue == 0UL )
629                 {
630                         ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
631                 }
632
633                 /* Use a different queue next time. */
634                 xQueueToWriteTo++;
635                 if( xQueueToWriteTo >= queuesetNUM_QUEUES_IN_SET )
636                 {
637                         xQueueToWriteTo = 0;
638                 }
639         }
640 }
641 /*-----------------------------------------------------------*/
642
643 static void prvSetupTest( void )
644 {
645 BaseType_t x;
646 uint32_t ulValueToSend = 0;
647
648         /* Ensure the queues are created and the queue set configured before the
649         sending task is unsuspended.
650
651         First Create the queue set such that it will be able to hold a message for
652         every space in every queue in the set. */
653         xQueueSet = xQueueCreateSet( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );
654
655         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
656         {
657                 /* Create the queue and add it to the set.  The queue is just holding
658                 uint32_t value. */
659                 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( uint32_t ) );
660                 configASSERT( xQueues[ x ] );
661                 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdPASS )
662                 {
663                         xQueueSetTasksStatus = pdFAIL;
664                 }
665                 else
666                 {
667                         /* The queue has now been added to the queue set and cannot be added to
668                         another. */
669                         if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdFAIL )
670                         {
671                                 xQueueSetTasksStatus = pdFAIL;
672                         }
673                 }
674         }
675
676         /* Attempt to remove a queue from a queue set it does not belong
677         to (NULL being passed as the queue set in this case). */
678         if( xQueueRemoveFromSet( xQueues[ 0 ], NULL ) != pdFAIL )
679         {
680                 /* It is not possible to successfully remove a queue from a queue
681                 set it does not belong to. */
682                 xQueueSetTasksStatus = pdFAIL;
683         }
684
685         /* Attempt to remove a queue from the queue set it does belong to. */
686         if( xQueueRemoveFromSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
687         {
688                 /* It should be possible to remove the queue from the queue set it
689                 does belong to. */
690                 xQueueSetTasksStatus = pdFAIL;
691         }
692
693         /* Add an item to the queue before attempting to add it back into the
694         set. */
695         xQueueSend( xQueues[ 0 ], ( void * ) &ulValueToSend, 0 );
696         if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdFAIL )
697         {
698                 /* Should not be able to add a non-empty queue to a set. */
699                 xQueueSetTasksStatus = pdFAIL;
700         }
701
702         /* Remove the item from the queue before adding the queue back into the
703         set so the dynamic tests can begin. */
704         xQueueReceive( xQueues[ 0 ], &ulValueToSend, 0 );
705         if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
706         {
707                 /* If the queue was successfully removed from the queue set then it
708                 should be possible to add it back in again. */
709                 xQueueSetTasksStatus = pdFAIL;
710         }
711
712         /* The task that sends to the queues is not running yet, so attempting to
713         read from the queue set should fail. */
714         if( xQueueSelectFromSet( xQueueSet, queuesetSHORT_DELAY ) != NULL )
715         {
716                 xQueueSetTasksStatus = pdFAIL;
717         }
718
719         /* Resume the task that writes to the queues. */
720         vTaskResume( xQueueSetSendingTask );
721
722         /* Let the ISR access the queues also. */
723         xSetupComplete = pdTRUE;
724 }
725 /*-----------------------------------------------------------*/
726
727 static size_t prvRand( void )
728 {
729         uxNextRand = ( uxNextRand * ( size_t ) 1103515245 ) + ( size_t ) 12345;
730         return ( uxNextRand / ( size_t ) 65536 ) % ( size_t ) 32768;
731 }
732 /*-----------------------------------------------------------*/
733
734 static void prvSRand( size_t uxSeed )
735 {
736         uxNextRand = uxSeed;
737 }
738