]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/AltPollQ.c
Update to V5.4.2. See http://www.freertos.org/History.txt .
[freertos] / Demo / Common / Minimal / AltPollQ.c
1 /*\r
2         FreeRTOS V5.4.2 - Copyright (C) 2009 Real Time Engineers Ltd.\r
3 \r
4         This file is part of the FreeRTOS distribution.\r
5 \r
6         FreeRTOS is free software; you can redistribute it and/or modify it     under \r
7         the terms of the GNU General Public License (version 2) as published by the \r
8         Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS without being obliged to provide the \r
11         source code for proprietary components outside of the FreeRTOS kernel.  \r
12         Alternative commercial license and support terms are also available upon \r
13         request.  See the licensing section of http://www.FreeRTOS.org for full \r
14         license details.\r
15 \r
16         FreeRTOS is distributed in the hope that it will be useful,     but WITHOUT\r
17         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
18         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
19         more details.\r
20 \r
21         You should have received a copy of the GNU General Public License along\r
22         with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59\r
23         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
24 \r
25 \r
26         ***************************************************************************\r
27         *                                                                         *\r
28         * Looking for a quick start?  Then check out the FreeRTOS eBook!          *\r
29         * See http://www.FreeRTOS.org/Documentation for details                   *\r
30         *                                                                         *\r
31         ***************************************************************************\r
32 \r
33         1 tab == 4 spaces!\r
34 \r
35         Please ensure to read the configuration and relevant port sections of the\r
36         online documentation.\r
37 \r
38         http://www.FreeRTOS.org - Documentation, latest information, license and\r
39         contact details.\r
40 \r
41         http://www.SafeRTOS.com - A version that is certified for use in safety\r
42         critical systems.\r
43 \r
44         http://www.OpenRTOS.com - Commercial support, development, porting,\r
45         licensing and training services.\r
46 */\r
47 \r
48 /*\r
49  * This is a version of PollQ.c that uses the alternative (Alt) API.\r
50  * \r
51  * Creates two tasks that communicate over a single queue.  One task acts as a\r
52  * producer, the other a consumer.\r
53  *\r
54  * The producer loops for three iteration, posting an incrementing number onto the\r
55  * queue each cycle.  It then delays for a fixed period before doing exactly the\r
56  * same again.\r
57  *\r
58  * The consumer loops emptying the queue.  Each item removed from the queue is\r
59  * checked to ensure it contains the expected value.  When the queue is empty it\r
60  * blocks for a fixed period, then does the same again.\r
61  *\r
62  * All queue access is performed without blocking.  The consumer completely empties\r
63  * the queue each time it runs so the producer should never find the queue full.\r
64  *\r
65  * An error is flagged if the consumer obtains an unexpected value or the producer\r
66  * find the queue is full.\r
67  */\r
68 \r
69 /*\r
70 Changes from V2.0.0\r
71 \r
72         + Delay periods are now specified using variables and constants of\r
73           portTickType rather than unsigned portLONG.\r
74 */\r
75 \r
76 #include <stdlib.h>\r
77 \r
78 /* Scheduler include files. */\r
79 #include "FreeRTOS.h"\r
80 #include "task.h"\r
81 #include "queue.h"\r
82 \r
83 /* Demo program include files. */\r
84 #include "AltPollQ.h"\r
85 \r
86 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
87 #define pollqQUEUE_SIZE                 ( 10 )\r
88 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )\r
89 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )\r
90 #define pollqNO_DELAY                   ( ( portTickType ) 0 )\r
91 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )\r
92 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )\r
93 \r
94 /* The task that posts the incrementing number onto the queue. */\r
95 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
96 \r
97 /* The task that empties the queue. */\r
98 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
99 \r
100 /* Variables that are used to check that the tasks are still running with no\r
101 errors. */\r
102 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
103 \r
104 /*-----------------------------------------------------------*/\r
105 \r
106 void vStartAltPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
107 {\r
108 static xQueueHandle xPolledQueue;\r
109 \r
110         /* Create the queue used by the producer and consumer. */\r
111         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
112 \r
113         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
114         in use.  The queue registry is provided as a means for kernel aware \r
115         debuggers to locate queues and has no purpose if a kernel aware debugger\r
116         is not being used.  The call to vQueueAddToRegistry() will be removed\r
117         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
118         defined to be less than 1. */\r
119         vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "AltPollQueue" );\r
120 \r
121 \r
122         /* Spawn the producer and consumer. */\r
123         xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
124         xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
125 }\r
126 /*-----------------------------------------------------------*/\r
127 \r
128 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
129 {\r
130 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;\r
131 signed portBASE_TYPE xError = pdFALSE, xLoop;\r
132 \r
133         #ifdef USE_STDIO\r
134         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
135         \r
136                 const portCHAR * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n";\r
137 \r
138                 /* Queue a message for printing to say the task has started. */\r
139                 vPrintDisplayMessage( &pcTaskStartMsg );\r
140         #endif\r
141 \r
142         for( ;; )\r
143         {               \r
144                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
145                 {\r
146                         /* Send an incrementing number on the queue without blocking. */\r
147                         if( xQueueAltSendToBack( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
148                         {\r
149                                 /* We should never find the queue full so if we get here there\r
150                                 has been an error. */\r
151                                 xError = pdTRUE;\r
152                         }\r
153                         else\r
154                         {\r
155                                 if( xError == pdFALSE )\r
156                                 {\r
157                                         /* If an error has ever been recorded we stop incrementing the\r
158                                         check variable. */\r
159                                         portENTER_CRITICAL();\r
160                                                 xPollingProducerCount++;\r
161                                         portEXIT_CRITICAL();\r
162                                 }\r
163 \r
164                                 /* Update the value we are going to post next time around. */\r
165                                 usValue++;\r
166                         }\r
167                 }\r
168 \r
169                 /* Wait before we start posting again to ensure the consumer runs and\r
170                 empties the queue. */\r
171                 vTaskDelay( pollqPRODUCER_DELAY );\r
172         }\r
173 }  /*lint !e818 Function prototype must conform to API. */\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
177 {\r
178 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;\r
179 signed portBASE_TYPE xError = pdFALSE;\r
180 \r
181         #ifdef USE_STDIO\r
182         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
183         \r
184                 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";\r
185 \r
186                 /* Queue a message for printing to say the task has started. */\r
187                 vPrintDisplayMessage( &pcTaskStartMsg );\r
188         #endif\r
189 \r
190         for( ;; )\r
191         {               \r
192                 /* Loop until the queue is empty. */\r
193                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )\r
194                 {\r
195                         if( xQueueAltReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
196                         {\r
197                                 if( usData != usExpectedValue )\r
198                                 {\r
199                                         /* This is not what we expected to receive so an error has\r
200                                         occurred. */\r
201                                         xError = pdTRUE;\r
202 \r
203                                         /* Catch-up to the value we received so our next expected\r
204                                         value should again be correct. */\r
205                                         usExpectedValue = usData;\r
206                                 }\r
207                                 else\r
208                                 {\r
209                                         if( xError == pdFALSE )\r
210                                         {\r
211                                                 /* Only increment the check variable if no errors have\r
212                                                 occurred. */\r
213                                                 portENTER_CRITICAL();\r
214                                                         xPollingConsumerCount++;\r
215                                                 portEXIT_CRITICAL();\r
216                                         }\r
217                                 }\r
218 \r
219                                 /* Next time round we would expect the number to be one higher. */\r
220                                 usExpectedValue++;\r
221                         }\r
222                 }\r
223 \r
224                 /* Now the queue is empty we block, allowing the producer to place more\r
225                 items in the queue. */\r
226                 vTaskDelay( pollqCONSUMER_DELAY );\r
227         }\r
228 } /*lint !e818 Function prototype must conform to API. */\r
229 /*-----------------------------------------------------------*/\r
230 \r
231 /* This is called to check that all the created tasks are still running with no errors. */\r
232 portBASE_TYPE xAreAltPollingQueuesStillRunning( void )\r
233 {\r
234 portBASE_TYPE xReturn;\r
235 \r
236         /* Check both the consumer and producer poll count to check they have both\r
237         been changed since out last trip round.  We do not need a critical section\r
238         around the check variables as this is called from a higher priority than\r
239         the other tasks that access the same variables. */\r
240         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
241                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
242           )\r
243         {\r
244                 xReturn = pdFALSE;\r
245         }\r
246         else\r
247         {\r
248                 xReturn = pdTRUE;\r
249         }\r
250 \r
251         /* Set the check variables back down so we know if they have been\r
252         incremented the next time around. */\r
253         xPollingConsumerCount = pollqINITIAL_VALUE;\r
254         xPollingProducerCount = pollqINITIAL_VALUE;\r
255 \r
256         return xReturn;\r
257 }\r