2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
56 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
57 * comprehensive version.
\r
59 * Creates two tasks that communicate over a single queue. One task acts as a
\r
60 * producer, the other a consumer.
\r
62 * The producer loops for three iteration, posting an incrementing number onto the
\r
63 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
66 * The consumer loops emptying the queue. Each item removed from the queue is
\r
67 * checked to ensure it contains the expected value. When the queue is empty it
\r
68 * blocks for a fixed period, then does the same again.
\r
70 * All queue access is performed without blocking. The consumer completely empties
\r
71 * the queue each time it runs so the producer should never find the queue full.
\r
73 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
74 * find the queue is full.
\r
76 * \page PollQC pollQ.c
\r
77 * \ingroup DemoFiles
\r
84 + Delay periods are now specified using variables and constants of
\r
85 portTickType rather than unsigned long.
\r
90 /* Scheduler include files. */
\r
91 #include "FreeRTOS.h"
\r
96 /* Demo program include files. */
\r
99 #define pollqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
101 /* The task that posts the incrementing number onto the queue. */
\r
102 static void vPolledQueueProducer( void *pvParameters );
\r
104 /* The task that empties the queue. */
\r
105 static void vPolledQueueConsumer( void *pvParameters );
\r
107 /* Variables that are used to check that the tasks are still running with no errors. */
\r
108 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
109 /*-----------------------------------------------------------*/
\r
111 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
113 static xQueueHandle xPolledQueue;
\r
114 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
116 /* Create the queue used by the producer and consumer. */
\r
117 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
119 /* Spawn the producer and consumer. */
\r
120 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
121 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
123 /*-----------------------------------------------------------*/
\r
125 static void vPolledQueueProducer( void *pvParameters )
\r
127 unsigned short usValue = 0, usLoop;
\r
128 xQueueHandle *pxQueue;
\r
129 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
130 const unsigned short usNumToProduce = 3;
\r
131 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
132 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
133 short sError = pdFALSE;
\r
135 /* Queue a message for printing to say the task has started. */
\r
136 vPrintDisplayMessage( &pcTaskStartMsg );
\r
138 /* The queue being used is passed in as the parameter. */
\r
139 pxQueue = ( xQueueHandle * ) pvParameters;
\r
143 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
145 /* Send an incrementing number on the queue without blocking. */
\r
146 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
148 /* We should never find the queue full - this is an error. */
\r
149 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
154 if( sError == pdFALSE )
\r
156 /* If an error has ever been recorded we stop incrementing the
\r
158 ++sPollingProducerCount;
\r
161 /* Update the value we are going to post next time around. */
\r
166 /* Wait before we start posting again to ensure the consumer runs and
\r
167 empties the queue. */
\r
168 vTaskDelay( xDelay );
\r
171 /*-----------------------------------------------------------*/
\r
173 static void vPolledQueueConsumer( void *pvParameters )
\r
175 unsigned short usData, usExpectedValue = 0;
\r
176 xQueueHandle *pxQueue;
\r
177 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
178 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
179 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
180 short sError = pdFALSE;
\r
182 /* Queue a message for printing to say the task has started. */
\r
183 vPrintDisplayMessage( &pcTaskStartMsg );
\r
185 /* The queue being used is passed in as the parameter. */
\r
186 pxQueue = ( xQueueHandle * ) pvParameters;
\r
190 /* Loop until the queue is empty. */
\r
191 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
193 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
195 if( usData != usExpectedValue )
\r
197 /* This is not what we expected to receive so an error has
\r
199 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
201 /* Catch-up to the value we received so our next expected value
\r
202 should again be correct. */
\r
203 usExpectedValue = usData;
\r
207 if( sError == pdFALSE )
\r
209 /* Only increment the check variable if no errors have
\r
211 ++sPollingConsumerCount;
\r
218 /* Now the queue is empty we block, allowing the producer to place more
\r
219 items in the queue. */
\r
220 vTaskDelay( xDelay );
\r
223 /*-----------------------------------------------------------*/
\r
225 /* This is called to check that all the created tasks are still running with no errors. */
\r
226 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
228 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
229 portBASE_TYPE xReturn;
\r
231 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
232 ( sLastPollingProducerCount == sPollingProducerCount )
\r
242 sLastPollingConsumerCount = sPollingConsumerCount;
\r
243 sLastPollingProducerCount = sPollingProducerCount;
\r