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