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