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