2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\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
34 * Creates two tasks that communicate over a single queue. One task acts as a
\r
35 * producer, the other a consumer.
\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
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
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
48 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
49 * find the queue is full.
\r
55 + Delay periods are now specified using variables and constants of
\r
56 TickType_t rather than uint32_t.
\r
61 /* Scheduler include files. */
\r
62 #include "FreeRTOS.h"
\r
66 /* Demo program include files. */
\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
77 /* The task that posts the incrementing number onto the queue. */
\r
78 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters );
\r
80 /* The task that empties the queue. */
\r
81 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters );
\r
83 /* Variables that are used to check that the tasks are still running with no
\r
85 static volatile BaseType_t xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE;
\r
87 /*-----------------------------------------------------------*/
\r
89 void vStartPolledQueueTasks( UBaseType_t uxPriority )
\r
91 static QueueHandle_t xPolledQueue;
\r
93 /* Create the queue used by the producer and consumer. */
\r
94 xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( UBaseType_t ) sizeof( uint16_t ) );
\r
96 if( xPolledQueue != NULL )
\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
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
111 /*-----------------------------------------------------------*/
\r
113 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters )
\r
115 uint16_t usValue = ( uint16_t ) 0;
\r
116 BaseType_t xError = pdFALSE, xLoop;
\r
120 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ )
\r
122 /* Send an incrementing number on the queue without blocking. */
\r
123 if( xQueueSend( *( ( QueueHandle_t * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS )
\r
125 /* We should never find the queue full so if we get here there
\r
126 has been an error. */
\r
131 if( xError == pdFALSE )
\r
133 /* If an error has ever been recorded we stop incrementing the
\r
135 portENTER_CRITICAL();
\r
136 xPollingProducerCount++;
\r
137 portEXIT_CRITICAL();
\r
140 /* Update the value we are going to post next time around. */
\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
149 } /*lint !e818 Function prototype must conform to API. */
\r
150 /*-----------------------------------------------------------*/
\r
152 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters )
\r
154 uint16_t usData, usExpectedValue = ( uint16_t ) 0;
\r
155 BaseType_t xError = pdFALSE;
\r
159 /* Loop until the queue is empty. */
\r
160 while( uxQueueMessagesWaiting( *( ( QueueHandle_t * ) pvParameters ) ) )
\r
162 if( xQueueReceive( *( ( QueueHandle_t * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS )
\r
164 if( usData != usExpectedValue )
\r
166 /* This is not what we expected to receive so an error has
\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
176 if( xError == pdFALSE )
\r
178 /* Only increment the check variable if no errors have
\r
180 portENTER_CRITICAL();
\r
181 xPollingConsumerCount++;
\r
182 portEXIT_CRITICAL();
\r
186 /* Next time round we would expect the number to be one higher. */
\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
195 } /*lint !e818 Function prototype must conform to API. */
\r
196 /*-----------------------------------------------------------*/
\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
201 BaseType_t xReturn;
\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
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