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