root/webserver/example/freeRTOS/Demo/Common/Minimal/PollQ.c

Revision 14, 8.7 kB (checked in by phil, 15 years ago)

added unmodified FreeRTOS package V5.4.1 with only web srv demo source for LPC2368 for CrossWorks?

Line 
1 /*
2         FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd.
3
4         This file is part of the FreeRTOS distribution.
5
6         FreeRTOS is free software; you can redistribute it and/or modify it     under
7         the terms of the GNU General Public License (version 2) as published by the
8         Free Software Foundation and modified by the FreeRTOS exception.
9         **NOTE** The exception to the GPL is included to allow you to distribute a
10         combined work that includes FreeRTOS without being obliged to provide the
11         source code for proprietary components outside of the FreeRTOS kernel. 
12         Alternative commercial license and support terms are also available upon
13         request.  See the licensing section of http://www.FreeRTOS.org for full
14         license details.
15
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
19         more details.
20
21         You should have received a copy of the GNU General Public License along
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.
24
25
26         ***************************************************************************
27         *                                                                         *
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *
29         * See http://www.FreeRTOS.org/Documentation for details                   *
30         *                                                                         *
31         ***************************************************************************
32
33         1 tab == 4 spaces!
34
35         Please ensure to read the configuration and relevant port sections of the
36         online documentation.
37
38         http://www.FreeRTOS.org - Documentation, latest information, license and
39         contact details.
40
41         http://www.SafeRTOS.com - A version that is certified for use in safety
42         critical systems.
43
44         http://www.OpenRTOS.com - Commercial support, development, porting,
45         licensing and training services.
46 */
47
48 /*
49  * This version of PollQ. c is for use on systems that have limited stack
50  * space and no display facilities.  The complete version can be found in
51  * the Demo/Common/Full directory.
52  *
53  * Creates two tasks that communicate over a single queue.  One task acts as a
54  * producer, the other a consumer.
55  *
56  * The producer loops for three iteration, posting an incrementing number onto the
57  * queue each cycle.  It then delays for a fixed period before doing exactly the
58  * same again.
59  *
60  * The consumer loops emptying the queue.  Each item removed from the queue is
61  * checked to ensure it contains the expected value.  When the queue is empty it
62  * blocks for a fixed period, then does the same again.
63  *
64  * All queue access is performed without blocking.  The consumer completely empties
65  * the queue each time it runs so the producer should never find the queue full.
66  *
67  * An error is flagged if the consumer obtains an unexpected value or the producer
68  * find the queue is full.
69  */
70
71 /*
72 Changes from V2.0.0
73
74         + Delay periods are now specified using variables and constants of
75           portTickType rather than unsigned portLONG.
76 */
77
78 #include <stdlib.h>
79
80 /* Scheduler include files. */
81 #include "FreeRTOS.h"
82 #include "task.h"
83 #include "queue.h"
84
85 /* Demo program include files. */
86 #include "PollQ.h"
87
88 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE
89 #define pollqQUEUE_SIZE                 ( 10 )
90 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )
91 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )
92 #define pollqNO_DELAY                   ( ( portTickType ) 0 )
93 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )
94 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )
95
96 /* The task that posts the incrementing number onto the queue. */
97 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
98
99 /* The task that empties the queue. */
100 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
101
102 /* Variables that are used to check that the tasks are still running with no
103 errors. */
104 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
105
106 /*-----------------------------------------------------------*/
107
108 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
109 {
110 static xQueueHandle xPolledQueue;
111
112         /* Create the queue used by the producer and consumer. */
113         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
114
115         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
116         in use.  The queue registry is provided as a means for kernel aware
117         debuggers to locate queues and has no purpose if a kernel aware debugger
118         is not being used.  The call to vQueueAddToRegistry() will be removed
119         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
120         defined to be less than 1. */
121         vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "Poll_Test_Queue" );
122
123         /* Spawn the producer and consumer. */
124         xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
125         xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
126 }
127 /*-----------------------------------------------------------*/
128
129 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
130 {
131 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;
132 signed portBASE_TYPE xError = pdFALSE, xLoop;
133
134         for( ;; )
135         {               
136                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
137                 {
138                         /* Send an incrementing number on the queue without blocking. */
139                         if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
140                         {
141                                 /* We should never find the queue full so if we get here there
142                                 has been an error. */
143                                 xError = pdTRUE;
144                         }
145                         else
146                         {
147                                 if( xError == pdFALSE )
148                                 {
149                                         /* If an error has ever been recorded we stop incrementing the
150                                         check variable. */
151                                         portENTER_CRITICAL();
152                                                 xPollingProducerCount++;
153                                         portEXIT_CRITICAL();
154                                 }
155
156                                 /* Update the value we are going to post next time around. */
157                                 usValue++;
158                         }
159                 }
160
161                 /* Wait before we start posting again to ensure the consumer runs and
162                 empties the queue. */
163                 vTaskDelay( pollqPRODUCER_DELAY );
164         }
165 }  /*lint !e818 Function prototype must conform to API. */
166 /*-----------------------------------------------------------*/
167
168 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
169 {
170 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;
171 signed portBASE_TYPE xError = pdFALSE;
172
173         for( ;; )
174         {               
175                 /* Loop until the queue is empty. */
176                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
177                 {
178                         if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
179                         {
180                                 if( usData != usExpectedValue )
181                                 {
182                                         /* This is not what we expected to receive so an error has
183                                         occurred. */
184                                         xError = pdTRUE;
185
186                                         /* Catch-up to the value we received so our next expected
187                                         value should again be correct. */
188                                         usExpectedValue = usData;
189                                 }
190                                 else
191                                 {
192                                         if( xError == pdFALSE )
193                                         {
194                                                 /* Only increment the check variable if no errors have
195                                                 occurred. */
196                                                 portENTER_CRITICAL();
197                                                         xPollingConsumerCount++;
198                                                 portEXIT_CRITICAL();
199                                         }
200                                 }
201
202                                 /* Next time round we would expect the number to be one higher. */
203                                 usExpectedValue++;
204                         }
205                 }
206
207                 /* Now the queue is empty we block, allowing the producer to place more
208                 items in the queue. */
209                 vTaskDelay( pollqCONSUMER_DELAY );
210         }
211 } /*lint !e818 Function prototype must conform to API. */
212 /*-----------------------------------------------------------*/
213
214 /* This is called to check that all the created tasks are still running with no errors. */
215 portBASE_TYPE xArePollingQueuesStillRunning( void )
216 {
217 portBASE_TYPE xReturn;
218
219         /* Check both the consumer and producer poll count to check they have both
220         been changed since out last trip round.  We do not need a critical section
221         around the check variables as this is called from a higher priority than
222         the other tasks that access the same variables. */
223         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
224                 ( xPollingProducerCount == pollqINITIAL_VALUE )
225           )
226         {
227                 xReturn = pdFALSE;
228         }
229         else
230         {
231                 xReturn = pdTRUE;
232         }
233
234         /* Set the check variables back down so we know if they have been
235         incremented the next time around. */
236         xPollingConsumerCount = pollqINITIAL_VALUE;
237         xPollingProducerCount = pollqINITIAL_VALUE;
238
239         return xReturn;
240 }
Note: See TracBrowser for help on using the browser.