2 FreeRTOS.org V4.1.3 - Copyright (C) 2003-2006 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
30 ***************************************************************************
\r
35 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
36 * comprehensive version.
\r
38 * Creates two tasks that communicate over a single queue. One task acts as a
\r
39 * producer, the other a consumer.
\r
41 * The producer loops for three iteration, posting an incrementing number onto the
\r
42 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
45 * The consumer loops emptying the queue. Each item removed from the queue is
\r
46 * checked to ensure it contains the expected value. When the queue is empty it
\r
47 * blocks for a fixed period, then does the same again.
\r
49 * All queue access is performed without blocking. The consumer completely empties
\r
50 * the queue each time it runs so the producer should never find the queue full.
\r
52 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
53 * find the queue is full.
\r
55 * \page PollQC pollQ.c
\r
56 * \ingroup DemoFiles
\r
63 + Delay periods are now specified using variables and constants of
\r
64 portTickType rather than unsigned portLONG.
\r
69 /* Scheduler include files. */
\r
70 #include "FreeRTOS.h"
\r
75 /* Demo program include files. */
\r
78 #define pollqSTACK_SIZE ( ( unsigned portSHORT ) 128 )
\r
80 /* The task that posts the incrementing number onto the queue. */
\r
81 static void vPolledQueueProducer( void *pvParameters );
\r
83 /* The task that empties the queue. */
\r
84 static void vPolledQueueConsumer( void *pvParameters );
\r
86 /* Variables that are used to check that the tasks are still running with no errors. */
\r
87 static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
88 /*-----------------------------------------------------------*/
\r
90 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
92 static xQueueHandle xPolledQueue;
\r
93 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
95 /* Create the queue used by the producer and consumer. */
\r
96 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
98 /* Spawn the producer and consumer. */
\r
99 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
100 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
102 /*-----------------------------------------------------------*/
\r
104 static void vPolledQueueProducer( void *pvParameters )
\r
106 unsigned portSHORT usValue = 0, usLoop;
\r
107 xQueueHandle *pxQueue;
\r
108 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
109 const unsigned portSHORT usNumToProduce = 3;
\r
110 const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
111 const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
112 portSHORT sError = pdFALSE;
\r
114 /* Queue a message for printing to say the task has started. */
\r
115 vPrintDisplayMessage( &pcTaskStartMsg );
\r
117 /* The queue being used is passed in as the parameter. */
\r
118 pxQueue = ( xQueueHandle * ) pvParameters;
\r
122 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
124 /* Send an incrementing number on the queue without blocking. */
\r
125 if( xQueueSend( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
127 /* We should never find the queue full - this is an error. */
\r
128 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
133 if( sError == pdFALSE )
\r
135 /* If an error has ever been recorded we stop incrementing the
\r
137 ++sPollingProducerCount;
\r
140 /* Update the value we are going to post next time around. */
\r
145 /* Wait before we start posting again to ensure the consumer runs and
\r
146 empties the queue. */
\r
147 vTaskDelay( xDelay );
\r
150 /*-----------------------------------------------------------*/
\r
152 static void vPolledQueueConsumer( void *pvParameters )
\r
154 unsigned portSHORT usData, usExpectedValue = 0;
\r
155 xQueueHandle *pxQueue;
\r
156 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
157 const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
158 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
159 portSHORT sError = pdFALSE;
\r
161 /* Queue a message for printing to say the task has started. */
\r
162 vPrintDisplayMessage( &pcTaskStartMsg );
\r
164 /* The queue being used is passed in as the parameter. */
\r
165 pxQueue = ( xQueueHandle * ) pvParameters;
\r
169 /* Loop until the queue is empty. */
\r
170 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
172 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
174 if( usData != usExpectedValue )
\r
176 /* This is not what we expected to receive so an error has
\r
178 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
180 /* Catch-up to the value we received so our next expected value
\r
181 should again be correct. */
\r
182 usExpectedValue = usData;
\r
186 if( sError == pdFALSE )
\r
188 /* Only increment the check variable if no errors have
\r
190 ++sPollingConsumerCount;
\r
197 /* Now the queue is empty we block, allowing the producer to place more
\r
198 items in the queue. */
\r
199 vTaskDelay( xDelay );
\r
202 /*-----------------------------------------------------------*/
\r
204 /* This is called to check that all the created tasks are still running with no errors. */
\r
205 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
207 static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
208 portBASE_TYPE xReturn;
\r
210 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
211 ( sLastPollingProducerCount == sPollingProducerCount )
\r
221 sLastPollingConsumerCount = sPollingConsumerCount;
\r
222 sLastPollingProducerCount = sPollingProducerCount;
\r