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