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 * This is a version of BlockQ.c that uses the alternative (Alt) API.
\r
99 * Creates six tasks that operate on three queues as follows:
\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
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
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
123 #include <stdlib.h>
\r
125 /* Scheduler include files. */
\r
126 #include "FreeRTOS.h"
\r
130 /* Demo program include files. */
\r
131 #include "AltBlckQ.h"
\r
133 #define blckqSTACK_SIZE configMINIMAL_STACK_SIZE
\r
134 #define blckqNUM_TASK_SETS ( 3 )
\r
136 /* Structure used to pass parameters to the blocking queue tasks. */
\r
137 typedef struct BLOCKING_QUEUE_PARAMETERS
\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
144 /* Task function that creates an incrementing number and posts it on a queue. */
\r
145 static portTASK_FUNCTION_PROTO( vBlockingQueueProducer, pvParameters );
\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
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
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
160 /*-----------------------------------------------------------*/
\r
162 void vStartAltBlockingQueueTasks( UBaseType_t uxPriority )
\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
171 /* Create the first two tasks as described at the top of the file. */
\r
173 /* First create the structure used to pass parameters to the consumer tasks. */
\r
174 pxQueueParameters1 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\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
180 /* The consumer is created first so gets a block time as described above. */
\r
181 pxQueueParameters1->xBlockTime = xBlockTime;
\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
187 /* Create the structure used to pass parameters to the producer task. */
\r
188 pxQueueParameters2 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
190 /* Pass the queue to this task also, using the parameter structure. */
\r
191 pxQueueParameters2->xQueue = pxQueueParameters1->xQueue;
\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
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
202 /* Note the producer has a lower priority than the consumer when the tasks are
\r
204 xTaskCreate( vBlockingQueueConsumer, "QConsB1", blckqSTACK_SIZE, ( void * ) pxQueueParameters1, uxPriority, NULL );
\r
205 xTaskCreate( vBlockingQueueProducer, "QProdB2", blckqSTACK_SIZE, ( void * ) pxQueueParameters2, tskIDLE_PRIORITY, NULL );
\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
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
217 pxQueueParameters4 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
218 pxQueueParameters4->xQueue = pxQueueParameters3->xQueue;
\r
219 pxQueueParameters4->xBlockTime = xBlockTime;
\r
220 pxQueueParameters4->psCheckVariable = &( sBlockingConsumerCount[ 1 ] );
\r
222 xTaskCreate( vBlockingQueueConsumer, "QProdB3", blckqSTACK_SIZE, ( void * ) pxQueueParameters3, tskIDLE_PRIORITY, NULL );
\r
223 xTaskCreate( vBlockingQueueProducer, "QConsB4", blckqSTACK_SIZE, ( void * ) pxQueueParameters4, uxPriority, NULL );
\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
234 pxQueueParameters6 = ( xBlockingQueueParameters * ) pvPortMalloc( sizeof( xBlockingQueueParameters ) );
\r
235 pxQueueParameters6->xQueue = pxQueueParameters5->xQueue;
\r
236 pxQueueParameters6->xBlockTime = xBlockTime;
\r
237 pxQueueParameters6->psCheckVariable = &( sBlockingConsumerCount[ 2 ] );
\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
242 /*-----------------------------------------------------------*/
\r
244 static portTASK_FUNCTION( vBlockingQueueProducer, pvParameters )
\r
246 uint16_t usValue = 0;
\r
247 xBlockingQueueParameters *pxQueueParameters;
\r
248 short sErrorEverOccurred = pdFALSE;
\r
251 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
253 const char * const pcTaskStartMsg = "Alt blocking queue producer task started.\r\n";
\r
255 /* Queue a message for printing to say the task has started. */
\r
256 vPrintDisplayMessage( &pcTaskStartMsg );
\r
259 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
\r
263 if( xQueueAltSendToBack( pxQueueParameters->xQueue, ( void * ) &usValue, pxQueueParameters->xBlockTime ) != pdPASS )
\r
265 sErrorEverOccurred = pdTRUE;
\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
273 ( *pxQueueParameters->psCheckVariable )++;
\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
282 /*-----------------------------------------------------------*/
\r
284 static portTASK_FUNCTION( vBlockingQueueConsumer, pvParameters )
\r
286 uint16_t usData, usExpectedValue = 0;
\r
287 xBlockingQueueParameters *pxQueueParameters;
\r
288 short sErrorEverOccurred = pdFALSE;
\r
291 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
293 const char * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";
\r
295 /* Queue a message for printing to say the task has started. */
\r
296 vPrintDisplayMessage( &pcTaskStartMsg );
\r
299 pxQueueParameters = ( xBlockingQueueParameters * ) pvParameters;
\r
303 if( xQueueAltReceive( pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime ) == pdPASS )
\r
305 if( usData != usExpectedValue )
\r
308 usExpectedValue = usData;
\r
310 sErrorEverOccurred = pdTRUE;
\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
318 ( *pxQueueParameters->psCheckVariable )++;
\r
321 /* Increment the value we expect to remove from the queue next time
\r
328 /*-----------------------------------------------------------*/
\r
330 /* This is called to check that all the created tasks are still running. */
\r
331 BaseType_t xAreAltBlockingQueuesStillRunning( void )
\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
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
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
344 for( xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++ )
\r
346 if( sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ] )
\r
350 sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ];
\r
353 if( sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ] )
\r
357 sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ];
\r