]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/PollQ.c
3ba950d34645a6880ccfa13444d955a496a5b894
[freertos] / Demo / Common / Minimal / PollQ.c
1 /*\r
2         FreeRTOS.org V4.1.2 - 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  * This version of PollQ. c is for use on systems that have limited stack\r
35  * space and no display facilities.  The complete version can be found in\r
36  * the Demo/Common/Full directory.\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 \r
56 /*\r
57 Changes from V2.0.0\r
58 \r
59         + Delay periods are now specified using variables and constants of\r
60           portTickType rather than unsigned portLONG.\r
61 */\r
62 \r
63 #include <stdlib.h>\r
64 \r
65 /* Scheduler include files. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 #include "queue.h"\r
69 \r
70 /* Demo program include files. */\r
71 #include "PollQ.h"\r
72 \r
73 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
74 #define pollqQUEUE_SIZE                 ( 10 )\r
75 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )\r
76 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) 20 )\r
77 #define pollqNO_DELAY                   ( ( portTickType ) 0 )\r
78 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )\r
79 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )\r
80 \r
81 /* The task that posts the incrementing number onto the queue. */\r
82 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
83 \r
84 /* The task that empties the queue. */\r
85 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
86 \r
87 /* Variables that are used to check that the tasks are still running with no\r
88 errors. */\r
89 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
94 {\r
95 static xQueueHandle xPolledQueue;\r
96 \r
97         /* Create the queue used by the producer and consumer. */\r
98         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
99 \r
100         /* Spawn the producer and consumer. */\r
101         xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
102         xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
103 }\r
104 /*-----------------------------------------------------------*/\r
105 \r
106 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
107 {\r
108 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;\r
109 signed portBASE_TYPE xError = pdFALSE, xLoop;\r
110 \r
111         for( ;; )\r
112         {               \r
113                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
114                 {\r
115                         /* Send an incrementing number on the queue without blocking. */\r
116                         if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
117                         {\r
118                                 /* We should never find the queue full so if we get here there\r
119                                 has been an error. */\r
120                                 xError = pdTRUE;\r
121                         }\r
122                         else\r
123                         {\r
124                                 if( xError == pdFALSE )\r
125                                 {\r
126                                         /* If an error has ever been recorded we stop incrementing the\r
127                                         check variable. */\r
128                                         portENTER_CRITICAL();\r
129                                                 xPollingProducerCount++;\r
130                                         portEXIT_CRITICAL();\r
131                                 }\r
132 \r
133                                 /* Update the value we are going to post next time around. */\r
134                                 usValue++;\r
135                         }\r
136                 }\r
137 \r
138                 /* Wait before we start posting again to ensure the consumer runs and\r
139                 empties the queue. */\r
140                 vTaskDelay( pollqPRODUCER_DELAY );\r
141         }\r
142 }  /*lint !e818 Function prototype must conform to API. */\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
146 {\r
147 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;\r
148 signed portBASE_TYPE xError = pdFALSE;\r
149 \r
150         for( ;; )\r
151         {               \r
152                 /* Loop until the queue is empty. */\r
153                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )\r
154                 {\r
155                         if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
156                         {\r
157                                 if( usData != usExpectedValue )\r
158                                 {\r
159                                         /* This is not what we expected to receive so an error has\r
160                                         occurred. */\r
161                                         xError = pdTRUE;\r
162 \r
163                                         /* Catch-up to the value we received so our next expected\r
164                                         value should again be correct. */\r
165                                         usExpectedValue = usData;\r
166                                 }\r
167                                 else\r
168                                 {\r
169                                         if( xError == pdFALSE )\r
170                                         {\r
171                                                 /* Only increment the check variable if no errors have\r
172                                                 occurred. */\r
173                                                 portENTER_CRITICAL();\r
174                                                         xPollingConsumerCount++;\r
175                                                 portEXIT_CRITICAL();\r
176                                         }\r
177                                 }\r
178 \r
179                                 /* Next time round we would expect the number to be one higher. */\r
180                                 usExpectedValue++;\r
181                         }\r
182                 }\r
183 \r
184                 /* Now the queue is empty we block, allowing the producer to place more\r
185                 items in the queue. */\r
186                 vTaskDelay( pollqCONSUMER_DELAY );\r
187         }\r
188 } /*lint !e818 Function prototype must conform to API. */\r
189 /*-----------------------------------------------------------*/\r
190 \r
191 /* This is called to check that all the created tasks are still running with no errors. */\r
192 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
193 {\r
194 portBASE_TYPE xReturn;\r
195 \r
196         /* Check both the consumer and producer poll count to check they have both\r
197         been changed since out last trip round.  We do not need a critical section\r
198         around the check variables as this is called from a higher priority than\r
199         the other tasks that access the same variables. */\r
200         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
201                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
202           )\r
203         {\r
204                 xReturn = pdFALSE;\r
205         }\r
206         else\r
207         {\r
208                 xReturn = pdTRUE;\r
209         }\r
210 \r
211         /* Set the check variables back down so we know if they have been\r
212         incremented the next time around. */\r
213         xPollingConsumerCount = pollqINITIAL_VALUE;\r
214         xPollingProducerCount = pollqINITIAL_VALUE;\r
215 \r
216         return xReturn;\r
217 }\r