1 |
606 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
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 books - available as PDF or paperback *
|
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 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
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.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/**
|
56 |
|
|
* This is a very simple queue test. See the BlockQ. c documentation for a more
|
57 |
|
|
* comprehensive version.
|
58 |
|
|
*
|
59 |
|
|
* Creates two tasks that communicate over a single queue. One task acts as a
|
60 |
|
|
* producer, the other a consumer.
|
61 |
|
|
*
|
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.
|
65 |
|
|
*
|
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.
|
69 |
|
|
*
|
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.
|
72 |
|
|
*
|
73 |
|
|
* An error is flagged if the consumer obtains an unexpected value or the producer
|
74 |
|
|
* find the queue is full.
|
75 |
|
|
*
|
76 |
|
|
* \page PollQC pollQ.c
|
77 |
|
|
* \ingroup DemoFiles
|
78 |
|
|
* <HR>
|
79 |
|
|
*/
|
80 |
|
|
|
81 |
|
|
/*
|
82 |
|
|
Changes from V2.0.0
|
83 |
|
|
|
84 |
|
|
+ Delay periods are now specified using variables and constants of
|
85 |
|
|
portTickType rather than unsigned long.
|
86 |
|
|
*/
|
87 |
|
|
|
88 |
|
|
#include <stdlib.h>
|
89 |
|
|
|
90 |
|
|
/* Scheduler include files. */
|
91 |
|
|
#include "FreeRTOS.h"
|
92 |
|
|
#include "task.h"
|
93 |
|
|
#include "queue.h"
|
94 |
|
|
#include "print.h"
|
95 |
|
|
|
96 |
|
|
/* Demo program include files. */
|
97 |
|
|
#include "PollQ.h"
|
98 |
|
|
|
99 |
|
|
#define pollqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
|
100 |
|
|
|
101 |
|
|
/* The task that posts the incrementing number onto the queue. */
|
102 |
|
|
static void vPolledQueueProducer( void *pvParameters );
|
103 |
|
|
|
104 |
|
|
/* The task that empties the queue. */
|
105 |
|
|
static void vPolledQueueConsumer( void *pvParameters );
|
106 |
|
|
|
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 |
|
|
/*-----------------------------------------------------------*/
|
110 |
|
|
|
111 |
|
|
void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
|
112 |
|
|
{
|
113 |
|
|
static xQueueHandle xPolledQueue;
|
114 |
|
|
const unsigned portBASE_TYPE uxQueueSize = 10;
|
115 |
|
|
|
116 |
|
|
/* Create the queue used by the producer and consumer. */
|
117 |
|
|
xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
|
118 |
|
|
|
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 );
|
122 |
|
|
}
|
123 |
|
|
/*-----------------------------------------------------------*/
|
124 |
|
|
|
125 |
|
|
static void vPolledQueueProducer( void *pvParameters )
|
126 |
|
|
{
|
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;
|
134 |
|
|
|
135 |
|
|
/* Queue a message for printing to say the task has started. */
|
136 |
|
|
vPrintDisplayMessage( &pcTaskStartMsg );
|
137 |
|
|
|
138 |
|
|
/* The queue being used is passed in as the parameter. */
|
139 |
|
|
pxQueue = ( xQueueHandle * ) pvParameters;
|
140 |
|
|
|
141 |
|
|
for( ;; )
|
142 |
|
|
{
|
143 |
|
|
for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
|
144 |
|
|
{
|
145 |
|
|
/* Send an incrementing number on the queue without blocking. */
|
146 |
|
|
if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )
|
147 |
|
|
{
|
148 |
|
|
/* We should never find the queue full - this is an error. */
|
149 |
|
|
vPrintDisplayMessage( &pcTaskErrorMsg );
|
150 |
|
|
sError = pdTRUE;
|
151 |
|
|
}
|
152 |
|
|
else
|
153 |
|
|
{
|
154 |
|
|
if( sError == pdFALSE )
|
155 |
|
|
{
|
156 |
|
|
/* If an error has ever been recorded we stop incrementing the
|
157 |
|
|
check variable. */
|
158 |
|
|
++sPollingProducerCount;
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
/* Update the value we are going to post next time around. */
|
162 |
|
|
++usValue;
|
163 |
|
|
}
|
164 |
|
|
}
|
165 |
|
|
|
166 |
|
|
/* Wait before we start posting again to ensure the consumer runs and
|
167 |
|
|
empties the queue. */
|
168 |
|
|
vTaskDelay( xDelay );
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
/*-----------------------------------------------------------*/
|
172 |
|
|
|
173 |
|
|
static void vPolledQueueConsumer( void *pvParameters )
|
174 |
|
|
{
|
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;
|
181 |
|
|
|
182 |
|
|
/* Queue a message for printing to say the task has started. */
|
183 |
|
|
vPrintDisplayMessage( &pcTaskStartMsg );
|
184 |
|
|
|
185 |
|
|
/* The queue being used is passed in as the parameter. */
|
186 |
|
|
pxQueue = ( xQueueHandle * ) pvParameters;
|
187 |
|
|
|
188 |
|
|
for( ;; )
|
189 |
|
|
{
|
190 |
|
|
/* Loop until the queue is empty. */
|
191 |
|
|
while( uxQueueMessagesWaiting( *pxQueue ) )
|
192 |
|
|
{
|
193 |
|
|
if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )
|
194 |
|
|
{
|
195 |
|
|
if( usData != usExpectedValue )
|
196 |
|
|
{
|
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;
|
204 |
|
|
}
|
205 |
|
|
else
|
206 |
|
|
{
|
207 |
|
|
if( sError == pdFALSE )
|
208 |
|
|
{
|
209 |
|
|
/* Only increment the check variable if no errors have
|
210 |
|
|
occurred. */
|
211 |
|
|
++sPollingConsumerCount;
|
212 |
|
|
}
|
213 |
|
|
}
|
214 |
|
|
++usExpectedValue;
|
215 |
|
|
}
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
/* Now the queue is empty we block, allowing the producer to place more
|
219 |
|
|
items in the queue. */
|
220 |
|
|
vTaskDelay( xDelay );
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
/*-----------------------------------------------------------*/
|
224 |
|
|
|
225 |
|
|
/* This is called to check that all the created tasks are still running with no errors. */
|
226 |
|
|
portBASE_TYPE xArePollingQueuesStillRunning( void )
|
227 |
|
|
{
|
228 |
|
|
static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
|
229 |
|
|
portBASE_TYPE xReturn;
|
230 |
|
|
|
231 |
|
|
if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
|
232 |
|
|
( sLastPollingProducerCount == sPollingProducerCount )
|
233 |
|
|
)
|
234 |
|
|
{
|
235 |
|
|
xReturn = pdFALSE;
|
236 |
|
|
}
|
237 |
|
|
else
|
238 |
|
|
{
|
239 |
|
|
xReturn = pdTRUE;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
sLastPollingConsumerCount = sPollingConsumerCount;
|
243 |
|
|
sLastPollingProducerCount = sPollingProducerCount;
|
244 |
|
|
|
245 |
|
|
return xReturn;
|
246 |
|
|
}
|