2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
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
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
25 ***************************************************************************
\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
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\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
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
46 ***************************************************************************
\r
48 ***************************************************************************
\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
62 ***************************************************************************
\r
64 ***************************************************************************
\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
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\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
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
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
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
97 * Creates six tasks that operate on three queues as follows:
\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
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
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
118 * \page BlockQC blockQ.c
\r
119 * \ingroup DemoFiles
\r
124 Changes from V1.00:
\r
126 + Reversed the priority and block times of the second two demo tasks so
\r
127 they operate as per the description above.
\r
129 Changes from V2.0.0
\r
131 + Delay periods are now specified using variables and constants of
\r
132 TickType_t rather than unsigned long.
\r
134 Changes from V4.0.2
\r
136 + The second set of tasks were created the wrong way around. This has been
\r
141 #include <stdlib.h>
\r
143 /* Scheduler include files. */
\r
144 #include "FreeRTOS.h"
\r
148 /* Demo program include files. */
\r
149 #include "BlockQ.h"
\r
152 #define blckqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
153 #define blckqNUM_TASK_SETS ( 3 )
\r
155 /* Structure used to pass parameters to the blocking queue tasks. */
\r
156 typedef struct BLOCKING_QUEUE_PARAMETERS
\r
158 QueueHandle_t xQueue; /*< The queue to be used by the task. */
\r
159 TickType_t xBlockTime; /*< The block time to use on queue reads/writes. */
\r
160 volatile short *psCheckVariable; /*< Incremented on each successful cycle to check the task is still running. */
\r
161 } xBlockingQueueParameters;
\r
163 /* Task function that creates an incrementing number and posts it on a queue. */
\r
164 static void vBlockingQueueProducer( void *pvParameters );
\r
166 /* Task function that removes the incrementing number from a queue and checks that
\r
167 it is the expected number. */
\r
168 static void vBlockingQueueConsumer( void *pvParameters );
\r
170 /* Variables which are incremented each time an item is removed from a queue, and
\r
171 found to be the expected value.
\r
172 These are used to check that the tasks are still running. */
\r
173 static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
\r
175 /* Variable which are incremented each time an item is posted on a queue. These
\r
176 are used to check that the tasks are still running. */
\r
177 static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
\r
179 /*-----------------------------------------------------------*/
\r
181 void vStartBlockingQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
183 xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2;
\r
184 xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4;
\r
185 xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6;
\r
186 const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5;
\r
187 const TickType_t xBlockTime = ( TickType_t ) 1000 / portTICK_PERIOD_MS;
\r
188 const TickType_t xDontBlock = ( TickType_t ) 0;
\r
190 /* Create the first two tasks as described at the top of the file. */
\r
192 /* First create the structure used to pass parameters to the consumer tasks. */
\r
193 pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
195 /* Create the queue used by the first two tasks to pass the incrementing number.
\r
196 Pass a pointer to the queue in the parameter structure. */
\r
197 pxQueueParameters1->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
199 /* The consumer is created first so gets a block time as described above. */
\r
200 pxQueueParameters1->xBlockTime = xBlockTime;
\r
202 /* Pass in the variable that this task is going to increment so we can check it
\r
203 is still running. */
\r
204 pxQueueParameters1->psCheckVariable = &( sBlockingConsumerCount[ 0 ] );
\r
206 /* Create the structure used to pass parameters to the producer task. */
\r
207 pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
209 /* Pass the queue to this task also, using the parameter structure. */
\r
210 pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;
\r
212 /* The producer is not going to block - as soon as it posts the consumer will
\r
213 wake and remove the item so the producer should always have room to post. */
\r
214 pxQueueParameters2->xBlockTime = xDontBlock;
\r
216 /* Pass in the variable that this task is going to increment so we can check
\r
217 it is still running. */
\r
218 pxQueueParameters2->psCheckVariable = &( sBlockingProducerCount[ 0 ] );
\r
221 /* Note the producer has a lower priority than the consumer when the tasks are
\r
223 xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );
\r
224 xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );
\r
228 /* Create the second two tasks as described at the top of the file. This uses
\r
229 the same mechanism but reverses the task priorities. */
\r
231 pxQueueParameters3 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
232 pxQueueParameters3->xQueue = xQueueCreate( uxQueueSize1, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
233 pxQueueParameters3->xBlockTime = xDontBlock;
\r
234 pxQueueParameters3->psCheckVariable = &( sBlockingProducerCount[ 1 ] );
\r
236 pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
237 pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;
\r
238 pxQueueParameters4->xBlockTime = xBlockTime;
\r
239 pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );
\r
241 xTaskCreate( vBlockingQueueProducer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );
\r
242 xTaskCreate( vBlockingQueueConsumer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );
\r
246 /* Create the last two tasks as described above. The mechanism is again just
\r
247 the same. This time both parameter structures are given a block time. */
\r
248 pxQueueParameters5 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
249 pxQueueParameters5->xQueue = xQueueCreate( uxQueueSize5, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
250 pxQueueParameters5->xBlockTime = xBlockTime;
\r
251 pxQueueParameters5->psCheckVariable = &( sBlockingProducerCount[ 2 ] );
\r
253 pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
254 pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;
\r
255 pxQueueParameters6->xBlockTime = xBlockTime;
\r
256 pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );
\r
258 xTaskCreate( vBlockingQueueProducer, "QProdB5", blckqSTACK_SIZE, ( void * ) pxQueueParameters5, tskIDLE_PRIORITY, NULL );
\r
259 xTaskCreate( vBlockingQueueConsumer, "QConsB6", blckqSTACK_SIZE, ( void * ) pxQueueParameters6, tskIDLE_PRIORITY, NULL );
\r
261 /*-----------------------------------------------------------*/
\r
263 static void vBlockingQueueProducer( void *pvParameters )
\r
265 unsigned short usValue = 0;
\r
266 xBlockingQueueParameters *pxQueueParameters;
\r
267 const char * const pcTaskStartMsg = "Blocking queue producer started.\r\n";
\r
268 const char * const pcTaskErrorMsg = "Could not post on blocking queue\r\n";
\r
269 short sErrorEverOccurred = pdFALSE;
\r
271 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
\r
273 /* Queue a message for printing to say the task has started. */
\r
274 vPrintDisplayMessage( &pcTaskStartMsg );
\r
278 if( xQueueSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )
\r
280 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
281 sErrorEverOccurred = pdTRUE;
\r
285 /* We have successfully posted a message, so increment the variable
\r
286 used to check we are still running. */
\r
287 if( sErrorEverOccurred == pdFALSE )
\r
289 ( *pxQueueParameters->psCheckVariable )++;
\r
292 /* Increment the variable we are going to post next time round. The
\r
293 consumer will expect the numbers to follow in numerical order. */
\r
298 /*-----------------------------------------------------------*/
\r
300 static void vBlockingQueueConsumer( void *pvParameters )
\r
302 unsigned short usData, usExpectedValue = 0;
\r
303 xBlockingQueueParameters *pxQueueParameters;
\r
304 const char * const pcTaskStartMsg = "Blocking queue consumer started.\r\n";
\r
305 const char * const pcTaskErrorMsg = "Incorrect value received on blocking queue.\r\n";
\r
306 short sErrorEverOccurred = pdFALSE;
\r
308 /* Queue a message for printing to say the task has started. */
\r
309 vPrintDisplayMessage( &pcTaskStartMsg );
\r
311 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
\r
315 if( xQueueReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )
\r
317 if( usData != usExpectedValue )
\r
319 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
322 usExpectedValue = usData;
\r
324 sErrorEverOccurred = pdTRUE;
\r
328 /* We have successfully received a message, so increment the
\r
329 variable used to check we are still running. */
\r
330 if( sErrorEverOccurred == pdFALSE )
\r
332 ( *pxQueueParameters->psCheckVariable )++;
\r
335 /* Increment the value we expect to remove from the queue next time
\r
342 /*-----------------------------------------------------------*/
\r
344 /* This is called to check that all the created tasks are still running. */
\r
345 portBASE_TYPE xAreBlockingQueuesStillRunning( void )
\r
347 static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
\r
348 static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = { ( short ) 0, ( short ) 0, ( short ) 0 };
\r
349 portBASE_TYPE xReturn = pdPASS, xTasks;
\r
351 /* Not too worried about mutual exclusion on these variables as they are 16
\r
352 bits and we are only reading them. We also only care to see if they have
\r
355 Loop through each check variable and return pdFALSE if any are found not
\r
356 to have changed since the last call. */
\r
358 for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )
\r
360 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ] )
\r
364 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];
\r
367 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ] )
\r
371 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];
\r