2 FreeRTOS V7.4.0 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
77 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
78 * comprehensive version.
\r
80 * Creates two tasks that communicate over a single queue. One task acts as a
\r
81 * producer, the other a consumer.
\r
83 * The producer loops for three iteration, posting an incrementing number onto the
\r
84 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
87 * The consumer loops emptying the queue. Each item removed from the queue is
\r
88 * checked to ensure it contains the expected value. When the queue is empty it
\r
89 * blocks for a fixed period, then does the same again.
\r
91 * All queue access is performed without blocking. The consumer completely empties
\r
92 * the queue each time it runs so the producer should never find the queue full.
\r
94 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
95 * find the queue is full.
\r
97 * \page PollQC pollQ.c
\r
98 * \ingroup DemoFiles
\r
103 Changes from V2.0.0
\r
105 + Delay periods are now specified using variables and constants of
\r
106 portTickType rather than unsigned long.
\r
109 #include <stdlib.h>
\r
111 /* Scheduler include files. */
\r
112 #include "FreeRTOS.h"
\r
117 /* Demo program include files. */
\r
120 #define pollqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
122 /* The task that posts the incrementing number onto the queue. */
\r
123 static void vPolledQueueProducer( void *pvParameters );
\r
125 /* The task that empties the queue. */
\r
126 static void vPolledQueueConsumer( void *pvParameters );
\r
128 /* Variables that are used to check that the tasks are still running with no errors. */
\r
129 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
130 /*-----------------------------------------------------------*/
\r
132 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
134 static xQueueHandle xPolledQueue;
\r
135 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
137 /* Create the queue used by the producer and consumer. */
\r
138 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
140 /* Spawn the producer and consumer. */
\r
141 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
142 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
144 /*-----------------------------------------------------------*/
\r
146 static void vPolledQueueProducer( void *pvParameters )
\r
148 unsigned short usValue = 0, usLoop;
\r
149 xQueueHandle *pxQueue;
\r
150 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
151 const unsigned short usNumToProduce = 3;
\r
152 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
153 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
154 short sError = pdFALSE;
\r
156 /* Queue a message for printing to say the task has started. */
\r
157 vPrintDisplayMessage( &pcTaskStartMsg );
\r
159 /* The queue being used is passed in as the parameter. */
\r
160 pxQueue = ( xQueueHandle * ) pvParameters;
\r
164 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
166 /* Send an incrementing number on the queue without blocking. */
\r
167 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
169 /* We should never find the queue full - this is an error. */
\r
170 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
175 if( sError == pdFALSE )
\r
177 /* If an error has ever been recorded we stop incrementing the
\r
179 ++sPollingProducerCount;
\r
182 /* Update the value we are going to post next time around. */
\r
187 /* Wait before we start posting again to ensure the consumer runs and
\r
188 empties the queue. */
\r
189 vTaskDelay( xDelay );
\r
192 /*-----------------------------------------------------------*/
\r
194 static void vPolledQueueConsumer( void *pvParameters )
\r
196 unsigned short usData, usExpectedValue = 0;
\r
197 xQueueHandle *pxQueue;
\r
198 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
199 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
200 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
201 short sError = pdFALSE;
\r
203 /* Queue a message for printing to say the task has started. */
\r
204 vPrintDisplayMessage( &pcTaskStartMsg );
\r
206 /* The queue being used is passed in as the parameter. */
\r
207 pxQueue = ( xQueueHandle * ) pvParameters;
\r
211 /* Loop until the queue is empty. */
\r
212 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
214 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
216 if( usData != usExpectedValue )
\r
218 /* This is not what we expected to receive so an error has
\r
220 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
222 /* Catch-up to the value we received so our next expected value
\r
223 should again be correct. */
\r
224 usExpectedValue = usData;
\r
228 if( sError == pdFALSE )
\r
230 /* Only increment the check variable if no errors have
\r
232 ++sPollingConsumerCount;
\r
239 /* Now the queue is empty we block, allowing the producer to place more
\r
240 items in the queue. */
\r
241 vTaskDelay( xDelay );
\r
244 /*-----------------------------------------------------------*/
\r
246 /* This is called to check that all the created tasks are still running with no errors. */
\r
247 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
249 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
250 portBASE_TYPE xReturn;
\r
252 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
253 ( sLastPollingProducerCount == sPollingProducerCount )
\r
263 sLastPollingConsumerCount = sPollingConsumerCount;
\r
264 sLastPollingProducerCount = sPollingProducerCount;
\r