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