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
55 * This version of PollQ. c is for use on systems that have limited stack
\r
56 * space and no display facilities. The complete version can be found in
\r
57 * the Demo/Common/Full directory.
\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
80 + Delay periods are now specified using variables and constants of
\r
81 portTickType rather than unsigned long.
\r
86 /* Scheduler include files. */
\r
87 #include "FreeRTOS.h"
\r
91 /* Demo program include files. */
\r
94 #define pollqSTACK_SIZE configMINIMAL_STACK_SIZE
\r
95 #define pollqQUEUE_SIZE ( 10 )
\r
96 #define pollqPRODUCER_DELAY ( ( portTickType ) 200 / portTICK_RATE_MS )
\r
97 #define pollqCONSUMER_DELAY ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )
\r
98 #define pollqNO_DELAY ( ( portTickType ) 0 )
\r
99 #define pollqVALUES_TO_PRODUCE ( ( signed portBASE_TYPE ) 3 )
\r
100 #define pollqINITIAL_VALUE ( ( signed portBASE_TYPE ) 0 )
\r
102 /* The task that posts the incrementing number onto the queue. */
\r
103 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
\r
105 /* The task that empties the queue. */
\r
106 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
\r
108 /* Variables that are used to check that the tasks are still running with no
\r
110 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
\r
112 /*-----------------------------------------------------------*/
\r
114 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
116 static xQueueHandle xPolledQueue;
\r
118 /* Create the queue used by the producer and consumer. */
\r
119 xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
121 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
122 in use. The queue registry is provided as a means for kernel aware
\r
123 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
124 is not being used. The call to vQueueAddToRegistry() will be removed
\r
125 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
126 defined to be less than 1. */
\r
127 vQueueAddToRegistry( xPolledQueue, ( signed char * ) "Poll_Test_Queue" );
\r
129 /* Spawn the producer and consumer. */
\r
130 xTaskCreate( vPolledQueueConsumer, ( signed char * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
131 xTaskCreate( vPolledQueueProducer, ( signed char * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
133 /*-----------------------------------------------------------*/
\r
135 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
\r
137 unsigned short usValue = ( unsigned short ) 0;
\r
138 signed portBASE_TYPE xError = pdFALSE, xLoop;
\r
142 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
\r
144 /* Send an incrementing number on the queue without blocking. */
\r
145 if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
\r
147 /* We should never find the queue full so if we get here there
\r
148 has been an error. */
\r
153 if( xError == pdFALSE )
\r
155 /* If an error has ever been recorded we stop incrementing the
\r
157 portENTER_CRITICAL();
\r
158 xPollingProducerCount++;
\r
159 portEXIT_CRITICAL();
\r
162 /* Update the value we are going to post next time around. */
\r
167 /* Wait before we start posting again to ensure the consumer runs and
\r
168 empties the queue. */
\r
169 vTaskDelay( pollqPRODUCER_DELAY );
\r
171 } /*lint !e818 Function prototype must conform to API. */
\r
172 /*-----------------------------------------------------------*/
\r
174 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
\r
176 unsigned short usData, usExpectedValue = ( unsigned short ) 0;
\r
177 signed portBASE_TYPE xError = pdFALSE;
\r
181 /* Loop until the queue is empty. */
\r
182 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
\r
184 if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
\r
186 if( usData != usExpectedValue )
\r
188 /* This is not what we expected to receive so an error has
\r
192 /* Catch-up to the value we received so our next expected
\r
193 value should again be correct. */
\r
194 usExpectedValue = usData;
\r
198 if( xError == pdFALSE )
\r
200 /* Only increment the check variable if no errors have
\r
202 portENTER_CRITICAL();
\r
203 xPollingConsumerCount++;
\r
204 portEXIT_CRITICAL();
\r
208 /* Next time round we would expect the number to be one higher. */
\r
213 /* Now the queue is empty we block, allowing the producer to place more
\r
214 items in the queue. */
\r
215 vTaskDelay( pollqCONSUMER_DELAY );
\r
217 } /*lint !e818 Function prototype must conform to API. */
\r
218 /*-----------------------------------------------------------*/
\r
220 /* This is called to check that all the created tasks are still running with no errors. */
\r
221 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
223 portBASE_TYPE xReturn;
\r
225 /* Check both the consumer and producer poll count to check they have both
\r
226 been changed since out last trip round. We do not need a critical section
\r
227 around the check variables as this is called from a higher priority than
\r
228 the other tasks that access the same variables. */
\r
229 if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
\r
230 ( xPollingProducerCount == pollqINITIAL_VALUE )
\r
240 /* Set the check variables back down so we know if they have been
\r
241 incremented the next time around. */
\r
242 xPollingConsumerCount = pollqINITIAL_VALUE;
\r
243 xPollingProducerCount = pollqINITIAL_VALUE;
\r