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