1 |
/* |
---|
2 |
FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd. |
---|
3 |
|
---|
4 |
This file is part of the FreeRTOS distribution. |
---|
5 |
|
---|
6 |
FreeRTOS is free software; you can redistribute it and/or modify it under |
---|
7 |
the terms of the GNU General Public License (version 2) as published by the |
---|
8 |
Free Software Foundation and modified by the FreeRTOS exception. |
---|
9 |
**NOTE** The exception to the GPL is included to allow you to distribute a |
---|
10 |
combined work that includes FreeRTOS without being obliged to provide the |
---|
11 |
source code for proprietary components outside of the FreeRTOS kernel. |
---|
12 |
Alternative commercial license and support terms are also available upon |
---|
13 |
request. See the licensing section of http://www.FreeRTOS.org for full |
---|
14 |
license details. |
---|
15 |
|
---|
16 |
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
---|
17 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
18 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
19 |
more details. |
---|
20 |
|
---|
21 |
You should have received a copy of the GNU General Public License along |
---|
22 |
with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59 |
---|
23 |
Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
---|
24 |
|
---|
25 |
|
---|
26 |
*************************************************************************** |
---|
27 |
* * |
---|
28 |
* Looking for a quick start? Then check out the FreeRTOS eBook! * |
---|
29 |
* See http://www.FreeRTOS.org/Documentation for details * |
---|
30 |
* * |
---|
31 |
*************************************************************************** |
---|
32 |
|
---|
33 |
1 tab == 4 spaces! |
---|
34 |
|
---|
35 |
Please ensure to read the configuration and relevant port sections of the |
---|
36 |
online documentation. |
---|
37 |
|
---|
38 |
http://www.FreeRTOS.org - Documentation, latest information, license and |
---|
39 |
contact details. |
---|
40 |
|
---|
41 |
http://www.SafeRTOS.com - A version that is certified for use in safety |
---|
42 |
critical systems. |
---|
43 |
|
---|
44 |
http://www.OpenRTOS.com - Commercial support, development, porting, |
---|
45 |
licensing and training services. |
---|
46 |
*/ |
---|
47 |
|
---|
48 |
|
---|
49 |
/** |
---|
50 |
* This is a very simple queue test. See the BlockQ. c documentation for a more |
---|
51 |
* comprehensive version. |
---|
52 |
* |
---|
53 |
* Creates two tasks that communicate over a single queue. One task acts as a |
---|
54 |
* producer, the other a consumer. |
---|
55 |
* |
---|
56 |
* The producer loops for three iteration, posting an incrementing number onto the |
---|
57 |
* queue each cycle. It then delays for a fixed period before doing exactly the |
---|
58 |
* same again. |
---|
59 |
* |
---|
60 |
* The consumer loops emptying the queue. Each item removed from the queue is |
---|
61 |
* checked to ensure it contains the expected value. When the queue is empty it |
---|
62 |
* blocks for a fixed period, then does the same again. |
---|
63 |
* |
---|
64 |
* All queue access is performed without blocking. The consumer completely empties |
---|
65 |
* the queue each time it runs so the producer should never find the queue full. |
---|
66 |
* |
---|
67 |
* An error is flagged if the consumer obtains an unexpected value or the producer |
---|
68 |
* find the queue is full. |
---|
69 |
* |
---|
70 |
* \page PollQC pollQ.c |
---|
71 |
* \ingroup DemoFiles |
---|
72 |
* <HR> |
---|
73 |
*/ |
---|
74 |
|
---|
75 |
/* |
---|
76 |
Changes from V2.0.0 |
---|
77 |
|
---|
78 |
+ Delay periods are now specified using variables and constants of |
---|
79 |
portTickType rather than unsigned portLONG. |
---|
80 |
*/ |
---|
81 |
|
---|
82 |
#include <stdlib.h> |
---|
83 |
|
---|
84 |
/* Scheduler include files. */ |
---|
85 |
#include "FreeRTOS.h" |
---|
86 |
#include "task.h" |
---|
87 |
#include "queue.h" |
---|
88 |
#include "print.h" |
---|
89 |
|
---|
90 |
/* Demo program include files. */ |
---|
91 |
#include "PollQ.h" |
---|
92 |
|
---|
93 |
#define pollqSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE ) |
---|
94 |
|
---|
95 |
/* The task that posts the incrementing number onto the queue. */ |
---|
96 |
static void vPolledQueueProducer( void *pvParameters ); |
---|
97 |
|
---|
98 |
/* The task that empties the queue. */ |
---|
99 |
static void vPolledQueueConsumer( void *pvParameters ); |
---|
100 |
|
---|
101 |
/* Variables that are used to check that the tasks are still running with no errors. */ |
---|
102 |
static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0; |
---|
103 |
/*-----------------------------------------------------------*/ |
---|
104 |
|
---|
105 |
void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority ) |
---|
106 |
{ |
---|
107 |
static xQueueHandle xPolledQueue; |
---|
108 |
const unsigned portBASE_TYPE uxQueueSize = 10; |
---|
109 |
|
---|
110 |
/* Create the queue used by the producer and consumer. */ |
---|
111 |
xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) ); |
---|
112 |
|
---|
113 |
/* Spawn the producer and consumer. */ |
---|
114 |
xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL ); |
---|
115 |
xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL ); |
---|
116 |
} |
---|
117 |
/*-----------------------------------------------------------*/ |
---|
118 |
|
---|
119 |
static void vPolledQueueProducer( void *pvParameters ) |
---|
120 |
{ |
---|
121 |
unsigned portSHORT usValue = 0, usLoop; |
---|
122 |
xQueueHandle *pxQueue; |
---|
123 |
const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS; |
---|
124 |
const unsigned portSHORT usNumToProduce = 3; |
---|
125 |
const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n"; |
---|
126 |
const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n"; |
---|
127 |
portSHORT sError = pdFALSE; |
---|
128 |
|
---|
129 |
/* Queue a message for printing to say the task has started. */ |
---|
130 |
vPrintDisplayMessage( &pcTaskStartMsg ); |
---|
131 |
|
---|
132 |
/* The queue being used is passed in as the parameter. */ |
---|
133 |
pxQueue = ( xQueueHandle * ) pvParameters; |
---|
134 |
|
---|
135 |
for( ;; ) |
---|
136 |
{ |
---|
137 |
for( usLoop = 0; usLoop < usNumToProduce; ++usLoop ) |
---|
138 |
{ |
---|
139 |
/* Send an incrementing number on the queue without blocking. */ |
---|
140 |
if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS ) |
---|
141 |
{ |
---|
142 |
/* We should never find the queue full - this is an error. */ |
---|
143 |
vPrintDisplayMessage( &pcTaskErrorMsg ); |
---|
144 |
sError = pdTRUE; |
---|
145 |
} |
---|
146 |
else |
---|
147 |
{ |
---|
148 |
if( sError == pdFALSE ) |
---|
149 |
{ |
---|
150 |
/* If an error has ever been recorded we stop incrementing the |
---|
151 |
check variable. */ |
---|
152 |
++sPollingProducerCount; |
---|
153 |
} |
---|
154 |
|
---|
155 |
/* Update the value we are going to post next time around. */ |
---|
156 |
++usValue; |
---|
157 |
} |
---|
158 |
} |
---|
159 |
|
---|
160 |
/* Wait before we start posting again to ensure the consumer runs and |
---|
161 |
empties the queue. */ |
---|
162 |
vTaskDelay( xDelay ); |
---|
163 |
} |
---|
164 |
} |
---|
165 |
/*-----------------------------------------------------------*/ |
---|
166 |
|
---|
167 |
static void vPolledQueueConsumer( void *pvParameters ) |
---|
168 |
{ |
---|
169 |
unsigned portSHORT usData, usExpectedValue = 0; |
---|
170 |
xQueueHandle *pxQueue; |
---|
171 |
const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS; |
---|
172 |
const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n"; |
---|
173 |
const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n"; |
---|
174 |
portSHORT sError = pdFALSE; |
---|
175 |
|
---|
176 |
/* Queue a message for printing to say the task has started. */ |
---|
177 |
vPrintDisplayMessage( &pcTaskStartMsg ); |
---|
178 |
|
---|
179 |
/* The queue being used is passed in as the parameter. */ |
---|
180 |
pxQueue = ( xQueueHandle * ) pvParameters; |
---|
181 |
|
---|
182 |
for( ;; ) |
---|
183 |
{ |
---|
184 |
/* Loop until the queue is empty. */ |
---|
185 |
while( uxQueueMessagesWaiting( *pxQueue ) ) |
---|
186 |
{ |
---|
187 |
if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS ) |
---|
188 |
{ |
---|
189 |
if( usData != usExpectedValue ) |
---|
190 |
{ |
---|
191 |
/* This is not what we expected to receive so an error has |
---|
192 |
occurred. */ |
---|
193 |
vPrintDisplayMessage( &pcTaskErrorMsg ); |
---|
194 |
sError = pdTRUE; |
---|
195 |
/* Catch-up to the value we received so our next expected value |
---|
196 |
should again be correct. */ |
---|
197 |
usExpectedValue = usData; |
---|
198 |
} |
---|
199 |
else |
---|
200 |
{ |
---|
201 |
if( sError == pdFALSE ) |
---|
202 |
{ |
---|
203 |
/* Only increment the check variable if no errors have |
---|
204 |
occurred. */ |
---|
205 |
++sPollingConsumerCount; |
---|
206 |
} |
---|
207 |
} |
---|
208 |
++usExpectedValue; |
---|
209 |
} |
---|
210 |
} |
---|
211 |
|
---|
212 |
/* Now the queue is empty we block, allowing the producer to place more |
---|
213 |
items in the queue. */ |
---|
214 |
vTaskDelay( xDelay ); |
---|
215 |
} |
---|
216 |
} |
---|
217 |
/*-----------------------------------------------------------*/ |
---|
218 |
|
---|
219 |
/* This is called to check that all the created tasks are still running with no errors. */ |
---|
220 |
portBASE_TYPE xArePollingQueuesStillRunning( void ) |
---|
221 |
{ |
---|
222 |
static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0; |
---|
223 |
portBASE_TYPE xReturn; |
---|
224 |
|
---|
225 |
if( ( sLastPollingConsumerCount == sPollingConsumerCount ) || |
---|
226 |
( sLastPollingProducerCount == sPollingProducerCount ) |
---|
227 |
) |
---|
228 |
{ |
---|
229 |
xReturn = pdFALSE; |
---|
230 |
} |
---|
231 |
else |
---|
232 |
{ |
---|
233 |
xReturn = pdTRUE; |
---|
234 |
} |
---|
235 |
|
---|
236 |
sLastPollingConsumerCount = sPollingConsumerCount; |
---|
237 |
sLastPollingProducerCount = sPollingProducerCount; |
---|
238 |
|
---|
239 |
return xReturn; |
---|
240 |
} |
---|