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