]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/AltPollQ.c
65fffd77713c3e55d0e0b49c6a3baf0e7c30fd3b
[freertos] / FreeRTOS / Demo / Common / Minimal / AltPollQ.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76  * This is a version of PollQ.c that uses the alternative (Alt) API.\r
77  * \r
78  * Creates two tasks that communicate over a single queue.  One task acts as a\r
79  * producer, the other a consumer.\r
80  *\r
81  * The producer loops for three iteration, posting an incrementing number onto the\r
82  * queue each cycle.  It then delays for a fixed period before doing exactly the\r
83  * same again.\r
84  *\r
85  * The consumer loops emptying the queue.  Each item removed from the queue is\r
86  * checked to ensure it contains the expected value.  When the queue is empty it\r
87  * blocks for a fixed period, then does the same again.\r
88  *\r
89  * All queue access is performed without blocking.  The consumer completely empties\r
90  * the queue each time it runs so the producer should never find the queue full.\r
91  *\r
92  * An error is flagged if the consumer obtains an unexpected value or the producer\r
93  * find the queue is full.\r
94  */\r
95 \r
96 /*\r
97 Changes from V2.0.0\r
98 \r
99         + Delay periods are now specified using variables and constants of\r
100           portTickType rather than unsigned portLONG.\r
101 */\r
102 \r
103 #include <stdlib.h>\r
104 \r
105 /* Scheduler include files. */\r
106 #include "FreeRTOS.h"\r
107 #include "task.h"\r
108 #include "queue.h"\r
109 \r
110 /* Demo program include files. */\r
111 #include "AltPollQ.h"\r
112 \r
113 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
114 #define pollqQUEUE_SIZE                 ( 10 )\r
115 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )\r
116 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )\r
117 #define pollqNO_DELAY                   ( ( portTickType ) 0 )\r
118 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )\r
119 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )\r
120 \r
121 /* The task that posts the incrementing number onto the queue. */\r
122 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
123 \r
124 /* The task that empties the queue. */\r
125 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
126 \r
127 /* Variables that are used to check that the tasks are still running with no\r
128 errors. */\r
129 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
130 \r
131 /*-----------------------------------------------------------*/\r
132 \r
133 void vStartAltPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
134 {\r
135 static xQueueHandle xPolledQueue;\r
136 \r
137         /* Create the queue used by the producer and consumer. */\r
138         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) );\r
139 \r
140         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
141         in use.  The queue registry is provided as a means for kernel aware \r
142         debuggers to locate queues and has no purpose if a kernel aware debugger\r
143         is not being used.  The call to vQueueAddToRegistry() will be removed\r
144         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
145         defined to be less than 1. */\r
146         vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "AltPollQueue" );\r
147 \r
148 \r
149         /* Spawn the producer and consumer. */\r
150         xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
151         xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
156 {\r
157 unsigned portSHORT usValue = ( unsigned portSHORT ) 0;\r
158 signed portBASE_TYPE xError = pdFALSE, xLoop;\r
159 \r
160         #ifdef USE_STDIO\r
161         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
162         \r
163                 const portCHAR * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n";\r
164 \r
165                 /* Queue a message for printing to say the task has started. */\r
166                 vPrintDisplayMessage( &pcTaskStartMsg );\r
167         #endif\r
168 \r
169         for( ;; )\r
170         {               \r
171                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
172                 {\r
173                         /* Send an incrementing number on the queue without blocking. */\r
174                         if( xQueueAltSendToBack( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
175                         {\r
176                                 /* We should never find the queue full so if we get here there\r
177                                 has been an error. */\r
178                                 xError = pdTRUE;\r
179                         }\r
180                         else\r
181                         {\r
182                                 if( xError == pdFALSE )\r
183                                 {\r
184                                         /* If an error has ever been recorded we stop incrementing the\r
185                                         check variable. */\r
186                                         portENTER_CRITICAL();\r
187                                                 xPollingProducerCount++;\r
188                                         portEXIT_CRITICAL();\r
189                                 }\r
190 \r
191                                 /* Update the value we are going to post next time around. */\r
192                                 usValue++;\r
193                         }\r
194                 }\r
195 \r
196                 /* Wait before we start posting again to ensure the consumer runs and\r
197                 empties the queue. */\r
198                 vTaskDelay( pollqPRODUCER_DELAY );\r
199         }\r
200 }  /*lint !e818 Function prototype must conform to API. */\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
204 {\r
205 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0;\r
206 signed portBASE_TYPE xError = pdFALSE;\r
207 \r
208         #ifdef USE_STDIO\r
209         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
210         \r
211                 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";\r
212 \r
213                 /* Queue a message for printing to say the task has started. */\r
214                 vPrintDisplayMessage( &pcTaskStartMsg );\r
215         #endif\r
216 \r
217         for( ;; )\r
218         {               \r
219                 /* Loop until the queue is empty. */\r
220                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )\r
221                 {\r
222                         if( xQueueAltReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
223                         {\r
224                                 if( usData != usExpectedValue )\r
225                                 {\r
226                                         /* This is not what we expected to receive so an error has\r
227                                         occurred. */\r
228                                         xError = pdTRUE;\r
229 \r
230                                         /* Catch-up to the value we received so our next expected\r
231                                         value should again be correct. */\r
232                                         usExpectedValue = usData;\r
233                                 }\r
234                                 else\r
235                                 {\r
236                                         if( xError == pdFALSE )\r
237                                         {\r
238                                                 /* Only increment the check variable if no errors have\r
239                                                 occurred. */\r
240                                                 portENTER_CRITICAL();\r
241                                                         xPollingConsumerCount++;\r
242                                                 portEXIT_CRITICAL();\r
243                                         }\r
244                                 }\r
245 \r
246                                 /* Next time round we would expect the number to be one higher. */\r
247                                 usExpectedValue++;\r
248                         }\r
249                 }\r
250 \r
251                 /* Now the queue is empty we block, allowing the producer to place more\r
252                 items in the queue. */\r
253                 vTaskDelay( pollqCONSUMER_DELAY );\r
254         }\r
255 } /*lint !e818 Function prototype must conform to API. */\r
256 /*-----------------------------------------------------------*/\r
257 \r
258 /* This is called to check that all the created tasks are still running with no errors. */\r
259 portBASE_TYPE xAreAltPollingQueuesStillRunning( void )\r
260 {\r
261 portBASE_TYPE xReturn;\r
262 \r
263         /* Check both the consumer and producer poll count to check they have both\r
264         been changed since out last trip round.  We do not need a critical section\r
265         around the check variables as this is called from a higher priority than\r
266         the other tasks that access the same variables. */\r
267         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
268                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
269           )\r
270         {\r
271                 xReturn = pdFALSE;\r
272         }\r
273         else\r
274         {\r
275                 xReturn = pdTRUE;\r
276         }\r
277 \r
278         /* Set the check variables back down so we know if they have been\r
279         incremented the next time around. */\r
280         xPollingConsumerCount = pollqINITIAL_VALUE;\r
281         xPollingProducerCount = pollqINITIAL_VALUE;\r
282 \r
283         return xReturn;\r
284 }\r