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