2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 * Tests the use of queue sets.
\r
78 * A receive task creates a number of queues and adds them to a queue set before
\r
79 * blocking on the queue set receive. A transmit task and (optionally) an
\r
80 * interrupt repeatedly unblocks the receive task by sending messages to the
\r
81 * queues in a pseudo random order. The receive task removes the messages from
\r
82 * the queues and flags an error if the received message does not match that
\r
83 * expected. The task sends values in the range 0 to
\r
84 * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range
\r
85 * queuesetINITIAL_ISR_TX_VALUE to ULONG_MAX.
\r
88 /* Standard includes. */
\r
92 /* Kernel includes. */
\r
93 #include "FreeRTOS.h"
\r
97 /* Demo includes. */
\r
98 #include "QueueSet.h"
\r
100 /* The number of queues that are created and added to the queue set. */
\r
101 #define queuesetNUM_QUEUES_IN_SET 3
\r
103 /* The length of each created queue. */
\r
104 #define queuesetQUEUE_LENGTH 3
\r
106 /* Block times used in this demo. A block time or 0 means "don't block". */
\r
107 #define queuesetSHORT_DELAY 200
\r
108 #define queuesetDONT_BLOCK 0
\r
110 /* Messages are sent in incrementing order from both a task and an interrupt.
\r
111 The task sends values in the range 0 to 0xfffe, and the interrupt sends values
\r
112 in the range of 0xffff to ULONG_MAX. */
\r
113 #define queuesetINITIAL_ISR_TX_VALUE 0xffffUL
\r
115 /* The priorities used in this demo. */
\r
116 #define queuesetLOW_PRIORITY ( tskIDLE_PRIORITY )
\r
117 #define queuesetMEDIUM_PRIORITY ( queuesetLOW_PRIORITY + 1 )
\r
118 #define queuesetHIGH_PRIORITY ( queuesetMEDIUM_PRIORITY + 1 )
\r
120 /* For test purposes the priority of the sending task is changed after every
\r
121 queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */
\r
122 #define queuesetPRIORITY_CHANGE_LOOPS 100UL
\r
124 /* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */
\r
125 #define queuesetISR_TX_PERIOD ( 100UL )
\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
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
138 * The task that periodically sends to the queue set.
\r
140 static void prvQueueSetSendingTask( void *pvParameters );
\r
143 * The task that reads from the queue set.
\r
145 static void prvQueueSetReceivingTask( void *pvParameters );
\r
148 * Check the value received from a queue is the expected value. Some values
\r
149 * originate from the send task, some values originate from the ISR, with the
\r
150 * range of the value being used to distinguish between the two message
\r
153 static void prvCheckReceivedValue( unsigned long ulReceived );
\r
156 * For purposes of test coverage, functions that read from and write to a
\r
157 * queue set from an ISR respectively.
\r
159 static void prvReceiveFromQueueInSetFromISR( void );
\r
160 static void prvSendToQueueInSetFromISR( void );
\r
163 * Create the queues and add them to a queue set before resuming the Tx
\r
166 static void prvSetupTest( xTaskHandle xQueueSetSendingTask );
\r
169 * Checks a value received from a queue falls within the range of expected
\r
172 static portBASE_TYPE prvCheckReceivedValueWithinExpectedRange( unsigned long ulReceived, unsigned long ulExpectedReceived );
\r
174 /*-----------------------------------------------------------*/
\r
176 /* The queues that are added to the set. */
\r
177 static xQueueHandle xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
179 /* Counts how many times each queue in the set is used to ensure all the
\r
180 queues are used. */
\r
181 static unsigned long ulQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
183 /* The handle of the queue set to which the queues are added. */
\r
184 static xQueueSetHandle xQueueSet;
\r
186 /* If the prvQueueSetReceivingTask() task has not detected any errors then
\r
187 it increments ulCycleCounter on each iteration.
\r
188 xAreQueueSetTasksStillRunning() returns pdPASS if the value of
\r
189 ulCycleCounter has changed between consecutive calls, and pdFALSE if
\r
190 ulCycleCounter has stopped incrementing (indicating an error condition). */
\r
191 static volatile unsigned long ulCycleCounter = 0UL;
\r
193 /* Set to pdFAIL if an error is detected by any queue set task.
\r
194 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
\r
195 static volatile portBASE_TYPE xQueueSetTasksStatus = pdPASS;
\r
197 /* Just a flag to let the function that writes to a queue from an ISR know that
\r
198 the queues are setup and can be used. */
\r
199 static volatile portBASE_TYPE xSetupComplete = pdFALSE;
\r
201 /* The value sent to the queue from the ISR is file scope so the
\r
202 xAreQueeuSetTasksStillRunning() function can check it is incrementing as
\r
204 static volatile unsigned long ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
206 /*-----------------------------------------------------------*/
\r
208 void vStartQueueSetTasks( void )
\r
210 xTaskHandle xQueueSetSendingTask;
\r
212 /* Create the two queues. The handle of the sending task is passed into
\r
213 the receiving task using the task parameter. The receiving task uses the
\r
214 handle to resume the sending task after it has created the queues. */
\r
215 xTaskCreate( prvQueueSetSendingTask, ( signed char * ) "SetTx", configMINIMAL_STACK_SIZE, NULL, queuesetMEDIUM_PRIORITY, &xQueueSetSendingTask );
\r
216 xTaskCreate( prvQueueSetReceivingTask, ( signed char * ) "SetRx", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, queuesetMEDIUM_PRIORITY, NULL );
\r
218 /* It is important that the sending task does not attempt to write to a
\r
219 queue before the queue has been created. It is therefore placed into the
\r
220 suspended state before the scheduler has started. It is resumed by the
\r
221 receiving task after the receiving task has created the queues and added the
\r
222 queues to the queue set. */
\r
223 vTaskSuspend( xQueueSetSendingTask );
\r
225 /*-----------------------------------------------------------*/
\r
227 portBASE_TYPE xAreQueueSetTasksStillRunning( void )
\r
229 static unsigned long ulLastCycleCounter, ulLastISRTxValue = 0;
\r
230 static unsigned long ulLastQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
231 portBASE_TYPE xReturn = pdPASS, x;
\r
233 if( ulLastCycleCounter == ulCycleCounter )
\r
235 /* The cycle counter is no longer being incremented. Either one of the
\r
236 tasks is stalled or an error has been detected. */
\r
240 ulLastCycleCounter = ulCycleCounter;
\r
242 /* Ensure that all the queues in the set have been used. This ensures the
\r
243 test is working as intended and guards against the rand() in the Tx task
\r
244 missing some values. */
\r
245 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
247 if( ulLastQueueUsedCounter[ x ] == ulQueueUsedCounter[ x ] )
\r
252 ulLastQueueUsedCounter[ x ] = ulQueueUsedCounter[ x ];
\r
255 /* Check the global status flag. */
\r
256 if( xQueueSetTasksStatus != pdPASS )
\r
261 /* Check that the ISR is still sending values to the queues too. */
\r
262 if( ulISRTxValue == ulLastISRTxValue )
\r
268 ulLastISRTxValue = ulISRTxValue;
\r
273 /*-----------------------------------------------------------*/
\r
275 static void prvQueueSetSendingTask( void *pvParameters )
\r
277 unsigned long ulTaskTxValue = 0;
\r
278 portBASE_TYPE xQueueToWriteTo;
\r
279 xQueueHandle xQueueInUse;
\r
280 unsigned portBASE_TYPE uxPriority = queuesetMEDIUM_PRIORITY, ulLoops = 0;
\r
282 /* Remove compiler warning about the unused parameter. */
\r
283 ( void ) pvParameters;
\r
285 srand( ( unsigned int ) &ulTaskTxValue );
\r
289 /* Generate the index for the queue to which a value is to be sent. */
\r
290 xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;
\r
291 xQueueInUse = xQueues[ xQueueToWriteTo ];
\r
293 /* Note which index is being written to to ensure all the queues are
\r
295 ( ulQueueUsedCounter[ xQueueToWriteTo ] )++;
\r
297 /* Send to the queue to unblock the task that is waiting for data to
\r
298 arrive on a queue within the queue set to which this queue belongs. */
\r
299 if( xQueueSendToBack( xQueueInUse, &ulTaskTxValue, portMAX_DELAY ) != pdPASS )
\r
301 /* The send should always pass as an infinite block time was
\r
303 xQueueSetTasksStatus = pdFAIL;
\r
308 /* If the Tx value has reached the range used by the ISR then set it
\r
310 if( ulTaskTxValue == queuesetINITIAL_ISR_TX_VALUE )
\r
315 /* Occasionally change the task priority relative to the priority of
\r
316 the receiving task. */
\r
318 if( ulLoops >= queuesetPRIORITY_CHANGE_LOOPS )
\r
322 if( uxPriority > queuesetHIGH_PRIORITY )
\r
324 uxPriority = queuesetLOW_PRIORITY;
\r
327 vTaskPrioritySet( NULL, uxPriority );
\r
331 /*-----------------------------------------------------------*/
\r
333 static void prvQueueSetReceivingTask( void *pvParameters )
\r
335 unsigned long ulReceived;
\r
336 xQueueHandle xActivatedQueue;
\r
337 xTaskHandle xQueueSetSendingTask;
\r
339 /* The handle to the sending task is passed in using the task parameter. */
\r
340 xQueueSetSendingTask = ( xTaskHandle ) pvParameters;
\r
342 /* Create the queues and add them to the queue set before resuming the Tx
\r
344 prvSetupTest( xQueueSetSendingTask );
\r
348 /* Wait for a message to arrive on one of the queues in the set. */
\r
349 xActivatedQueue = xQueueSelectFromSet( xQueueSet, portMAX_DELAY );
\r
350 configASSERT( xActivatedQueue );
\r
352 if( xActivatedQueue == NULL )
\r
354 /* This should not happen as an infinite delay was used. */
\r
355 xQueueSetTasksStatus = pdFAIL;
\r
359 /* Reading from the queue should pass with a zero block time as
\r
360 this task will only run when something has been posted to a task
\r
361 in the queue set. */
\r
362 if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )
\r
364 xQueueSetTasksStatus = pdFAIL;
\r
367 /* Ensure the value received was the value expected. This function
\r
368 manipulates file scope data and is also called from an ISR, hence
\r
369 the critical section. */
\r
370 taskENTER_CRITICAL();
\r
372 prvCheckReceivedValue( ulReceived );
\r
374 taskEXIT_CRITICAL();
\r
377 if( xQueueSetTasksStatus == pdPASS )
\r
383 /*-----------------------------------------------------------*/
\r
385 void vQueueSetAccessQueueSetFromISR( void )
\r
387 static unsigned long ulCallCount = 0;
\r
389 /* xSetupComplete is set to pdTRUE when the queues have been created and
\r
390 are available for use. */
\r
391 if( xSetupComplete == pdTRUE )
\r
393 /* It is intended that this function is called from the tick hook
\r
394 function, so each call is one tick period apart. */
\r
396 if( ulCallCount > queuesetISR_TX_PERIOD )
\r
400 /* First attempt to read from the queue set. */
\r
401 prvReceiveFromQueueInSetFromISR();
\r
403 /* Then write to the queue set. */
\r
404 prvSendToQueueInSetFromISR();
\r
408 /*-----------------------------------------------------------*/
\r
410 static void prvCheckReceivedValue( unsigned long ulReceived )
\r
412 static unsigned long ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
414 /* Values are received in tasks and interrupts. It is likely that the
\r
415 receiving task will sometimes get preempted by the receiving interrupt
\r
416 between reading a value from the queue and calling this function. When
\r
417 that happens, if the receiving interrupt calls this function the values
\r
418 will get passed into this function slightly out of order. For that
\r
419 reason the value passed in is tested against a small range of expected
\r
420 values, rather than a single absolute value. To make the range testing
\r
421 easier values in the range limits are ignored. */
\r
423 /* If the received value is equal to or greater than
\r
424 queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */
\r
425 if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )
\r
427 /* The value was sent from the ISR. */
\r
428 if( ( ulReceived - queuesetINITIAL_ISR_TX_VALUE ) < queuesetIGNORED_BOUNDARY )
\r
430 /* The value received is at the lower limit of the expected range.
\r
431 Don't test it and expect to receive one higher next time. */
\r
432 ulExpectedReceivedFromISR++;
\r
434 else if( ( ULONG_MAX - ulReceived ) <= queuesetIGNORED_BOUNDARY )
\r
436 /* The value received is at the higher limit of the expected range.
\r
437 Don't test it and expect to wrap soon. */
\r
438 ulExpectedReceivedFromISR++;
\r
439 if( ulExpectedReceivedFromISR == 0 )
\r
441 ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
446 /* Check the value against its expected value range. */
\r
447 if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromISR ) != pdPASS )
\r
449 xQueueSetTasksStatus = pdFAIL;
\r
453 /* It is expected to receive an incrementing value. */
\r
454 ulExpectedReceivedFromISR++;
\r
460 /* The value was sent from the Tx task. */
\r
461 if( ulReceived < queuesetIGNORED_BOUNDARY )
\r
463 /* The value received is at the lower limit of the expected range.
\r
464 Don't test it, and expect to receive one higher next time. */
\r
465 ulExpectedReceivedFromTask++;
\r
467 else if( ( ( queuesetINITIAL_ISR_TX_VALUE - 1 ) - ulReceived ) <= queuesetIGNORED_BOUNDARY )
\r
469 /* The value received is at the higher limit of the expected range.
\r
470 Don't test it and expect to wrap soon. */
\r
471 ulExpectedReceivedFromTask++;
\r
472 if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )
\r
474 ulExpectedReceivedFromTask = 0;
\r
479 /* Check the value against its expected value range. */
\r
480 if( prvCheckReceivedValueWithinExpectedRange( ulReceived, ulExpectedReceivedFromTask ) != pdPASS )
\r
482 xQueueSetTasksStatus = pdFAIL;
\r
486 /* It is expected to receive an incrementing value. */
\r
487 ulExpectedReceivedFromTask++;
\r
492 /*-----------------------------------------------------------*/
\r
494 static portBASE_TYPE prvCheckReceivedValueWithinExpectedRange( unsigned long ulReceived, unsigned long ulExpectedReceived )
\r
496 portBASE_TYPE xReturn = pdPASS;
\r
498 if( ulReceived > ulExpectedReceived )
\r
500 configASSERT( ( ulReceived - ulExpectedReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
\r
501 if( ( ulReceived - ulExpectedReceived ) > queuesetALLOWABLE_RX_DEVIATION )
\r
508 configASSERT( ( ulExpectedReceived - ulReceived ) <= queuesetALLOWABLE_RX_DEVIATION );
\r
509 if( ( ulExpectedReceived - ulReceived ) > queuesetALLOWABLE_RX_DEVIATION )
\r
517 /*-----------------------------------------------------------*/
\r
519 static void prvReceiveFromQueueInSetFromISR( void )
\r
521 xQueueSetMemberHandle xActivatedQueue;
\r
522 unsigned long ulReceived;
\r
524 /* See if any of the queues in the set contain data. */
\r
525 xActivatedQueue = xQueueSelectFromSetFromISR( xQueueSet );
\r
527 if( xActivatedQueue != NULL )
\r
529 /* Reading from the queue for test purposes only. */
\r
530 if( xQueueReceiveFromISR( xActivatedQueue, &ulReceived, NULL ) != pdPASS )
\r
532 /* Data should have been available as the handle was returned from
\r
533 xQueueSelectFromSetFromISR(). */
\r
534 xQueueSetTasksStatus = pdFAIL;
\r
537 /* Ensure the value received was the value expected. */
\r
538 prvCheckReceivedValue( ulReceived );
\r
541 /*-----------------------------------------------------------*/
\r
543 static void prvSendToQueueInSetFromISR( void )
\r
545 static portBASE_TYPE xQueueToWriteTo = 0;
\r
547 if( xQueueSendFromISR( xQueues[ xQueueToWriteTo ], ( void * ) &ulISRTxValue, NULL ) == pdPASS )
\r
551 /* If the Tx value has wrapped then set it back to its
\r
553 if( ulISRTxValue == 0UL )
\r
555 ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
558 /* Use a different queue next time. */
\r
560 if( xQueueToWriteTo >= queuesetNUM_QUEUES_IN_SET )
\r
562 xQueueToWriteTo = 0;
\r
566 /*-----------------------------------------------------------*/
\r
568 static void prvSetupTest( xTaskHandle xQueueSetSendingTask )
\r
572 /* Ensure the queues are created and the queue set configured before the
\r
573 sending task is unsuspended.
\r
575 First Create the queue set such that it will be able to hold a message for
\r
576 every space in every queue in the set. */
\r
577 xQueueSet = xQueueCreateSet( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );
\r
579 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
581 /* Create the queue and add it to the set. The queue is just holding
\r
582 unsigned long value. */
\r
583 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( unsigned long ) );
\r
584 configASSERT( xQueues[ x ] );
\r
585 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdPASS )
\r
587 xQueueSetTasksStatus = pdFAIL;
\r
591 /* The queue has now been added to the queue set and cannot be added to
\r
593 if( xQueueAddToSet( xQueues[ x ], xQueueSet ) != pdFAIL )
\r
595 xQueueSetTasksStatus = pdFAIL;
\r
600 /* Attempt to remove a queue from a queue set it does not belong
\r
601 to (NULL being passed as the queue set in this case). */
\r
602 if( xQueueRemoveFromSet( xQueues[ 0 ], NULL ) != pdFAIL )
\r
604 /* It is not possible to successfully remove a queue from a queue
\r
605 set it does not belong to. */
\r
606 xQueueSetTasksStatus = pdFAIL;
\r
609 /* Attempt to remove a queue from the queue set it does belong to. */
\r
610 if( xQueueRemoveFromSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
\r
612 /* It should be possible to remove the queue from the queue set it
\r
614 xQueueSetTasksStatus = pdFAIL;
\r
617 /* Add the queue back again before starting the dynamic tests. */
\r
618 if( xQueueAddToSet( xQueues[ 0 ], xQueueSet ) != pdPASS )
\r
620 /* If the queue was successfully removed from the queue set then it
\r
621 should be possible to add it back in again. */
\r
622 xQueueSetTasksStatus = pdFAIL;
\r
625 /* The task that sends to the queues is not running yet, so attempting to
\r
626 read from the queue set should fail. */
\r
627 if( xQueueSelectFromSet( xQueueSet, queuesetSHORT_DELAY ) != NULL )
\r
629 xQueueSetTasksStatus = pdFAIL;
\r
632 /* Resume the task that writes to the queues. */
\r
633 vTaskResume( xQueueSetSendingTask );
\r
635 /* Let the ISR access the queues also. */
\r
636 xSetupComplete = pdTRUE;
\r