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