]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/PollQ.c
Comment the command line interpreter and lwIP sockets based server code.
[freertos] / Demo / Common / Full / PollQ.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 \r
55 /**\r
56  * This is a very simple queue test.  See the BlockQ. c documentation for a more \r
57  * comprehensive version.\r
58  *\r
59  * Creates two tasks that communicate over a single queue.  One task acts as a \r
60  * producer, the other a consumer.  \r
61  *\r
62  * The producer loops for three iteration, posting an incrementing number onto the \r
63  * queue each cycle.  It then delays for a fixed period before doing exactly the \r
64  * same again.\r
65  *\r
66  * The consumer loops emptying the queue.  Each item removed from the queue is \r
67  * checked to ensure it contains the expected value.  When the queue is empty it \r
68  * blocks for a fixed period, then does the same again.\r
69  *\r
70  * All queue access is performed without blocking.  The consumer completely empties \r
71  * the queue each time it runs so the producer should never find the queue full.  \r
72  *\r
73  * An error is flagged if the consumer obtains an unexpected value or the producer \r
74  * find the queue is full.\r
75  *\r
76  * \page PollQC pollQ.c\r
77  * \ingroup DemoFiles\r
78  * <HR>\r
79  */\r
80 \r
81 /*\r
82 Changes from V2.0.0\r
83 \r
84         + Delay periods are now specified using variables and constants of\r
85           portTickType rather than unsigned long.\r
86 */\r
87 \r
88 #include <stdlib.h>\r
89 \r
90 /* Scheduler include files. */\r
91 #include "FreeRTOS.h"\r
92 #include "task.h"\r
93 #include "queue.h"\r
94 #include "print.h"\r
95 \r
96 /* Demo program include files. */\r
97 #include "PollQ.h"\r
98 \r
99 #define pollqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
100 \r
101 /* The task that posts the incrementing number onto the queue. */\r
102 static void vPolledQueueProducer( void *pvParameters );\r
103 \r
104 /* The task that empties the queue. */\r
105 static void vPolledQueueConsumer( void *pvParameters );\r
106 \r
107 /* Variables that are used to check that the tasks are still running with no errors. */\r
108 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;\r
109 /*-----------------------------------------------------------*/\r
110 \r
111 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
112 {\r
113 static xQueueHandle xPolledQueue;\r
114 const unsigned portBASE_TYPE uxQueueSize = 10;\r
115 \r
116         /* Create the queue used by the producer and consumer. */\r
117         xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
118 \r
119         /* Spawn the producer and consumer. */\r
120         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
121         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
122 }\r
123 /*-----------------------------------------------------------*/\r
124 \r
125 static void vPolledQueueProducer( void *pvParameters )\r
126 {\r
127 unsigned short usValue = 0, usLoop;\r
128 xQueueHandle *pxQueue;\r
129 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
130 const unsigned short usNumToProduce = 3;\r
131 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";\r
132 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";\r
133 short sError = pdFALSE;\r
134 \r
135         /* Queue a message for printing to say the task has started. */\r
136         vPrintDisplayMessage( &pcTaskStartMsg );\r
137 \r
138         /* The queue being used is passed in as the parameter. */\r
139         pxQueue = ( xQueueHandle * ) pvParameters;\r
140 \r
141         for( ;; )\r
142         {               \r
143                 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )\r
144                 {\r
145                         /* Send an incrementing number on the queue without blocking. */\r
146                         if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )\r
147                         {\r
148                                 /* We should never find the queue full - this is an error. */\r
149                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
150                                 sError = pdTRUE;\r
151                         }\r
152                         else\r
153                         {\r
154                                 if( sError == pdFALSE )\r
155                                 {\r
156                                         /* If an error has ever been recorded we stop incrementing the \r
157                                         check variable. */\r
158                                         ++sPollingProducerCount;\r
159                                 }\r
160 \r
161                                 /* Update the value we are going to post next time around. */\r
162                                 ++usValue;\r
163                         }\r
164                 }\r
165 \r
166                 /* Wait before we start posting again to ensure the consumer runs and \r
167                 empties the queue. */\r
168                 vTaskDelay( xDelay );\r
169         }\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static void vPolledQueueConsumer( void *pvParameters )\r
174 {\r
175 unsigned short usData, usExpectedValue = 0;\r
176 xQueueHandle *pxQueue;\r
177 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
178 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";\r
179 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";\r
180 short sError = pdFALSE;\r
181 \r
182         /* Queue a message for printing to say the task has started. */\r
183         vPrintDisplayMessage( &pcTaskStartMsg );\r
184 \r
185         /* The queue being used is passed in as the parameter. */\r
186         pxQueue = ( xQueueHandle * ) pvParameters;\r
187 \r
188         for( ;; )\r
189         {               \r
190                 /* Loop until the queue is empty. */\r
191                 while( uxQueueMessagesWaiting( *pxQueue ) )\r
192                 {\r
193                         if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )\r
194                         {\r
195                                 if( usData != usExpectedValue )\r
196                                 {\r
197                                         /* This is not what we expected to receive so an error has \r
198                                         occurred. */\r
199                                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
200                                         sError = pdTRUE;\r
201                                         /* Catch-up to the value we received so our next expected value \r
202                                         should again be correct. */\r
203                                         usExpectedValue = usData;\r
204                                 }\r
205                                 else\r
206                                 {\r
207                                         if( sError == pdFALSE )\r
208                                         {\r
209                                                 /* Only increment the check variable if no errors have \r
210                                                 occurred. */\r
211                                                 ++sPollingConsumerCount;\r
212                                         }\r
213                                 }\r
214                                 ++usExpectedValue;\r
215                         }\r
216                 }\r
217 \r
218                 /* Now the queue is empty we block, allowing the producer to place more \r
219                 items in the queue. */\r
220                 vTaskDelay( xDelay );\r
221         }\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 /* This is called to check that all the created tasks are still running with no errors. */\r
226 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
227 {\r
228 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;\r
229 portBASE_TYPE xReturn;\r
230 \r
231         if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||\r
232                 ( sLastPollingProducerCount == sPollingProducerCount ) \r
233           )\r
234         {\r
235                 xReturn = pdFALSE;\r
236         }\r
237         else\r
238         {\r
239                 xReturn = pdTRUE;\r
240         }\r
241 \r
242         sLastPollingConsumerCount = sPollingConsumerCount;\r
243         sLastPollingProducerCount = sPollingProducerCount;\r
244 \r
245         return xReturn;\r
246 }\r