]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/PollQ.c
f24dd071e7af90d76cf7a128417dea084940825c
[freertos] / FreeRTOS / Demo / Common / Minimal / PollQ.c
1 /*\r
2  * FreeRTOS Kernel V10.2.1\r
3  * Copyright (C) 2019 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  * This version of PollQ. c is for use on systems that have limited stack\r
30  * space and no display facilities.  The complete version can be found in\r
31  * the Demo/Common/Full directory.\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 \r
51 /*\r
52 Changes from V2.0.0\r
53 \r
54         + Delay periods are now specified using variables and constants of\r
55           TickType_t rather than uint32_t.\r
56 */\r
57 \r
58 #include <stdlib.h>\r
59 \r
60 /* Scheduler include files. */\r
61 #include "FreeRTOS.h"\r
62 #include "task.h"\r
63 #include "queue.h"\r
64 \r
65 /* Demo program include files. */\r
66 #include "PollQ.h"\r
67 \r
68 #define pollqSTACK_SIZE                 configMINIMAL_STACK_SIZE\r
69 #define pollqQUEUE_SIZE                 ( 10 )\r
70 #define pollqPRODUCER_DELAY             ( pdMS_TO_TICKS( ( TickType_t ) 200 ) )\r
71 #define pollqCONSUMER_DELAY             ( pollqPRODUCER_DELAY - ( TickType_t ) ( 20 / portTICK_PERIOD_MS ) )\r
72 #define pollqNO_DELAY                   ( ( TickType_t ) 0 )\r
73 #define pollqVALUES_TO_PRODUCE  ( ( BaseType_t ) 3 )\r
74 #define pollqINITIAL_VALUE              ( ( BaseType_t ) 0 )\r
75 \r
76 /* The task that posts the incrementing number onto the queue. */\r
77 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );\r
78 \r
79 /* The task that empties the queue. */\r
80 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );\r
81 \r
82 /* Variables that are used to check that the tasks are still running with no\r
83 errors. */\r
84 static volatile BaseType_t xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;\r
85 \r
86 /*-----------------------------------------------------------*/\r
87 \r
88 void vStartPolledQueueTasks( UBaseType_t uxPriority )\r
89 {\r
90 static QueueHandle_t xPolledQueue;\r
91 \r
92         /* Create the queue used by the producer and consumer. */\r
93         xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( UBaseType_t ) sizeof( uint16_t ) );\r
94 \r
95         if( xPolledQueue != NULL )\r
96         {\r
97                 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is\r
98                 in use.  The queue registry is provided as a means for kernel aware\r
99                 debuggers to locate queues and has no purpose if a kernel aware debugger\r
100                 is not being used.  The call to vQueueAddToRegistry() will be removed\r
101                 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
102                 defined to be less than 1. */\r
103                 vQueueAddToRegistry( xPolledQueue, "Poll_Test_Queue" );\r
104 \r
105                 /* Spawn the producer and consumer. */\r
106                 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( TaskHandle_t * ) NULL );\r
107                 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( TaskHandle_t * ) NULL );\r
108         }\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )\r
113 {\r
114 uint16_t usValue = ( uint16_t ) 0;\r
115 BaseType_t xError = pdFALSE, xLoop;\r
116 \r
117         for( ;; )\r
118         {\r
119                 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )\r
120                 {\r
121                         /* Send an incrementing number on the queue without blocking. */\r
122                         if( xQueueSend( *( ( QueueHandle_t * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )\r
123                         {\r
124                                 /* We should never find the queue full so if we get here there\r
125                                 has been an error. */\r
126                                 xError = pdTRUE;\r
127                         }\r
128                         else\r
129                         {\r
130                                 if( xError == pdFALSE )\r
131                                 {\r
132                                         /* If an error has ever been recorded we stop incrementing the\r
133                                         check variable. */\r
134                                         portENTER_CRITICAL();\r
135                                                 xPollingProducerCount++;\r
136                                         portEXIT_CRITICAL();\r
137                                 }\r
138 \r
139                                 /* Update the value we are going to post next time around. */\r
140                                 usValue++;\r
141                         }\r
142                 }\r
143 \r
144                 /* Wait before we start posting again to ensure the consumer runs and\r
145                 empties the queue. */\r
146                 vTaskDelay( pollqPRODUCER_DELAY );\r
147         }\r
148 }  /*lint !e818 Function prototype must conform to API. */\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )\r
152 {\r
153 uint16_t usData, usExpectedValue = ( uint16_t ) 0;\r
154 BaseType_t xError = pdFALSE;\r
155 \r
156         for( ;; )\r
157         {\r
158                 /* Loop until the queue is empty. */\r
159                 while( uxQueueMessagesWaiting( *( ( QueueHandle_t * ) pvParameters ) ) )\r
160                 {\r
161                         if( xQueueReceive( *( ( QueueHandle_t * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )\r
162                         {\r
163                                 if( usData != usExpectedValue )\r
164                                 {\r
165                                         /* This is not what we expected to receive so an error has\r
166                                         occurred. */\r
167                                         xError = pdTRUE;\r
168 \r
169                                         /* Catch-up to the value we received so our next expected\r
170                                         value should again be correct. */\r
171                                         usExpectedValue = usData;\r
172                                 }\r
173                                 else\r
174                                 {\r
175                                         if( xError == pdFALSE )\r
176                                         {\r
177                                                 /* Only increment the check variable if no errors have\r
178                                                 occurred. */\r
179                                                 portENTER_CRITICAL();\r
180                                                         xPollingConsumerCount++;\r
181                                                 portEXIT_CRITICAL();\r
182                                         }\r
183                                 }\r
184 \r
185                                 /* Next time round we would expect the number to be one higher. */\r
186                                 usExpectedValue++;\r
187                         }\r
188                 }\r
189 \r
190                 /* Now the queue is empty we block, allowing the producer to place more\r
191                 items in the queue. */\r
192                 vTaskDelay( pollqCONSUMER_DELAY );\r
193         }\r
194 } /*lint !e818 Function prototype must conform to API. */\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 /* This is called to check that all the created tasks are still running with no errors. */\r
198 BaseType_t xArePollingQueuesStillRunning( void )\r
199 {\r
200 BaseType_t xReturn;\r
201 \r
202         /* Check both the consumer and producer poll count to check they have both\r
203         been changed since out last trip round.  We do not need a critical section\r
204         around the check variables as this is called from a higher priority than\r
205         the other tasks that access the same variables. */\r
206         if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) ||\r
207                 ( xPollingProducerCount == pollqINITIAL_VALUE )\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         /* Set the check variables back down so we know if they have been\r
218         incremented the next time around. */\r
219         xPollingConsumerCount = pollqINITIAL_VALUE;\r
220         xPollingProducerCount = pollqINITIAL_VALUE;\r
221 \r
222         return xReturn;\r
223 }\r