]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/PollQ.c
Correct task names in BlockQ.c.
[freertos] / Demo / Common / Minimal / PollQ.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /*\r
55  * This version of PollQ. c is for use on systems that have limited stack\r
56  * space and no display facilities.  The complete version can be found in\r
57  * the Demo/Common/Full directory.\r
58  *\r
59  * Creates two tasks that communicate over a single queue.  One task acts as a\r
60  * producer, the other a consumer.\r
61  *\r
62  * The producer loops for three iteration, posting an incrementing number onto the\r
63  * queue each cycle.  It then delays for a fixed period before doing exactly the\r
64  * same again.\r
65  *\r
66  * The consumer loops emptying the queue.  Each item removed from the queue is\r
67  * checked to ensure it contains the expected value.  When the queue is empty it\r
68  * blocks for a fixed period, then does the same again.\r
69  *\r
70  * All queue access is performed without blocking.  The consumer completely empties\r
71  * the queue each time it runs so the producer should never find the queue full.\r
72  *\r
73  * An error is flagged if the consumer obtains an unexpected value or the producer\r
74  * find the queue is full.\r
75  */\r
76 \r
77 /*\r
78 Changes from V2.0.0\r
79 \r
80         + Delay periods are now specified using variables and constants of\r
81           portTickType rather than unsigned long.\r
82 */\r
83 \r
84 #include <stdlib.h>\r
85 \r
86 /* Scheduler include files. */\r
87 #include "FreeRTOS.h"\r
88 #include "task.h"\r
89 #include "queue.h"\r
90 \r
91 /* Demo program include files. */\r
92 #include "PollQ.h"\r
93 \r
94 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
95 #define pollqQUEUE_SIZE                 ( 10 )\r
96 #define pollqPRODUCER_DELAY             ( ( portTickType ) 200 / portTICK_RATE_MS )\r
97 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) )\r
98 #define pollqNO_DELAY                   ( ( portTickType ) 0 )\r
99 #define pollqVALUES_TO_PRODUCE  ( ( signed portBASE_TYPE ) 3 )\r
100 #define pollqINITIAL_VALUE              ( ( signed portBASE_TYPE ) 0 )\r
101 \r
102 /* The task that posts the incrementing number onto the queue. */\r
103 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
104 \r
105 /* The task that empties the queue. */\r
106 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
107 \r
108 /* Variables that are used to check that the tasks are still running with no\r
109 errors. */\r
110 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
115 {\r
116 static xQueueHandle xPolledQueue;\r
117 \r
118         /* Create the queue used by the producer and consumer. */\r
119         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
120 \r
121         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
122         in use.  The queue registry is provided as a means for kernel aware \r
123         debuggers to locate queues and has no purpose if a kernel aware debugger\r
124         is not being used.  The call to vQueueAddToRegistry() will be removed\r
125         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
126         defined to be less than 1. */\r
127         vQueueAddToRegistry( xPolledQueue, ( signed char * ) "Poll_Test_Queue" );\r
128 \r
129         /* Spawn the producer and consumer. */\r
130         xTaskCreate( vPolledQueueConsumer, ( signed char * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
131         xTaskCreate( vPolledQueueProducer, ( signed char * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL );\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
136 {\r
137 unsigned short usValue = ( unsigned short ) 0;\r
138 signed portBASE_TYPE xError = pdFALSE, xLoop;\r
139 \r
140         for( ;; )\r
141         {               \r
142                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
143                 {\r
144                         /* Send an incrementing number on the queue without blocking. */\r
145                         if( xQueueSend( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
146                         {\r
147                                 /* We should never find the queue full so if we get here there\r
148                                 has been an error. */\r
149                                 xError = pdTRUE;\r
150                         }\r
151                         else\r
152                         {\r
153                                 if( xError == pdFALSE )\r
154                                 {\r
155                                         /* If an error has ever been recorded we stop incrementing the\r
156                                         check variable. */\r
157                                         portENTER_CRITICAL();\r
158                                                 xPollingProducerCount++;\r
159                                         portEXIT_CRITICAL();\r
160                                 }\r
161 \r
162                                 /* Update the value we are going to post next time around. */\r
163                                 usValue++;\r
164                         }\r
165                 }\r
166 \r
167                 /* Wait before we start posting again to ensure the consumer runs and\r
168                 empties the queue. */\r
169                 vTaskDelay( pollqPRODUCER_DELAY );\r
170         }\r
171 }  /*lint !e818 Function prototype must conform to API. */\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
175 {\r
176 unsigned short usData, usExpectedValue = ( unsigned short ) 0;\r
177 signed portBASE_TYPE xError = pdFALSE;\r
178 \r
179         for( ;; )\r
180         {               \r
181                 /* Loop until the queue is empty. */\r
182                 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) )\r
183                 {\r
184                         if( xQueueReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
185                         {\r
186                                 if( usData != usExpectedValue )\r
187                                 {\r
188                                         /* This is not what we expected to receive so an error has\r
189                                         occurred. */\r
190                                         xError = pdTRUE;\r
191 \r
192                                         /* Catch-up to the value we received so our next expected\r
193                                         value should again be correct. */\r
194                                         usExpectedValue = usData;\r
195                                 }\r
196                                 else\r
197                                 {\r
198                                         if( xError == pdFALSE )\r
199                                         {\r
200                                                 /* Only increment the check variable if no errors have\r
201                                                 occurred. */\r
202                                                 portENTER_CRITICAL();\r
203                                                         xPollingConsumerCount++;\r
204                                                 portEXIT_CRITICAL();\r
205                                         }\r
206                                 }\r
207 \r
208                                 /* Next time round we would expect the number to be one higher. */\r
209                                 usExpectedValue++;\r
210                         }\r
211                 }\r
212 \r
213                 /* Now the queue is empty we block, allowing the producer to place more\r
214                 items in the queue. */\r
215                 vTaskDelay( pollqCONSUMER_DELAY );\r
216         }\r
217 } /*lint !e818 Function prototype must conform to API. */\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 /* This is called to check that all the created tasks are still running with no errors. */\r
221 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
222 {\r
223 portBASE_TYPE xReturn;\r
224 \r
225         /* Check both the consumer and producer poll count to check they have both\r
226         been changed since out last trip round.  We do not need a critical section\r
227         around the check variables as this is called from a higher priority than\r
228         the other tasks that access the same variables. */\r
229         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
230                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
231           )\r
232         {\r
233                 xReturn = pdFALSE;\r
234         }\r
235         else\r
236         {\r
237                 xReturn = pdTRUE;\r
238         }\r
239 \r
240         /* Set the check variables back down so we know if they have been\r
241         incremented the next time around. */\r
242         xPollingConsumerCount = pollqINITIAL_VALUE;\r
243         xPollingProducerCount = pollqINITIAL_VALUE;\r
244 \r
245         return xReturn;\r
246 }\r