2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * This is a version of PollQ.c that uses the alternative (Alt) API.
\r
72 * Creates two tasks that communicate over a single queue. One task acts as a
\r
73 * producer, the other a consumer.
\r
75 * The producer loops for three iteration, posting an incrementing number onto the
\r
76 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
79 * The consumer loops emptying the queue. Each item removed from the queue is
\r
80 * checked to ensure it contains the expected value. When the queue is empty it
\r
81 * blocks for a fixed period, then does the same again.
\r
83 * All queue access is performed without blocking. The consumer completely empties
\r
84 * the queue each time it runs so the producer should never find the queue full.
\r
86 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
87 * find the queue is full.
\r
93 + Delay periods are now specified using variables and constants of
\r
94 portTickType rather than unsigned portLONG.
\r
99 /* Scheduler include files. */
\r
100 #include "FreeRTOS.h"
\r
104 /* Demo program include files. */
\r
105 #include "AltPollQ.h"
\r
107 #define pollqSTACK_SIZE configMINIMAL_STACK_SIZE
\r
108 #define pollqQUEUE_SIZE ( 10 )
\r
109 #define pollqPRODUCER_DELAY ( ( portTickType ) 200 / portTICK_RATE_MS )
\r
110 #define pollqCONSUMER_DELAY ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )
\r
111 #define pollqNO_DELAY ( ( portTickType ) 0 )
\r
112 #define pollqVALUES_TO_PRODUCE ( ( signed portBASE_TYPE ) 3 )
\r
113 #define pollqINITIAL_VALUE ( ( signed portBASE_TYPE ) 0 )
\r
115 /* The task that posts the incrementing number onto the queue. */
\r
116 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
\r
118 /* The task that empties the queue. */
\r
119 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
\r
121 /* Variables that are used to check that the tasks are still running with no
\r
123 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
\r
125 /*-----------------------------------------------------------*/
\r
127 void vStartAltPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
129 static xQueueHandle xPolledQueue;
\r
131 /* Create the queue used by the producer and consumer. */
\r
132 xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
\r
134 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
135 in use. The queue registry is provided as a means for kernel aware
\r
136 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
137 is not being used. The call to vQueueAddToRegistry() will be removed
\r
138 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
139 defined to be less than 1. */
\r
140 vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "AltPollQueue" );
\r
143 /* Spawn the producer and consumer. */
\r
144 xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
145 xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
147 /*-----------------------------------------------------------*/
\r
149 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
\r
151 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;
\r
152 signed portBASE_TYPE xError = pdFALSE, xLoop;
\r
155 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
157 const portCHAR * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n";
\r
159 /* Queue a message for printing to say the task has started. */
\r
160 vPrintDisplayMessage( &pcTaskStartMsg );
\r
165 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
\r
167 /* Send an incrementing number on the queue without blocking. */
\r
168 if( xQueueAltSendToBack( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
\r
170 /* We should never find the queue full so if we get here there
\r
171 has been an error. */
\r
176 if( xError == pdFALSE )
\r
178 /* If an error has ever been recorded we stop incrementing the
\r
180 portENTER_CRITICAL();
\r
181 xPollingProducerCount++;
\r
182 portEXIT_CRITICAL();
\r
185 /* Update the value we are going to post next time around. */
\r
190 /* Wait before we start posting again to ensure the consumer runs and
\r
191 empties the queue. */
\r
192 vTaskDelay( pollqPRODUCER_DELAY );
\r
194 } /*lint !e818 Function prototype must conform to API. */
\r
195 /*-----------------------------------------------------------*/
\r
197 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
\r
199 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;
\r
200 signed portBASE_TYPE xError = pdFALSE;
\r
203 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
205 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";
\r
207 /* Queue a message for printing to say the task has started. */
\r
208 vPrintDisplayMessage( &pcTaskStartMsg );
\r
213 /* Loop until the queue is empty. */
\r
214 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
\r
216 if( xQueueAltReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
\r
218 if( usData != usExpectedValue )
\r
220 /* This is not what we expected to receive so an error has
\r
224 /* Catch-up to the value we received so our next expected
\r
225 value should again be correct. */
\r
226 usExpectedValue = usData;
\r
230 if( xError == pdFALSE )
\r
232 /* Only increment the check variable if no errors have
\r
234 portENTER_CRITICAL();
\r
235 xPollingConsumerCount++;
\r
236 portEXIT_CRITICAL();
\r
240 /* Next time round we would expect the number to be one higher. */
\r
245 /* Now the queue is empty we block, allowing the producer to place more
\r
246 items in the queue. */
\r
247 vTaskDelay( pollqCONSUMER_DELAY );
\r
249 } /*lint !e818 Function prototype must conform to API. */
\r
250 /*-----------------------------------------------------------*/
\r
252 /* This is called to check that all the created tasks are still running with no errors. */
\r
253 portBASE_TYPE xAreAltPollingQueuesStillRunning( void )
\r
255 portBASE_TYPE xReturn;
\r
257 /* Check both the consumer and producer poll count to check they have both
\r
258 been changed since out last trip round. We do not need a critical section
\r
259 around the check variables as this is called from a higher priority than
\r
260 the other tasks that access the same variables. */
\r
261 if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
\r
262 ( xPollingProducerCount == pollqINITIAL_VALUE )
\r
272 /* Set the check variables back down so we know if they have been
\r
273 incremented the next time around. */
\r
274 xPollingConsumerCount = pollqINITIAL_VALUE;
\r
275 xPollingProducerCount = pollqINITIAL_VALUE;
\r