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