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