2 FreeRTOS.org V5.1.2 - 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
\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 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
55 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
56 * comprehensive version.
\r
58 * Creates two tasks that communicate over a single queue. One task acts as a
\r
59 * producer, the other a consumer.
\r
61 * The producer loops for three iteration, posting an incrementing number onto the
\r
62 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
65 * The consumer loops emptying the queue. Each item removed from the queue is
\r
66 * checked to ensure it contains the expected value. When the queue is empty it
\r
67 * blocks for a fixed period, then does the same again.
\r
69 * All queue access is performed without blocking. The consumer completely empties
\r
70 * the queue each time it runs so the producer should never find the queue full.
\r
72 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
73 * find the queue is full.
\r
75 * \page PollQC pollQ.c
\r
76 * \ingroup DemoFiles
\r
83 + Delay periods are now specified using variables and constants of
\r
84 portTickType rather than unsigned portLONG.
\r
89 /* Scheduler include files. */
\r
90 #include "FreeRTOS.h"
\r
95 /* Demo program include files. */
\r
98 #define pollqSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE )
\r
100 /* The task that posts the incrementing number onto the queue. */
\r
101 static void vPolledQueueProducer( void *pvParameters );
\r
103 /* The task that empties the queue. */
\r
104 static void vPolledQueueConsumer( void *pvParameters );
\r
106 /* Variables that are used to check that the tasks are still running with no errors. */
\r
107 static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
108 /*-----------------------------------------------------------*/
\r
110 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
112 static xQueueHandle xPolledQueue;
\r
113 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
115 /* Create the queue used by the producer and consumer. */
\r
116 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
118 /* Spawn the producer and consumer. */
\r
119 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
120 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
122 /*-----------------------------------------------------------*/
\r
124 static void vPolledQueueProducer( void *pvParameters )
\r
126 unsigned portSHORT usValue = 0, usLoop;
\r
127 xQueueHandle *pxQueue;
\r
128 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
129 const unsigned portSHORT usNumToProduce = 3;
\r
130 const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
131 const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
132 portSHORT sError = pdFALSE;
\r
134 /* Queue a message for printing to say the task has started. */
\r
135 vPrintDisplayMessage( &pcTaskStartMsg );
\r
137 /* The queue being used is passed in as the parameter. */
\r
138 pxQueue = ( xQueueHandle * ) pvParameters;
\r
142 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
144 /* Send an incrementing number on the queue without blocking. */
\r
145 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
147 /* We should never find the queue full - this is an error. */
\r
148 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
153 if( sError == pdFALSE )
\r
155 /* If an error has ever been recorded we stop incrementing the
\r
157 ++sPollingProducerCount;
\r
160 /* Update the value we are going to post next time around. */
\r
165 /* Wait before we start posting again to ensure the consumer runs and
\r
166 empties the queue. */
\r
167 vTaskDelay( xDelay );
\r
170 /*-----------------------------------------------------------*/
\r
172 static void vPolledQueueConsumer( void *pvParameters )
\r
174 unsigned portSHORT usData, usExpectedValue = 0;
\r
175 xQueueHandle *pxQueue;
\r
176 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
177 const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
178 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
179 portSHORT sError = pdFALSE;
\r
181 /* Queue a message for printing to say the task has started. */
\r
182 vPrintDisplayMessage( &pcTaskStartMsg );
\r
184 /* The queue being used is passed in as the parameter. */
\r
185 pxQueue = ( xQueueHandle * ) pvParameters;
\r
189 /* Loop until the queue is empty. */
\r
190 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
192 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
194 if( usData != usExpectedValue )
\r
196 /* This is not what we expected to receive so an error has
\r
198 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
200 /* Catch-up to the value we received so our next expected value
\r
201 should again be correct. */
\r
202 usExpectedValue = usData;
\r
206 if( sError == pdFALSE )
\r
208 /* Only increment the check variable if no errors have
\r
210 ++sPollingConsumerCount;
\r
217 /* Now the queue is empty we block, allowing the producer to place more
\r
218 items in the queue. */
\r
219 vTaskDelay( xDelay );
\r
222 /*-----------------------------------------------------------*/
\r
224 /* This is called to check that all the created tasks are still running with no errors. */
\r
225 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
227 static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
228 portBASE_TYPE xReturn;
\r
230 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
231 ( sLastPollingProducerCount == sPollingProducerCount )
\r
241 sLastPollingConsumerCount = sPollingConsumerCount;
\r
242 sLastPollingProducerCount = sPollingProducerCount;
\r