]> begriffs open source - freertos/blob - Demo/Common/Minimal/PollQ.c
Ready for V5.2.0 release.
[freertos] / Demo / Common / Minimal / 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  * This version of PollQ. c is for use on systems that have limited stack\r
54  * space and no display facilities.  The complete version can be found in\r
55  * the Demo/Common/Full directory.\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 \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 \r
89 /* Demo program include files. */\r
90 #include "PollQ.h"\r
91 \r
92 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
93 #define pollqQUEUE_SIZE                 ( 10 )\r
94 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )\r
95 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )\r
96 #define pollqNO_DELAY                   ( ( portTickType ) 0 )\r
97 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )\r
98 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )\r
99 \r
100 /* The task that posts the incrementing number onto the queue. */\r
101 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
102 \r
103 /* The task that empties the queue. */\r
104 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
105 \r
106 /* Variables that are used to check that the tasks are still running with no\r
107 errors. */\r
108 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
113 {\r
114 static xQueueHandle xPolledQueue;\r
115 \r
116         /* Create the queue used by the producer and consumer. */\r
117         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
118 \r
119         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
120         in use.  The queue registry is provided as a means for kernel aware \r
121         debuggers to locate queues and has no purpose if a kernel aware debugger\r
122         is not being used.  The call to vQueueAddToRegistry() will be removed\r
123         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
124         defined to be less than 1. */\r
125         vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "Poll_Test_Queue" );\r
126 \r
127         /* Spawn the producer and consumer. */\r
128         xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
129         xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
134 {\r
135 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;\r
136 signed portBASE_TYPE xError = pdFALSE, xLoop;\r
137 \r
138         for( ;; )\r
139         {               \r
140                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
141                 {\r
142                         /* Send an incrementing number on the queue without blocking. */\r
143                         if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
144                         {\r
145                                 /* We should never find the queue full so if we get here there\r
146                                 has been an error. */\r
147                                 xError = pdTRUE;\r
148                         }\r
149                         else\r
150                         {\r
151                                 if( xError == pdFALSE )\r
152                                 {\r
153                                         /* If an error has ever been recorded we stop incrementing the\r
154                                         check variable. */\r
155                                         portENTER_CRITICAL();\r
156                                                 xPollingProducerCount++;\r
157                                         portEXIT_CRITICAL();\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( pollqPRODUCER_DELAY );\r
168         }\r
169 }  /*lint !e818 Function prototype must conform to API. */\r
170 /*-----------------------------------------------------------*/\r
171 \r
172 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
173 {\r
174 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;\r
175 signed portBASE_TYPE xError = pdFALSE;\r
176 \r
177         for( ;; )\r
178         {               \r
179                 /* Loop until the queue is empty. */\r
180                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )\r
181                 {\r
182                         if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
183                         {\r
184                                 if( usData != usExpectedValue )\r
185                                 {\r
186                                         /* This is not what we expected to receive so an error has\r
187                                         occurred. */\r
188                                         xError = pdTRUE;\r
189 \r
190                                         /* Catch-up to the value we received so our next expected\r
191                                         value should again be correct. */\r
192                                         usExpectedValue = usData;\r
193                                 }\r
194                                 else\r
195                                 {\r
196                                         if( xError == pdFALSE )\r
197                                         {\r
198                                                 /* Only increment the check variable if no errors have\r
199                                                 occurred. */\r
200                                                 portENTER_CRITICAL();\r
201                                                         xPollingConsumerCount++;\r
202                                                 portEXIT_CRITICAL();\r
203                                         }\r
204                                 }\r
205 \r
206                                 /* Next time round we would expect the number to be one higher. */\r
207                                 usExpectedValue++;\r
208                         }\r
209                 }\r
210 \r
211                 /* Now the queue is empty we block, allowing the producer to place more\r
212                 items in the queue. */\r
213                 vTaskDelay( pollqCONSUMER_DELAY );\r
214         }\r
215 } /*lint !e818 Function prototype must conform to API. */\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 /* This is called to check that all the created tasks are still running with no errors. */\r
219 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
220 {\r
221 portBASE_TYPE xReturn;\r
222 \r
223         /* Check both the consumer and producer poll count to check they have both\r
224         been changed since out last trip round.  We do not need a critical section\r
225         around the check variables as this is called from a higher priority than\r
226         the other tasks that access the same variables. */\r
227         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
228                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
229           )\r
230         {\r
231                 xReturn = pdFALSE;\r
232         }\r
233         else\r
234         {\r
235                 xReturn = pdTRUE;\r
236         }\r
237 \r
238         /* Set the check variables back down so we know if they have been\r
239         incremented the next time around. */\r
240         xPollingConsumerCount = pollqINITIAL_VALUE;\r
241         xPollingProducerCount = pollqINITIAL_VALUE;\r
242 \r
243         return xReturn;\r
244 }\r