]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/BlockQ.c
Update to V6.1.1
[freertos] / Demo / Common / Full / BlockQ.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /**\r
55  * Creates six tasks that operate on three queues as follows:\r
56  *\r
57  * The first two tasks send and receive an incrementing number to/from a queue.  \r
58  * One task acts as a producer and the other as the consumer.  The consumer is a \r
59  * higher priority than the producer and is set to block on queue reads.  The queue \r
60  * only has space for one item - as soon as the producer posts a message on the \r
61  * queue the consumer will unblock, pre-empt the producer, and remove the item.\r
62  * \r
63  * The second two tasks work the other way around.  Again the queue used only has\r
64  * enough space for one item.  This time the consumer has a lower priority than the \r
65  * producer.  The producer will try to post on the queue blocking when the queue is \r
66  * full.  When the consumer wakes it will remove the item from the queue, causing \r
67  * the producer to unblock, pre-empt the consumer, and immediately re-fill the \r
68  * queue.\r
69  * \r
70  * The last two tasks use the same queue producer and consumer functions.  This time the queue has\r
71  * enough space for lots of items and the tasks operate at the same priority.  The \r
72  * producer will execute, placing items into the queue.  The consumer will start \r
73  * executing when either the queue becomes full (causing the producer to block) or \r
74  * a context switch occurs (tasks of the same priority will time slice).\r
75  *\r
76  * \page BlockQC blockQ.c\r
77  * \ingroup DemoFiles\r
78  * <HR>\r
79  */\r
80 \r
81 /*\r
82 Changes from V1.00:\r
83         \r
84         + Reversed the priority and block times of the second two demo tasks so\r
85           they operate as per the description above.\r
86 \r
87 Changes from V2.0.0\r
88 \r
89         + Delay periods are now specified using variables and constants of\r
90           portTickType rather than unsigned long.\r
91 \r
92 Changes from V4.0.2\r
93 \r
94         + The second set of tasks were created the wrong way around.  This has been\r
95           corrected.\r
96 */\r
97 \r
98 \r
99 #include <stdlib.h>\r
100 \r
101 /* Scheduler include files. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 #include "queue.h"\r
105 \r
106 /* Demo program include files. */\r
107 #include "BlockQ.h"\r
108 #include "print.h"\r
109 \r
110 #define blckqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
111 #define blckqNUM_TASK_SETS      ( 3 )\r
112 \r
113 /* Structure used to pass parameters to the blocking queue tasks. */\r
114 typedef struct BLOCKING_QUEUE_PARAMETERS\r
115 {\r
116         xQueueHandle xQueue;                                    /*< The queue to be used by the task. */\r
117         portTickType xBlockTime;                        /*< The block time to use on queue reads/writes. */\r
118         volatile short *psCheckVariable;        /*< Incremented on each successful cycle to check the task is still running. */\r
119 } xBlockingQueueParameters;\r
120 \r
121 /* Task function that creates an incrementing number and posts it on a queue. */\r
122 static void vBlockingQueueProducer( void *pvParameters );\r
123 \r
124 /* Task function that removes the incrementing number from a queue and checks that \r
125 it is the expected number. */\r
126 static void vBlockingQueueConsumer( void *pvParameters );\r
127 \r
128 /* Variables which are incremented each time an item is removed from a queue, and \r
129 found to be the expected value. \r
130 These are used to check that the tasks are still running. */\r
131 static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
132 \r
133 /* Variable which are incremented each time an item is posted on a queue.   These \r
134 are used to check that the tasks are still running. */\r
135 static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )\r
140 {\r
141 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;\r
142 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;\r
143 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;\r
144 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;\r
145 const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;\r
146 const portTickType xDontBlock = ( portTickType ) 0;\r
147 \r
148         /* Create the first two tasks as described at the top of the file. */ \r
149         \r
150         /* First create the structure used to pass parameters to the consumer tasks. */\r
151         pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
152 \r
153         /* Create the queue used by the first two tasks to pass the incrementing number.  \r
154         Pass a pointer to the queue in the parameter structure. */\r
155         pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
156 \r
157         /* The consumer is created first so gets a block time as described above. */\r
158         pxQueueParameters1->xBlockTime = xBlockTime;\r
159 \r
160         /* Pass in the variable that this task is going to increment so we can check it \r
161         is still running. */\r
162         pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );\r
163                 \r
164         /* Create the structure used to pass parameters to the producer task. */\r
165         pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
166 \r
167         /* Pass the queue to this task also, using the parameter structure. */\r
168         pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;\r
169 \r
170         /* The producer is not going to block - as soon as it posts the consumer will \r
171         wake and remove the item so the producer should always have room to post. */\r
172         pxQueueParameters2->xBlockTime = xDontBlock;\r
173 \r
174         /* Pass in the variable that this task is going to increment so we can check \r
175         it is still running. */\r
176         pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );\r
177 \r
178 \r
179         /* Note the producer has a lower priority than the consumer when the tasks are \r
180         spawned. */\r
181         xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );\r
182         xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );\r
183 \r
184         \r
185 \r
186         /* Create the second two tasks as described at the top of the file.   This uses \r
187         the same mechanism but reverses the task priorities. */\r
188 \r
189         pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
190         pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
191         pxQueueParameters3->xBlockTime = xDontBlock;\r
192         pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );\r
193 \r
194         pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
195         pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;\r
196         pxQueueParameters4->xBlockTime = xBlockTime;\r
197         pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );\r
198 \r
199         xTaskCreate( vBlockingQueueProducer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );\r
200         xTaskCreate( vBlockingQueueConsumer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );\r
201 \r
202 \r
203 \r
204         /* Create the last two tasks as described above.  The mechanism is again just \r
205         the same.  This time both parameter structures are given a block time. */\r
206         pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
207         pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
208         pxQueueParameters5->xBlockTime = xBlockTime;\r
209         pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );\r
210 \r
211         pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
212         pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;\r
213         pxQueueParameters6->xBlockTime = xBlockTime;\r
214         pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] ); \r
215 \r
216         xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );\r
217         xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void vBlockingQueueProducer( void *pvParameters )\r
222 {\r
223 unsigned short usValue = 0;\r
224 xBlockingQueueParameters *pxQueueParameters;\r
225 const char * const pcTaskStartMsg = "Blocking queue producer started.\r\n";\r
226 const char * const pcTaskErrorMsg = "Could not post on blocking queue\r\n";\r
227 short sErrorEverOccurred = pdFALSE;\r
228 \r
229         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
230 \r
231         /* Queue a message for printing to say the task has started. */\r
232         vPrintDisplayMessage( &pcTaskStartMsg );\r
233 \r
234         for( ;; )\r
235         {               \r
236                 if( xQueueSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )\r
237                 {\r
238                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
239                         sErrorEverOccurred = pdTRUE;\r
240                 }\r
241                 else\r
242                 {\r
243                         /* We have successfully posted a message, so increment the variable \r
244                         used to check we are still running. */\r
245                         if( sErrorEverOccurred == pdFALSE )\r
246                         {\r
247                                 ( *pxQueueParameters->psCheckVariable )++;\r
248                         }\r
249 \r
250                         /* Increment the variable we are going to post next time round.  The \r
251                         consumer will expect the numbers to     follow in numerical order. */\r
252                         ++usValue;\r
253                 }\r
254         }\r
255 }\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 static void vBlockingQueueConsumer( void *pvParameters )\r
259 {\r
260 unsigned short usData, usExpectedValue = 0;\r
261 xBlockingQueueParameters *pxQueueParameters;\r
262 const char * const pcTaskStartMsg = "Blocking queue consumer started.\r\n";\r
263 const char * const pcTaskErrorMsg = "Incorrect value received on blocking queue.\r\n";\r
264 short sErrorEverOccurred = pdFALSE;\r
265 \r
266         /* Queue a message for printing to say the task has started. */\r
267         vPrintDisplayMessage( &pcTaskStartMsg );\r
268 \r
269         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
270 \r
271         for( ;; )\r
272         {       \r
273                 if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )\r
274                 {\r
275                         if( usData != usExpectedValue )\r
276                         {\r
277                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
278 \r
279                                 /* Catch-up. */\r
280                                 usExpectedValue = usData;\r
281 \r
282                                 sErrorEverOccurred = pdTRUE;\r
283                         }\r
284                         else\r
285                         {\r
286                                 /* We have successfully received a message, so increment the \r
287                                 variable used to check we are still running. */ \r
288                                 if( sErrorEverOccurred == pdFALSE )\r
289                                 {\r
290                                         ( *pxQueueParameters->psCheckVariable )++;\r
291                                 }\r
292                                                         \r
293                                 /* Increment the value we expect to remove from the queue next time \r
294                                 round. */\r
295                                 ++usExpectedValue;\r
296                         }                       \r
297                 }               \r
298         }\r
299 }\r
300 /*-----------------------------------------------------------*/\r
301 \r
302 /* This is called to check that all the created tasks are still running. */\r
303 portBASE_TYPE xAreBlockingQueuesStillRunning( void )\r
304 {\r
305 static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
306 static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };\r
307 portBASE_TYPE xReturn = pdPASS, xTasks;\r
308 \r
309         /* Not too worried about mutual exclusion on these variables as they are 16 \r
310         bits and we are only reading them. We also only care to see if they have \r
311         changed or not.\r
312         \r
313         Loop through each check variable and return pdFALSE if any are found not \r
314         to have changed since the last call. */\r
315 \r
316         for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )\r
317         {\r
318                 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )\r
319                 {\r
320                         xReturn = pdFALSE;\r
321                 }\r
322                 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];\r
323 \r
324 \r
325                 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )\r
326                 {\r
327                         xReturn = pdFALSE;\r
328                 }\r
329                 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];\r
330         }\r
331 \r
332         return xReturn;\r
333 }\r
334 \r