]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/AltBlckQ.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / Common / Minimal / AltBlckQ.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /*\r
97  * This is a version of BlockQ.c that uses the alternative (Alt) API.\r
98  *\r
99  * Creates six tasks that operate on three queues as follows:\r
100  *\r
101  * The first two tasks send and receive an incrementing number to/from a queue.\r
102  * One task acts as a producer and the other as the consumer.  The consumer is a\r
103  * higher priority than the producer and is set to block on queue reads.  The queue\r
104  * only has space for one item - as soon as the producer posts a message on the\r
105  * queue the consumer will unblock, pre-empt the producer, and remove the item.\r
106  *\r
107  * The second two tasks work the other way around.  Again the queue used only has\r
108  * enough space for one item.  This time the consumer has a lower priority than the\r
109  * producer.  The producer will try to post on the queue blocking when the queue is\r
110  * full.  When the consumer wakes it will remove the item from the queue, causing\r
111  * the producer to unblock, pre-empt the consumer, and immediately re-fill the\r
112  * queue.\r
113  *\r
114  * The last two tasks use the same queue producer and consumer functions.  This time the queue has\r
115  * enough space for lots of items and the tasks operate at the same priority.  The\r
116  * producer will execute, placing items into the queue.  The consumer will start\r
117  * executing when either the queue becomes full (causing the producer to block) or\r
118  * a context switch occurs (tasks of the same priority will time slice).\r
119  *\r
120  */\r
121 \r
122 \r
123 #include <stdlib.h>\r
124 \r
125 /* Scheduler include files. */\r
126 #include "FreeRTOS.h"\r
127 #include "task.h"\r
128 #include "queue.h"\r
129 \r
130 /* Demo program include files. */\r
131 #include "AltBlckQ.h"\r
132 \r
133 #define blckqSTACK_SIZE         configMINIMAL_STACK_SIZE\r
134 #define blckqNUM_TASK_SETS      ( 3 )\r
135 \r
136 /* Structure used to pass parameters to the blocking queue tasks. */\r
137 typedef struct BLOCKING_QUEUE_PARAMETERS\r
138 {\r
139         QueueHandle_t xQueue;                                   /*< The queue to be used by the task. */\r
140         TickType_t xBlockTime;                          /*< The block time to use on queue reads/writes. */\r
141         volatile short *psCheckVariable;        /*< Incremented on each successful cycle to check the task is still running. */\r
142 } xBlockingQueueParameters;\r
143 \r
144 /* Task function that creates an incrementing number and posts it on a queue. */\r
145 static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );\r
146 \r
147 /* Task function that removes the incrementing number from a queue and checks that\r
148 it is the expected number. */\r
149 static portTASK_FUNCTION_PROTO( vBlockingQueueConsumer, pvParameters );\r
150 \r
151 /* Variables which are incremented each time an item is removed from a queue, and\r
152 found to be the expected value.\r
153 These are used to check that the tasks are still running. */\r
154 static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
155 \r
156 /* Variable which are incremented each time an item is posted on a queue.   These\r
157 are used to check that the tasks are still running. */\r
158 static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vStartAltBlockingQueueTasks( UBaseType_t uxPriority )\r
163 {\r
164 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;\r
165 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;\r
166 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;\r
167 const UBaseType_t uxQueueSize1 = 1, uxQueueSize5 = 5;\r
168 const TickType_t xBlockTime = ( TickType_t ) 1000 / portTICK_PERIOD_MS;\r
169 const TickType_t xDontBlock = ( TickType_t ) 0;\r
170 \r
171         /* Create the first two tasks as described at the top of the file. */\r
172         \r
173         /* First create the structure used to pass parameters to the consumer tasks. */\r
174         pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
175 \r
176         /* Create the queue used by the first two tasks to pass the incrementing number.\r
177         Pass a pointer to the queue in the parameter structure. */\r
178         pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( UBaseType_t ) sizeof( uint16_t ) );\r
179 \r
180         /* The consumer is created first so gets a block time as described above. */\r
181         pxQueueParameters1->xBlockTime = xBlockTime;\r
182 \r
183         /* Pass in the variable that this task is going to increment so we can check it\r
184         is still running. */\r
185         pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );\r
186                 \r
187         /* Create the structure used to pass parameters to the producer task. */\r
188         pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
189 \r
190         /* Pass the queue to this task also, using the parameter structure. */\r
191         pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;\r
192 \r
193         /* The producer is not going to block - as soon as it posts the consumer will\r
194         wake and remove the item so the producer should always have room to post. */\r
195         pxQueueParameters2->xBlockTime = xDontBlock;\r
196 \r
197         /* Pass in the variable that this task is going to increment so we can check\r
198         it is still running. */\r
199         pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );\r
200 \r
201 \r
202         /* Note the producer has a lower priority than the consumer when the tasks are\r
203         spawned. */\r
204         xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );\r
205         xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );\r
206 \r
207         \r
208 \r
209         /* Create the second two tasks as described at the top of the file.   This uses\r
210         the same mechanism but reverses the task priorities. */\r
211 \r
212         pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
213         pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( UBaseType_t ) sizeof( uint16_t ) );\r
214         pxQueueParameters3->xBlockTime = xDontBlock;\r
215         pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );\r
216 \r
217         pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
218         pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;\r
219         pxQueueParameters4->xBlockTime = xBlockTime;\r
220         pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );\r
221 \r
222         xTaskCreate( vBlockingQueueConsumer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );\r
223         xTaskCreate( vBlockingQueueProducer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );\r
224 \r
225 \r
226 \r
227         /* Create the last two tasks as described above.  The mechanism is again just\r
228         the same.  This time both parameter structures are given a block time. */\r
229         pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
230         pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( UBaseType_t ) sizeof( uint16_t ) );\r
231         pxQueueParameters5->xBlockTime = xBlockTime;\r
232         pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );\r
233 \r
234         pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
235         pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;\r
236         pxQueueParameters6->xBlockTime = xBlockTime;\r
237         pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] ); \r
238 \r
239         xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );\r
240         xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )\r
245 {\r
246 uint16_t usValue = 0;\r
247 xBlockingQueueParameters *pxQueueParameters;\r
248 short sErrorEverOccurred = pdFALSE;\r
249 \r
250         #ifdef USE_STDIO\r
251         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
252         \r
253                 const char * const pcTaskStartMsg = "Alt blocking queue producer task started.\r\n";\r
254 \r
255                 /* Queue a message for printing to say the task has started. */\r
256                 vPrintDisplayMessage( &pcTaskStartMsg );\r
257         #endif\r
258 \r
259         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
260 \r
261         for( ;; )\r
262         {               \r
263                 if( xQueueAltSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )\r
264                 {\r
265                         sErrorEverOccurred = pdTRUE;\r
266                 }\r
267                 else\r
268                 {\r
269                         /* We have successfully posted a message, so increment the variable\r
270                         used to check we are still running. */\r
271                         if( sErrorEverOccurred == pdFALSE )\r
272                         {\r
273                                 ( *pxQueueParameters->psCheckVariable )++;\r
274                         }\r
275 \r
276                         /* Increment the variable we are going to post next time round.  The\r
277                         consumer will expect the numbers to     follow in numerical order. */\r
278                         ++usValue;\r
279                 }\r
280         }\r
281 }\r
282 /*-----------------------------------------------------------*/\r
283 \r
284 static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )\r
285 {\r
286 uint16_t usData, usExpectedValue = 0;\r
287 xBlockingQueueParameters *pxQueueParameters;\r
288 short sErrorEverOccurred = pdFALSE;\r
289 \r
290         #ifdef USE_STDIO\r
291         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
292         \r
293                 const char * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";\r
294 \r
295                 /* Queue a message for printing to say the task has started. */\r
296                 vPrintDisplayMessage( &pcTaskStartMsg );\r
297         #endif\r
298 \r
299         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
300 \r
301         for( ;; )\r
302         {       \r
303                 if( xQueueAltReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )\r
304                 {\r
305                         if( usData != usExpectedValue )\r
306                         {\r
307                                 /* Catch-up. */\r
308                                 usExpectedValue = usData;\r
309 \r
310                                 sErrorEverOccurred = pdTRUE;\r
311                         }\r
312                         else\r
313                         {\r
314                                 /* We have successfully received a message, so increment the\r
315                                 variable used to check we are still running. */ \r
316                                 if( sErrorEverOccurred == pdFALSE )\r
317                                 {\r
318                                         ( *pxQueueParameters->psCheckVariable )++;\r
319                                 }\r
320                                                         \r
321                                 /* Increment the value we expect to remove from the queue next time\r
322                                 round. */\r
323                                 ++usExpectedValue;\r
324                         }                       \r
325                 }               \r
326         }\r
327 }\r
328 /*-----------------------------------------------------------*/\r
329 \r
330 /* This is called to check that all the created tasks are still running. */\r
331 BaseType_t xAreAltBlockingQueuesStillRunning( void )\r
332 {\r
333 static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
334 static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
335 BaseType_t xReturn = pdPASS, xTasks;\r
336 \r
337         /* Not too worried about mutual exclusion on these variables as they are 16\r
338         bits and we are only reading them. We also only care to see if they have\r
339         changed or not.\r
340         \r
341         Loop through each check variable to and return pdFALSE if any are found not\r
342         to have changed since the last call. */\r
343 \r
344         for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )\r
345         {\r
346                 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )\r
347                 {\r
348                         xReturn = pdFALSE;\r
349                 }\r
350                 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];\r
351 \r
352 \r
353                 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )\r
354                 {\r
355                         xReturn = pdFALSE;\r
356                 }\r
357                 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];\r
358         }\r
359 \r
360         return xReturn;\r
361 }\r
362 \r