2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
55 * comprehensive version.
\r
57 * Creates two tasks that communicate over a single queue. One task acts as a
\r
58 * producer, the other a consumer.
\r
60 * The producer loops for three iteration, posting an incrementing number onto the
\r
61 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
64 * The consumer loops emptying the queue. Each item removed from the queue is
\r
65 * checked to ensure it contains the expected value. When the queue is empty it
\r
66 * blocks for a fixed period, then does the same again.
\r
68 * All queue access is performed without blocking. The consumer completely empties
\r
69 * the queue each time it runs so the producer should never find the queue full.
\r
71 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
72 * find the queue is full.
\r
74 * \page PollQC pollQ.c
\r
75 * \ingroup DemoFiles
\r
82 + Delay periods are now specified using variables and constants of
\r
83 portTickType rather than unsigned portLONG.
\r
88 /* Scheduler include files. */
\r
89 #include "FreeRTOS.h"
\r
94 /* Demo program include files. */
\r
97 #define pollqSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE )
\r
99 /* The task that posts the incrementing number onto the queue. */
\r
100 static void vPolledQueueProducer( void *pvParameters );
\r
102 /* The task that empties the queue. */
\r
103 static void vPolledQueueConsumer( void *pvParameters );
\r
105 /* Variables that are used to check that the tasks are still running with no errors. */
\r
106 static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
107 /*-----------------------------------------------------------*/
\r
109 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
111 static xQueueHandle xPolledQueue;
\r
112 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
114 /* Create the queue used by the producer and consumer. */
\r
115 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
117 /* Spawn the producer and consumer. */
\r
118 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
119 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
121 /*-----------------------------------------------------------*/
\r
123 static void vPolledQueueProducer( void *pvParameters )
\r
125 unsigned portSHORT usValue = 0, usLoop;
\r
126 xQueueHandle *pxQueue;
\r
127 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
128 const unsigned portSHORT usNumToProduce = 3;
\r
129 const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
130 const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
131 portSHORT sError = pdFALSE;
\r
133 /* Queue a message for printing to say the task has started. */
\r
134 vPrintDisplayMessage( &pcTaskStartMsg );
\r
136 /* The queue being used is passed in as the parameter. */
\r
137 pxQueue = ( xQueueHandle * ) pvParameters;
\r
141 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
143 /* Send an incrementing number on the queue without blocking. */
\r
144 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
146 /* We should never find the queue full - this is an error. */
\r
147 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
152 if( sError == pdFALSE )
\r
154 /* If an error has ever been recorded we stop incrementing the
\r
156 ++sPollingProducerCount;
\r
159 /* Update the value we are going to post next time around. */
\r
164 /* Wait before we start posting again to ensure the consumer runs and
\r
165 empties the queue. */
\r
166 vTaskDelay( xDelay );
\r
169 /*-----------------------------------------------------------*/
\r
171 static void vPolledQueueConsumer( void *pvParameters )
\r
173 unsigned portSHORT usData, usExpectedValue = 0;
\r
174 xQueueHandle *pxQueue;
\r
175 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
176 const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
177 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
178 portSHORT sError = pdFALSE;
\r
180 /* Queue a message for printing to say the task has started. */
\r
181 vPrintDisplayMessage( &pcTaskStartMsg );
\r
183 /* The queue being used is passed in as the parameter. */
\r
184 pxQueue = ( xQueueHandle * ) pvParameters;
\r
188 /* Loop until the queue is empty. */
\r
189 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
191 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
193 if( usData != usExpectedValue )
\r
195 /* This is not what we expected to receive so an error has
\r
197 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
199 /* Catch-up to the value we received so our next expected value
\r
200 should again be correct. */
\r
201 usExpectedValue = usData;
\r
205 if( sError == pdFALSE )
\r
207 /* Only increment the check variable if no errors have
\r
209 ++sPollingConsumerCount;
\r
216 /* Now the queue is empty we block, allowing the producer to place more
\r
217 items in the queue. */
\r
218 vTaskDelay( xDelay );
\r
221 /*-----------------------------------------------------------*/
\r
223 /* This is called to check that all the created tasks are still running with no errors. */
\r
224 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
226 static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
227 portBASE_TYPE xReturn;
\r
229 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
230 ( sLastPollingProducerCount == sPollingProducerCount )
\r
240 sLastPollingConsumerCount = sPollingConsumerCount;
\r
241 sLastPollingProducerCount = sPollingProducerCount;
\r