2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
67 * This version of PollQ. c is for use on systems that have limited stack
\r
68 * space and no display facilities. The complete version can be found in
\r
69 * the Demo/Common/Full directory.
\r
71 * Creates two tasks that communicate over a single queue. One task acts as a
\r
72 * producer, the other a consumer.
\r
74 * The producer loops for three iteration, posting an incrementing number onto the
\r
75 * queue each cycle. It then delays for a fixed period before doing exactly the
\r
78 * The consumer loops emptying the queue. Each item removed from the queue is
\r
79 * checked to ensure it contains the expected value. When the queue is empty it
\r
80 * blocks for a fixed period, then does the same again.
\r
82 * All queue access is performed without blocking. The consumer completely empties
\r
83 * the queue each time it runs so the producer should never find the queue full.
\r
85 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
86 * find the queue is full.
\r
92 + Delay periods are now specified using variables and constants of
\r
93 portTickType rather than unsigned long.
\r
98 /* Scheduler include files. */
\r
99 #include "FreeRTOS.h"
\r
103 /* Demo program include files. */
\r
106 #define pollqSTACK_SIZE configMINIMAL_STACK_SIZE
\r
107 #define pollqQUEUE_SIZE ( 10 )
\r
108 #define pollqPRODUCER_DELAY ( ( portTickType ) 200 / portTICK_RATE_MS )
\r
109 #define pollqCONSUMER_DELAY ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )
\r
110 #define pollqNO_DELAY ( ( portTickType ) 0 )
\r
111 #define pollqVALUES_TO_PRODUCE ( ( signed portBASE_TYPE ) 3 )
\r
112 #define pollqINITIAL_VALUE ( ( signed portBASE_TYPE ) 0 )
\r
114 /* The task that posts the incrementing number onto the queue. */
\r
115 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
\r
117 /* The task that empties the queue. */
\r
118 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
\r
120 /* Variables that are used to check that the tasks are still running with no
\r
122 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
\r
124 /*-----------------------------------------------------------*/
\r
126 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
128 static xQueueHandle xPolledQueue;
\r
130 /* Create the queue used by the producer and consumer. */
\r
131 xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\r
133 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
134 in use. The queue registry is provided as a means for kernel aware
\r
135 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
136 is not being used. The call to vQueueAddToRegistry() will be removed
\r
137 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
138 defined to be less than 1. */
\r
139 vQueueAddToRegistry( xPolledQueue, "Poll_Test_Queue" );
\r
141 /* Spawn the producer and consumer. */
\r
142 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
143 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
\r
145 /*-----------------------------------------------------------*/
\r
147 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
\r
149 unsigned short usValue = ( unsigned short ) 0;
\r
150 signed portBASE_TYPE xError = pdFALSE, xLoop;
\r
154 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
\r
156 /* Send an incrementing number on the queue without blocking. */
\r
157 if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
\r
159 /* We should never find the queue full so if we get here there
\r
160 has been an error. */
\r
165 if( xError == pdFALSE )
\r
167 /* If an error has ever been recorded we stop incrementing the
\r
169 portENTER_CRITICAL();
\r
170 xPollingProducerCount++;
\r
171 portEXIT_CRITICAL();
\r
174 /* Update the value we are going to post next time around. */
\r
179 /* Wait before we start posting again to ensure the consumer runs and
\r
180 empties the queue. */
\r
181 vTaskDelay( pollqPRODUCER_DELAY );
\r
183 } /*lint !e818 Function prototype must conform to API. */
\r
184 /*-----------------------------------------------------------*/
\r
186 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
\r
188 unsigned short usData, usExpectedValue = ( unsigned short ) 0;
\r
189 signed portBASE_TYPE xError = pdFALSE;
\r
193 /* Loop until the queue is empty. */
\r
194 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
\r
196 if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
\r
198 if( usData != usExpectedValue )
\r
200 /* This is not what we expected to receive so an error has
\r
204 /* Catch-up to the value we received so our next expected
\r
205 value should again be correct. */
\r
206 usExpectedValue = usData;
\r
210 if( xError == pdFALSE )
\r
212 /* Only increment the check variable if no errors have
\r
214 portENTER_CRITICAL();
\r
215 xPollingConsumerCount++;
\r
216 portEXIT_CRITICAL();
\r
220 /* Next time round we would expect the number to be one higher. */
\r
225 /* Now the queue is empty we block, allowing the producer to place more
\r
226 items in the queue. */
\r
227 vTaskDelay( pollqCONSUMER_DELAY );
\r
229 } /*lint !e818 Function prototype must conform to API. */
\r
230 /*-----------------------------------------------------------*/
\r
232 /* This is called to check that all the created tasks are still running with no errors. */
\r
233 portBASE_TYPE xArePollingQueuesStillRunning( void )
\r
235 portBASE_TYPE xReturn;
\r
237 /* Check both the consumer and producer poll count to check they have both
\r
238 been changed since out last trip round. We do not need a critical section
\r
239 around the check variables as this is called from a higher priority than
\r
240 the other tasks that access the same variables. */
\r
241 if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
\r
242 ( xPollingProducerCount == pollqINITIAL_VALUE )
\r
252 /* Set the check variables back down so we know if they have been
\r
253 incremented the next time around. */
\r
254 xPollingConsumerCount = pollqINITIAL_VALUE;
\r
255 xPollingProducerCount = pollqINITIAL_VALUE;
\r