]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/AltPollQ.c
b9089b3e3367899567f473fc041342c0507bf3a0
[freertos] / FreeRTOS / Demo / Common / Minimal / AltPollQ.c
1 /*\r
2     FreeRTOS V8.2.2 - Copyright (C) 2015 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  * This is a version of PollQ.c that uses the alternative (Alt) API.\r
72  * \r
73  * Creates two tasks that communicate over a single queue.  One task acts as a\r
74  * producer, the other a consumer.\r
75  *\r
76  * The producer loops for three iteration, posting an incrementing number onto the\r
77  * queue each cycle.  It then delays for a fixed period before doing exactly the\r
78  * same again.\r
79  *\r
80  * The consumer loops emptying the queue.  Each item removed from the queue is\r
81  * checked to ensure it contains the expected value.  When the queue is empty it\r
82  * blocks for a fixed period, then does the same again.\r
83  *\r
84  * All queue access is performed without blocking.  The consumer completely empties\r
85  * the queue each time it runs so the producer should never find the queue full.\r
86  *\r
87  * An error is flagged if the consumer obtains an unexpected value or the producer\r
88  * find the queue is full.\r
89  */\r
90 \r
91 /*\r
92 Changes from V2.0.0\r
93 \r
94         + Delay periods are now specified using variables and constants of\r
95           TickType_t rather than uint32_t.\r
96 */\r
97 \r
98 #include <stdlib.h>\r
99 \r
100 /* Scheduler include files. */\r
101 #include "FreeRTOS.h"\r
102 #include "task.h"\r
103 #include "queue.h"\r
104 \r
105 /* Demo program include files. */\r
106 #include "AltPollQ.h"\r
107 \r
108 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
109 #define pollqQUEUE_SIZE                 ( 10 )\r
110 #define pollqPRODUCER_DELAY             ( ( TickType_t ) 200 / portTICK_PERIOD_MS )\r
111 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( TickType_t ) ( 20 / portTICK_PERIOD_MS ) )\r
112 #define pollqNO_DELAY                   ( ( TickType_t ) 0 )\r
113 #define pollqVALUES_TO_PRODUCE  ( ( BaseType_t ) 3 )\r
114 #define pollqINITIAL_VALUE              ( ( BaseType_t ) 0 )\r
115 \r
116 /* The task that posts the incrementing number onto the queue. */\r
117 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
118 \r
119 /* The task that empties the queue. */\r
120 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
121 \r
122 /* Variables that are used to check that the tasks are still running with no\r
123 errors. */\r
124 static volatile BaseType_t xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
125 \r
126 /*-----------------------------------------------------------*/\r
127 \r
128 void vStartAltPolledQueueTasks( UBaseType_t uxPriority )\r
129 {\r
130 static QueueHandle_t xPolledQueue;\r
131 \r
132         /* Create the queue used by the producer and consumer. */\r
133         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( UBaseType_t ) sizeof( uint16_t ) );\r
134 \r
135         /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
136         in use.  The queue registry is provided as a means for kernel aware \r
137         debuggers to locate queues and has no purpose if a kernel aware debugger\r
138         is not being used.  The call to vQueueAddToRegistry() will be removed\r
139         by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is \r
140         defined to be less than 1. */\r
141         vQueueAddToRegistry( xPolledQueue, "AltPollQueue" );\r
142 \r
143 \r
144         /* Spawn the producer and consumer. */\r
145         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( TaskHandle_t * ) NULL );\r
146         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( TaskHandle_t * ) NULL );\r
147 }\r
148 /*-----------------------------------------------------------*/\r
149 \r
150 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
151 {\r
152 uint16_t usValue = ( uint16_t ) 0;\r
153 BaseType_t xError = pdFALSE, xLoop;\r
154 \r
155         #ifdef USE_STDIO\r
156         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
157         \r
158                 const char * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n";\r
159 \r
160                 /* Queue a message for printing to say the task has started. */\r
161                 vPrintDisplayMessage( &pcTaskStartMsg );\r
162         #endif\r
163 \r
164         for( ;; )\r
165         {               \r
166                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
167                 {\r
168                         /* Send an incrementing number on the queue without blocking. */\r
169                         if( xQueueAltSendToBack( *( ( QueueHandle_t * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
170                         {\r
171                                 /* We should never find the queue full so if we get here there\r
172                                 has been an error. */\r
173                                 xError = pdTRUE;\r
174                         }\r
175                         else\r
176                         {\r
177                                 if( xError == pdFALSE )\r
178                                 {\r
179                                         /* If an error has ever been recorded we stop incrementing the\r
180                                         check variable. */\r
181                                         portENTER_CRITICAL();\r
182                                                 xPollingProducerCount++;\r
183                                         portEXIT_CRITICAL();\r
184                                 }\r
185 \r
186                                 /* Update the value we are going to post next time around. */\r
187                                 usValue++;\r
188                         }\r
189                 }\r
190 \r
191                 /* Wait before we start posting again to ensure the consumer runs and\r
192                 empties the queue. */\r
193                 vTaskDelay( pollqPRODUCER_DELAY );\r
194         }\r
195 }  /*lint !e818 Function prototype must conform to API. */\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
199 {\r
200 uint16_t usData, usExpectedValue = ( uint16_t ) 0;\r
201 BaseType_t xError = pdFALSE;\r
202 \r
203         #ifdef USE_STDIO\r
204         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
205         \r
206                 const char * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n";\r
207 \r
208                 /* Queue a message for printing to say the task has started. */\r
209                 vPrintDisplayMessage( &pcTaskStartMsg );\r
210         #endif\r
211 \r
212         for( ;; )\r
213         {               \r
214                 /* Loop until the queue is empty. */\r
215                 while( uxQueueMessagesWaiting( *( ( QueueHandle_t * ) pvParameters ) ) )\r
216                 {\r
217                         if( xQueueAltReceive( *( ( QueueHandle_t * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
218                         {\r
219                                 if( usData != usExpectedValue )\r
220                                 {\r
221                                         /* This is not what we expected to receive so an error has\r
222                                         occurred. */\r
223                                         xError = pdTRUE;\r
224 \r
225                                         /* Catch-up to the value we received so our next expected\r
226                                         value should again be correct. */\r
227                                         usExpectedValue = usData;\r
228                                 }\r
229                                 else\r
230                                 {\r
231                                         if( xError == pdFALSE )\r
232                                         {\r
233                                                 /* Only increment the check variable if no errors have\r
234                                                 occurred. */\r
235                                                 portENTER_CRITICAL();\r
236                                                         xPollingConsumerCount++;\r
237                                                 portEXIT_CRITICAL();\r
238                                         }\r
239                                 }\r
240 \r
241                                 /* Next time round we would expect the number to be one higher. */\r
242                                 usExpectedValue++;\r
243                         }\r
244                 }\r
245 \r
246                 /* Now the queue is empty we block, allowing the producer to place more\r
247                 items in the queue. */\r
248                 vTaskDelay( pollqCONSUMER_DELAY );\r
249         }\r
250 } /*lint !e818 Function prototype must conform to API. */\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 /* This is called to check that all the created tasks are still running with no errors. */\r
254 BaseType_t xAreAltPollingQueuesStillRunning( void )\r
255 {\r
256 BaseType_t xReturn;\r
257 \r
258         /* Check both the consumer and producer poll count to check they have both\r
259         been changed since out last trip round.  We do not need a critical section\r
260         around the check variables as this is called from a higher priority than\r
261         the other tasks that access the same variables. */\r
262         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
263                 ( xPollingProducerCount == pollqINITIAL_VALUE )\r
264           )\r
265         {\r
266                 xReturn = pdFALSE;\r
267         }\r
268         else\r
269         {\r
270                 xReturn = pdTRUE;\r
271         }\r
272 \r
273         /* Set the check variables back down so we know if they have been\r
274         incremented the next time around. */\r
275         xPollingConsumerCount = pollqINITIAL_VALUE;\r
276         xPollingProducerCount = pollqINITIAL_VALUE;\r
277 \r
278         return xReturn;\r
279 }\r