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