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