]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/PollQ.c
a0c503f9b512e8113c542397bca28ea3083b8953
[freertos] / FreeRTOS / Demo / Common / Full / PollQ.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /**\r
67  * This is a very simple queue test.  See the BlockQ. c documentation for a more \r
68  * comprehensive version.\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  * \page PollQC pollQ.c\r
88  * \ingroup DemoFiles\r
89  * <HR>\r
90  */\r
91 \r
92 /*\r
93 Changes from V2.0.0\r
94 \r
95         + Delay periods are now specified using variables and constants of\r
96           portTickType rather than unsigned long.\r
97 */\r
98 \r
99 #include <stdlib.h>\r
100 \r
101 /* Scheduler include files. */\r
102 #include "FreeRTOS.h"\r
103 #include "task.h"\r
104 #include "queue.h"\r
105 #include "print.h"\r
106 \r
107 /* Demo program include files. */\r
108 #include "PollQ.h"\r
109 \r
110 #define pollqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
111 \r
112 /* The task that posts the incrementing number onto the queue. */\r
113 static void vPolledQueueProducer( void *pvParameters );\r
114 \r
115 /* The task that empties the queue. */\r
116 static void vPolledQueueConsumer( void *pvParameters );\r
117 \r
118 /* Variables that are used to check that the tasks are still running with no errors. */\r
119 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;\r
120 /*-----------------------------------------------------------*/\r
121 \r
122 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
123 {\r
124 static xQueueHandle xPolledQueue;\r
125 const unsigned portBASE_TYPE uxQueueSize = 10;\r
126 \r
127         /* Create the queue used by the producer and consumer. */\r
128         xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
129 \r
130         /* Spawn the producer and consumer. */\r
131         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
132         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
133 }\r
134 /*-----------------------------------------------------------*/\r
135 \r
136 static void vPolledQueueProducer( void *pvParameters )\r
137 {\r
138 unsigned short usValue = 0, usLoop;\r
139 xQueueHandle *pxQueue;\r
140 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
141 const unsigned short usNumToProduce = 3;\r
142 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";\r
143 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";\r
144 short sError = pdFALSE;\r
145 \r
146         /* Queue a message for printing to say the task has started. */\r
147         vPrintDisplayMessage( &pcTaskStartMsg );\r
148 \r
149         /* The queue being used is passed in as the parameter. */\r
150         pxQueue = ( xQueueHandle * ) pvParameters;\r
151 \r
152         for( ;; )\r
153         {               \r
154                 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )\r
155                 {\r
156                         /* Send an incrementing number on the queue without blocking. */\r
157                         if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )\r
158                         {\r
159                                 /* We should never find the queue full - this is an error. */\r
160                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
161                                 sError = pdTRUE;\r
162                         }\r
163                         else\r
164                         {\r
165                                 if( sError == pdFALSE )\r
166                                 {\r
167                                         /* If an error has ever been recorded we stop incrementing the \r
168                                         check variable. */\r
169                                         ++sPollingProducerCount;\r
170                                 }\r
171 \r
172                                 /* Update the value we are going to post next time around. */\r
173                                 ++usValue;\r
174                         }\r
175                 }\r
176 \r
177                 /* Wait before we start posting again to ensure the consumer runs and \r
178                 empties the queue. */\r
179                 vTaskDelay( xDelay );\r
180         }\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 static void vPolledQueueConsumer( void *pvParameters )\r
185 {\r
186 unsigned short usData, usExpectedValue = 0;\r
187 xQueueHandle *pxQueue;\r
188 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
189 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";\r
190 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";\r
191 short sError = pdFALSE;\r
192 \r
193         /* Queue a message for printing to say the task has started. */\r
194         vPrintDisplayMessage( &pcTaskStartMsg );\r
195 \r
196         /* The queue being used is passed in as the parameter. */\r
197         pxQueue = ( xQueueHandle * ) pvParameters;\r
198 \r
199         for( ;; )\r
200         {               \r
201                 /* Loop until the queue is empty. */\r
202                 while( uxQueueMessagesWaiting( *pxQueue ) )\r
203                 {\r
204                         if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )\r
205                         {\r
206                                 if( usData != usExpectedValue )\r
207                                 {\r
208                                         /* This is not what we expected to receive so an error has \r
209                                         occurred. */\r
210                                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
211                                         sError = pdTRUE;\r
212                                         /* Catch-up to the value we received so our next expected value \r
213                                         should again be correct. */\r
214                                         usExpectedValue = usData;\r
215                                 }\r
216                                 else\r
217                                 {\r
218                                         if( sError == pdFALSE )\r
219                                         {\r
220                                                 /* Only increment the check variable if no errors have \r
221                                                 occurred. */\r
222                                                 ++sPollingConsumerCount;\r
223                                         }\r
224                                 }\r
225                                 ++usExpectedValue;\r
226                         }\r
227                 }\r
228 \r
229                 /* Now the queue is empty we block, allowing the producer to place more \r
230                 items in the queue. */\r
231                 vTaskDelay( xDelay );\r
232         }\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 /* This is called to check that all the created tasks are still running with no errors. */\r
237 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
238 {\r
239 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;\r
240 portBASE_TYPE xReturn;\r
241 \r
242         if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||\r
243                 ( sLastPollingProducerCount == sPollingProducerCount ) \r
244           )\r
245         {\r
246                 xReturn = pdFALSE;\r
247         }\r
248         else\r
249         {\r
250                 xReturn = pdTRUE;\r
251         }\r
252 \r
253         sLastPollingConsumerCount = sPollingConsumerCount;\r
254         sLastPollingProducerCount = sPollingProducerCount;\r
255 \r
256         return xReturn;\r
257 }\r