]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueSet.c
Clear up a few compiler warnings.
[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         /* Remove compiler warnings. */\r
391         ( void ) pvParameters;\r
392 \r
393         /* Create the queues and add them to the queue set before resuming the Tx\r
394         task. */\r
395         prvSetupTest();\r
396 \r
397         for( ;; )\r
398         {\r
399                 /* Wait for a message to arrive on one of the queues in the set. */\r
400                 xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );\r
401                 configASSERT( xActivatedQueue );\r
402 \r
403                 if( xActivatedQueue == NULL )\r
404                 {\r
405                         /* This should not happen as an infinite delay was used. */\r
406                         xQueueSetTasksStatus = pdFAIL;\r
407                 }\r
408                 else\r
409                 {\r
410                         /* Reading from the queue should pass with a zero block time as\r
411                         this task will only run when something has been posted to a task\r
412                         in the queue set. */\r
413                         if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )\r
414                         {\r
415                                 xQueueSetTasksStatus = pdFAIL;\r
416                         }\r
417 \r
418                         /* Ensure the value received was the value expected.  This function\r
419                         manipulates file scope data and is also called from an ISR, hence\r
420                         the critical section. */\r
421                         taskENTER_CRITICAL();\r
422                         {\r
423                                 prvCheckReceivedValue( ulReceived );\r
424                         }\r
425                         taskEXIT_CRITICAL();\r
426                 }\r
427 \r
428                 if( xQueueSetTasksStatus == pdPASS )\r
429                 {\r
430                         ulCycleCounter++;\r
431                 }\r
432         }\r
433 }\r
434 /*-----------------------------------------------------------*/\r
435 \r
436 void vQueueSetAccessQueueSetFromISR( void )\r
437 {\r
438 static unsigned long ulCallCount = 0;\r
439 \r
440         /* xSetupComplete is set to pdTRUE when the queues have been created and\r
441         are available for use. */\r
442         if( xSetupComplete == pdTRUE )\r
443         {\r
444                 /* It is intended that this function is called from the tick hook\r
445                 function, so each call is one tick period apart. */\r
446                 ulCallCount++;\r
447                 if( ulCallCount > queuesetISR_TX_PERIOD )\r
448                 {\r
449                         ulCallCount = 0;\r
450 \r
451                         /* First attempt to read from the queue set. */\r
452                         prvReceiveFromQueueInSetFromISR();\r
453 \r
454                         /* Then write to the queue set. */\r
455                         prvSendToQueueInSetFromISR();\r
456                 }\r
457         }\r
458 }\r
459 /*-----------------------------------------------------------*/\r
460 \r
461 static void prvCheckReceivedValue( unsigned long ulReceived )\r
462 {\r
463 static unsigned long ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;\r
464 \r
465         /* Values are received in tasks and interrupts.  It is likely that the\r
466         receiving task will sometimes get preempted by the receiving interrupt\r
467         between reading a value from the queue and calling this function.  When\r
468         that happens, if the receiving interrupt calls this function the values\r
469         will get passed into this function slightly out of order.  For that\r
470         reason the value passed in is tested against a small range of expected\r
471         values, rather than a single absolute value.  To make the range testing\r
472         easier values in the range limits are ignored. */\r
473 \r
474         /* If the received value is equal to or greater than\r
475         queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */\r
476         if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )\r
477         {\r
478                 /* The value was sent from the ISR. */\r
479                 if( ( ulReceived - queuesetINITIAL_ISR_TX_VALUE ) < queuesetIGNORED_BOUNDARY )\r
480                 {\r
481                         /* The value received is at the lower limit of the expected range.\r
482                         Don't test it and expect to receive one higher next time. */\r
483                 }\r
484                 else if( ( ULONG_MAX - ulReceived ) <= queuesetIGNORED_BOUNDARY )\r
485                 {\r
486                         /* The value received is at the higher limit of the expected range.\r
487                         Don't test it and expect to wrap soon. */\r
488                 }\r
489                 else\r
490                 {\r
491                         /* Check the value against its expected value range. */\r
492                         if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromISR ) != pdPASS )\r
493                         {\r
494                                 xQueueSetTasksStatus = pdFAIL;\r
495                         }\r
496                 }\r
497 \r
498                 configASSERT( xQueueSetTasksStatus );\r
499 \r
500                 /* It is expected to receive an incrementing number. */\r
501                 ulExpectedReceivedFromISR++;\r
502                 if( ulExpectedReceivedFromISR == 0 )\r
503                 {\r
504                         ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;\r
505                 }\r
506         }\r
507         else\r
508         {\r
509                 /* The value was sent from the Tx task. */\r
510                 if( ulReceived < queuesetIGNORED_BOUNDARY )\r
511                 {\r
512                         /* The value received is at the lower limit of the expected range.\r
513                         Don't test it, and expect to receive one higher next time. */\r
514                 }\r
515                 else if( ( ( queuesetINITIAL_ISR_TX_VALUE - 1 ) - ulReceived ) <= queuesetIGNORED_BOUNDARY )\r
516                 {\r
517                         /* The value received is at the higher limit of the expected range.\r
518                         Don't test it and expect to wrap soon. */\r
519                 }\r
520                 else\r
521                 {\r
522                         /* Check the value against its expected value range. */\r
523                         if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromTask ) != pdPASS )\r
524                         {\r
525                                 xQueueSetTasksStatus = pdFAIL;\r
526                         }\r
527                 }\r
528 \r
529                 configASSERT( xQueueSetTasksStatus );\r
530 \r
531                 /* It is expected to receive an incrementing number. */\r
532                 ulExpectedReceivedFromTask++;\r
533                 if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )\r
534                 {\r
535                         ulExpectedReceivedFromTask = 0;\r
536                 }\r
537         }\r
538 }\r
539 /*-----------------------------------------------------------*/\r
540 \r
541 static portBASE_TYPE prvCheckReceivedValueWithinExpectedRange( unsigned long ulReceived, unsigned long ulExpectedReceived )\r
542 {\r
543 portBASE_TYPE xReturn = pdPASS;\r
544 \r
545         if( ulReceived > ulExpectedReceived )\r
546         {\r
547                 configASSERT( ( ulReceived - ulExpectedReceived ) <= queuesetALLOWABLE_RX_DEVIATION );\r
548                 if( ( ulReceived - ulExpectedReceived ) > queuesetALLOWABLE_RX_DEVIATION )\r
549                 {\r
550                         xReturn = pdFALSE;\r
551                 }\r
552         }\r
553         else\r
554         {\r
555                 configASSERT( ( ulExpectedReceived - ulReceived ) <= queuesetALLOWABLE_RX_DEVIATION );\r
556                 if( ( ulExpectedReceived - ulReceived ) > queuesetALLOWABLE_RX_DEVIATION )\r
557                 {\r
558                         xReturn = pdFALSE;\r
559                 }\r
560         }\r
561 \r
562         return xReturn;\r
563 }\r
564 /*-----------------------------------------------------------*/\r
565 \r
566 static void prvReceiveFromQueueInSetFromISR( void )\r
567 {\r
568 xQueueSetMemberHandle xActivatedQueue;\r
569 unsigned long ulReceived;\r
570 \r
571         /* See if any of the queues in the set contain data. */\r
572         xActivatedQueue = xQueueSelectFromSetFromISR( xQueueSet );\r
573 \r
574         if( xActivatedQueue != NULL )\r
575         {\r
576                 /* Reading from the queue for test purposes only. */\r
577                 if( xQueueReceiveFromISR( xActivatedQueue, &ulReceived, NULL ) != pdPASS )\r
578                 {\r
579                         /* Data should have been available as the handle was returned from\r
580                         xQueueSelectFromSetFromISR(). */\r
581                         xQueueSetTasksStatus = pdFAIL;\r
582                 }\r
583 \r
584                 /* Ensure the value received was the value expected. */\r
585                 prvCheckReceivedValue( ulReceived );\r
586         }\r
587 }\r
588 /*-----------------------------------------------------------*/\r
589 \r
590 static void prvSendToQueueInSetFromISR( void )\r
591 {\r
592 static portBASE_TYPE xQueueToWriteTo = 0;\r
593 \r
594         if( xQueueSendFromISR( xQueues[ xQueueToWriteTo ], ( void * ) &ulISRTxValue, NULL ) == pdPASS )\r
595         {\r
596                 ulISRTxValue++;\r
597 \r
598                 /* If the Tx value has wrapped then set it back to its\r
599                 initial value. */\r
600                 if( ulISRTxValue == 0UL )\r
601                 {\r
602                         ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;\r
603                 }\r
604 \r
605                 /* Use a different queue next time. */\r
606                 xQueueToWriteTo++;\r
607                 if( xQueueToWriteTo >= queuesetNUM_QUEUES_IN_SET )\r
608                 {\r
609                         xQueueToWriteTo = 0;\r
610                 }\r
611         }\r
612 }\r
613 /*-----------------------------------------------------------*/\r
614 \r
615 static void prvSetupTest( void )\r
616 {\r
617 portBASE_TYPE x;\r
618 unsigned long ulValueToSend = 0;\r
619 \r
620         /* Ensure the queues are created and the queue set configured before the\r
621         sending task is unsuspended.\r
622 \r
623         First Create the queue set such that it will be able to hold a message for\r
624         every space in every queue in the set. */\r
625         xQueueSet = xQueueCreateSet( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );\r
626 \r
627         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )\r
628         {\r
629                 /* Create the queue and add it to the set.  The queue is just holding\r
630                 unsigned long value. */\r
631                 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( unsigned long ) );\r
632                 configASSERT( xQueues[ x ] );\r
633                 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdPASS )\r
634                 {\r
635                         xQueueSetTasksStatus = pdFAIL;\r
636                 }\r
637                 else\r
638                 {\r
639                         /* The queue has now been added to the queue set and cannot be added to\r
640                         another. */\r
641                         if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdFAIL )\r
642                         {\r
643                                 xQueueSetTasksStatus = pdFAIL;\r
644                         }\r
645                 }\r
646         }\r
647 \r
648         /* Attempt to remove a queue from a queue set it does not belong\r
649         to (NULL being passed as the queue set in this case). */\r
650         if( xQueueRemoveFromSet( xQueues[ 0 ], NULL ) != pdFAIL )\r
651         {\r
652                 /* It is not possible to successfully remove a queue from a queue\r
653                 set it does not belong to. */\r
654                 xQueueSetTasksStatus = pdFAIL;\r
655         }\r
656 \r
657         /* Attempt to remove a queue from the queue set it does belong to. */\r
658         if( xQueueRemoveFromSet( xQueues[ 0 ], xQueueSet ) != pdPASS )\r
659         {\r
660                 /* It should be possible to remove the queue from the queue set it\r
661                 does belong to. */\r
662                 xQueueSetTasksStatus = pdFAIL;\r
663         }\r
664 \r
665         /* Add an item to the queue before attempting to add it back into the\r
666         set. */\r
667         xQueueSend( xQueues[ 0 ], ( void * ) &ulValueToSend, 0 );\r
668         if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdFAIL )\r
669         {\r
670                 /* Should not be able to add a non-empty queue to a set. */\r
671                 xQueueSetTasksStatus = pdFAIL;\r
672         }\r
673 \r
674         /* Remove the item from the queue before adding the queue back into the\r
675         set so the dynamic tests can begin. */\r
676         xQueueReceive( xQueues[ 0 ], &ulValueToSend, 0 );\r
677         if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdPASS )\r
678         {\r
679                 /* If the queue was successfully removed from the queue set then it\r
680                 should be possible to add it back in again. */\r
681                 xQueueSetTasksStatus = pdFAIL;\r
682         }\r
683 \r
684         /* The task that sends to the queues is not running yet, so attempting to\r
685         read from the queue set should fail. */\r
686         if( xQueueSelectFromSet( xQueueSet, queuesetSHORT_DELAY ) != NULL )\r
687         {\r
688                 xQueueSetTasksStatus = pdFAIL;\r
689         }\r
690 \r
691         /* Resume the task that writes to the queues. */\r
692         vTaskResume( xQueueSetSendingTask );\r
693 \r
694         /* Let the ISR access the queues also. */\r
695         xSetupComplete = pdTRUE;\r
696 }\r
697 /*-----------------------------------------------------------*/\r
698 \r
699 static unsigned long prvRand( void )\r
700 {\r
701         ulNextRand = ( ulNextRand * 1103515245UL ) + 12345UL;\r
702     return ( ulNextRand / 65536UL ) % 32768UL;\r
703 }\r
704 /*-----------------------------------------------------------*/\r
705 \r
706 static void prvSRand( unsigned long ulSeed )\r
707 {\r
708     ulNextRand = ulSeed;\r
709 }\r
710 \r