2 FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
52 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
53 * comprehensive version.
\r
55 * Creates two tasks that communicate over a single queue. One task acts as a
\r
56 * producer, the other a consumer.
\r
58 * The producer loops for three iteration, posting an incrementing number onto the
\r
59 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
62 * The consumer loops emptying the queue. Each item removed from the queue is
\r
63 * checked to ensure it contains the expected value. When the queue is empty it
\r
64 * blocks for a fixed period, then does the same again.
\r
66 * All queue access is performed without blocking. The consumer completely empties
\r
67 * the queue each time it runs so the producer should never find the queue full.
\r
69 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
70 * find the queue is full.
\r
72 * \page PollQC pollQ.c
\r
73 * \ingroup DemoFiles
\r
80 + Delay periods are now specified using variables and constants of
\r
81 portTickType rather than unsigned portLONG.
\r
86 /* Scheduler include files. */
\r
87 #include "FreeRTOS.h"
\r
92 /* Demo program include files. */
\r
95 #define pollqSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE )
\r
97 /* The task that posts the incrementing number onto the queue. */
\r
98 static void vPolledQueueProducer( void *pvParameters );
\r
100 /* The task that empties the queue. */
\r
101 static void vPolledQueueConsumer( void *pvParameters );
\r
103 /* Variables that are used to check that the tasks are still running with no errors. */
\r
104 static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
105 /*-----------------------------------------------------------*/
\r
107 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
109 static xQueueHandle xPolledQueue;
\r
110 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
112 /* Create the queue used by the producer and consumer. */
\r
113 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
115 /* Spawn the producer and consumer. */
\r
116 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
117 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
119 /*-----------------------------------------------------------*/
\r
121 static void vPolledQueueProducer( void *pvParameters )
\r
123 unsigned portSHORT usValue = 0, usLoop;
\r
124 xQueueHandle *pxQueue;
\r
125 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
126 const unsigned portSHORT usNumToProduce = 3;
\r
127 const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
128 const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
129 portSHORT sError = pdFALSE;
\r
131 /* Queue a message for printing to say the task has started. */
\r
132 vPrintDisplayMessage( &pcTaskStartMsg );
\r
134 /* The queue being used is passed in as the parameter. */
\r
135 pxQueue = ( xQueueHandle * ) pvParameters;
\r
139 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
141 /* Send an incrementing number on the queue without blocking. */
\r
142 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
144 /* We should never find the queue full - this is an error. */
\r
145 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
150 if( sError == pdFALSE )
\r
152 /* If an error has ever been recorded we stop incrementing the
\r
154 ++sPollingProducerCount;
\r
157 /* Update the value we are going to post next time around. */
\r
162 /* Wait before we start posting again to ensure the consumer runs and
\r
163 empties the queue. */
\r
164 vTaskDelay( xDelay );
\r
167 /*-----------------------------------------------------------*/
\r
169 static void vPolledQueueConsumer( void *pvParameters )
\r
171 unsigned portSHORT usData, usExpectedValue = 0;
\r
172 xQueueHandle *pxQueue;
\r
173 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
174 const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
175 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
176 portSHORT sError = pdFALSE;
\r
178 /* Queue a message for printing to say the task has started. */
\r
179 vPrintDisplayMessage( &pcTaskStartMsg );
\r
181 /* The queue being used is passed in as the parameter. */
\r
182 pxQueue = ( xQueueHandle * ) pvParameters;
\r
186 /* Loop until the queue is empty. */
\r
187 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
189 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
191 if( usData != usExpectedValue )
\r
193 /* This is not what we expected to receive so an error has
\r
195 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
197 /* Catch-up to the value we received so our next expected value
\r
198 should again be correct. */
\r
199 usExpectedValue = usData;
\r
203 if( sError == pdFALSE )
\r
205 /* Only increment the check variable if no errors have
\r
207 ++sPollingConsumerCount;
\r
214 /* Now the queue is empty we block, allowing the producer to place more
\r
215 items in the queue. */
\r
216 vTaskDelay( xDelay );
\r
219 /*-----------------------------------------------------------*/
\r
221 /* This is called to check that all the created tasks are still running with no errors. */
\r
222 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
224 static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
225 portBASE_TYPE xReturn;
\r
227 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
228 ( sLastPollingProducerCount == sPollingProducerCount )
\r
238 sLastPollingConsumerCount = sPollingConsumerCount;
\r
239 sLastPollingProducerCount = sPollingProducerCount;
\r