]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/PollQ.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Demo / Common / Full / PollQ.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 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     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 \r
70 /**\r
71  * This is a very simple queue test.  See the BlockQ. c documentation for a more \r
72  * comprehensive version.\r
73  *\r
74  * Creates two tasks that communicate over a single queue.  One task acts as a \r
75  * producer, the other a consumer.  \r
76  *\r
77  * The producer loops for three iteration, posting an incrementing number onto the \r
78  * queue each cycle.  It then delays for a fixed period before doing exactly the \r
79  * same again.\r
80  *\r
81  * The consumer loops emptying the queue.  Each item removed from the queue is \r
82  * checked to ensure it contains the expected value.  When the queue is empty it \r
83  * blocks for a fixed period, then does the same again.\r
84  *\r
85  * All queue access is performed without blocking.  The consumer completely empties \r
86  * the queue each time it runs so the producer should never find the queue full.  \r
87  *\r
88  * An error is flagged if the consumer obtains an unexpected value or the producer \r
89  * find the queue is full.\r
90  *\r
91  * \page PollQC pollQ.c\r
92  * \ingroup DemoFiles\r
93  * <HR>\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 long.\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 #include "print.h"\r
110 \r
111 /* Demo program include files. */\r
112 #include "PollQ.h"\r
113 \r
114 #define pollqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
115 \r
116 /* The task that posts the incrementing number onto the queue. */\r
117 static void vPolledQueueProducer( void *pvParameters );\r
118 \r
119 /* The task that empties the queue. */\r
120 static void vPolledQueueConsumer( void *pvParameters );\r
121 \r
122 /* Variables that are used to check that the tasks are still running with no errors. */\r
123 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
127 {\r
128 static xQueueHandle xPolledQueue;\r
129 const unsigned portBASE_TYPE uxQueueSize = 10;\r
130 \r
131         /* Create the queue used by the producer and consumer. */\r
132         xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
133 \r
134         /* Spawn the producer and consumer. */\r
135         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
136         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 static void vPolledQueueProducer( void *pvParameters )\r
141 {\r
142 unsigned short usValue = 0, usLoop;\r
143 xQueueHandle *pxQueue;\r
144 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
145 const unsigned short usNumToProduce = 3;\r
146 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";\r
147 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";\r
148 short sError = pdFALSE;\r
149 \r
150         /* Queue a message for printing to say the task has started. */\r
151         vPrintDisplayMessage( &pcTaskStartMsg );\r
152 \r
153         /* The queue being used is passed in as the parameter. */\r
154         pxQueue = ( xQueueHandle * ) pvParameters;\r
155 \r
156         for( ;; )\r
157         {               \r
158                 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )\r
159                 {\r
160                         /* Send an incrementing number on the queue without blocking. */\r
161                         if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( portTickType ) 0 ) != pdPASS )\r
162                         {\r
163                                 /* We should never find the queue full - this is an error. */\r
164                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
165                                 sError = pdTRUE;\r
166                         }\r
167                         else\r
168                         {\r
169                                 if( sError == pdFALSE )\r
170                                 {\r
171                                         /* If an error has ever been recorded we stop incrementing the \r
172                                         check variable. */\r
173                                         ++sPollingProducerCount;\r
174                                 }\r
175 \r
176                                 /* Update the value we are going to post next time around. */\r
177                                 ++usValue;\r
178                         }\r
179                 }\r
180 \r
181                 /* Wait before we start posting again to ensure the consumer runs and \r
182                 empties the queue. */\r
183                 vTaskDelay( xDelay );\r
184         }\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void vPolledQueueConsumer( void *pvParameters )\r
189 {\r
190 unsigned short usData, usExpectedValue = 0;\r
191 xQueueHandle *pxQueue;\r
192 const portTickType xDelay = ( portTickType ) 200 / portTICK_RATE_MS;\r
193 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";\r
194 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";\r
195 short sError = pdFALSE;\r
196 \r
197         /* Queue a message for printing to say the task has started. */\r
198         vPrintDisplayMessage( &pcTaskStartMsg );\r
199 \r
200         /* The queue being used is passed in as the parameter. */\r
201         pxQueue = ( xQueueHandle * ) pvParameters;\r
202 \r
203         for( ;; )\r
204         {               \r
205                 /* Loop until the queue is empty. */\r
206                 while( uxQueueMessagesWaiting( *pxQueue ) )\r
207                 {\r
208                         if( xQueueReceive( *pxQueue, &usData, ( portTickType ) 0 ) == pdPASS )\r
209                         {\r
210                                 if( usData != usExpectedValue )\r
211                                 {\r
212                                         /* This is not what we expected to receive so an error has \r
213                                         occurred. */\r
214                                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
215                                         sError = pdTRUE;\r
216                                         /* Catch-up to the value we received so our next expected value \r
217                                         should again be correct. */\r
218                                         usExpectedValue = usData;\r
219                                 }\r
220                                 else\r
221                                 {\r
222                                         if( sError == pdFALSE )\r
223                                         {\r
224                                                 /* Only increment the check variable if no errors have \r
225                                                 occurred. */\r
226                                                 ++sPollingConsumerCount;\r
227                                         }\r
228                                 }\r
229                                 ++usExpectedValue;\r
230                         }\r
231                 }\r
232 \r
233                 /* Now the queue is empty we block, allowing the producer to place more \r
234                 items in the queue. */\r
235                 vTaskDelay( xDelay );\r
236         }\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 /* This is called to check that all the created tasks are still running with no errors. */\r
241 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
242 {\r
243 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;\r
244 portBASE_TYPE xReturn;\r
245 \r
246         if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||\r
247                 ( sLastPollingProducerCount == sPollingProducerCount ) \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         sLastPollingConsumerCount = sPollingConsumerCount;\r
258         sLastPollingProducerCount = sPollingProducerCount;\r
259 \r
260         return xReturn;\r
261 }\r