2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Tests the use of queue sets.
\r
68 * A receive task creates a number of queues and adds them to a queue set before
\r
69 * blocking on the queue set receive. A transmit task and (optionally) an
\r
70 * interrupt repeatedly unblocks the receive task by sending messages to the
\r
71 * queues in a pseudo random order. The receive task removes the messages from
\r
72 * the queues and flags an error if the received message does not match that
\r
73 * expected. The task sends values in the range 0 to
\r
74 * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range
\r
75 * queuesetINITIAL_ISR_TX_VALUE to ULONG_MAX.
\r
78 /* Standard includes. */
\r
82 /* Kernel includes. */
\r
83 #include "FreeRTOS.h"
\r
87 /* Demo includes. */
\r
88 #include "QueueSet.h"
\r
90 /* The number of queues that are created and added to the queue set. */
\r
91 #define queuesetNUM_QUEUES_IN_SET 3
\r
93 /* The length of each created queue. */
\r
94 #define queuesetQUEUE_LENGTH 3
\r
96 /* Block times used in this demo. A block time or 0 means "don't block". */
\r
97 #define queuesetSHORT_DELAY 200
\r
98 #define queuesetDONT_BLOCK 0
\r
100 /* Messages are sent in incrementing order from both a task and an interrupt.
\r
101 The task sends values in the range 0 to 0xfffe, and the interrupt sends values
\r
102 in the range of 0xffff to ULONG_MAX. */
\r
103 #define queuesetINITIAL_ISR_TX_VALUE 0xffffUL
\r
105 /* The priorities used in this demo. */
\r
106 #define queuesetLOW_PRIORITY ( tskIDLE_PRIORITY )
\r
107 #define queuesetMEDIUM_PRIORITY ( queuesetLOW_PRIORITY + 1 )
\r
108 #define queuesetHIGH_PRIORITY ( queuesetMEDIUM_PRIORITY + 1 )
\r
110 /* For test purposes the priority of the sending task is changed after every
\r
111 queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */
\r
112 #define queuesetPRIORITY_CHANGE_LOOPS 100UL
\r
114 /* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */
\r
115 #define queuesetISR_TX_PERIOD ( 100UL )
\r
117 /* The allowable maximum deviation between a received value and the expected
\r
118 received value. A deviation will occur when data is received from a queue
\r
119 inside an ISR in between a task receiving from a queue and the task checking
\r
120 the received value. */
\r
121 #define queuesetALLOWABLE_RX_DEVIATION 3
\r
123 /* Ignore values that are at the boundaries of allowable values to make the
\r
124 testing of limits easier (don't have to deal with wrapping values). */
\r
125 #define queuesetIGNORED_BOUNDARY ( queuesetALLOWABLE_RX_DEVIATION * 2 )
\r
128 * The task that periodically sends to the queue set.
\r
130 static void prvQueueSetSendingTask( void *pvParameters );
\r
133 * The task that reads from the queue set.
\r
135 static void prvQueueSetReceivingTask( void *pvParameters );
\r
138 * Check the value received from a queue is the expected value. Some values
\r
139 * originate from the send task, some values originate from the ISR, with the
\r
140 * range of the value being used to distinguish between the two message
\r
143 static void prvCheckReceivedValue( unsigned long ulReceived );
\r
146 * For purposes of test coverage, functions that read from and write to a
\r
147 * queue set from an ISR respectively.
\r
149 static void prvReceiveFromQueueInSetFromISR( void );
\r
150 static void prvSendToQueueInSetFromISR( void );
\r
153 * Create the queues and add them to a queue set before resuming the Tx
\r
156 static void prvSetupTest( xTaskHandle xQueueSetSendingTask );
\r
159 * Checks a value received from a queue falls within the range of expected
\r
162 static portBASE_TYPE prvCheckReceivedValueWithinExpectedRange( unsigned long ulReceived, unsigned long ulExpectedReceived );
\r
164 /*-----------------------------------------------------------*/
\r
166 /* The queues that are added to the set. */
\r
167 static xQueueHandle xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
169 /* Counts how many times each queue in the set is used to ensure all the
\r
170 queues are used. */
\r
171 static unsigned long ulQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
173 /* The handle of the queue set to which the queues are added. */
\r
174 static xQueueSetHandle xQueueSet;
\r
176 /* If the prvQueueSetReceivingTask() task has not detected any errors then
\r
177 it increments ulCycleCounter on each iteration.
\r
178 xAreQueueSetTasksStillRunning() returns pdPASS if the value of
\r
179 ulCycleCounter has changed between consecutive calls, and pdFALSE if
\r
180 ulCycleCounter has stopped incrementing (indicating an error condition). */
\r
181 static volatile unsigned long ulCycleCounter = 0UL;
\r
183 /* Set to pdFAIL if an error is detected by any queue set task.
\r
184 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
\r
185 static volatile portBASE_TYPE xQueueSetTasksStatus = pdPASS;
\r
187 /* Just a flag to let the function that writes to a queue from an ISR know that
\r
188 the queues are setup and can be used. */
\r
189 static volatile portBASE_TYPE xSetupComplete = pdFALSE;
\r
191 /* The value sent to the queue from the ISR is file scope so the
\r
192 xAreQueeuSetTasksStillRunning() function can check it is incrementing as
\r
194 static volatile unsigned long ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
196 /*-----------------------------------------------------------*/
\r
198 void vStartQueueSetTasks( void )
\r
200 xTaskHandle xQueueSetSendingTask;
\r
202 /* Create the two queues. The handle of the sending task is passed into
\r
203 the receiving task using the task parameter. The receiving task uses the
\r
204 handle to resume the sending task after it has created the queues. */
\r
205 xTaskCreate( prvQueueSetSendingTask, ( signed char * ) "SetTx", configMINIMAL_STACK_SIZE, NULL, queuesetMEDIUM_PRIORITY, &xQueueSetSendingTask );
\r
206 xTaskCreate( prvQueueSetReceivingTask, ( signed char * ) "SetRx", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, queuesetMEDIUM_PRIORITY, NULL );
\r
208 /* It is important that the sending task does not attempt to write to a
\r
209 queue before the queue has been created. It is therefore placed into the
\r
210 suspended state before the scheduler has started. It is resumed by the
\r
211 receiving task after the receiving task has created the queues and added the
\r
212 queues to the queue set. */
\r
213 vTaskSuspend( xQueueSetSendingTask );
\r
215 /*-----------------------------------------------------------*/
\r
217 portBASE_TYPE xAreQueueSetTasksStillRunning( void )
\r
219 static unsigned long ulLastCycleCounter, ulLastISRTxValue = 0;
\r
220 static unsigned long ulLastQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
221 portBASE_TYPE xReturn = pdPASS, x;
\r
223 if( ulLastCycleCounter == ulCycleCounter )
\r
225 /* The cycle counter is no longer being incremented. Either one of the
\r
226 tasks is stalled or an error has been detected. */
\r
230 ulLastCycleCounter = ulCycleCounter;
\r
232 /* Ensure that all the queues in the set have been used. This ensures the
\r
233 test is working as intended and guards against the rand() in the Tx task
\r
234 missing some values. */
\r
235 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
237 if( ulLastQueueUsedCounter[ x ] == ulQueueUsedCounter[ x ] )
\r
242 ulLastQueueUsedCounter[ x ] = ulQueueUsedCounter[ x ];
\r
245 /* Check the global status flag. */
\r
246 if( xQueueSetTasksStatus != pdPASS )
\r
251 /* Check that the ISR is still sending values to the queues too. */
\r
252 if( ulISRTxValue == ulLastISRTxValue )
\r
258 ulLastISRTxValue = ulISRTxValue;
\r
263 /*-----------------------------------------------------------*/
\r
265 static void prvQueueSetSendingTask( void *pvParameters )
\r
267 unsigned long ulTaskTxValue = 0;
\r
268 portBASE_TYPE xQueueToWriteTo;
\r
269 xQueueHandle xQueueInUse;
\r
270 unsigned portBASE_TYPE uxPriority = queuesetMEDIUM_PRIORITY, ulLoops = 0;
\r
272 /* Remove compiler warning about the unused parameter. */
\r
273 ( void ) pvParameters;
\r
275 srand( ( unsigned int ) &ulTaskTxValue );
\r
279 /* Generate the index for the queue to which a value is to be sent. */
\r
280 xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;
\r
281 xQueueInUse = xQueues[ xQueueToWriteTo ];
\r
283 /* Note which index is being written to to ensure all the queues are
\r
285 ( ulQueueUsedCounter[ xQueueToWriteTo ] )++;
\r
287 /* Send to the queue to unblock the task that is waiting for data to
\r
288 arrive on a queue within the queue set to which this queue belongs. */
\r
289 if( xQueueSendToBack( xQueueInUse, &ulTaskTxValue, portMAX_DELAY ) != pdPASS )
\r
291 /* The send should always pass as an infinite block time was
\r
293 xQueueSetTasksStatus = pdFAIL;
\r
298 /* If the Tx value has reached the range used by the ISR then set it
\r
300 if( ulTaskTxValue == queuesetINITIAL_ISR_TX_VALUE )
\r
305 /* Occasionally change the task priority relative to the priority of
\r
306 the receiving task. */
\r
308 if( ulLoops >= queuesetPRIORITY_CHANGE_LOOPS )
\r
312 if( uxPriority > queuesetHIGH_PRIORITY )
\r
314 uxPriority = queuesetLOW_PRIORITY;
\r
317 vTaskPrioritySet( NULL, uxPriority );
\r
321 /*-----------------------------------------------------------*/
\r
323 static void prvQueueSetReceivingTask( void *pvParameters )
\r
325 unsigned long ulReceived;
\r
326 xQueueHandle xActivatedQueue;
\r
327 xTaskHandle xQueueSetSendingTask;
\r
329 /* The handle to the sending task is passed in using the task parameter. */
\r
330 xQueueSetSendingTask = ( xTaskHandle ) pvParameters;
\r
332 /* Create the queues and add them to the queue set before resuming the Tx
\r
334 prvSetupTest( xQueueSetSendingTask );
\r
338 /* Wait for a message to arrive on one of the queues in the set. */
\r
339 xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );
\r
340 configASSERT( xActivatedQueue );
\r
342 if( xActivatedQueue == NULL )
\r
344 /* This should not happen as an infinite delay was used. */
\r
345 xQueueSetTasksStatus = pdFAIL;
\r
349 /* Reading from the queue should pass with a zero block time as
\r
350 this task will only run when something has been posted to a task
\r
351 in the queue set. */
\r
352 if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )
\r
354 xQueueSetTasksStatus = pdFAIL;
\r
357 /* Ensure the value received was the value expected. This function
\r
358 manipulates file scope data and is also called from an ISR, hence
\r
359 the critical section. */
\r
360 taskENTER_CRITICAL();
\r
362 prvCheckReceivedValue( ulReceived );
\r
364 taskEXIT_CRITICAL();
\r
367 if( xQueueSetTasksStatus == pdPASS )
\r
373 /*-----------------------------------------------------------*/
\r
375 void vQueueSetAccessQueueSetFromISR( void )
\r
377 static unsigned long ulCallCount = 0;
\r
379 /* xSetupComplete is set to pdTRUE when the queues have been created and
\r
380 are available for use. */
\r
381 if( xSetupComplete == pdTRUE )
\r
383 /* It is intended that this function is called from the tick hook
\r
384 function, so each call is one tick period apart. */
\r
386 if( ulCallCount > queuesetISR_TX_PERIOD )
\r
390 /* First attempt to read from the queue set. */
\r
391 prvReceiveFromQueueInSetFromISR();
\r
393 /* Then write to the queue set. */
\r
394 prvSendToQueueInSetFromISR();
\r
398 /*-----------------------------------------------------------*/
\r
400 static void prvCheckReceivedValue( unsigned long ulReceived )
\r
402 static unsigned long ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
404 /* Values are received in tasks and interrupts. It is likely that the
\r
405 receiving task will sometimes get preempted by the receiving interrupt
\r
406 between reading a value from the queue and calling this function. When
\r
407 that happens, if the receiving interrupt calls this function the values
\r
408 will get passed into this function slightly out of order. For that
\r
409 reason the value passed in is tested against a small range of expected
\r
410 values, rather than a single absolute value. To make the range testing
\r
411 easier values in the range limits are ignored. */
\r
413 /* If the received value is equal to or greater than
\r
414 queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */
\r
415 if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )
\r
417 /* The value was sent from the ISR. */
\r
418 if( ( ulReceived - queuesetINITIAL_ISR_TX_VALUE ) < queuesetIGNORED_BOUNDARY )
\r
420 /* The value received is at the lower limit of the expected range.
\r
421 Don't test it and expect to receive one higher next time. */
\r
422 ulExpectedReceivedFromISR++;
\r
424 else if( ( ULONG_MAX - ulReceived ) <= queuesetIGNORED_BOUNDARY )
\r
426 /* The value received is at the higher limit of the expected range.
\r
427 Don't test it and expect to wrap soon. */
\r
428 ulExpectedReceivedFromISR++;
\r
429 if( ulExpectedReceivedFromISR == 0 )
\r
431 ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
436 /* Check the value against its expected value range. */
\r
437 if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromISR ) != pdPASS )
\r
439 xQueueSetTasksStatus = pdFAIL;
\r
443 /* It is expected to receive an incrementing value. */
\r
444 ulExpectedReceivedFromISR++;
\r
450 /* The value was sent from the Tx task. */
\r
451 if( ulReceived < queuesetIGNORED_BOUNDARY )
\r
453 /* The value received is at the lower limit of the expected range.
\r
454 Don't test it, and expect to receive one higher next time. */
\r
455 ulExpectedReceivedFromTask++;
\r
457 else if( ( ( queuesetINITIAL_ISR_TX_VALUE - 1 ) - ulReceived ) <= queuesetIGNORED_BOUNDARY )
\r
459 /* The value received is at the higher limit of the expected range.
\r
460 Don't test it and expect to wrap soon. */
\r
461 ulExpectedReceivedFromTask++;
\r
462 if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )
\r
464 ulExpectedReceivedFromTask = 0;
\r
469 /* Check the value against its expected value range. */
\r
470 if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromTask ) != pdPASS )
\r
472 xQueueSetTasksStatus = pdFAIL;
\r
476 /* It is expected to receive an incrementing value. */
\r
477 ulExpectedReceivedFromTask++;
\r
482 /*-----------------------------------------------------------*/
\r
484 static portBASE_TYPE prvCheckReceivedValueWithinExpectedRange( unsigned long ulReceived, unsigned long ulExpectedReceived )
\r
486 portBASE_TYPE xReturn = pdPASS;
\r
488 if( ulReceived > ulExpectedReceived )
\r
490 configASSERT( ( ulReceived - ulExpectedReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
\r
491 if( ( ulReceived - ulExpectedReceived ) > queuesetALLOWABLE_RX_DEVIATION )
\r
498 configASSERT( ( ulExpectedReceived - ulReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
\r
499 if( ( ulExpectedReceived - ulReceived ) > queuesetALLOWABLE_RX_DEVIATION )
\r
507 /*-----------------------------------------------------------*/
\r
509 static void prvReceiveFromQueueInSetFromISR( void )
\r
511 xQueueSetMemberHandle xActivatedQueue;
\r
512 unsigned long ulReceived;
\r
514 /* See if any of the queues in the set contain data. */
\r
515 xActivatedQueue = xQueueSelectFromSetFromISR( xQueueSet );
\r
517 if( xActivatedQueue != NULL )
\r
519 /* Reading from the queue for test purposes only. */
\r
520 if( xQueueReceiveFromISR( xActivatedQueue, &ulReceived, NULL ) != pdPASS )
\r
522 /* Data should have been available as the handle was returned from
\r
523 xQueueSelectFromSetFromISR(). */
\r
524 xQueueSetTasksStatus = pdFAIL;
\r
527 /* Ensure the value received was the value expected. */
\r
528 prvCheckReceivedValue( ulReceived );
\r
531 /*-----------------------------------------------------------*/
\r
533 static void prvSendToQueueInSetFromISR( void )
\r
535 static portBASE_TYPE xQueueToWriteTo = 0;
\r
537 if( xQueueSendFromISR( xQueues[ xQueueToWriteTo ], ( void * ) &ulISRTxValue, NULL ) == pdPASS )
\r
541 /* If the Tx value has wrapped then set it back to its
\r
543 if( ulISRTxValue == 0UL )
\r
545 ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
548 /* Use a different queue next time. */
\r
550 if( xQueueToWriteTo >= queuesetNUM_QUEUES_IN_SET )
\r
552 xQueueToWriteTo = 0;
\r
556 /*-----------------------------------------------------------*/
\r
558 static void prvSetupTest( xTaskHandle xQueueSetSendingTask )
\r
561 unsigned long ulValueToSend = 0;
\r
563 /* Ensure the queues are created and the queue set configured before the
\r
564 sending task is unsuspended.
\r
566 First Create the queue set such that it will be able to hold a message for
\r
567 every space in every queue in the set. */
\r
568 xQueueSet = xQueueCreateSet( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );
\r
570 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
572 /* Create the queue and add it to the set. The queue is just holding
\r
573 unsigned long value. */
\r
574 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( unsigned long ) );
\r
575 configASSERT( xQueues[ x ] );
\r
576 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdPASS )
\r
578 xQueueSetTasksStatus = pdFAIL;
\r
582 /* The queue has now been added to the queue set and cannot be added to
\r
584 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdFAIL )
\r
586 xQueueSetTasksStatus = pdFAIL;
\r
591 /* Attempt to remove a queue from a queue set it does not belong
\r
592 to (NULL being passed as the queue set in this case). */
\r
593 if( xQueueRemoveFromSet( xQueues[ 0 ], NULL ) != pdFAIL )
\r
595 /* It is not possible to successfully remove a queue from a queue
\r
596 set it does not belong to. */
\r
597 xQueueSetTasksStatus = pdFAIL;
\r
600 /* Attempt to remove a queue from the queue set it does belong to. */
\r
601 if( xQueueRemoveFromSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
\r
603 /* It should be possible to remove the queue from the queue set it
\r
605 xQueueSetTasksStatus = pdFAIL;
\r
608 /* Add an item to the queue before attempting to add it back into the
\r
610 xQueueSend( xQueues[ 0 ], ( void * ) &ulValueToSend, 0 );
\r
611 if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdFAIL )
\r
613 /* Should not be able to add a non-empty queue to a set. */
\r
614 xQueueSetTasksStatus = pdFAIL;
\r
617 /* Remove the item from the queue before adding the queue back into the
\r
618 set so the dynamic tests can begin. */
\r
619 xQueueReceive( xQueues[ 0 ], &ulValueToSend, 0 );
\r
620 if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
\r
622 /* If the queue was successfully removed from the queue set then it
\r
623 should be possible to add it back in again. */
\r
624 xQueueSetTasksStatus = pdFAIL;
\r
627 /* The task that sends to the queues is not running yet, so attempting to
\r
628 read from the queue set should fail. */
\r
629 if( xQueueSelectFromSet( xQueueSet, queuesetSHORT_DELAY ) != NULL )
\r
631 xQueueSetTasksStatus = pdFAIL;
\r
634 /* Resume the task that writes to the queues. */
\r
635 vTaskResume( xQueueSetSendingTask );
\r
637 /* Let the ISR access the queues also. */
\r
638 xSetupComplete = pdTRUE;
\r