]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/PollQ.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / Common / Full / PollQ.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 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     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS 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 /**\r
68  * This is a very simple queue test.  See the BlockQ. c documentation for a more \r
69  * comprehensive version.\r
70  *\r
71  * Creates two tasks that communicate over a single queue.  One task acts as a \r
72  * producer, the other a consumer.  \r
73  *\r
74  * The producer loops for three iteration, posting an incrementing number onto the \r
75  * queue each cycle.  It then delays for a fixed period before doing exactly the \r
76  * same again.\r
77  *\r
78  * The consumer loops emptying the queue.  Each item removed from the queue is \r
79  * checked to ensure it contains the expected value.  When the queue is empty it \r
80  * blocks for a fixed period, then does the same again.\r
81  *\r
82  * All queue access is performed without blocking.  The consumer completely empties \r
83  * the queue each time it runs so the producer should never find the queue full.  \r
84  *\r
85  * An error is flagged if the consumer obtains an unexpected value or the producer \r
86  * find the queue is full.\r
87  *\r
88  * \page PollQC pollQ.c\r
89  * \ingroup DemoFiles\r
90  * <HR>\r
91  */\r
92 \r
93 /*\r
94 Changes from V2.0.0\r
95 \r
96         + Delay periods are now specified using variables and constants of\r
97           TickType_t rather than unsigned long.\r
98 */\r
99 \r
100 #include <stdlib.h>\r
101 \r
102 /* Scheduler include files. */\r
103 #include "FreeRTOS.h"\r
104 #include "task.h"\r
105 #include "queue.h"\r
106 #include "print.h"\r
107 \r
108 /* Demo program include files. */\r
109 #include "PollQ.h"\r
110 \r
111 #define pollqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
112 \r
113 /* The task that posts the incrementing number onto the queue. */\r
114 static void vPolledQueueProducer( void *pvParameters );\r
115 \r
116 /* The task that empties the queue. */\r
117 static void vPolledQueueConsumer( void *pvParameters );\r
118 \r
119 /* Variables that are used to check that the tasks are still running with no errors. */\r
120 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;\r
121 /*-----------------------------------------------------------*/\r
122 \r
123 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
124 {\r
125 static QueueHandle_t xPolledQueue;\r
126 const unsigned portBASE_TYPE uxQueueSize = 10;\r
127 \r
128         /* Create the queue used by the producer and consumer. */\r
129         xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
130 \r
131         /* Spawn the producer and consumer. */\r
132         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
133         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 static void vPolledQueueProducer( void *pvParameters )\r
138 {\r
139 unsigned short usValue = 0, usLoop;\r
140 QueueHandle_t *pxQueue;\r
141 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;\r
142 const unsigned short usNumToProduce = 3;\r
143 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";\r
144 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";\r
145 short sError = pdFALSE;\r
146 \r
147         /* Queue a message for printing to say the task has started. */\r
148         vPrintDisplayMessage( &pcTaskStartMsg );\r
149 \r
150         /* The queue being used is passed in as the parameter. */\r
151         pxQueue = ( QueueHandle_t * ) pvParameters;\r
152 \r
153         for( ;; )\r
154         {               \r
155                 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )\r
156                 {\r
157                         /* Send an incrementing number on the queue without blocking. */\r
158                         if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( TickType_t ) 0 ) != pdPASS )\r
159                         {\r
160                                 /* We should never find the queue full - this is an error. */\r
161                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
162                                 sError = pdTRUE;\r
163                         }\r
164                         else\r
165                         {\r
166                                 if( sError == pdFALSE )\r
167                                 {\r
168                                         /* If an error has ever been recorded we stop incrementing the \r
169                                         check variable. */\r
170                                         ++sPollingProducerCount;\r
171                                 }\r
172 \r
173                                 /* Update the value we are going to post next time around. */\r
174                                 ++usValue;\r
175                         }\r
176                 }\r
177 \r
178                 /* Wait before we start posting again to ensure the consumer runs and \r
179                 empties the queue. */\r
180                 vTaskDelay( xDelay );\r
181         }\r
182 }\r
183 /*-----------------------------------------------------------*/\r
184 \r
185 static void vPolledQueueConsumer( void *pvParameters )\r
186 {\r
187 unsigned short usData, usExpectedValue = 0;\r
188 QueueHandle_t *pxQueue;\r
189 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;\r
190 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";\r
191 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";\r
192 short sError = pdFALSE;\r
193 \r
194         /* Queue a message for printing to say the task has started. */\r
195         vPrintDisplayMessage( &pcTaskStartMsg );\r
196 \r
197         /* The queue being used is passed in as the parameter. */\r
198         pxQueue = ( QueueHandle_t * ) pvParameters;\r
199 \r
200         for( ;; )\r
201         {               \r
202                 /* Loop until the queue is empty. */\r
203                 while( uxQueueMessagesWaiting( *pxQueue ) )\r
204                 {\r
205                         if( xQueueReceive( *pxQueue, &usData, ( TickType_t ) 0 ) == pdPASS )\r
206                         {\r
207                                 if( usData != usExpectedValue )\r
208                                 {\r
209                                         /* This is not what we expected to receive so an error has \r
210                                         occurred. */\r
211                                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
212                                         sError = pdTRUE;\r
213                                         /* Catch-up to the value we received so our next expected value \r
214                                         should again be correct. */\r
215                                         usExpectedValue = usData;\r
216                                 }\r
217                                 else\r
218                                 {\r
219                                         if( sError == pdFALSE )\r
220                                         {\r
221                                                 /* Only increment the check variable if no errors have \r
222                                                 occurred. */\r
223                                                 ++sPollingConsumerCount;\r
224                                         }\r
225                                 }\r
226                                 ++usExpectedValue;\r
227                         }\r
228                 }\r
229 \r
230                 /* Now the queue is empty we block, allowing the producer to place more \r
231                 items in the queue. */\r
232                 vTaskDelay( xDelay );\r
233         }\r
234 }\r
235 /*-----------------------------------------------------------*/\r
236 \r
237 /* This is called to check that all the created tasks are still running with no errors. */\r
238 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
239 {\r
240 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;\r
241 portBASE_TYPE xReturn;\r
242 \r
243         if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||\r
244                 ( sLastPollingProducerCount == sPollingProducerCount ) \r
245           )\r
246         {\r
247                 xReturn = pdFALSE;\r
248         }\r
249         else\r
250         {\r
251                 xReturn = pdTRUE;\r
252         }\r
253 \r
254         sLastPollingConsumerCount = sPollingConsumerCount;\r
255         sLastPollingProducerCount = sPollingProducerCount;\r
256 \r
257         return xReturn;\r
258 }\r