2 FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
50 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
51 * comprehensive version.
\r
53 * Creates two tasks that communicate over a single queue. One task acts as a
\r
54 * producer, the other a consumer.
\r
56 * The producer loops for three iteration, posting an incrementing number onto the
\r
57 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
60 * The consumer loops emptying the queue. Each item removed from the queue is
\r
61 * checked to ensure it contains the expected value. When the queue is empty it
\r
62 * blocks for a fixed period, then does the same again.
\r
64 * All queue access is performed without blocking. The consumer completely empties
\r
65 * the queue each time it runs so the producer should never find the queue full.
\r
67 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
68 * find the queue is full.
\r
70 * \page PollQC pollQ.c
\r
71 * \ingroup DemoFiles
\r
78 + Delay periods are now specified using variables and constants of
\r
79 portTickType rather than unsigned portLONG.
\r
84 /* Scheduler include files. */
\r
85 #include "FreeRTOS.h"
\r
90 /* Demo program include files. */
\r
93 #define pollqSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE )
\r
95 /* The task that posts the incrementing number onto the queue. */
\r
96 static void vPolledQueueProducer( void *pvParameters );
\r
98 /* The task that empties the queue. */
\r
99 static void vPolledQueueConsumer( void *pvParameters );
\r
101 /* Variables that are used to check that the tasks are still running with no errors. */
\r
102 static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
103 /*-----------------------------------------------------------*/
\r
105 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
107 static xQueueHandle xPolledQueue;
\r
108 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
110 /* Create the queue used by the producer and consumer. */
\r
111 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
113 /* Spawn the producer and consumer. */
\r
114 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
115 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
117 /*-----------------------------------------------------------*/
\r
119 static void vPolledQueueProducer( void *pvParameters )
\r
121 unsigned portSHORT usValue = 0, usLoop;
\r
122 xQueueHandle *pxQueue;
\r
123 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
124 const unsigned portSHORT usNumToProduce = 3;
\r
125 const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
126 const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
127 portSHORT sError = pdFALSE;
\r
129 /* Queue a message for printing to say the task has started. */
\r
130 vPrintDisplayMessage( &pcTaskStartMsg );
\r
132 /* The queue being used is passed in as the parameter. */
\r
133 pxQueue = ( xQueueHandle * ) pvParameters;
\r
137 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
139 /* Send an incrementing number on the queue without blocking. */
\r
140 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
142 /* We should never find the queue full - this is an error. */
\r
143 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
148 if( sError == pdFALSE )
\r
150 /* If an error has ever been recorded we stop incrementing the
\r
152 ++sPollingProducerCount;
\r
155 /* Update the value we are going to post next time around. */
\r
160 /* Wait before we start posting again to ensure the consumer runs and
\r
161 empties the queue. */
\r
162 vTaskDelay( xDelay );
\r
165 /*-----------------------------------------------------------*/
\r
167 static void vPolledQueueConsumer( void *pvParameters )
\r
169 unsigned portSHORT usData, usExpectedValue = 0;
\r
170 xQueueHandle *pxQueue;
\r
171 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
172 const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
173 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
174 portSHORT sError = pdFALSE;
\r
176 /* Queue a message for printing to say the task has started. */
\r
177 vPrintDisplayMessage( &pcTaskStartMsg );
\r
179 /* The queue being used is passed in as the parameter. */
\r
180 pxQueue = ( xQueueHandle * ) pvParameters;
\r
184 /* Loop until the queue is empty. */
\r
185 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
187 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
189 if( usData != usExpectedValue )
\r
191 /* This is not what we expected to receive so an error has
\r
193 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
195 /* Catch-up to the value we received so our next expected value
\r
196 should again be correct. */
\r
197 usExpectedValue = usData;
\r
201 if( sError == pdFALSE )
\r
203 /* Only increment the check variable if no errors have
\r
205 ++sPollingConsumerCount;
\r
212 /* Now the queue is empty we block, allowing the producer to place more
\r
213 items in the queue. */
\r
214 vTaskDelay( xDelay );
\r
217 /*-----------------------------------------------------------*/
\r
219 /* This is called to check that all the created tasks are still running with no errors. */
\r
220 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
222 static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
223 portBASE_TYPE xReturn;
\r
225 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
226 ( sLastPollingProducerCount == sPollingProducerCount )
\r
236 sLastPollingConsumerCount = sPollingConsumerCount;
\r
237 sLastPollingProducerCount = sPollingProducerCount;
\r