]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/PollQ.c
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@82 1d2547de-c912-0410-9cb9...
[freertos] / Demo / Common / Full / PollQ.c
1 /*\r
2         FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 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         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 \r
37 /**\r
38  * This is a very simple queue test.  See the BlockQ. c documentation for a more \r
39  * comprehensive version.\r
40  *\r
41  * Creates two tasks that communicate over a single queue.  One task acts as a \r
42  * producer, the other a consumer.  \r
43  *\r
44  * The producer loops for three iteration, posting an incrementing number onto the \r
45  * queue each cycle.  It then delays for a fixed period before doing exactly the \r
46  * same again.\r
47  *\r
48  * The consumer loops emptying the queue.  Each item removed from the queue is \r
49  * checked to ensure it contains the expected value.  When the queue is empty it \r
50  * blocks for a fixed period, then does the same again.\r
51  *\r
52  * All queue access is performed without blocking.  The consumer completely empties \r
53  * the queue each time it runs so the producer should never find the queue full.  \r
54  *\r
55  * An error is flagged if the consumer obtains an unexpected value or the producer \r
56  * find the queue is full.\r
57  *\r
58  * \page PollQC pollQ.c\r
59  * \ingroup DemoFiles\r
60  * <HR>\r
61  */\r
62 \r
63 /*\r
64 Changes from V2.0.0\r
65 \r
66         + Delay periods are now specified using variables and constants of\r
67           portTickType rather than unsigned portLONG.\r
68 */\r
69 \r
70 #include <stdlib.h>\r
71 \r
72 /* Scheduler include files. */\r
73 #include "FreeRTOS.h"\r
74 #include "task.h"\r
75 #include "queue.h"\r
76 #include "print.h"\r
77 \r
78 /* Demo program include files. */\r
79 #include "PollQ.h"\r
80 \r
81 #define pollqSTACK_SIZE         ( ( unsigned portSHORT ) 128 )\r
82 \r
83 /* The task that posts the incrementing number onto the queue. */\r
84 static void vPolledQueueProducer( void *pvParameters );\r
85 \r
86 /* The task that empties the queue. */\r
87 static void vPolledQueueConsumer( void *pvParameters );\r
88 \r
89 /* Variables that are used to check that the tasks are still running with no errors. */\r
90 static volatile portSHORT sPollingConsumerCount = 0, sPollingProducerCount = 0;\r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
94 {\r
95 static xQueueHandle xPolledQueue;\r
96 const unsigned portBASE_TYPE uxQueueSize = 10;\r
97 \r
98         /* Create the queue used by the producer and consumer. */\r
99         xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
100 \r
101         /* Spawn the producer and consumer. */\r
102         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
103         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
104 }\r
105 /*-----------------------------------------------------------*/\r
106 \r
107 static void vPolledQueueProducer( void *pvParameters )\r
108 {\r
109 unsigned portSHORT usValue = 0, usLoop;\r
110 xQueueHandle *pxQueue;\r
111 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
112 const unsigned portSHORT usNumToProduce = 3;\r
113 const portCHAR * const pcTaskStartMsg = "Polled queue producer started.\r\n";\r
114 const portCHAR * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";\r
115 portSHORT sError = pdFALSE;\r
116 \r
117         /* Queue a message for printing to say the task has started. */\r
118         vPrintDisplayMessage( &pcTaskStartMsg );\r
119 \r
120         /* The queue being used is passed in as the parameter. */\r
121         pxQueue = ( xQueueHandle * ) pvParameters;\r
122 \r
123         for( ;; )\r
124         {               \r
125                 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )\r
126                 {\r
127                         /* Send an incrementing number on the queue without blocking. */\r
128                         if( xQueueSend( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )\r
129                         {\r
130                                 /* We should never find the queue full - this is an error. */\r
131                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
132                                 sError = pdTRUE;\r
133                         }\r
134                         else\r
135                         {\r
136                                 if( sError == pdFALSE )\r
137                                 {\r
138                                         /* If an error has ever been recorded we stop incrementing the \r
139                                         check variable. */\r
140                                         ++sPollingProducerCount;\r
141                                 }\r
142 \r
143                                 /* Update the value we are going to post next time around. */\r
144                                 ++usValue;\r
145                         }\r
146                 }\r
147 \r
148                 /* Wait before we start posting again to ensure the consumer runs and \r
149                 empties the queue. */\r
150                 vTaskDelay( xDelay );\r
151         }\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 static void vPolledQueueConsumer( void *pvParameters )\r
156 {\r
157 unsigned portSHORT usData, usExpectedValue = 0;\r
158 xQueueHandle *pxQueue;\r
159 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
160 const portCHAR * const pcTaskStartMsg = "Polled queue consumer started.\r\n";\r
161 const portCHAR * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";\r
162 portSHORT sError = pdFALSE;\r
163 \r
164         /* Queue a message for printing to say the task has started. */\r
165         vPrintDisplayMessage( &pcTaskStartMsg );\r
166 \r
167         /* The queue being used is passed in as the parameter. */\r
168         pxQueue = ( xQueueHandle * ) pvParameters;\r
169 \r
170         for( ;; )\r
171         {               \r
172                 /* Loop until the queue is empty. */\r
173                 while( uxQueueMessagesWaiting( *pxQueue ) )\r
174                 {\r
175                         if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )\r
176                         {\r
177                                 if( usData != usExpectedValue )\r
178                                 {\r
179                                         /* This is not what we expected to receive so an error has \r
180                                         occurred. */\r
181                                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
182                                         sError = pdTRUE;\r
183                                         /* Catch-up to the value we received so our next expected value \r
184                                         should again be correct. */\r
185                                         usExpectedValue = usData;\r
186                                 }\r
187                                 else\r
188                                 {\r
189                                         if( sError == pdFALSE )\r
190                                         {\r
191                                                 /* Only increment the check variable if no errors have \r
192                                                 occurred. */\r
193                                                 ++sPollingConsumerCount;\r
194                                         }\r
195                                 }\r
196                                 ++usExpectedValue;\r
197                         }\r
198                 }\r
199 \r
200                 /* Now the queue is empty we block, allowing the producer to place more \r
201                 items in the queue. */\r
202                 vTaskDelay( xDelay );\r
203         }\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 /* This is called to check that all the created tasks are still running with no errors. */\r
208 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
209 {\r
210 static portSHORT sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;\r
211 portBASE_TYPE xReturn;\r
212 \r
213         if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||\r
214                 ( sLastPollingProducerCount == sPollingProducerCount ) \r
215           )\r
216         {\r
217                 xReturn = pdFALSE;\r
218         }\r
219         else\r
220         {\r
221                 xReturn = pdTRUE;\r
222         }\r
223 \r
224         sLastPollingConsumerCount = sPollingConsumerCount;\r
225         sLastPollingProducerCount = sPollingProducerCount;\r
226 \r
227         return xReturn;\r
228 }\r