]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueSet.c
Roll up the minor changes checked into svn since V10.0.0 into new V10.0.1 ready for...
[freertos] / FreeRTOS / Demo / Common / Minimal / QueueSet.c
1 /*\r
2  * FreeRTOS Kernel V10.0.1\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.\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 static void prvChangeRelativePriorities( void );\r
143 \r
144 /*\r
145  * Local pseudo random number seed and return functions.  Used to avoid calls\r
146  * to the standard library.\r
147  */\r
148 static size_t prvRand( void );\r
149 static void prvSRand( size_t uxSeed );\r
150 \r
151 /*-----------------------------------------------------------*/\r
152 \r
153 /* The queues that are added to the set. */\r
154 static QueueHandle_t xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };\r
155 \r
156 /* Counts how many times each queue in the set is used to ensure all the\r
157 queues are used. */\r
158 static uint32_t ulQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };\r
159 \r
160 /* The handle of the queue set to which the queues are added. */\r
161 static QueueSetHandle_t xQueueSet;\r
162 \r
163 /* If the prvQueueSetReceivingTask() task has not detected any errors then\r
164 it increments ulCycleCounter on each iteration.\r
165 xAreQueueSetTasksStillRunning() returns pdPASS if the value of\r
166 ulCycleCounter has changed between consecutive calls, and pdFALSE if\r
167 ulCycleCounter has stopped incrementing (indicating an error condition). */\r
168 static volatile uint32_t ulCycleCounter = 0UL;\r
169 \r
170 /* Set to pdFAIL if an error is detected by any queue set task.\r
171 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */\r
172 static volatile BaseType_t xQueueSetTasksStatus = pdPASS;\r
173 \r
174 /* Just a flag to let the function that writes to a queue from an ISR know that\r
175 the queues are setup and can be used. */\r
176 static volatile BaseType_t xSetupComplete = pdFALSE;\r
177 \r
178 /* The value sent to the queue from the ISR is file scope so the\r
179 xAreQueeuSetTasksStillRunning() function can check it is incrementing as\r
180 expected. */\r
181 static volatile uint32_t ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;\r
182 \r
183 /* Used by the pseudo random number generator. */\r
184 static size_t uxNextRand = 0;\r
185 \r
186 /* The task handles are stored so their priorities can be changed. */\r
187 TaskHandle_t xQueueSetSendingTask, xQueueSetReceivingTask;\r
188 \r
189 /*-----------------------------------------------------------*/\r
190 \r
191 void vStartQueueSetTasks( void )\r
192 {\r
193         /* Create the tasks. */\r
194         xTaskCreate( prvQueueSetSendingTask, "SetTx", configMINIMAL_STACK_SIZE, NULL, queuesetMEDIUM_PRIORITY, &xQueueSetSendingTask );\r
195 \r
196         if( xQueueSetSendingTask != NULL )\r
197         {\r
198                 xTaskCreate( prvQueueSetReceivingTask, "SetRx", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, queuesetMEDIUM_PRIORITY, &xQueueSetReceivingTask );\r
199 \r
200                 /* It is important that the sending task does not attempt to write to a\r
201                 queue before the queue has been created.  It is therefore placed into\r
202                 the suspended state before the scheduler has started.  It is resumed by\r
203                 the receiving task after the receiving task has created the queues and\r
204                 added the queues to the queue set. */\r
205                 vTaskSuspend( xQueueSetSendingTask );\r
206         }\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 BaseType_t xAreQueueSetTasksStillRunning( void )\r
211 {\r
212 static uint32_t ulLastCycleCounter, ulLastISRTxValue = 0;\r
213 static uint32_t ulLastQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };\r
214 BaseType_t xReturn = pdPASS, x;\r
215 \r
216         if( ulLastCycleCounter == ulCycleCounter )\r
217         {\r
218                 /* The cycle counter is no longer being incremented.  Either one of the\r
219                 tasks is stalled or an error has been detected. */\r
220                 xReturn = pdFAIL;\r
221         }\r
222 \r
223         ulLastCycleCounter = ulCycleCounter;\r
224 \r
225         /* Ensure that all the queues in the set have been used.  This ensures the\r
226         test is working as intended and guards against the rand() in the Tx task\r
227         missing some values. */\r
228         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )\r
229         {\r
230                 if( ulLastQueueUsedCounter[ x ] == ulQueueUsedCounter[ x ] )\r
231                 {\r
232                         xReturn = pdFAIL;\r
233                 }\r
234 \r
235                 ulLastQueueUsedCounter[ x ] = ulQueueUsedCounter[ x ];\r
236         }\r
237 \r
238         /* Check the global status flag. */\r
239         if( xQueueSetTasksStatus != pdPASS )\r
240         {\r
241                 xReturn = pdFAIL;\r
242         }\r
243 \r
244         /* Check that the ISR is still sending values to the queues too. */\r
245         if( ulISRTxValue == ulLastISRTxValue )\r
246         {\r
247                 xReturn = pdFAIL;\r
248         }\r
249         else\r
250         {\r
251                 ulLastISRTxValue = ulISRTxValue;\r
252         }\r
253 \r
254         return xReturn;\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void prvQueueSetSendingTask( void *pvParameters )\r
259 {\r
260 uint32_t ulTaskTxValue = 0;\r
261 size_t uxQueueToWriteTo;\r
262 QueueHandle_t xQueueInUse;\r
263 \r
264         /* Remove compiler warning about the unused parameter. */\r
265         ( void ) pvParameters;\r
266 \r
267         /* Seed mini pseudo random number generator. */\r
268         prvSRand( ( size_t ) &ulTaskTxValue );\r
269 \r
270         for( ;; )\r
271         {\r
272                 /* Generate the index for the queue to which a value is to be sent. */\r
273                 uxQueueToWriteTo = prvRand() % queuesetNUM_QUEUES_IN_SET;\r
274                 xQueueInUse = xQueues[ uxQueueToWriteTo ];\r
275 \r
276                 /* Note which index is being written to to ensure all the queues are\r
277                 used. */\r
278                 ( ulQueueUsedCounter[ uxQueueToWriteTo ] )++;\r
279 \r
280                 /* Send to the queue to unblock the task that is waiting for data to\r
281                 arrive on a queue within the queue set to which this queue belongs. */\r
282                 if( xQueueSendToBack( xQueueInUse, &ulTaskTxValue, portMAX_DELAY ) != pdPASS )\r
283                 {\r
284                         /* The send should always pass as an infinite block time was\r
285                         used. */\r
286                         xQueueSetTasksStatus = pdFAIL;\r
287                 }\r
288 \r
289                 #if( configUSE_PREEMPTION == 0 )\r
290                         taskYIELD();\r
291                 #endif\r
292 \r
293                 ulTaskTxValue++;\r
294 \r
295                 /* If the Tx value has reached the range used by the ISR then set it\r
296                 back to 0. */\r
297                 if( ulTaskTxValue == queuesetINITIAL_ISR_TX_VALUE )\r
298                 {\r
299                         ulTaskTxValue = 0;\r
300                 }\r
301 \r
302                 /* Increase test coverage by occasionally change the priorities of the\r
303                 two tasks relative to each other. */\r
304                 prvChangeRelativePriorities();\r
305         }\r
306 }\r
307 /*-----------------------------------------------------------*/\r
308 \r
309 static void prvChangeRelativePriorities( void )\r
310 {\r
311 static UBaseType_t ulLoops = 0;\r
312 static eRelativePriorities ePriorities = eEqualPriority;\r
313 \r
314         /* Occasionally change the task priority relative to the priority of\r
315         the receiving task. */\r
316         ulLoops++;\r
317         if( ulLoops >= queuesetPRIORITY_CHANGE_LOOPS )\r
318         {\r
319                 ulLoops = 0;\r
320 \r
321                 switch( ePriorities )\r
322                 {\r
323                         case eEqualPriority:\r
324                                 /* Both tasks are running with medium priority.  Now lower the\r
325                                 priority of the receiving task so the Tx task has the higher\r
326                                 relative priority. */\r
327                                 vTaskPrioritySet( xQueueSetReceivingTask, queuesetLOW_PRIORITY );\r
328                                 ePriorities = eTxHigherPriority;\r
329                                 break;\r
330 \r
331                         case eTxHigherPriority:\r
332                                 /* The Tx task is running with a higher priority than the Rx\r
333                                 task.  Switch the priorities around so the Rx task has the\r
334                                 higher relative priority. */\r
335                                 vTaskPrioritySet( xQueueSetReceivingTask, queuesetMEDIUM_PRIORITY );\r
336                                 vTaskPrioritySet( xQueueSetSendingTask, queuesetLOW_PRIORITY );\r
337                                 ePriorities = eTxLowerPriority;\r
338                                 break;\r
339 \r
340                         case eTxLowerPriority:\r
341                                 /* The Tx task is running with a lower priority than the Rx\r
342                                 task.  Make the priorities equal again. */\r
343                                 vTaskPrioritySet( xQueueSetSendingTask, queuesetMEDIUM_PRIORITY );\r
344                                 ePriorities = eEqualPriority;\r
345 \r
346                                 /* When both tasks are using a non-idle priority the queue set\r
347                                 tasks will starve idle priority tasks of execution time - so\r
348                                 relax a bit before the next iteration to minimise the impact. */\r
349                                 vTaskDelay( queuesetTX_LOOP_DELAY );\r
350 \r
351                                 break;\r
352                 }\r
353         }\r
354 }\r
355 /*-----------------------------------------------------------*/\r
356 \r
357 static void prvQueueSetReceivingTask( void *pvParameters )\r
358 {\r
359 uint32_t ulReceived;\r
360 QueueHandle_t xActivatedQueue;\r
361 TickType_t xBlockTime;\r
362 \r
363         /* Remove compiler warnings. */\r
364         ( void ) pvParameters;\r
365 \r
366         /* Create the queues and add them to the queue set before resuming the Tx\r
367         task. */\r
368         prvSetupTest();\r
369 \r
370         for( ;; )\r
371         {\r
372                 /* For test coverage reasons, the block time is dependent on the\r
373                 priority of this task - which changes during the test.  When the task\r
374                 is at the idle priority it polls the queue set. */\r
375                 if( uxTaskPriorityGet( NULL ) == tskIDLE_PRIORITY )\r
376                 {\r
377                         xBlockTime = 0;\r
378                 }\r
379                 else\r
380                 {\r
381                         xBlockTime = portMAX_DELAY;\r
382                 }\r
383 \r
384                 /* Wait for a message to arrive on one of the queues in the set. */\r
385                 xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );\r
386 \r
387                 if( xActivatedQueue == NULL )\r
388                 {\r
389                         if( xBlockTime != 0 )\r
390                         {\r
391                                 /* This should not happen as an infinite delay was used. */\r
392                                 xQueueSetTasksStatus = pdFAIL;\r
393                         }\r
394                 }\r
395                 else\r
396                 {\r
397                         /* Reading from the queue should pass with a zero block time as\r
398                         this task will only run when something has been posted to a task\r
399                         in the queue set. */\r
400                         if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )\r
401                         {\r
402                                 xQueueSetTasksStatus = pdFAIL;\r
403                         }\r
404 \r
405                         /* Ensure the value received was the value expected.  This function\r
406                         manipulates file scope data and is also called from an ISR, hence\r
407                         the critical section. */\r
408                         taskENTER_CRITICAL();\r
409                         {\r
410                                 prvCheckReceivedValue( ulReceived );\r
411                         }\r
412                         taskEXIT_CRITICAL();\r
413 \r
414                         if( xQueueSetTasksStatus == pdPASS )\r
415                         {\r
416                                 ulCycleCounter++;\r
417                         }\r
418                 }\r
419         }\r
420 }\r
421 /*-----------------------------------------------------------*/\r
422 \r
423 void vQueueSetAccessQueueSetFromISR( void )\r
424 {\r
425 static uint32_t ulCallCount = 0;\r
426 \r
427         /* xSetupComplete is set to pdTRUE when the queues have been created and\r
428         are available for use. */\r
429         if( xSetupComplete == pdTRUE )\r
430         {\r
431                 /* It is intended that this function is called from the tick hook\r
432                 function, so each call is one tick period apart. */\r
433                 ulCallCount++;\r
434                 if( ulCallCount > queuesetISR_TX_PERIOD )\r
435                 {\r
436                         ulCallCount = 0;\r
437 \r
438                         /* First attempt to read from the queue set. */\r
439                         prvReceiveFromQueueInSetFromISR();\r
440 \r
441                         /* Then write to the queue set. */\r
442                         prvSendToQueueInSetFromISR();\r
443                 }\r
444         }\r
445 }\r
446 /*-----------------------------------------------------------*/\r
447 \r
448 static void prvCheckReceivedValue( uint32_t ulReceived )\r
449 {\r
450 static uint32_t ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;\r
451 \r
452         /* Values are received in tasks and interrupts.  It is likely that the\r
453         receiving task will sometimes get preempted by the receiving interrupt\r
454         between reading a value from the queue and calling this function.  When\r
455         that happens, if the receiving interrupt calls this function the values\r
456         will get passed into this function slightly out of order.  For that\r
457         reason the value passed in is tested against a small range of expected\r
458         values, rather than a single absolute value.  To make the range testing\r
459         easier values in the range limits are ignored. */\r
460 \r
461         /* If the received value is equal to or greater than\r
462         queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */\r
463         if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )\r
464         {\r
465                 /* The value was sent from the ISR. */\r
466                 if( ( ulReceived - queuesetINITIAL_ISR_TX_VALUE ) < queuesetIGNORED_BOUNDARY )\r
467                 {\r
468                         /* The value received is at the lower limit of the expected range.\r
469                         Don't test it and expect to receive one higher next time. */\r
470                 }\r
471                 else if( ( ULONG_MAX - ulReceived ) <= queuesetIGNORED_BOUNDARY )\r
472                 {\r
473                         /* The value received is at the higher limit of the expected range.\r
474                         Don't test it and expect to wrap soon. */\r
475                 }\r
476                 else\r
477                 {\r
478                         /* Check the value against its expected value range. */\r
479                         if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromISR ) != pdPASS )\r
480                         {\r
481                                 xQueueSetTasksStatus = pdFAIL;\r
482                         }\r
483                 }\r
484 \r
485                 configASSERT( xQueueSetTasksStatus );\r
486 \r
487                 /* It is expected to receive an incrementing number. */\r
488                 ulExpectedReceivedFromISR++;\r
489                 if( ulExpectedReceivedFromISR == 0 )\r
490                 {\r
491                         ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;\r
492                 }\r
493         }\r
494         else\r
495         {\r
496                 /* The value was sent from the Tx task. */\r
497                 if( ulReceived < queuesetIGNORED_BOUNDARY )\r
498                 {\r
499                         /* The value received is at the lower limit of the expected range.\r
500                         Don't test it, and expect to receive one higher next time. */\r
501                 }\r
502                 else if( ( ( queuesetINITIAL_ISR_TX_VALUE - 1 ) - ulReceived ) <= queuesetIGNORED_BOUNDARY )\r
503                 {\r
504                         /* The value received is at the higher limit of the expected range.\r
505                         Don't test it and expect to wrap soon. */\r
506                 }\r
507                 else\r
508                 {\r
509                         /* Check the value against its expected value range. */\r
510                         if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromTask ) != pdPASS )\r
511                         {\r
512                                 xQueueSetTasksStatus = pdFAIL;\r
513                         }\r
514                 }\r
515 \r
516                 configASSERT( xQueueSetTasksStatus );\r
517 \r
518                 /* It is expected to receive an incrementing number. */\r
519                 ulExpectedReceivedFromTask++;\r
520                 if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )\r
521                 {\r
522                         ulExpectedReceivedFromTask = 0;\r
523                 }\r
524         }\r
525 }\r
526 /*-----------------------------------------------------------*/\r
527 \r
528 static BaseType_t prvCheckReceivedValueWithinExpectedRange( uint32_t ulReceived, uint32_t ulExpectedReceived )\r
529 {\r
530 BaseType_t xReturn = pdPASS;\r
531 \r
532         if( ulReceived > ulExpectedReceived )\r
533         {\r
534                 configASSERT( ( ulReceived - ulExpectedReceived ) <= queuesetALLOWABLE_RX_DEVIATION );\r
535                 if( ( ulReceived - ulExpectedReceived ) > queuesetALLOWABLE_RX_DEVIATION )\r
536                 {\r
537                         xReturn = pdFALSE;\r
538                 }\r
539         }\r
540         else\r
541         {\r
542                 configASSERT( ( ulExpectedReceived - ulReceived ) <= queuesetALLOWABLE_RX_DEVIATION );\r
543                 if( ( ulExpectedReceived - ulReceived ) > queuesetALLOWABLE_RX_DEVIATION )\r
544                 {\r
545                         xReturn = pdFALSE;\r
546                 }\r
547         }\r
548 \r
549         return xReturn;\r
550 }\r
551 /*-----------------------------------------------------------*/\r
552 \r
553 static void prvReceiveFromQueueInSetFromISR( void )\r
554 {\r
555 QueueSetMemberHandle_t xActivatedQueue;\r
556 uint32_t ulReceived;\r
557 \r
558         /* See if any of the queues in the set contain data. */\r
559         xActivatedQueue = xQueueSelectFromSetFromISR( xQueueSet );\r
560 \r
561         if( xActivatedQueue != NULL )\r
562         {\r
563                 /* Reading from the queue for test purposes only. */\r
564                 if( xQueueReceiveFromISR( xActivatedQueue, &ulReceived, NULL ) != pdPASS )\r
565                 {\r
566                         /* Data should have been available as the handle was returned from\r
567                         xQueueSelectFromSetFromISR(). */\r
568                         xQueueSetTasksStatus = pdFAIL;\r
569                 }\r
570 \r
571                 /* Ensure the value received was the value expected. */\r
572                 prvCheckReceivedValue( ulReceived );\r
573         }\r
574 }\r
575 /*-----------------------------------------------------------*/\r
576 \r
577 static void prvSendToQueueInSetFromISR( void )\r
578 {\r
579 static BaseType_t xQueueToWriteTo = 0;\r
580 uint32_t ulTxValueSnapshot = ulISRTxValue;\r
581 \r
582         if( xQueueSendFromISR( xQueues[ xQueueToWriteTo ], ( void * ) &ulTxValueSnapshot, NULL ) == pdPASS )\r
583         {\r
584                 ulISRTxValue++;\r
585 \r
586                 /* If the Tx value has wrapped then set it back to its initial value. */\r
587                 if( ulISRTxValue == 0UL )\r
588                 {\r
589                         ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;\r
590                 }\r
591 \r
592                 /* Use a different queue next time. */\r
593                 xQueueToWriteTo++;\r
594                 if( xQueueToWriteTo >= queuesetNUM_QUEUES_IN_SET )\r
595                 {\r
596                         xQueueToWriteTo = 0;\r
597                 }\r
598         }\r
599 }\r
600 /*-----------------------------------------------------------*/\r
601 \r
602 static void prvSetupTest( void )\r
603 {\r
604 BaseType_t x;\r
605 uint32_t ulValueToSend = 0;\r
606 \r
607         /* Ensure the queues are created and the queue set configured before the\r
608         sending task is unsuspended.\r
609 \r
610         First Create the queue set such that it will be able to hold a message for\r
611         every space in every queue in the set. */\r
612         xQueueSet = xQueueCreateSet( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );\r
613 \r
614         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )\r
615         {\r
616                 /* Create the queue and add it to the set.  The queue is just holding\r
617                 uint32_t value. */\r
618                 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( uint32_t ) );\r
619                 configASSERT( xQueues[ x ] );\r
620                 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdPASS )\r
621                 {\r
622                         xQueueSetTasksStatus = pdFAIL;\r
623                 }\r
624                 else\r
625                 {\r
626                         /* The queue has now been added to the queue set and cannot be added to\r
627                         another. */\r
628                         if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdFAIL )\r
629                         {\r
630                                 xQueueSetTasksStatus = pdFAIL;\r
631                         }\r
632                 }\r
633         }\r
634 \r
635         /* Attempt to remove a queue from a queue set it does not belong\r
636         to (NULL being passed as the queue set in this case). */\r
637         if( xQueueRemoveFromSet( xQueues[ 0 ], NULL ) != pdFAIL )\r
638         {\r
639                 /* It is not possible to successfully remove a queue from a queue\r
640                 set it does not belong to. */\r
641                 xQueueSetTasksStatus = pdFAIL;\r
642         }\r
643 \r
644         /* Attempt to remove a queue from the queue set it does belong to. */\r
645         if( xQueueRemoveFromSet( xQueues[ 0 ], xQueueSet ) != pdPASS )\r
646         {\r
647                 /* It should be possible to remove the queue from the queue set it\r
648                 does belong to. */\r
649                 xQueueSetTasksStatus = pdFAIL;\r
650         }\r
651 \r
652         /* Add an item to the queue before attempting to add it back into the\r
653         set. */\r
654         xQueueSend( xQueues[ 0 ], ( void * ) &ulValueToSend, 0 );\r
655         if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdFAIL )\r
656         {\r
657                 /* Should not be able to add a non-empty queue to a set. */\r
658                 xQueueSetTasksStatus = pdFAIL;\r
659         }\r
660 \r
661         /* Remove the item from the queue before adding the queue back into the\r
662         set so the dynamic tests can begin. */\r
663         xQueueReceive( xQueues[ 0 ], &ulValueToSend, 0 );\r
664         if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdPASS )\r
665         {\r
666                 /* If the queue was successfully removed from the queue set then it\r
667                 should be possible to add it back in again. */\r
668                 xQueueSetTasksStatus = pdFAIL;\r
669         }\r
670 \r
671         /* The task that sends to the queues is not running yet, so attempting to\r
672         read from the queue set should fail. */\r
673         if( xQueueSelectFromSet( xQueueSet, queuesetSHORT_DELAY ) != NULL )\r
674         {\r
675                 xQueueSetTasksStatus = pdFAIL;\r
676         }\r
677 \r
678         /* Resume the task that writes to the queues. */\r
679         vTaskResume( xQueueSetSendingTask );\r
680 \r
681         /* Let the ISR access the queues also. */\r
682         xSetupComplete = pdTRUE;\r
683 }\r
684 /*-----------------------------------------------------------*/\r
685 \r
686 static size_t prvRand( void )\r
687 {\r
688         uxNextRand = ( uxNextRand * ( size_t ) 1103515245 ) + ( size_t ) 12345;\r
689         return ( uxNextRand / ( size_t ) 65536 ) % ( size_t ) 32768;\r
690 }\r
691 /*-----------------------------------------------------------*/\r
692 \r
693 static void prvSRand( size_t uxSeed )\r
694 {\r
695         uxNextRand = uxSeed;\r
696 }\r
697 \r