2 FreeRTOS V7.3.0 - Copyright (C) 2012 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
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * Demonstrates the creation an use of queue sets.
\r
72 * A receive task creates a number of queues and adds them to a queue set before
\r
73 * blocking on the queue set receive. A transmit task and (optionally) an
\r
74 * interrupt repeatedly unblocks the receive task by sending messages to the
\r
75 * queues in a pseudo random order. The receive task removes the messages from
\r
76 * the queues and flags an error if the received message does not match that
\r
77 * expected. The task sends values in the range 0 to
\r
78 * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range
\r
79 * queuesetINITIAL_ISR_TX_VALUE to 0xffffffffUL;
\r
82 /* Standard includes. */
\r
85 /* Kernel includes. */
\r
86 #include "FreeRTOS.h"
\r
90 /* Demo includes. */
\r
91 #include "QueueSet.h"
\r
93 /* The number of queues that are created and added to the queue set. */
\r
94 #define queuesetNUM_QUEUES_IN_SET 3
\r
96 /* The length of each created queue. */
\r
97 #define queuesetQUEUE_LENGTH 3
\r
99 /* Block times used in this demo. A block time or 0 means "don't block". */
\r
100 #define queuesetSHORT_DELAY 200
\r
101 #define queuesetDONT_BLOCK 0
\r
103 /* Messages are sent in incrementing order from both a task and an interrupt.
\r
104 The task sends values in the range 0 to 0xfffe, and the interrupt sends values
\r
105 in the range of 0xffff to 0xffffffff; */
\r
106 #define queuesetINITIAL_ISR_TX_VALUE 0xffffUL
\r
108 /* The priorities used in this demo. */
\r
109 #define queuesetLOW_PRIORITY ( tskIDLE_PRIORITY )
\r
110 #define queuesetMEDIUM_PRIORITY ( queuesetLOW_PRIORITY + 1 )
\r
111 #define queuesetHIGH_PRIORITY ( queuesetMEDIUM_PRIORITY + 1 )
\r
113 /* For test purposes the priority of the sending task is changed after every
\r
114 queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */
\r
115 #define queuesetPRIORITY_CHANGE_LOOPS 100UL
\r
117 /* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */
\r
118 #define queuesetISR_TX_PERIOD ( 100UL )
\r
121 * The task that periodically sends to the queue set.
\r
123 static void prvQueueSetSendingTask( void *pvParameters );
\r
126 * The task that reads from the queue set.
\r
128 static void prvQueueSetReceivingTask( void *pvParameters );
\r
130 /* The queues that are added to the set. */
\r
131 static xQueueHandle xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
133 /* Counts how many times each queue in the set is used to ensure all the
\r
134 queues are used. */
\r
135 static unsigned long ulQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
137 /* The handle of the queue set to which the queues are added. */
\r
138 static xQueueSetHandle xQueueSet;
\r
140 /* If the prvQueueSetReceivingTask() task has not detected any errors then
\r
141 it increments ulCycleCounter on each iteration.
\r
142 xAreQueueSetTasksStillRunning() returns pdPASS if the value of
\r
143 ulCycleCounter has changed between consecutive calls, and pdFALSE if
\r
144 ulCycleCounter has stopped incrementing (indicating an error condition). */
\r
145 static volatile unsigned long ulCycleCounter = 0UL;
\r
147 /* Set to pdFAIL if an error is detected by any queue set task.
\r
148 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
\r
149 static volatile portBASE_TYPE xQueueSetTasksStatus = pdPASS;
\r
151 /* Just a flag to let the function that writes to a queue from an ISR know that
\r
152 the queues are setup and can be used. */
\r
153 static volatile portBASE_TYPE xSetupComplete = pdFALSE;
\r
155 /* The value sent to the queue from the ISR is file scope so the
\r
156 xAreQueeuSetTasksStillRunning() function can check it is incrementing as
\r
158 static volatile unsigned long ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
160 /*-----------------------------------------------------------*/
\r
162 void vStartQueueSetTasks( void )
\r
164 xTaskHandle xQueueSetSendingTask;
\r
166 /* Create the two queues. The handle of the sending task is passed into
\r
167 the receiving task using the task parameter. The receiving task uses the
\r
168 handle to resume the sending task after it has created the queues. */
\r
169 xTaskCreate( prvQueueSetSendingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, queuesetMEDIUM_PRIORITY, &xQueueSetSendingTask );
\r
170 xTaskCreate( prvQueueSetReceivingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, queuesetMEDIUM_PRIORITY, NULL );
\r
172 /* It is important that the sending task does not attempt to write to a
\r
173 queue before the queue has been created. It is therefore placed into the
\r
174 suspended state before the scheduler has started. It is resumed by the
\r
175 receiving task after the receiving task has created the queues and added the
\r
176 queues to the queue set. */
\r
177 vTaskSuspend( xQueueSetSendingTask );
\r
179 /*-----------------------------------------------------------*/
\r
181 portBASE_TYPE xAreQueueSetTasksStillRunning( void )
\r
183 static unsigned long ulLastCycleCounter, ulLastISRTxValue = 0;
\r
184 static unsigned long ulLastQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
185 portBASE_TYPE xReturn = pdPASS, x;
\r
187 if( ulLastCycleCounter == ulCycleCounter )
\r
189 /* The cycle counter is no longer being incremented. Either one of the
\r
190 tasks is stalled or an error has been detected. */
\r
194 ulLastCycleCounter = ulCycleCounter;
\r
196 /* Ensure that all the queues in the set have been used. This ensures the
\r
197 test is working as intended and guards against the rand() in the Tx task
\r
198 missing some values. */
\r
199 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
201 if( ulLastQueueUsedCounter[ x ] == ulQueueUsedCounter[ x ] )
\r
206 ulLastQueueUsedCounter[ x ] = ulQueueUsedCounter[ x ];
\r
209 /* Check the global status flag. */
\r
210 if( xQueueSetTasksStatus != pdPASS )
\r
215 /* Check that the ISR is still sending values to the queues too. */
\r
216 if( ulISRTxValue == ulLastISRTxValue )
\r
222 ulLastISRTxValue = ulISRTxValue;
\r
227 /*-----------------------------------------------------------*/
\r
229 void vQueueSetWriteToQueueFromISR( void )
\r
231 portBASE_TYPE x = 0;
\r
232 static unsigned long ulCallCount = 0;
\r
234 /* xSetupComplete is set to pdTRUE when the queues have been created and
\r
235 are available for use. */
\r
236 if( xSetupComplete == pdTRUE )
\r
238 /* It is intended that this function is called from the tick hook
\r
239 function, so each call is one tick period apart. */
\r
241 if( ulCallCount > queuesetISR_TX_PERIOD )
\r
245 /* Look for a queue that can be written to. */
\r
246 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
248 if( xQueues[ x ] != NULL )
\r
250 /* xQueues[ x ] can be written to. Send the next value. */
\r
251 if( xQueueSendFromISR( xQueues[ x ], ( void * ) &ulISRTxValue, NULL ) == pdPASS )
\r
255 /* If the Tx value has wrapped then set it back to its
\r
257 if( ulISRTxValue == 0UL )
\r
259 ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;
\r
269 /*-----------------------------------------------------------*/
\r
271 static void prvQueueSetSendingTask( void *pvParameters )
\r
273 unsigned long ulTaskTxValue = 0;
\r
274 portBASE_TYPE xQueueToWriteTo;
\r
275 xQueueHandle xQueueInUse;
\r
276 unsigned portBASE_TYPE uxPriority = queuesetMEDIUM_PRIORITY, ulLoops = 0;
\r
278 /* Remove compiler warning about the unused parameter. */
\r
279 ( void ) pvParameters;
\r
281 srand( ( unsigned int ) &ulTaskTxValue );
\r
285 /* Generate the index for the queue to which a value is to be sent. */
\r
286 xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;
\r
287 xQueueInUse = xQueues[ xQueueToWriteTo ];
\r
289 /* Note which index is being written to to ensure all the queues are
\r
291 ( ulQueueUsedCounter[ xQueueToWriteTo ] )++;
\r
293 /* Send to the queue to unblock the task that is waiting for data to
\r
294 arrive on a queue within the queue set to which this queue belongs. */
\r
295 if( xQueueSendToBack( xQueueInUse, &ulTaskTxValue, portMAX_DELAY ) != pdPASS )
\r
297 /* The send should always pass as an infinite block time was
\r
299 xQueueSetTasksStatus = pdFAIL;
\r
302 /* Attempt to remove the queue from a queue set it does not belong
\r
303 to (NULL being passed as the queue set in this case). */
\r
304 if( xQueueRemoveFromQueueSet( xQueueInUse, NULL ) != pdFAIL )
\r
306 /* It is not possible to successfully remove a queue from a queue
\r
307 set it does not belong to. */
\r
308 xQueueSetTasksStatus = pdFAIL;
\r
311 /* Mark the space in the array of queues as being empty before actually
\r
312 removing the queue from the queue set. This is to prevent the code
\r
313 that accesses the queue set from an interrupt from attempting to access
\r
314 a queue that is no longer in the set. */
\r
315 xQueues[ xQueueToWriteTo ] = 0;
\r
317 /* Attempt to remove the queue from the queue set it does belong to. */
\r
318 if( xQueueRemoveFromQueueSet( xQueueInUse, xQueueSet ) != pdPASS )
\r
320 /* It should be possible to remove the queue from the queue set it
\r
322 xQueueSetTasksStatus = pdFAIL;
\r
325 /* Add the queue back before cycling back to run again. */
\r
326 if( xQueueAddToQueueSet( xQueueInUse, xQueueSet ) != pdPASS )
\r
328 /* If the queue was successfully removed from the queue set then it
\r
329 should be possible to add it back in again. */
\r
330 xQueueSetTasksStatus = pdFAIL;
\r
333 /* Now the queue is back in the set it is ok for the interrupt that
\r
334 writes to the queues to access it again. */
\r
335 xQueues[ xQueueToWriteTo ] = xQueueInUse;
\r
339 /* If the Tx value has reached the range used by the ISR then set it
\r
341 if( ulTaskTxValue == queuesetINITIAL_ISR_TX_VALUE )
\r
346 /* Occasionally change the task priority relative to the priority of
\r
347 the receiving task. */
\r
349 if( ulLoops >= queuesetPRIORITY_CHANGE_LOOPS )
\r
353 if( uxPriority > queuesetHIGH_PRIORITY )
\r
355 uxPriority = queuesetLOW_PRIORITY;
\r
358 vTaskPrioritySet( NULL, uxPriority );
\r
362 /*-----------------------------------------------------------*/
\r
364 static void prvQueueSetReceivingTask( void *pvParameters )
\r
366 unsigned long ulReceived, ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
367 xQueueHandle xActivatedQueue;
\r
369 xTaskHandle xQueueSetSendingTask;
\r
371 /* The handle to the sending task is passed in using the task parameter. */
\r
372 xQueueSetSendingTask = ( xTaskHandle ) pvParameters;
\r
374 /* Ensure the queues are created and the queue set configured before the
\r
375 sending task is unsuspended.
\r
377 First Create the queue set such that it will be able to hold a message for
\r
378 every space in every queue in the set. */
\r
379 xQueueSet = xQueueSetCreate( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );
\r
381 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
383 /* Create the queue and add it to the set. The queue is just holding
\r
384 unsigned long value. */
\r
385 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( unsigned long ) );
\r
386 configASSERT( xQueues[ x ] );
\r
387 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdPASS )
\r
389 xQueueSetTasksStatus = pdFAIL;
\r
392 /* The queue has now been added to the queue set and cannot be added to
\r
394 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdFAIL )
\r
396 xQueueSetTasksStatus = pdFAIL;
\r
400 /* The task that sends to the queues is not running yet, so attempting to
\r
401 read from the queue set should fail, resulting in xActivatedQueue being set
\r
403 xActivatedQueue = xQueueBlockMultiple( xQueueSet, queuesetSHORT_DELAY );
\r
404 configASSERT( xActivatedQueue == NULL );
\r
406 /* Resume the task that writes to the queues. */
\r
407 vTaskResume( xQueueSetSendingTask );
\r
409 /* Let the ISR access the queues also. */
\r
410 xSetupComplete = pdTRUE;
\r
414 /* Wait for a message to arrive on one of the queues in the set. */
\r
415 xActivatedQueue = xQueueBlockMultiple( xQueueSet, portMAX_DELAY );
\r
416 configASSERT( xActivatedQueue );
\r
418 if( xActivatedQueue == NULL )
\r
420 /* This should not happen as an infinite delay was used. */
\r
421 xQueueSetTasksStatus = pdFAIL;
\r
425 /* Reading from the queue should pass with a zero block time as
\r
426 this task will only run when something has been posted to a task
\r
427 in the queue set. */
\r
428 if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )
\r
430 xQueueSetTasksStatus = pdFAIL;
\r
433 /* If the received value is equal to or greater than
\r
434 queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */
\r
435 if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )
\r
437 /* The value was sent from the ISR. Check it against its
\r
439 configASSERT( ulReceived == ulExpectedReceivedFromISR );
\r
440 if( ulReceived != ulExpectedReceivedFromISR )
\r
442 xQueueSetTasksStatus = pdFAIL;
\r
446 /* It is expected to receive an incrementing value. */
\r
447 ulExpectedReceivedFromISR++;
\r
449 /* If the expected value has wrapped then set it back to
\r
450 its initial value. */
\r
451 if( ulExpectedReceivedFromISR == 0 )
\r
453 ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;
\r
459 /* The value was sent from the Tx task. Check it against its
\r
461 configASSERT( ulReceived == ulExpectedReceivedFromTask );
\r
462 if( ulReceived != ulExpectedReceivedFromTask )
\r
464 xQueueSetTasksStatus = pdFAIL;
\r
468 /* It is expected to receive an incrementing value. */
\r
469 ulExpectedReceivedFromTask++;
\r
471 /* If the expected value has reached the range of values
\r
472 used by the ISR then set it back to 0. */
\r
473 if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )
\r
475 ulExpectedReceivedFromTask = 0;
\r
481 if( xQueueSetTasksStatus == pdPASS )
\r