2 * FreeRTOS Kernel V10.3.1
3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://www.FreeRTOS.org
23 * http://aws.amazon.com/freertos
30 * This is a very simple queue test. See the BlockQ. c documentation for a more
31 * comprehensive version.
33 * Creates two tasks that communicate over a single queue. One task acts as a
34 * producer, the other a consumer.
36 * The producer loops for three iteration, posting an incrementing number onto the
37 * queue each cycle. It then delays for a fixed period before doing exactly the
40 * The consumer loops emptying the queue. Each item removed from the queue is
41 * checked to ensure it contains the expected value. When the queue is empty it
42 * blocks for a fixed period, then does the same again.
44 * All queue access is performed without blocking. The consumer completely empties
45 * the queue each time it runs so the producer should never find the queue full.
47 * An error is flagged if the consumer obtains an unexpected value or the producer
48 * find the queue is full.
50 * \page PollQC pollQ.c
58 + Delay periods are now specified using variables and constants of
59 TickType_t rather than unsigned long.
64 /* Scheduler include files. */
70 /* Demo program include files. */
73 #define pollqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
75 /* The task that posts the incrementing number onto the queue. */
76 static void vPolledQueueProducer( void *pvParameters );
78 /* The task that empties the queue. */
79 static void vPolledQueueConsumer( void *pvParameters );
81 /* Variables that are used to check that the tasks are still running with no errors. */
82 static volatile short sPollingConsumerCount = 0, sPollingProducerCount = 0;
83 /*-----------------------------------------------------------*/
85 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
87 static QueueHandle_t xPolledQueue;
88 const unsigned portBASE_TYPE uxQueueSize = 10;
90 /* Create the queue used by the producer and consumer. */
91 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
93 /* Spawn the producer and consumer. */
94 xTaskCreate( vPolledQueueConsumer, "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
95 xTaskCreate( vPolledQueueProducer, "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, NULL );
97 /*-----------------------------------------------------------*/
99 static void vPolledQueueProducer( void *pvParameters )
101 unsigned short usValue = 0, usLoop;
102 QueueHandle_t *pxQueue;
103 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;
104 const unsigned short usNumToProduce = 3;
105 const char * const pcTaskStartMsg = "Polled queue producer started.\r\n";
106 const char * const pcTaskErrorMsg = "Could not post on polled queue.\r\n";
107 short sError = pdFALSE;
109 /* Queue a message for printing to say the task has started. */
110 vPrintDisplayMessage( &pcTaskStartMsg );
112 /* The queue being used is passed in as the parameter. */
113 pxQueue = ( QueueHandle_t * ) pvParameters;
117 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
119 /* Send an incrementing number on the queue without blocking. */
120 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( TickType_t ) 0 ) != pdPASS )
122 /* We should never find the queue full - this is an error. */
123 vPrintDisplayMessage( &pcTaskErrorMsg );
128 if( sError == pdFALSE )
130 /* If an error has ever been recorded we stop incrementing the
132 ++sPollingProducerCount;
135 /* Update the value we are going to post next time around. */
140 /* Wait before we start posting again to ensure the consumer runs and
141 empties the queue. */
142 vTaskDelay( xDelay );
145 /*-----------------------------------------------------------*/
147 static void vPolledQueueConsumer( void *pvParameters )
149 unsigned short usData, usExpectedValue = 0;
150 QueueHandle_t *pxQueue;
151 const TickType_t xDelay = ( TickType_t ) 200 / portTICK_PERIOD_MS;
152 const char * const pcTaskStartMsg = "Polled queue consumer started.\r\n";
153 const char * const pcTaskErrorMsg = "Incorrect value received on polled queue.\r\n";
154 short sError = pdFALSE;
156 /* Queue a message for printing to say the task has started. */
157 vPrintDisplayMessage( &pcTaskStartMsg );
159 /* The queue being used is passed in as the parameter. */
160 pxQueue = ( QueueHandle_t * ) pvParameters;
164 /* Loop until the queue is empty. */
165 while( uxQueueMessagesWaiting( *pxQueue ) )
167 if( xQueueReceive( *pxQueue, &usData, ( TickType_t ) 0 ) == pdPASS )
169 if( usData != usExpectedValue )
171 /* This is not what we expected to receive so an error has
173 vPrintDisplayMessage( &pcTaskErrorMsg );
175 /* Catch-up to the value we received so our next expected value
176 should again be correct. */
177 usExpectedValue = usData;
181 if( sError == pdFALSE )
183 /* Only increment the check variable if no errors have
185 ++sPollingConsumerCount;
192 /* Now the queue is empty we block, allowing the producer to place more
193 items in the queue. */
194 vTaskDelay( xDelay );
197 /*-----------------------------------------------------------*/
199 /* This is called to check that all the created tasks are still running with no errors. */
200 portBASE_TYPE xArePollingQueuesStillRunning( void )
202 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
203 portBASE_TYPE xReturn;
205 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
206 ( sLastPollingProducerCount == sPollingProducerCount )
216 sLastPollingConsumerCount = sPollingConsumerCount;
217 sLastPollingProducerCount = sPollingProducerCount;