FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Full / PollQ.c
blobba0156cfcff31235b3581d6d56fffa7aaf7bc8c5
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
55 /**
56 * This is a very simple queue test. See the BlockQ. c documentation for a more
57 * comprehensive version.
59 * Creates two tasks that communicate over a single queue. One task acts as a
60 * producer, the other a consumer.
62 * The producer loops for three iteration, posting an incrementing number onto the
63 * queue each cycle. It then delays for a fixed period before doing exactly the
64 * same again.
66 * The consumer loops emptying the queue. Each item removed from the queue is
67 * checked to ensure it contains the expected value. When the queue is empty it
68 * blocks for a fixed period, then does the same again.
70 * All queue access is performed without blocking. The consumer completely empties
71 * the queue each time it runs so the producer should never find the queue full.
73 * An error is flagged if the consumer obtains an unexpected value or the producer
74 * find the queue is full.
76 * \page PollQC pollQ.c
77 * \ingroup DemoFiles
78 * <HR>
82 Changes from V2.0.0
84 + Delay periods are now specified using variables and constants of
85 portTickType rather than unsigned long.
88 #include <stdlib.h>
90 /* Scheduler include files. */
91 #include "FreeRTOS.h"
92 #include "task.h"
93 #include "queue.h"
94 #include "print.h"
96 /* Demo program include files. */
97 #include "PollQ.h"
99 #define pollqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
101 /* The task that posts the incrementing number onto the queue. */
102 static void vPolledQueueProducer( void *pvParameters );
104 /* The task that empties the queue. */
105 static void vPolledQueueConsumer( void *pvParameters );
107 /* Variables that are used to check that the tasks are still running with no errors. */
108 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;
109 /*-----------------------------------------------------------*/
111 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
113 static xQueueHandle xPolledQueue;
114 const unsigned portBASE_TYPE uxQueueSize = 10;
116 /* Create the queue used by the producer and consumer. */
117 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
119 /* Spawn the producer and consumer. */
120 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
121 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
123 /*-----------------------------------------------------------*/
125 static void vPolledQueueProducer( void *pvParameters )
127 unsigned short usValue = 0, usLoop;
128 xQueueHandle *pxQueue;
129 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
130 const unsigned short usNumToProduce = 3;
131 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";
132 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
133 short sError = pdFALSE;
135 /* Queue a message for printing to say the task has started. */
136 vPrintDisplayMessage( &pcTaskStartMsg );
138 /* The queue being used is passed in as the parameter. */
139 pxQueue = ( xQueueHandle * ) pvParameters;
141 for( ;; )
143 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
145 /* Send an incrementing number on the queue without blocking. */
146 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
148 /* We should never find the queue full - this is an error. */
149 vPrintDisplayMessage( &pcTaskErrorMsg );
150 sError = pdTRUE;
152 else
154 if( sError == pdFALSE )
156 /* If an error has ever been recorded we stop incrementing the
157 check variable. */
158 ++sPollingProducerCount;
161 /* Update the value we are going to post next time around. */
162 ++usValue;
166 /* Wait before we start posting again to ensure the consumer runs and
167 empties the queue. */
168 vTaskDelay( xDelay );
171 /*-----------------------------------------------------------*/
173 static void vPolledQueueConsumer( void *pvParameters )
175 unsigned short usData, usExpectedValue = 0;
176 xQueueHandle *pxQueue;
177 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;
178 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
179 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
180 short sError = pdFALSE;
182 /* Queue a message for printing to say the task has started. */
183 vPrintDisplayMessage( &pcTaskStartMsg );
185 /* The queue being used is passed in as the parameter. */
186 pxQueue = ( xQueueHandle * ) pvParameters;
188 for( ;; )
190 /* Loop until the queue is empty. */
191 while( uxQueueMessagesWaiting( *pxQueue ) )
193 if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
195 if( usData != usExpectedValue )
197 /* This is not what we expected to receive so an error has
198 occurred. */
199 vPrintDisplayMessage( &pcTaskErrorMsg );
200 sError = pdTRUE;
201 /* Catch-up to the value we received so our next expected value
202 should again be correct. */
203 usExpectedValue = usData;
205 else
207 if( sError == pdFALSE )
209 /* Only increment the check variable if no errors have
210 occurred. */
211 ++sPollingConsumerCount;
214 ++usExpectedValue;
218 /* Now the queue is empty we block, allowing the producer to place more
219 items in the queue. */
220 vTaskDelay( xDelay );
223 /*-----------------------------------------------------------*/
225 /* This is called to check that all the created tasks are still running with no errors. */
226 portBASE_TYPE xArePollingQueuesStillRunning( void )
228 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
229 portBASE_TYPE xReturn;
231 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
232 ( sLastPollingProducerCount == sPollingProducerCount )
235 xReturn = pdFALSE;
237 else
239 xReturn = pdTRUE;
242 sLastPollingConsumerCount = sPollingConsumerCount;
243 sLastPollingProducerCount = sPollingProducerCount;
245 return xReturn;