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 |
* Creates six tasks that operate on three queues as follows: |
---|
50 |
* |
---|
51 |
* The first two tasks send and receive an incrementing number to/from a queue. |
---|
52 |
* One task acts as a producer and the other as the consumer. The consumer is a |
---|
53 |
* higher priority than the producer and is set to block on queue reads. The queue |
---|
54 |
* only has space for one item - as soon as the producer posts a message on the |
---|
55 |
* queue the consumer will unblock, pre-empt the producer, and remove the item. |
---|
56 |
* |
---|
57 |
* The second two tasks work the other way around. Again the queue used only has |
---|
58 |
* enough space for one item. This time the consumer has a lower priority than the |
---|
59 |
* producer. The producer will try to post on the queue blocking when the queue is |
---|
60 |
* full. When the consumer wakes it will remove the item from the queue, causing |
---|
61 |
* the producer to unblock, pre-empt the consumer, and immediately re-fill the |
---|
62 |
* queue. |
---|
63 |
* |
---|
64 |
* The last two tasks use the same queue producer and consumer functions. This time the queue has |
---|
65 |
* enough space for lots of items and the tasks operate at the same priority. The |
---|
66 |
* producer will execute, placing items into the queue. The consumer will start |
---|
67 |
* executing when either the queue becomes full (causing the producer to block) or |
---|
68 |
* a context switch occurs (tasks of the same priority will time slice). |
---|
69 |
* |
---|
70 |
* \page BlockQC blockQ.c |
---|
71 |
* \ingroup DemoFiles |
---|
72 |
* <HR> |
---|
73 |
*/ |
---|
74 |
|
---|
75 |
/* |
---|
76 |
Changes from V1.00: |
---|
77 |
|
---|
78 |
+ Reversed the priority and block times of the second two demo tasks so |
---|
79 |
they operate as per the description above. |
---|
80 |
|
---|
81 |
Changes from V2.0.0 |
---|
82 |
|
---|
83 |
+ Delay periods are now specified using variables and constants of |
---|
84 |
portTickType rather than unsigned portLONG. |
---|
85 |
|
---|
86 |
Changes from V4.0.2 |
---|
87 |
|
---|
88 |
+ The second set of tasks were created the wrong way around. This has been |
---|
89 |
corrected. |
---|
90 |
*/ |
---|
91 |
|
---|
92 |
|
---|
93 |
#include <stdlib.h> |
---|
94 |
|
---|
95 |
/* Scheduler include files. */ |
---|
96 |
#include "FreeRTOS.h" |
---|
97 |
#include "task.h" |
---|
98 |
#include "queue.h" |
---|
99 |
|
---|
100 |
/* Demo program include files. */ |
---|
101 |
#include "BlockQ.h" |
---|
102 |
#include "print.h" |
---|
103 |
|
---|
104 |
#define blckqSTACK_SIZE ( ( unsigned portSHORT ) configMINIMAL_STACK_SIZE ) |
---|
105 |
#define blckqNUM_TASK_SETS ( 3 ) |
---|
106 |
|
---|
107 |
/* Structure used to pass parameters to the blocking queue tasks. */ |
---|
108 |
typedef struct BLOCKING_QUEUE_PARAMETERS |
---|
109 |
{ |
---|
110 |
xQueueHandle xQueue; /*< The queue to be used by the task. */ |
---|
111 |
portTickType xBlockTime; /*< The block time to use on queue reads/writes. */ |
---|
112 |
volatile portSHORT *psCheckVariable; /*< Incremented on each successful cycle to check the task is still running. */ |
---|
113 |
} xBlockingQueueParameters; |
---|
114 |
|
---|
115 |
/* Task function that creates an incrementing number and posts it on a queue. */ |
---|
116 |
static void vBlockingQueueProducer( void *pvParameters ); |
---|
117 |
|
---|
118 |
/* Task function that removes the incrementing number from a queue and checks that |
---|
119 |
it is the expected number. */ |
---|
120 |
static void vBlockingQueueConsumer( void *pvParameters ); |
---|
121 |
|
---|
122 |
/* Variables which are incremented each time an item is removed from a queue, and |
---|
123 |
found to be the expected value. |
---|
124 |
These are used to check that the tasks are still running. */ |
---|
125 |
static volatile portSHORT sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 }; |
---|
126 |
|
---|
127 |
/* Variable which are incremented each time an item is posted on a queue. These |
---|
128 |
are used to check that the tasks are still running. */ |
---|
129 |
static volatile portSHORT sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 }; |
---|
130 |
|
---|
131 |
/*-----------------------------------------------------------*/ |
---|
132 |
|
---|
133 |
void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority ) |
---|
134 |
{ |
---|
135 |
xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2; |
---|
136 |
xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4; |
---|
137 |
xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6; |
---|
138 |
const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5; |
---|
139 |
const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS; |
---|
140 |
const portTickType xDontBlock = ( portTickType ) 0; |
---|
141 |
|
---|
142 |
/* Create the first two tasks as described at the top of the file. */ |
---|
143 |
|
---|
144 |
/* First create the structure used to pass parameters to the consumer tasks. */ |
---|
145 |
pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) ); |
---|
146 |
|
---|
147 |
/* Create the queue used by the first two tasks to pass the incrementing number. |
---|
148 |
Pass a pointer to the queue in the parameter structure. */ |
---|
149 |
pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) ); |
---|
150 |
|
---|
151 |
/* The consumer is created first so gets a block time as described above. */ |
---|
152 |
pxQueueParameters1->xBlockTime = xBlockTime; |
---|
153 |
|
---|
154 |
/* Pass in the variable that this task is going to increment so we can check it |
---|
155 |
is still running. */ |
---|
156 |
pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] ); |
---|
157 |
|
---|
158 |
/* Create the structure used to pass parameters to the producer task. */ |
---|
159 |
pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) ); |
---|
160 |
|
---|
161 |
/* Pass the queue to this task also, using the parameter structure. */ |
---|
162 |
pxQueueParameters2->xQueue = pxQueueParameters1->xQueue; |
---|
163 |
|
---|
164 |
/* The producer is not going to block - as soon as it posts the consumer will |
---|
165 |
wake and remove the item so the producer should always have room to post. */ |
---|
166 |
pxQueueParameters2->xBlockTime = xDontBlock; |
---|
167 |
|
---|
168 |
/* Pass in the variable that this task is going to increment so we can check |
---|
169 |
it is still running. */ |
---|
170 |
pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] ); |
---|
171 |
|
---|
172 |
|
---|
173 |
/* Note the producer has a lower priority than the consumer when the tasks are |
---|
174 |
spawned. */ |
---|
175 |
xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL ); |
---|
176 |
xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL ); |
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 |
/* Create the second two tasks as described at the top of the file. This uses |
---|
181 |
the same mechanism but reverses the task priorities. */ |
---|
182 |
|
---|
183 |
pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) ); |
---|
184 |
pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) ); |
---|
185 |
pxQueueParameters3->xBlockTime = xDontBlock; |
---|
186 |
pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] ); |
---|
187 |
|
---|
188 |
pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) ); |
---|
189 |
pxQueueParameters4->xQueue = pxQueueParameters3->xQueue; |
---|
190 |
pxQueueParameters4->xBlockTime = xBlockTime; |
---|
191 |
pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] ); |
---|
192 |
|
---|
193 |
xTaskCreate( vBlockingQueueProducer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL ); |
---|
194 |
xTaskCreate( vBlockingQueueConsumer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL ); |
---|
195 |
|
---|
196 |
|
---|
197 |
|
---|
198 |
/* Create the last two tasks as described above. The mechanism is again just |
---|
199 |
the same. This time both parameter structures are given a block time. */ |
---|
200 |
pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) ); |
---|
201 |
pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) ); |
---|
202 |
pxQueueParameters5->xBlockTime = xBlockTime; |
---|
203 |
pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] ); |
---|
204 |
|
---|
205 |
pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) ); |
---|
206 |
pxQueueParameters6->xQueue = pxQueueParameters5->xQueue; |
---|
207 |
pxQueueParameters6->xBlockTime = xBlockTime; |
---|
208 |
pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] ); |
---|
209 |
|
---|
210 |
xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL ); |
---|
211 |
xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL ); |
---|
212 |
} |
---|
213 |
/*-----------------------------------------------------------*/ |
---|
214 |
|
---|
215 |
static void vBlockingQueueProducer( void *pvParameters ) |
---|
216 |
{ |
---|
217 |
unsigned portSHORT usValue = 0; |
---|
218 |
xBlockingQueueParameters *pxQueueParameters; |
---|
219 |
const portCHAR * const pcTaskStartMsg = "Blocking queue producer started.\r\n"; |
---|
220 |
const portCHAR * const pcTaskErrorMsg = "Could not post on blocking queue\r\n"; |
---|
221 |
portSHORT sErrorEverOccurred = pdFALSE; |
---|
222 |
|
---|
223 |
pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters; |
---|
224 |
|
---|
225 |
/* Queue a message for printing to say the task has started. */ |
---|
226 |
vPrintDisplayMessage( &pcTaskStartMsg ); |
---|
227 |
|
---|
228 |
for( ;; ) |
---|
229 |
{ |
---|
230 |
if( xQueueSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS ) |
---|
231 |
{ |
---|
232 |
vPrintDisplayMessage( &pcTaskErrorMsg ); |
---|
233 |
sErrorEverOccurred = pdTRUE; |
---|
234 |
} |
---|
235 |
else |
---|
236 |
{ |
---|
237 |
/* We have successfully posted a message, so increment the variable |
---|
238 |
used to check we are still running. */ |
---|
239 |
if( sErrorEverOccurred == pdFALSE ) |
---|
240 |
{ |
---|
241 |
( *pxQueueParameters->psCheckVariable )++; |
---|
242 |
} |
---|
243 |
|
---|
244 |
/* Increment the variable we are going to post next time round. The |
---|
245 |
consumer will expect the numbers to follow in numerical order. */ |
---|
246 |
++usValue; |
---|
247 |
} |
---|
248 |
} |
---|
249 |
} |
---|
250 |
/*-----------------------------------------------------------*/ |
---|
251 |
|
---|
252 |
static void vBlockingQueueConsumer( void *pvParameters ) |
---|
253 |
{ |
---|
254 |
unsigned portSHORT usData, usExpectedValue = 0; |
---|
255 |
xBlockingQueueParameters *pxQueueParameters; |
---|
256 |
const portCHAR * const pcTaskStartMsg = "Blocking queue consumer started.\r\n"; |
---|
257 |
const portCHAR * const pcTaskErrorMsg = "Incorrect value received on blocking queue.\r\n"; |
---|
258 |
portSHORT sErrorEverOccurred = pdFALSE; |
---|
259 |
|
---|
260 |
/* Queue a message for printing to say the task has started. */ |
---|
261 |
vPrintDisplayMessage( &pcTaskStartMsg ); |
---|
262 |
|
---|
263 |
pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters; |
---|
264 |
|
---|
265 |
for( ;; ) |
---|
266 |
{ |
---|
267 |
if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS ) |
---|
268 |
{ |
---|
269 |
if( usData != usExpectedValue ) |
---|
270 |
{ |
---|
271 |
vPrintDisplayMessage( &pcTaskErrorMsg ); |
---|
272 |
|
---|
273 |
/* Catch-up. */ |
---|
274 |
usExpectedValue = usData; |
---|
275 |
|
---|
276 |
sErrorEverOccurred = pdTRUE; |
---|
277 |
} |
---|
278 |
else |
---|
279 |
{ |
---|
280 |
/* We have successfully received a message, so increment the |
---|
281 |
variable used to check we are still running. */ |
---|
282 |
if( sErrorEverOccurred == pdFALSE ) |
---|
283 |
{ |
---|
284 |
( *pxQueueParameters->psCheckVariable )++; |
---|
285 |
} |
---|
286 |
|
---|
287 |
/* Increment the value we expect to remove from the queue next time |
---|
288 |
round. */ |
---|
289 |
++usExpectedValue; |
---|
290 |
} |
---|
291 |
} |
---|
292 |
} |
---|
293 |
} |
---|
294 |
/*-----------------------------------------------------------*/ |
---|
295 |
|
---|
296 |
/* This is called to check that all the created tasks are still running. */ |
---|
297 |
portBASE_TYPE xAreBlockingQueuesStillRunning( void ) |
---|
298 |
{ |
---|
299 |
static portSHORT sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 }; |
---|
300 |
static portSHORT sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( portSHORT ) 0, ( portSHORT ) 0, ( portSHORT ) 0 }; |
---|
301 |
portBASE_TYPE xReturn = pdPASS, xTasks; |
---|
302 |
|
---|
303 |
/* Not too worried about mutual exclusion on these variables as they are 16 |
---|
304 |
bits and we are only reading them. We also only care to see if they have |
---|
305 |
changed or not. |
---|
306 |
|
---|
307 |
Loop through each check variable and return pdFALSE if any are found not |
---|
308 |
to have changed since the last call. */ |
---|
309 |
|
---|
310 |
for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ ) |
---|
311 |
{ |
---|
312 |
if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ] ) |
---|
313 |
{ |
---|
314 |
xReturn = pdFALSE; |
---|
315 |
} |
---|
316 |
sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ]; |
---|
317 |
|
---|
318 |
|
---|
319 |
if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ] ) |
---|
320 |
{ |
---|
321 |
xReturn = pdFALSE; |
---|
322 |
} |
---|
323 |
sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ]; |
---|
324 |
} |
---|
325 |
|
---|
326 |
return xReturn; |
---|
327 |
} |
---|
328 |
|
---|