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

Revision 14, 9.2 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 is a version of PollQ.c that uses the alternative (Alt) API.
50  *
51  * Creates two tasks that communicate over a single queue.  One task acts as a
52  * producer, the other a consumer.
53  *
54  * The producer loops for three iteration, posting an incrementing number onto the
55  * queue each cycle.  It then delays for a fixed period before doing exactly the
56  * same again.
57  *
58  * The consumer loops emptying the queue.  Each item removed from the queue is
59  * checked to ensure it contains the expected value.  When the queue is empty it
60  * blocks for a fixed period, then does the same again.
61  *
62  * All queue access is performed without blocking.  The consumer completely empties
63  * the queue each time it runs so the producer should never find the queue full.
64  *
65  * An error is flagged if the consumer obtains an unexpected value or the producer
66  * find the queue is full.
67  */
68
69 /*
70 Changes from V2.0.0
71
72         + Delay periods are now specified using variables and constants of
73           portTickType rather than unsigned portLONG.
74 */
75
76 #include <stdlib.h>
77
78 /* Scheduler include files. */
79 #include "FreeRTOS.h"
80 #include "task.h"
81 #include "queue.h"
82
83 /* Demo program include files. */
84 #include "AltPollQ.h"
85
86 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE
87 #define pollqQUEUE_SIZE                 ( 10 )
88 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )
89 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )
90 #define pollqNO_DELAY                   ( ( portTickType ) 0 )
91 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )
92 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )
93
94 /* The task that posts the incrementing number onto the queue. */
95 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
96
97 /* The task that empties the queue. */
98 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
99
100 /* Variables that are used to check that the tasks are still running with no
101 errors. */
102 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
103
104 /*-----------------------------------------------------------*/
105
106 void vStartAltPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
107 {
108 static xQueueHandle xPolledQueue;
109
110         /* Create the queue used by the producer and consumer. */
111         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );
112
113         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
114         in use.  The queue registry is provided as a means for kernel aware
115         debuggers to locate queues and has no purpose if a kernel aware debugger
116         is not being used.  The call to vQueueAddToRegistry() will be removed
117         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
118         defined to be less than 1. */
119         vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "AltPollQueue" );
120
121
122         /* Spawn the producer and consumer. */
123         xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
124         xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );
125 }
126 /*-----------------------------------------------------------*/
127
128 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
129 {
130 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;
131 signed portBASE_TYPE xError = pdFALSE, xLoop;
132
133         #ifdef USE_STDIO
134         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
135        
136                 const portCHAR * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n";
137
138                 /* Queue a message for printing to say the task has started. */
139                 vPrintDisplayMessage( &pcTaskStartMsg );
140         #endif
141
142         for( ;; )
143         {               
144                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
145                 {
146                         /* Send an incrementing number on the queue without blocking. */
147                         if( xQueueAltSendToBack( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
148                         {
149                                 /* We should never find the queue full so if we get here there
150                                 has been an error. */
151                                 xError = pdTRUE;
152                         }
153                         else
154                         {
155                                 if( xError == pdFALSE )
156                                 {
157                                         /* If an error has ever been recorded we stop incrementing the
158                                         check variable. */
159                                         portENTER_CRITICAL();
160                                                 xPollingProducerCount++;
161                                         portEXIT_CRITICAL();
162                                 }
163
164                                 /* Update the value we are going to post next time around. */
165                                 usValue++;
166                         }
167                 }
168
169                 /* Wait before we start posting again to ensure the consumer runs and
170                 empties the queue. */
171                 vTaskDelay( pollqPRODUCER_DELAY );
172         }
173 }  /*lint !e818 Function prototype must conform to API. */
174 /*-----------------------------------------------------------*/
175
176 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
177 {
178 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;
179 signed portBASE_TYPE xError = pdFALSE;
180
181         #ifdef USE_STDIO
182         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
183        
184                 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";
185
186                 /* Queue a message for printing to say the task has started. */
187                 vPrintDisplayMessage( &pcTaskStartMsg );
188         #endif
189
190         for( ;; )
191         {               
192                 /* Loop until the queue is empty. */
193                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )
194                 {
195                         if( xQueueAltReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
196                         {
197                                 if( usData != usExpectedValue )
198                                 {
199                                         /* This is not what we expected to receive so an error has
200                                         occurred. */
201                                         xError = pdTRUE;
202
203                                         /* Catch-up to the value we received so our next expected
204                                         value should again be correct. */
205                                         usExpectedValue = usData;
206                                 }
207                                 else
208                                 {
209                                         if( xError == pdFALSE )
210                                         {
211                                                 /* Only increment the check variable if no errors have
212                                                 occurred. */
213                                                 portENTER_CRITICAL();
214                                                         xPollingConsumerCount++;
215                                                 portEXIT_CRITICAL();
216                                         }
217                                 }
218
219                                 /* Next time round we would expect the number to be one higher. */
220                                 usExpectedValue++;
221                         }
222                 }
223
224                 /* Now the queue is empty we block, allowing the producer to place more
225                 items in the queue. */
226                 vTaskDelay( pollqCONSUMER_DELAY );
227         }
228 } /*lint !e818 Function prototype must conform to API. */
229 /*-----------------------------------------------------------*/
230
231 /* This is called to check that all the created tasks are still running with no errors. */
232 portBASE_TYPE xAreAltPollingQueuesStillRunning( void )
233 {
234 portBASE_TYPE xReturn;
235
236         /* Check both the consumer and producer poll count to check they have both
237         been changed since out last trip round.  We do not need a critical section
238         around the check variables as this is called from a higher priority than
239         the other tasks that access the same variables. */
240         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||
241                 ( xPollingProducerCount == pollqINITIAL_VALUE )
242           )
243         {
244                 xReturn = pdFALSE;
245         }
246         else
247         {
248                 xReturn = pdTRUE;
249         }
250
251         /* Set the check variables back down so we know if they have been
252         incremented the next time around. */
253         xPollingConsumerCount = pollqINITIAL_VALUE;
254         xPollingProducerCount = pollqINITIAL_VALUE;
255
256         return xReturn;
257 }
Note: See TracBrowser for help on using the browser.