2 FreeRTOS.org V5.2.0 - 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 * This version of PollQ. c is for use on systems that have limited stack
\r
54 * space and no display facilities. The complete version can be found in
\r
55 * the Demo/Common/Full directory.
\r
57 * Creates two tasks that communicate over a single queue. One task acts as a
\r
58 * producer, the other a consumer.
\r
60 * The producer loops for three iteration, posting an incrementing number onto the
\r
61 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
64 * The consumer loops emptying the queue. Each item removed from the queue is
\r
65 * checked to ensure it contains the expected value. When the queue is empty it
\r
66 * blocks for a fixed period, then does the same again.
\r
68 * All queue access is performed without blocking. The consumer completely empties
\r
69 * the queue each time it runs so the producer should never find the queue full.
\r
71 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
72 * find the queue is full.
\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
89 /* Demo program include files. */
\r
92 #define pollqSTACK_SIZE configMINIMAL_STACK_SIZE
\r
93 #define pollqQUEUE_SIZE ( 10 )
\r
94 #define pollqPRODUCER_DELAY ( ( portTickType ) 200 / portTICK_RATE_MS )
\r
95 #define pollqCONSUMER_DELAY ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )
\r
96 #define pollqNO_DELAY ( ( portTickType ) 0 )
\r
97 #define pollqVALUES_TO_PRODUCE ( ( signed portBASE_TYPE ) 3 )
\r
98 #define pollqINITIAL_VALUE ( ( signed portBASE_TYPE ) 0 )
\r
100 /* The task that posts the incrementing number onto the queue. */
\r
101 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
\r
103 /* The task that empties the queue. */
\r
104 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
\r
106 /* Variables that are used to check that the tasks are still running with no
\r
108 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
\r
110 /*-----------------------------------------------------------*/
\r
112 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
114 static xQueueHandle xPolledQueue;
\r
116 /* Create the queue used by the producer and consumer. */
\r
117 xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
119 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
120 in use. The queue registry is provided as a means for kernel aware
\r
121 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
122 is not being used. The call to vQueueAddToRegistry() will be removed
\r
123 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
124 defined to be less than 1. */
\r
125 vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "Poll_Test_Queue" );
\r
127 /* Spawn the producer and consumer. */
\r
128 xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
129 xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
131 /*-----------------------------------------------------------*/
\r
133 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
\r
135 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;
\r
136 signed portBASE_TYPE xError = pdFALSE, xLoop;
\r
140 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
\r
142 /* Send an incrementing number on the queue without blocking. */
\r
143 if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
\r
145 /* We should never find the queue full so if we get here there
\r
146 has been an error. */
\r
151 if( xError == pdFALSE )
\r
153 /* If an error has ever been recorded we stop incrementing the
\r
155 portENTER_CRITICAL();
\r
156 xPollingProducerCount++;
\r
157 portEXIT_CRITICAL();
\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( pollqPRODUCER_DELAY );
\r
169 } /*lint !e818 Function prototype must conform to API. */
\r
170 /*-----------------------------------------------------------*/
\r
172 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
\r
174 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;
\r
175 signed portBASE_TYPE xError = pdFALSE;
\r
179 /* Loop until the queue is empty. */
\r
180 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
\r
182 if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
\r
184 if( usData != usExpectedValue )
\r
186 /* This is not what we expected to receive so an error has
\r
190 /* Catch-up to the value we received so our next expected
\r
191 value should again be correct. */
\r
192 usExpectedValue = usData;
\r
196 if( xError == pdFALSE )
\r
198 /* Only increment the check variable if no errors have
\r
200 portENTER_CRITICAL();
\r
201 xPollingConsumerCount++;
\r
202 portEXIT_CRITICAL();
\r
206 /* Next time round we would expect the number to be one higher. */
\r
211 /* Now the queue is empty we block, allowing the producer to place more
\r
212 items in the queue. */
\r
213 vTaskDelay( pollqCONSUMER_DELAY );
\r
215 } /*lint !e818 Function prototype must conform to API. */
\r
216 /*-----------------------------------------------------------*/
\r
218 /* This is called to check that all the created tasks are still running with no errors. */
\r
219 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
221 portBASE_TYPE xReturn;
\r
223 /* Check both the consumer and producer poll count to check they have both
\r
224 been changed since out last trip round. We do not need a critical section
\r
225 around the check variables as this is called from a higher priority than
\r
226 the other tasks that access the same variables. */
\r
227 if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
\r
228 ( xPollingProducerCount == pollqINITIAL_VALUE )
\r
238 /* Set the check variables back down so we know if they have been
\r
239 incremented the next time around. */
\r
240 xPollingConsumerCount = pollqINITIAL_VALUE;
\r
241 xPollingProducerCount = pollqINITIAL_VALUE;
\r