2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2018 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.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
30 * This is a very simple queue test. See the BlockQ. c documentation for a more
\r
31 * comprehensive version.
\r
33 * Creates two tasks that communicate over a single queue. One task acts as a
\r
34 * producer, the other a consumer.
\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
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
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
47 * An error is flagged if the consumer obtains an unexpected value or the producer
\r
48 * find the queue is full.
\r
50 * \page PollQC pollQ.c
\r
51 * \ingroup DemoFiles
\r
58 + Delay periods are now specified using variables and constants of
\r
59 TickType_t rather than unsigned long.
\r
64 /* Scheduler include files. */
\r
65 #include "FreeRTOS.h"
\r
70 /* Demo program include files. */
\r
73 #define pollqSTACK_SIZE ( ( unsigned short ) configMINIMAL_STACK_SIZE )
\r
75 /* The task that posts the incrementing number onto the queue. */
\r
76 static void vPolledQueueProducer( void *pvParameters );
\r
78 /* The task that empties the queue. */
\r
79 static void vPolledQueueConsumer( void *pvParameters );
\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
85 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
87 static QueueHandle_t xPolledQueue;
\r
88 const unsigned portBASE_TYPE uxQueueSize = 10;
\r
90 /* Create the queue used by the producer and consumer. */
\r
91 xPolledQueue = xQueueCreate( uxQueueSize, ( unsigned portBASE_TYPE ) sizeof( unsigned short ) );
\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
97 /*-----------------------------------------------------------*/
\r
99 static void vPolledQueueProducer( void *pvParameters )
\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
109 /* Queue a message for printing to say the task has started. */
\r
110 vPrintDisplayMessage( &pcTaskStartMsg );
\r
112 /* The queue being used is passed in as the parameter. */
\r
113 pxQueue = ( QueueHandle_t * ) pvParameters;
\r
117 for( usLoop = 0; usLoop < usNumToProduce; ++usLoop )
\r
119 /* Send an incrementing number on the queue without blocking. */
\r
120 if( xQueueSendToBack( *pxQueue, ( void * ) &usValue, ( TickType_t ) 0 ) != pdPASS )
\r
122 /* We should never find the queue full - this is an error. */
\r
123 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
128 if( sError == pdFALSE )
\r
130 /* If an error has ever been recorded we stop incrementing the
\r
132 ++sPollingProducerCount;
\r
135 /* Update the value we are going to post next time around. */
\r
140 /* Wait before we start posting again to ensure the consumer runs and
\r
141 empties the queue. */
\r
142 vTaskDelay( xDelay );
\r
145 /*-----------------------------------------------------------*/
\r
147 static void vPolledQueueConsumer( void *pvParameters )
\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
156 /* Queue a message for printing to say the task has started. */
\r
157 vPrintDisplayMessage( &pcTaskStartMsg );
\r
159 /* The queue being used is passed in as the parameter. */
\r
160 pxQueue = ( QueueHandle_t * ) pvParameters;
\r
164 /* Loop until the queue is empty. */
\r
165 while( uxQueueMessagesWaiting( *pxQueue ) )
\r
167 if( xQueueReceive( *pxQueue, &usData, ( TickType_t ) 0 ) == pdPASS )
\r
169 if( usData != usExpectedValue )
\r
171 /* This is not what we expected to receive so an error has
\r
173 vPrintDisplayMessage( &pcTaskErrorMsg );
\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
181 if( sError == pdFALSE )
\r
183 /* Only increment the check variable if no errors have
\r
185 ++sPollingConsumerCount;
\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
197 /*-----------------------------------------------------------*/
\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
202 static short sLastPollingConsumerCount = 0, sLastPollingProducerCount = 0;
\r
203 portBASE_TYPE xReturn;
\r
205 if( ( sLastPollingConsumerCount == sPollingConsumerCount ) ||
\r
206 ( sLastPollingProducerCount == sPollingProducerCount )
\r
216 sLastPollingConsumerCount = sPollingConsumerCount;
\r
217 sLastPollingProducerCount = sPollingProducerCount;
\r