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