2 FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 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
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
38 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
39 * comprehensive version.
\r
41 * Creates two tasks that communicate over a single queue. One task acts as a
\r
42 * producer, the other a consumer.
\r
44 * The producer loops for three iteration, posting an incrementing number onto the
\r
45 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
48 * The consumer loops emptying the queue. Each item removed from the queue is
\r
49 * checked to ensure it contains the expected value. When the queue is empty it
\r
50 * blocks for a fixed period, then does the same again.
\r
52 * All queue access is performed without blocking. The consumer completely empties
\r
53 * the queue each time it runs so the producer should never find the queue full.
\r
55 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
56 * find the queue is full.
\r
58 * \page PollQC pollQ.c
\r
59 * \ingroup DemoFiles
\r
66 + Delay periods are now specified using variables and constants of
\r
67 portTickType rather than unsigned portLONG.
\r
72 /* Scheduler include files. */
\r
73 #include "FreeRTOS.h"
\r
78 /* Demo program include files. */
\r
81 #define pollqSTACK_SIZE ( ( unsigned portSHORT ) 128 )
\r
83 /* The task that posts the incrementing number onto the queue. */
\r
84 static void vPolledQueueProducer( void *pvParameters );
\r
86 /* The task that empties the queue. */
\r
87 static void vPolledQueueConsumer( void *pvParameters );
\r
89 /* Variables that are used to check that the tasks are still running with no errors. */
\r
90 static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
91 /*-----------------------------------------------------------*/
\r
93 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
95 static xQueueHandle xPolledQueue;
\r
96 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
98 /* Create the queue used by the producer and consumer. */
\r
99 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
101 /* Spawn the producer and consumer. */
\r
102 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
103 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
105 /*-----------------------------------------------------------*/
\r
107 static void vPolledQueueProducer( void *pvParameters )
\r
109 unsigned portSHORT usValue = 0, usLoop;
\r
110 xQueueHandle *pxQueue;
\r
111 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
112 const unsigned portSHORT usNumToProduce = 3;
\r
113 const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
114 const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
115 portSHORT sError = pdFALSE;
\r
117 /* Queue a message for printing to say the task has started. */
\r
118 vPrintDisplayMessage( &pcTaskStartMsg );
\r
120 /* The queue being used is passed in as the parameter. */
\r
121 pxQueue = ( xQueueHandle * ) pvParameters;
\r
125 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
127 /* Send an incrementing number on the queue without blocking. */
\r
128 if( xQueueSend( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
130 /* We should never find the queue full - this is an error. */
\r
131 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
136 if( sError == pdFALSE )
\r
138 /* If an error has ever been recorded we stop incrementing the
\r
140 ++sPollingProducerCount;
\r
143 /* Update the value we are going to post next time around. */
\r
148 /* Wait before we start posting again to ensure the consumer runs and
\r
149 empties the queue. */
\r
150 vTaskDelay( xDelay );
\r
153 /*-----------------------------------------------------------*/
\r
155 static void vPolledQueueConsumer( void *pvParameters )
\r
157 unsigned portSHORT usData, usExpectedValue = 0;
\r
158 xQueueHandle *pxQueue;
\r
159 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
160 const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
161 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
162 portSHORT sError = pdFALSE;
\r
164 /* Queue a message for printing to say the task has started. */
\r
165 vPrintDisplayMessage( &pcTaskStartMsg );
\r
167 /* The queue being used is passed in as the parameter. */
\r
168 pxQueue = ( xQueueHandle * ) pvParameters;
\r
172 /* Loop until the queue is empty. */
\r
173 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
175 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
177 if( usData != usExpectedValue )
\r
179 /* This is not what we expected to receive so an error has
\r
181 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
183 /* Catch-up to the value we received so our next expected value
\r
184 should again be correct. */
\r
185 usExpectedValue = usData;
\r
189 if( sError == pdFALSE )
\r
191 /* Only increment the check variable if no errors have
\r
193 ++sPollingConsumerCount;
\r
200 /* Now the queue is empty we block, allowing the producer to place more
\r
201 items in the queue. */
\r
202 vTaskDelay( xDelay );
\r
205 /*-----------------------------------------------------------*/
\r
207 /* This is called to check that all the created tasks are still running with no errors. */
\r
208 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
210 static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
211 portBASE_TYPE xReturn;
\r
213 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
214 ( sLastPollingProducerCount == sPollingProducerCount )
\r
224 sLastPollingConsumerCount = sPollingConsumerCount;
\r
225 sLastPollingProducerCount = sPollingProducerCount;
\r