]> begriffs open source - freertos/blob - Demo/Common/Minimal/AltBlckQ.c
Update to V5.1.2.
[freertos] / Demo / Common / Minimal / AltBlckQ.c
1 /*\r
2         FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
30         *                                                                         *\r
31         * This is a concise, step by step, 'hands on' guide that describes both   *\r
32         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
33         * explains numerous examples that are written using the FreeRTOS API.     *\r
34         * Full source code for all the examples is provided in an accompanying    *\r
35         * .zip file.                                                              *\r
36     *                                                                         *\r
37     ***************************************************************************\r
38     ***************************************************************************\r
39 \r
40         Please ensure to read the configuration and relevant port sections of the\r
41         online documentation.\r
42 \r
43         http://www.FreeRTOS.org - Documentation, latest information, license and \r
44         contact details.\r
45 \r
46         http://www.SafeRTOS.com - A version that is certified for use in safety \r
47         critical systems.\r
48 \r
49         http://www.OpenRTOS.com - Commercial support, development, porting, \r
50         licensing and training services.\r
51 */\r
52 \r
53 /*\r
54  * This is a version of BlockQ.c that uses the alternative (Alt) API.\r
55  *\r
56  * Creates six tasks that operate on three queues as follows:\r
57  *\r
58  * The first two tasks send and receive an incrementing number to/from a queue.\r
59  * One task acts as a producer and the other as the consumer.  The consumer is a\r
60  * higher priority than the producer and is set to block on queue reads.  The queue\r
61  * only has space for one item - as soon as the producer posts a message on the\r
62  * queue the consumer will unblock, pre-empt the producer, and remove the item.\r
63  *\r
64  * The second two tasks work the other way around.  Again the queue used only has\r
65  * enough space for one item.  This time the consumer has a lower priority than the\r
66  * producer.  The producer will try to post on the queue blocking when the queue is\r
67  * full.  When the consumer wakes it will remove the item from the queue, causing\r
68  * the producer to unblock, pre-empt the consumer, and immediately re-fill the\r
69  * queue.\r
70  *\r
71  * The last two tasks use the same queue producer and consumer functions.  This time the queue has\r
72  * enough space for lots of items and the tasks operate at the same priority.  The\r
73  * producer will execute, placing items into the queue.  The consumer will start\r
74  * executing when either the queue becomes full (causing the producer to block) or\r
75  * a context switch occurs (tasks of the same priority will time slice).\r
76  *\r
77  */\r
78 \r
79 \r
80 #include <stdlib.h>\r
81 \r
82 /* Scheduler include files. */\r
83 #include "FreeRTOS.h"\r
84 #include "task.h"\r
85 #include "queue.h"\r
86 \r
87 /* Demo program include files. */\r
88 #include "AltBlckQ.h"\r
89 \r
90 #define blckqSTACK_SIZE         configMINIMAL_STACK_SIZE\r
91 #define blckqNUM_TASK_SETS      ( 3 )\r
92 \r
93 /* Structure used to pass parameters to the blocking queue tasks. */\r
94 typedef struct BLOCKING_QUEUE_PARAMETERS\r
95 {\r
96         xQueueHandle xQueue;                                    /*< The queue to be used by the task. */\r
97         portTickType xBlockTime;                                /*< The block time to use on queue reads/writes. */\r
98         volatile portSHORT *psCheckVariable;    /*< Incremented on each successful cycle to check the task is still running. */\r
99 } xBlockingQueueParameters;\r
100 \r
101 /* Task function that creates an incrementing number and posts it on a queue. */\r
102 static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );\r
103 \r
104 /* Task function that removes the incrementing number from a queue and checks that\r
105 it is the expected number. */\r
106 static portTASK_FUNCTION_PROTO( vBlockingQueueConsumer, pvParameters );\r
107 \r
108 /* Variables which are incremented each time an item is removed from a queue, and\r
109 found to be the expected value.\r
110 These are used to check that the tasks are still running. */\r
111 static volatile portSHORT sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };\r
112 \r
113 /* Variable which are incremented each time an item is posted on a queue.   These\r
114 are used to check that the tasks are still running. */\r
115 static volatile portSHORT sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vStartAltBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )\r
120 {\r
121 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;\r
122 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;\r
123 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;\r
124 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;\r
125 const portTickType xBlockTime = ( portTickType ) 1000 / portTICK_RATE_MS;\r
126 const portTickType xDontBlock = ( portTickType ) 0;\r
127 \r
128         /* Create the first two tasks as described at the top of the file. */\r
129         \r
130         /* First create the structure used to pass parameters to the consumer tasks. */\r
131         pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
132 \r
133         /* Create the queue used by the first two tasks to pass the incrementing number.\r
134         Pass a pointer to the queue in the parameter structure. */\r
135         pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
136 \r
137         /* The consumer is created first so gets a block time as described above. */\r
138         pxQueueParameters1->xBlockTime = xBlockTime;\r
139 \r
140         /* Pass in the variable that this task is going to increment so we can check it\r
141         is still running. */\r
142         pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );\r
143                 \r
144         /* Create the structure used to pass parameters to the producer task. */\r
145         pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
146 \r
147         /* Pass the queue to this task also, using the parameter structure. */\r
148         pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;\r
149 \r
150         /* The producer is not going to block - as soon as it posts the consumer will\r
151         wake and remove the item so the producer should always have room to post. */\r
152         pxQueueParameters2->xBlockTime = xDontBlock;\r
153 \r
154         /* Pass in the variable that this task is going to increment so we can check\r
155         it is still running. */\r
156         pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );\r
157 \r
158 \r
159         /* Note the producer has a lower priority than the consumer when the tasks are\r
160         spawned. */\r
161         xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );\r
162         xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );\r
163 \r
164         \r
165 \r
166         /* Create the second two tasks as described at the top of the file.   This uses\r
167         the same mechanism but reverses the task priorities. */\r
168 \r
169         pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
170         pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
171         pxQueueParameters3->xBlockTime = xDontBlock;\r
172         pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );\r
173 \r
174         pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
175         pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;\r
176         pxQueueParameters4->xBlockTime = xBlockTime;\r
177         pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );\r
178 \r
179         xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );\r
180         xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );\r
181 \r
182 \r
183 \r
184         /* Create the last two tasks as described above.  The mechanism is again just\r
185         the same.  This time both parameter structures are given a block time. */\r
186         pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
187         pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
188         pxQueueParameters5->xBlockTime = xBlockTime;\r
189         pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );\r
190 \r
191         pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
192         pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;\r
193         pxQueueParameters6->xBlockTime = xBlockTime;\r
194         pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] ); \r
195 \r
196         xTaskCreate( vBlockingQueueProducer, ( signed portCHAR * ) "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );\r
197         xTaskCreate( vBlockingQueueConsumer, ( signed portCHAR * ) "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )\r
202 {\r
203 unsigned portSHORT usValue = 0;\r
204 xBlockingQueueParameters *pxQueueParameters;\r
205 portSHORT sErrorEverOccurred = pdFALSE;\r
206 \r
207         #ifdef USE_STDIO\r
208         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
209         \r
210                 const portCHAR * const pcTaskStartMsg = "Alt blocking queue producer task started.\r\n";\r
211 \r
212                 /* Queue a message for printing to say the task has started. */\r
213                 vPrintDisplayMessage( &pcTaskStartMsg );\r
214         #endif\r
215 \r
216         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
217 \r
218         for( ;; )\r
219         {               \r
220                 if( xQueueAltSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )\r
221                 {\r
222                         sErrorEverOccurred = pdTRUE;\r
223                 }\r
224                 else\r
225                 {\r
226                         /* We have successfully posted a message, so increment the variable\r
227                         used to check we are still running. */\r
228                         if( sErrorEverOccurred == pdFALSE )\r
229                         {\r
230                                 ( *pxQueueParameters->psCheckVariable )++;\r
231                         }\r
232 \r
233                         /* Increment the variable we are going to post next time round.  The\r
234                         consumer will expect the numbers to     follow in numerical order. */\r
235                         ++usValue;\r
236                 }\r
237         }\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )\r
242 {\r
243 unsigned portSHORT usData, usExpectedValue = 0;\r
244 xBlockingQueueParameters *pxQueueParameters;\r
245 portSHORT sErrorEverOccurred = pdFALSE;\r
246 \r
247         #ifdef USE_STDIO\r
248         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
249         \r
250                 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";\r
251 \r
252                 /* Queue a message for printing to say the task has started. */\r
253                 vPrintDisplayMessage( &pcTaskStartMsg );\r
254         #endif\r
255 \r
256         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
257 \r
258         for( ;; )\r
259         {       \r
260                 if( xQueueAltReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )\r
261                 {\r
262                         if( usData != usExpectedValue )\r
263                         {\r
264                                 /* Catch-up. */\r
265                                 usExpectedValue = usData;\r
266 \r
267                                 sErrorEverOccurred = pdTRUE;\r
268                         }\r
269                         else\r
270                         {\r
271                                 /* We have successfully received a message, so increment the\r
272                                 variable used to check we are still running. */ \r
273                                 if( sErrorEverOccurred == pdFALSE )\r
274                                 {\r
275                                         ( *pxQueueParameters->psCheckVariable )++;\r
276                                 }\r
277                                                         \r
278                                 /* Increment the value we expect to remove from the queue next time\r
279                                 round. */\r
280                                 ++usExpectedValue;\r
281                         }                       \r
282                 }               \r
283         }\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 /* This is called to check that all the created tasks are still running. */\r
288 portBASE_TYPE xAreAltBlockingQueuesStillRunning( void )\r
289 {\r
290 static portSHORT sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };\r
291 static portSHORT sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0, ( unsigned portSHORT ) 0 };\r
292 portBASE_TYPE xReturn = pdPASS, xTasks;\r
293 \r
294         /* Not too worried about mutual exclusion on these variables as they are 16\r
295         bits and we are only reading them. We also only care to see if they have\r
296         changed or not.\r
297         \r
298         Loop through each check variable to and return pdFALSE if any are found not\r
299         to have changed since the last call. */\r
300 \r
301         for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )\r
302         {\r
303                 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )\r
304                 {\r
305                         xReturn = pdFALSE;\r
306                 }\r
307                 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];\r
308 \r
309 \r
310                 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )\r
311                 {\r
312                         xReturn = pdFALSE;\r
313                 }\r
314                 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];\r
315         }\r
316 \r
317         return xReturn;\r
318 }\r
319 \r