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 is a version of PollQ.c that uses the alternative (Alt) API.
\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
90 #include "AltPollQ.h"
\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 vStartAltPolledQueueTasks( 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 * ) "AltPollQueue" );
\r
128 /* Spawn the producer and consumer. */
\r
129 xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
130 xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
132 /*-----------------------------------------------------------*/
\r
134 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
\r
136 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;
\r
137 signed portBASE_TYPE xError = pdFALSE, xLoop;
\r
140 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
142 const portCHAR * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n";
\r
144 /* Queue a message for printing to say the task has started. */
\r
145 vPrintDisplayMessage( &pcTaskStartMsg );
\r
150 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
\r
152 /* Send an incrementing number on the queue without blocking. */
\r
153 if( xQueueAltSendToBack( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
\r
155 /* We should never find the queue full so if we get here there
\r
156 has been an error. */
\r
161 if( xError == pdFALSE )
\r
163 /* If an error has ever been recorded we stop incrementing the
\r
165 portENTER_CRITICAL();
\r
166 xPollingProducerCount++;
\r
167 portEXIT_CRITICAL();
\r
170 /* Update the value we are going to post next time around. */
\r
175 /* Wait before we start posting again to ensure the consumer runs and
\r
176 empties the queue. */
\r
177 vTaskDelay( pollqPRODUCER_DELAY );
\r
179 } /*lint !e818 Function prototype must conform to API. */
\r
180 /*-----------------------------------------------------------*/
\r
182 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
\r
184 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;
\r
185 signed portBASE_TYPE xError = pdFALSE;
\r
188 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
190 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";
\r
192 /* Queue a message for printing to say the task has started. */
\r
193 vPrintDisplayMessage( &pcTaskStartMsg );
\r
198 /* Loop until the queue is empty. */
\r
199 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
\r
201 if( xQueueAltReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
\r
203 if( usData != usExpectedValue )
\r
205 /* This is not what we expected to receive so an error has
\r
209 /* Catch-up to the value we received so our next expected
\r
210 value should again be correct. */
\r
211 usExpectedValue = usData;
\r
215 if( xError == pdFALSE )
\r
217 /* Only increment the check variable if no errors have
\r
219 portENTER_CRITICAL();
\r
220 xPollingConsumerCount++;
\r
221 portEXIT_CRITICAL();
\r
225 /* Next time round we would expect the number to be one higher. */
\r
230 /* Now the queue is empty we block, allowing the producer to place more
\r
231 items in the queue. */
\r
232 vTaskDelay( pollqCONSUMER_DELAY );
\r
234 } /*lint !e818 Function prototype must conform to API. */
\r
235 /*-----------------------------------------------------------*/
\r
237 /* This is called to check that all the created tasks are still running with no errors. */
\r
238 portBASE_TYPE xAreAltPollingQueuesStillRunning( void )
\r
240 portBASE_TYPE xReturn;
\r
242 /* Check both the consumer and producer poll count to check they have both
\r
243 been changed since out last trip round. We do not need a critical section
\r
244 around the check variables as this is called from a higher priority than
\r
245 the other tasks that access the same variables. */
\r
246 if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
\r
247 ( xPollingProducerCount == pollqINITIAL_VALUE )
\r
257 /* Set the check variables back down so we know if they have been
\r
258 incremented the next time around. */
\r
259 xPollingConsumerCount = pollqINITIAL_VALUE;
\r
260 xPollingProducerCount = pollqINITIAL_VALUE;
\r