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