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