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