2 FreeRTOS V7.2.0 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
70 * comprehensive version.
\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
89 * \page PollQC pollQ.c
\r
90 * \ingroup DemoFiles
\r
97 + Delay periods are now specified using variables and constants of
\r
98 portTickType rather than unsigned long.
\r
101 #include <stdlib.h>
\r
103 /* Scheduler include files. */
\r
104 #include "FreeRTOS.h"
\r
109 /* Demo program include files. */
\r
112 #define pollqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
114 /* The task that posts the incrementing number onto the queue. */
\r
115 static void vPolledQueueProducer( void *pvParameters );
\r
117 /* The task that empties the queue. */
\r
118 static void vPolledQueueConsumer( void *pvParameters );
\r
120 /* Variables that are used to check that the tasks are still running with no errors. */
\r
121 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;
\r
122 /*-----------------------------------------------------------*/
\r
124 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
126 static xQueueHandle xPolledQueue;
\r
127 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
129 /* Create the queue used by the producer and consumer. */
\r
130 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
132 /* Spawn the producer and consumer. */
\r
133 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
134 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
\r
136 /*-----------------------------------------------------------*/
\r
138 static void vPolledQueueProducer( void *pvParameters )
\r
140 unsigned short usValue = 0, usLoop;
\r
141 xQueueHandle *pxQueue;
\r
142 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
143 const unsigned short usNumToProduce = 3;
\r
144 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";
\r
145 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
\r
146 short sError = pdFALSE;
\r
148 /* Queue a message for printing to say the task has started. */
\r
149 vPrintDisplayMessage( &pcTaskStartMsg );
\r
151 /* The queue being used is passed in as the parameter. */
\r
152 pxQueue = ( xQueueHandle * ) pvParameters;
\r
156 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
158 /* Send an incrementing number on the queue without blocking. */
\r
159 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
\r
161 /* We should never find the queue full - this is an error. */
\r
162 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
167 if( sError == pdFALSE )
\r
169 /* If an error has ever been recorded we stop incrementing the
\r
171 ++sPollingProducerCount;
\r
174 /* Update the value we are going to post next time around. */
\r
179 /* Wait before we start posting again to ensure the consumer runs and
\r
180 empties the queue. */
\r
181 vTaskDelay( xDelay );
\r
184 /*-----------------------------------------------------------*/
\r
186 static void vPolledQueueConsumer( void *pvParameters )
\r
188 unsigned short usData, usExpectedValue = 0;
\r
189 xQueueHandle *pxQueue;
\r
190 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
\r
191 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
\r
192 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
\r
193 short sError = pdFALSE;
\r
195 /* Queue a message for printing to say the task has started. */
\r
196 vPrintDisplayMessage( &pcTaskStartMsg );
\r
198 /* The queue being used is passed in as the parameter. */
\r
199 pxQueue = ( xQueueHandle * ) pvParameters;
\r
203 /* Loop until the queue is empty. */
\r
204 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
206 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
\r
208 if( usData != usExpectedValue )
\r
210 /* This is not what we expected to receive so an error has
\r
212 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
214 /* Catch-up to the value we received so our next expected value
\r
215 should again be correct. */
\r
216 usExpectedValue = usData;
\r
220 if( sError == pdFALSE )
\r
222 /* Only increment the check variable if no errors have
\r
224 ++sPollingConsumerCount;
\r
231 /* Now the queue is empty we block, allowing the producer to place more
\r
232 items in the queue. */
\r
233 vTaskDelay( xDelay );
\r
236 /*-----------------------------------------------------------*/
\r
238 /* This is called to check that all the created tasks are still running with no errors. */
\r
239 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
241 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
242 portBASE_TYPE xReturn;
\r
244 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
245 ( sLastPollingProducerCount == sPollingProducerCount )
\r
255 sLastPollingConsumerCount = sPollingConsumerCount;
\r
256 sLastPollingProducerCount = sPollingProducerCount;
\r