]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/PollQ.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Full / PollQ.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /**\r
31  * This is a very simple queue test.  See the BlockQ. c documentation for a more \r
32  * comprehensive version.\r
33  *\r
34  * Creates two tasks that communicate over a single queue.  One task acts as a \r
35  * producer, the other a consumer.  \r
36  *\r
37  * The producer loops for three iteration, posting an incrementing number onto the \r
38  * queue each cycle.  It then delays for a fixed period before doing exactly the \r
39  * same again.\r
40  *\r
41  * The consumer loops emptying the queue.  Each item removed from the queue is \r
42  * checked to ensure it contains the expected value.  When the queue is empty it \r
43  * blocks for a fixed period, then does the same again.\r
44  *\r
45  * All queue access is performed without blocking.  The consumer completely empties \r
46  * the queue each time it runs so the producer should never find the queue full.  \r
47  *\r
48  * An error is flagged if the consumer obtains an unexpected value or the producer \r
49  * find the queue is full.\r
50  *\r
51  * \page PollQC pollQ.c\r
52  * \ingroup DemoFiles\r
53  * <HR>\r
54  */\r
55 \r
56 /*\r
57 Changes from V2.0.0\r
58 \r
59         + Delay periods are now specified using variables and constants of\r
60           TickType_t rather than unsigned long.\r
61 */\r
62 \r
63 #include <stdlib.h>\r
64 \r
65 /* Scheduler include files. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 #include "queue.h"\r
69 #include "print.h"\r
70 \r
71 /* Demo program include files. */\r
72 #include "PollQ.h"\r
73 \r
74 #define pollqSTACK_SIZE         ( ( unsigned short ) configMINIMAL_STACK_SIZE )\r
75 \r
76 /* The task that posts the incrementing number onto the queue. */\r
77 static void vPolledQueueProducer( void *pvParameters );\r
78 \r
79 /* The task that empties the queue. */\r
80 static void vPolledQueueConsumer( void *pvParameters );\r
81 \r
82 /* Variables that are used to check that the tasks are still running with no errors. */\r
83 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;\r
84 /*-----------------------------------------------------------*/\r
85 \r
86 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )\r
87 {\r
88 static QueueHandle_t xPolledQueue;\r
89 const unsigned portBASE_TYPE uxQueueSize = 10;\r
90 \r
91         /* Create the queue used by the producer and consumer. */\r
92         xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );\r
93 \r
94         /* Spawn the producer and consumer. */\r
95         xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
96         xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );\r
97 }\r
98 /*-----------------------------------------------------------*/\r
99 \r
100 static void vPolledQueueProducer( void *pvParameters )\r
101 {\r
102 unsigned short usValue = 0, usLoop;\r
103 QueueHandle_t *pxQueue;\r
104 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;\r
105 const unsigned short usNumToProduce = 3;\r
106 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";\r
107 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";\r
108 short sError = pdFALSE;\r
109 \r
110         /* Queue a message for printing to say the task has started. */\r
111         vPrintDisplayMessage( &pcTaskStartMsg );\r
112 \r
113         /* The queue being used is passed in as the parameter. */\r
114         pxQueue = ( QueueHandle_t * ) pvParameters;\r
115 \r
116         for( ;; )\r
117         {               \r
118                 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )\r
119                 {\r
120                         /* Send an incrementing number on the queue without blocking. */\r
121                         if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( TickType_t ) 0 ) != pdPASS )\r
122                         {\r
123                                 /* We should never find the queue full - this is an error. */\r
124                                 vPrintDisplayMessage( &pcTaskErrorMsg );\r
125                                 sError = pdTRUE;\r
126                         }\r
127                         else\r
128                         {\r
129                                 if( sError == pdFALSE )\r
130                                 {\r
131                                         /* If an error has ever been recorded we stop incrementing the \r
132                                         check variable. */\r
133                                         ++sPollingProducerCount;\r
134                                 }\r
135 \r
136                                 /* Update the value we are going to post next time around. */\r
137                                 ++usValue;\r
138                         }\r
139                 }\r
140 \r
141                 /* Wait before we start posting again to ensure the consumer runs and \r
142                 empties the queue. */\r
143                 vTaskDelay( xDelay );\r
144         }\r
145 }\r
146 /*-----------------------------------------------------------*/\r
147 \r
148 static void vPolledQueueConsumer( void *pvParameters )\r
149 {\r
150 unsigned short usData, usExpectedValue = 0;\r
151 QueueHandle_t *pxQueue;\r
152 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;\r
153 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";\r
154 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";\r
155 short sError = pdFALSE;\r
156 \r
157         /* Queue a message for printing to say the task has started. */\r
158         vPrintDisplayMessage( &pcTaskStartMsg );\r
159 \r
160         /* The queue being used is passed in as the parameter. */\r
161         pxQueue = ( QueueHandle_t * ) pvParameters;\r
162 \r
163         for( ;; )\r
164         {               \r
165                 /* Loop until the queue is empty. */\r
166                 while( uxQueueMessagesWaiting( *pxQueue ) )\r
167                 {\r
168                         if( xQueueReceive( *pxQueue, &usData, ( TickType_t ) 0 ) == pdPASS )\r
169                         {\r
170                                 if( usData != usExpectedValue )\r
171                                 {\r
172                                         /* This is not what we expected to receive so an error has \r
173                                         occurred. */\r
174                                         vPrintDisplayMessage( &pcTaskErrorMsg );\r
175                                         sError = pdTRUE;\r
176                                         /* Catch-up to the value we received so our next expected value \r
177                                         should again be correct. */\r
178                                         usExpectedValue = usData;\r
179                                 }\r
180                                 else\r
181                                 {\r
182                                         if( sError == pdFALSE )\r
183                                         {\r
184                                                 /* Only increment the check variable if no errors have \r
185                                                 occurred. */\r
186                                                 ++sPollingConsumerCount;\r
187                                         }\r
188                                 }\r
189                                 ++usExpectedValue;\r
190                         }\r
191                 }\r
192 \r
193                 /* Now the queue is empty we block, allowing the producer to place more \r
194                 items in the queue. */\r
195                 vTaskDelay( xDelay );\r
196         }\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 /* This is called to check that all the created tasks are still running with no errors. */\r
201 portBASE_TYPE xArePollingQueuesStillRunning( void )\r
202 {\r
203 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;\r
204 portBASE_TYPE xReturn;\r
205 \r
206         if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||\r
207                 ( sLastPollingProducerCount == sPollingProducerCount ) \r
208           )\r
209         {\r
210                 xReturn = pdFALSE;\r
211         }\r
212         else\r
213         {\r
214                 xReturn = pdTRUE;\r
215         }\r
216 \r
217         sLastPollingConsumerCount = sPollingConsumerCount;\r
218         sLastPollingProducerCount = sPollingProducerCount;\r
219 \r
220         return xReturn;\r
221 }\r