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