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