]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/AltPollQ.c
Update to V6.0.2.
[freertos] / Demo / Common / Minimal / AltPollQ.c
1 /*\r
2     FreeRTOS V6.0.2 - Copyright (C) 2010 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS eBook                                  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\r
22     *                                                                         *\r
23     ***************************************************************************\r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     ***NOTE*** The exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public \r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it \r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained \r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * This is a version of PollQ.c that uses the alternative (Alt) API.\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 "AltPollQ.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 vStartAltPolledQueueTasks( 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 * ) "AltPollQueue" );\r
126 \r
127 \r
128         /* Spawn the producer and consumer. */\r
129         xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
130         xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
131 }\r
132 /*-----------------------------------------------------------*/\r
133 \r
134 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
135 {\r
136 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;\r
137 signed portBASE_TYPE xError = pdFALSE, xLoop;\r
138 \r
139         #ifdef USE_STDIO\r
140         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
141         \r
142                 const portCHAR * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n";\r
143 \r
144                 /* Queue a message for printing to say the task has started. */\r
145                 vPrintDisplayMessage( &pcTaskStartMsg );\r
146         #endif\r
147 \r
148         for( ;; )\r
149         {               \r
150                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
151                 {\r
152                         /* Send an incrementing number on the queue without blocking. */\r
153                         if( xQueueAltSendToBack( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
154                         {\r
155                                 /* We should never find the queue full so if we get here there\r
156                                 has been an error. */\r
157                                 xError = pdTRUE;\r
158                         }\r
159                         else\r
160                         {\r
161                                 if( xError == pdFALSE )\r
162                                 {\r
163                                         /* If an error has ever been recorded we stop incrementing the\r
164                                         check variable. */\r
165                                         portENTER_CRITICAL();\r
166                                                 xPollingProducerCount++;\r
167                                         portEXIT_CRITICAL();\r
168                                 }\r
169 \r
170                                 /* Update the value we are going to post next time around. */\r
171                                 usValue++;\r
172                         }\r
173                 }\r
174 \r
175                 /* Wait before we start posting again to ensure the consumer runs and\r
176                 empties the queue. */\r
177                 vTaskDelay( pollqPRODUCER_DELAY );\r
178         }\r
179 }  /*lint !e818 Function prototype must conform to API. */\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
183 {\r
184 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;\r
185 signed portBASE_TYPE xError = pdFALSE;\r
186 \r
187         #ifdef USE_STDIO\r
188         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
189         \r
190                 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";\r
191 \r
192                 /* Queue a message for printing to say the task has started. */\r
193                 vPrintDisplayMessage( &pcTaskStartMsg );\r
194         #endif\r
195 \r
196         for( ;; )\r
197         {               \r
198                 /* Loop until the queue is empty. */\r
199                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )\r
200                 {\r
201                         if( xQueueAltReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
202                         {\r
203                                 if( usData != usExpectedValue )\r
204                                 {\r
205                                         /* This is not what we expected to receive so an error has\r
206                                         occurred. */\r
207                                         xError = pdTRUE;\r
208 \r
209                                         /* Catch-up to the value we received so our next expected\r
210                                         value should again be correct. */\r
211                                         usExpectedValue = usData;\r
212                                 }\r
213                                 else\r
214                                 {\r
215                                         if( xError == pdFALSE )\r
216                                         {\r
217                                                 /* Only increment the check variable if no errors have\r
218                                                 occurred. */\r
219                                                 portENTER_CRITICAL();\r
220                                                         xPollingConsumerCount++;\r
221                                                 portEXIT_CRITICAL();\r
222                                         }\r
223                                 }\r
224 \r
225                                 /* Next time round we would expect the number to be one higher. */\r
226                                 usExpectedValue++;\r
227                         }\r
228                 }\r
229 \r
230                 /* Now the queue is empty we block, allowing the producer to place more\r
231                 items in the queue. */\r
232                 vTaskDelay( pollqCONSUMER_DELAY );\r
233         }\r
234 } /*lint !e818 Function prototype must conform to API. */\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 /* This is called to check that all the created tasks are still running with no errors. */\r
238 portBASE_TYPE xAreAltPollingQueuesStillRunning( void )\r
239 {\r
240 portBASE_TYPE xReturn;\r
241 \r
242         /* Check both the consumer and producer poll count to check they have both\r
243         been changed since out last trip round.  We do not need a critical section\r
244         around the check variables as this is called from a higher priority than\r
245         the other tasks that access the same variables. */\r
246         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
247                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
248           )\r
249         {\r
250                 xReturn = pdFALSE;\r
251         }\r
252         else\r
253         {\r
254                 xReturn = pdTRUE;\r
255         }\r
256 \r
257         /* Set the check variables back down so we know if they have been\r
258         incremented the next time around. */\r
259         xPollingConsumerCount = pollqINITIAL_VALUE;\r
260         xPollingProducerCount = pollqINITIAL_VALUE;\r
261 \r
262         return xReturn;\r
263 }\r