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