]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/BlockQ.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / Common / Minimal / BlockQ.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  * Creates six tasks that operate on three queues as follows:\r
98  *\r
99  * The first two tasks send and receive an incrementing number to/from a queue.\r
100  * One task acts as a producer and the other as the consumer.  The consumer is a\r
101  * higher priority than the producer and is set to block on queue reads.  The queue\r
102  * only has space for one item - as soon as the producer posts a message on the\r
103  * queue the consumer will unblock, pre-empt the producer, and remove the item.\r
104  *\r
105  * The second two tasks work the other way around.  Again the queue used only has\r
106  * enough space for one item.  This time the consumer has a lower priority than the\r
107  * producer.  The producer will try to post on the queue blocking when the queue is\r
108  * full.  When the consumer wakes it will remove the item from the queue, causing\r
109  * the producer to unblock, pre-empt the consumer, and immediately re-fill the\r
110  * queue.\r
111  *\r
112  * The last two tasks use the same queue producer and consumer functions.  This time the queue has\r
113  * enough space for lots of items and the tasks operate at the same priority.  The\r
114  * producer will execute, placing items into the queue.  The consumer will start\r
115  * executing when either the queue becomes full (causing the producer to block) or\r
116  * a context switch occurs (tasks of the same priority will time slice).\r
117  *\r
118  */\r
119 \r
120 #include <stdlib.h>\r
121 \r
122 /* Scheduler include files. */\r
123 #include "FreeRTOS.h"\r
124 #include "task.h"\r
125 #include "queue.h"\r
126 \r
127 /* Demo program include files. */\r
128 #include "BlockQ.h"\r
129 \r
130 #define blckqSTACK_SIZE         configMINIMAL_STACK_SIZE\r
131 #define blckqNUM_TASK_SETS      ( 3 )\r
132 \r
133 /* Structure used to pass parameters to the blocking queue tasks. */\r
134 typedef struct BLOCKING_QUEUE_PARAMETERS\r
135 {\r
136         QueueHandle_t xQueue;                                   /*< The queue to be used by the task. */\r
137         TickType_t xBlockTime;                          /*< The block time to use on queue reads/writes. */\r
138         volatile short *psCheckVariable;        /*< Incremented on each successful cycle to check the task is still running. */\r
139 } xBlockingQueueParameters;\r
140 \r
141 /* Task function that creates an incrementing number and posts it on a queue. */\r
142 static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );\r
143 \r
144 /* Task function that removes the incrementing number from a queue and checks that\r
145 it is the expected number. */\r
146 static portTASK_FUNCTION_PROTO( vBlockingQueueConsumer, pvParameters );\r
147 \r
148 /* Variables which are incremented each time an item is removed from a queue, and\r
149 found to be the expected value.\r
150 These are used to check that the tasks are still running. */\r
151 static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
152 \r
153 /* Variable which are incremented each time an item is posted on a queue.   These\r
154 are used to check that the tasks are still running. */\r
155 static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
156 \r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vStartBlockingQueueTasks( UBaseType_t uxPriority )\r
160 {\r
161 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;\r
162 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;\r
163 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;\r
164 const UBaseType_t uxQueueSize1 = 1, uxQueueSize5 = 5;\r
165 const TickType_t xBlockTime = ( TickType_t ) 1000 / portTICK_PERIOD_MS;\r
166 const TickType_t xDontBlock = ( TickType_t ) 0;\r
167 \r
168         /* Create the first two tasks as described at the top of the file. */\r
169 \r
170         /* First create the structure used to pass parameters to the consumer tasks. */\r
171         pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
172 \r
173         /* Create the queue used by the first two tasks to pass the incrementing number.\r
174         Pass a pointer to the queue in the parameter structure. */\r
175         pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( UBaseType_t ) sizeof( uint16_t ) );\r
176 \r
177         /* The consumer is created first so gets a block time as described above. */\r
178         pxQueueParameters1->xBlockTime = xBlockTime;\r
179 \r
180         /* Pass in the variable that this task is going to increment so we can check it\r
181         is still running. */\r
182         pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );\r
183 \r
184         /* Create the structure used to pass parameters to the producer task. */\r
185         pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
186 \r
187         /* Pass the queue to this task also, using the parameter structure. */\r
188         pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;\r
189 \r
190         /* The producer is not going to block - as soon as it posts the consumer will\r
191         wake and remove the item so the producer should always have room to post. */\r
192         pxQueueParameters2->xBlockTime = xDontBlock;\r
193 \r
194         /* Pass in the variable that this task is going to increment so we can check\r
195         it is still running. */\r
196         pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );\r
197 \r
198 \r
199         /* Note the producer has a lower priority than the consumer when the tasks are\r
200         spawned. */\r
201         xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );\r
202         xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );\r
203 \r
204 \r
205 \r
206         /* Create the second two tasks as described at the top of the file.   This uses\r
207         the same mechanism but reverses the task priorities. */\r
208 \r
209         pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
210         pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( UBaseType_t ) sizeof( uint16_t ) );\r
211         pxQueueParameters3->xBlockTime = xDontBlock;\r
212         pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );\r
213 \r
214         pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
215         pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;\r
216         pxQueueParameters4->xBlockTime = xBlockTime;\r
217         pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );\r
218 \r
219         xTaskCreate( vBlockingQueueConsumer, "QConsB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );\r
220         xTaskCreate( vBlockingQueueProducer, "QProdB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );\r
221 \r
222 \r
223 \r
224         /* Create the last two tasks as described above.  The mechanism is again just\r
225         the same.  This time both parameter structures are given a block time. */\r
226         pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
227         pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( UBaseType_t ) sizeof( uint16_t ) );\r
228         pxQueueParameters5->xBlockTime = xBlockTime;\r
229         pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );\r
230 \r
231         pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );\r
232         pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;\r
233         pxQueueParameters6->xBlockTime = xBlockTime;\r
234         pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );\r
235 \r
236         xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );\r
237         xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )\r
242 {\r
243 uint16_t usValue = 0;\r
244 xBlockingQueueParameters *pxQueueParameters;\r
245 short sErrorEverOccurred = pdFALSE;\r
246 \r
247         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
248 \r
249         for( ;; )\r
250         {\r
251                 if( xQueueSend( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )\r
252                 {\r
253                         sErrorEverOccurred = pdTRUE;\r
254                 }\r
255                 else\r
256                 {\r
257                         /* We have successfully posted a message, so increment the variable\r
258                         used to check we are still running. */\r
259                         if( sErrorEverOccurred == pdFALSE )\r
260                         {\r
261                                 ( *pxQueueParameters->psCheckVariable )++;\r
262                         }\r
263 \r
264                         /* Increment the variable we are going to post next time round.  The\r
265                         consumer will expect the numbers to     follow in numerical order. */\r
266                         ++usValue;\r
267 \r
268                         #if configUSE_PREEMPTION == 0\r
269                                 taskYIELD();\r
270                         #endif\r
271                 }\r
272         }\r
273 }\r
274 /*-----------------------------------------------------------*/\r
275 \r
276 static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )\r
277 {\r
278 uint16_t usData, usExpectedValue = 0;\r
279 xBlockingQueueParameters *pxQueueParameters;\r
280 short sErrorEverOccurred = pdFALSE;\r
281 \r
282         pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;\r
283 \r
284         for( ;; )\r
285         {\r
286                 if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )\r
287                 {\r
288                         if( usData != usExpectedValue )\r
289                         {\r
290                                 /* Catch-up. */\r
291                                 usExpectedValue = usData;\r
292 \r
293                                 sErrorEverOccurred = pdTRUE;\r
294                         }\r
295                         else\r
296                         {\r
297                                 /* We have successfully received a message, so increment the\r
298                                 variable used to check we are still running. */\r
299                                 if( sErrorEverOccurred == pdFALSE )\r
300                                 {\r
301                                         ( *pxQueueParameters->psCheckVariable )++;\r
302                                 }\r
303 \r
304                                 /* Increment the value we expect to remove from the queue next time\r
305                                 round. */\r
306                                 ++usExpectedValue;\r
307                         }\r
308 \r
309                         #if configUSE_PREEMPTION == 0\r
310                         {\r
311                                 if( pxQueueParameters->xBlockTime == 0 )\r
312                                 {\r
313                                         taskYIELD();\r
314                                 }\r
315                         }\r
316                         #endif\r
317                 }\r
318         }\r
319 }\r
320 /*-----------------------------------------------------------*/\r
321 \r
322 /* This is called to check that all the created tasks are still running. */\r
323 BaseType_t xAreBlockingQueuesStillRunning( void )\r
324 {\r
325 static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
326 static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( uint16_t ) 0, ( uint16_t ) 0, ( uint16_t ) 0 };\r
327 BaseType_t xReturn = pdPASS, xTasks;\r
328 \r
329         /* Not too worried about mutual exclusion on these variables as they are 16\r
330         bits and we are only reading them. We also only care to see if they have\r
331         changed or not.\r
332 \r
333         Loop through each check variable to and return pdFALSE if any are found not\r
334         to have changed since the last call. */\r
335 \r
336         for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )\r
337         {\r
338                 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]  )\r
339                 {\r
340                         xReturn = pdFALSE;\r
341                 }\r
342                 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];\r
343 \r
344 \r
345                 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]  )\r
346                 {\r
347                         xReturn = pdFALSE;\r
348                 }\r
349                 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];\r
350         }\r
351 \r
352         return xReturn;\r
353 }\r
354 \r