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