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 a queue set receive. A transmit task repeatedly unblocks the
\r
74 * receive task by sending messages to the queues in a pseudo random order.
\r
75 * The receive task removes the messages from the queues and flags an error if
\r
76 * the received message does not match that expected.
\r
79 /* Kernel includes. */
\r
80 #include <FreeRTOS.h>
\r
84 /* Demo includes. */
\r
85 #include "QueueSet.h"
\r
87 /* The number of queues that are created and added to the queue set. */
\r
88 #define queuesetNUM_QUEUES_IN_SET 3
\r
90 /* The length of each created queue. */
\r
91 #define queuesetQUEUE_LENGTH 3
\r
93 /* Block times used in this demo. A block time or 0 means "don't block". */
\r
94 #define queuesetSHORT_DELAY 200
\r
95 #define queuesetDONT_BLOCK 0
\r
98 * The task that periodically sends to the queue set.
\r
100 static void prvQueueSetSendingTask( void *pvParameters );
\r
103 * The task that reads from the queue set.
\r
105 static void prvQueueSetReceivingTask( void *pvParameters );
\r
107 /* The queues that are added to the set. */
\r
108 static xQueueHandle xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };
\r
110 /* The handle of the queue set to which the queues are added. */
\r
111 static xQueueSetHandle xQueueSet;
\r
113 /* If the prvQueueSetReceivingTask() task has not detected any errors then
\r
114 it increments ulCycleCounter on each iteration.
\r
115 xAreQueueSetTasksStillRunning() returns pdPASS if the value of
\r
116 ulCycleCounter has changed between consecutive calls, and pdFALSE if
\r
117 ulCycleCounter has stopped incrementing (indicating an error condition). */
\r
118 volatile unsigned long ulCycleCounter = 0UL;
\r
120 /* Set to pdFAIL if an error is detected by any queue set task.
\r
121 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
\r
122 volatile portBASE_TYPE xQueueSetTasksStatus = pdPASS;
\r
125 /*-----------------------------------------------------------*/
\r
127 void vStartQueueSetTasks( unsigned portBASE_TYPE uxPriority )
\r
129 xTaskHandle xQueueSetSendingTask;
\r
131 /* Create the two queues. The handle of the sending task is passed into
\r
132 the receiving task using the task parameter. The receiving task uses the
\r
133 handle to resume the sending task after it has created the queues. */
\r
134 xTaskCreate( prvQueueSetSendingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xQueueSetSendingTask );
\r
135 xTaskCreate( prvQueueSetReceivingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, uxPriority, NULL );
\r
137 /* It is important that the sending task does not attempt to write to a
\r
138 queue before the queue has been created. It is therefore placed into the
\r
139 suspended state before the scheduler has started. It is resumed by the
\r
140 receiving task after the receiving task has created the queues and added the
\r
141 queues to the queue set. */
\r
142 vTaskSuspend( xQueueSetSendingTask );
\r
144 /*-----------------------------------------------------------*/
\r
146 portBASE_TYPE xAreQueueSetTasksStillRunning( void )
\r
148 static unsigned long ulLastCycleCounter;
\r
149 portBASE_TYPE xReturn;
\r
151 if( ulLastCycleCounter == ulCycleCounter )
\r
153 /* The cycle counter is no longer being incremented. Either one of the
\r
154 tasks is stalled or an error has been detected. */
\r
164 /*-----------------------------------------------------------*/
\r
166 static void prvQueueSetSendingTask( void *pvParameters )
\r
168 unsigned long ulTxValue = 0;
\r
169 portBASE_TYPE xQueueToWriteTo;
\r
171 /* Remove compiler warning about the unused parameter. */
\r
172 ( void ) pvParameters;
\r
174 srand( ( unsigned int ) &ulTxValue );
\r
178 /* Generate the index for the queue to which a value is to be sent. */
\r
179 xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;
\r
180 if( xQueueSendToBack( xQueues[ xQueueToWriteTo ], &ulTxValue, portMAX_DELAY ) != pdPASS )
\r
182 /* The send should always pass as an infinite block time was
\r
184 xQueueSetTasksStatus = pdFAIL;
\r
190 /*-----------------------------------------------------------*/
\r
192 static void prvQueueSetReceivingTask( void *pvParameters )
\r
194 unsigned long ulReceived, ulLastReceived = ~0UL;
\r
195 xQueueHandle xActivatedQueue;
\r
197 xTaskHandle xQueueSetSendingTask;
\r
199 /* The handle to the sending task is passed in using the task parameter. */
\r
200 xQueueSetSendingTask = ( xTaskHandle ) pvParameters;
\r
202 /* Ensure the queues are created and the queue set configured before the
\r
203 sending task is unsuspended.
\r
205 First Create the queue set such that it will be able to hold a message for
\r
206 every space in every queue in the set. */
\r
207 xQueueSet = xQueueSetCreate( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );
\r
209 for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )
\r
211 /* Create the queue and add it to the set. */
\r
212 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( unsigned long ) );
\r
213 configASSERT( xQueues[ x ] );
\r
214 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdPASS )
\r
216 xQueueSetTasksStatus = pdFAIL;
\r
219 /* The queue has now been added to the queue set and cannot be added to
\r
221 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdFAIL )
\r
223 xQueueSetTasksStatus = pdFAIL;
\r
227 /* The task that sends to the queues is not running yet, so attempting to
\r
228 read from the queue set should fail, resulting in xActivatedQueue being set
\r
230 xActivatedQueue = xQueueReadMultiple( xQueueSet, queuesetSHORT_DELAY );
\r
231 configASSERT( xActivatedQueue == NULL );
\r
233 /* Resume the task that writes to the queues. */
\r
234 vTaskResume( xQueueSetSendingTask );
\r
238 /* Wait for a message to arrive on one of the queues in the set. */
\r
239 xActivatedQueue = xQueueReadMultiple( xQueueSet, portMAX_DELAY );
\r
240 configASSERT( xActivatedQueue );
\r
242 if( xActivatedQueue == NULL )
\r
244 /* This should not happen as an infinite delay was used. */
\r
245 xQueueSetTasksStatus = pdFAIL;
\r
249 /* Reading from the queue should pass with a zero block time as
\r
250 this task will only run when something has been posted to a task
\r
251 in the queue set. */
\r
252 if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )
\r
254 xQueueSetTasksStatus = pdFAIL;
\r
257 /* It is always expected that the received value will be one
\r
258 greater than the previously received value. */
\r
259 configASSERT( ulReceived == ( ulLastReceived + 1 ) );
\r
260 if( ulReceived != ( ulLastReceived + 1 ) )
\r
262 xQueueSetTasksStatus = pdFAIL;
\r
266 ulLastReceived = ulReceived;
\r
270 if( xQueueSetTasksStatus == pdPASS )
\r