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
29 * Tests the use of queue sets.
31 * A receive task creates a number of queues and adds them to a queue set before
32 * blocking on the queue set receive. A transmit task and (optionally) an
33 * interrupt repeatedly unblocks the receive task by sending messages to the
34 * queues in a pseudo random order. The receive task removes the messages from
35 * the queues and flags an error if the received message does not match that
36 * expected. The task sends values in the range 0 to
37 * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range
38 * queuesetINITIAL_ISR_TX_VALUE to ULONG_MAX.
42 /* Standard includes. */
46 /* Kernel includes. */
52 #include "QueueSetPolling.h"
54 #if ( configUSE_QUEUE_SETS == 1 ) /* Remove tests if queue sets are not defined. */
56 /* The length of each created queue. */
57 #define setpollQUEUE_LENGTH 10
59 /* Block times used in this demo. A block time or 0 means "don't block". */
60 #define setpollDONT_BLOCK 0
62 /* The ISR sends to the queue every setpollISR_TX_PERIOD ticks. */
63 #define queuesetISR_TX_PERIOD ( 50UL )
66 * The task that reads from the queue set.
68 static void prvQueueSetReceivingTask( void * pvParameters );
70 /*-----------------------------------------------------------*/
72 /* The queue that is added to the set. */
73 static QueueHandle_t xQueue = NULL;
75 /* The handle of the queue set to which the queue is added. */
76 static QueueSetHandle_t xQueueSet = NULL;
78 /* Set to pdFAIL if an error is detected by any queue set task.
79 * ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
80 static volatile BaseType_t xQueueSetPollStatus = pdPASS;
82 /* Counter used to ensure the task is still running. */
83 static uint32_t ulCycleCounter = 0;
85 /*-----------------------------------------------------------*/
87 void vStartQueueSetPollingTask( void )
89 /* Create the queue that is added to the set, the set, and add the queue to
91 xQueue = xQueueCreate( setpollQUEUE_LENGTH, sizeof( uint32_t ) );
92 xQueueSet = xQueueCreateSet( setpollQUEUE_LENGTH );
94 if( ( xQueue != NULL ) && ( xQueueSet != NULL ) )
96 xQueueAddToSet( xQueue, xQueueSet );
98 /* Create the task. */
99 xTaskCreate( prvQueueSetReceivingTask, "SetPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
102 /*-----------------------------------------------------------*/
104 static void prvQueueSetReceivingTask( void * pvParameters )
106 uint32_t ulReceived, ulExpected = 0;
107 QueueHandle_t xActivatedQueue;
109 /* Remove compiler warnings. */
110 ( void ) pvParameters;
114 /* Is a message waiting? A block time is not used to ensure the queue
115 * set is polled while it is being written to from an interrupt. */
116 xActivatedQueue = xQueueSelectFromSet( xQueueSet, setpollDONT_BLOCK );
118 if( xActivatedQueue != NULL )
120 /* Reading from the queue should pass with a zero block time as
121 * this task will only run when something has been posted to a task
122 * in the queue set. */
123 if( xQueueReceive( xActivatedQueue, &ulReceived, setpollDONT_BLOCK ) != pdPASS )
125 xQueueSetPollStatus = pdFAIL;
128 if( ulReceived == ulExpected )
134 xQueueSetPollStatus = pdFAIL;
137 if( xQueueSetPollStatus == pdPASS )
144 /*-----------------------------------------------------------*/
146 void vQueueSetPollingInterruptAccess( void )
148 static uint32_t ulCallCount = 0, ulValueToSend = 0;
150 /* It is intended that this function is called from the tick hook
151 * function, so each call is one tick period apart. */
154 if( ulCallCount > queuesetISR_TX_PERIOD )
158 if( xQueueSendFromISR( xQueue, ( void * ) &ulValueToSend, NULL ) == pdPASS )
160 /* Send the next value next time. */
165 /*-----------------------------------------------------------*/
167 BaseType_t xAreQueueSetPollTasksStillRunning( void )
169 static uint32_t ulLastCycleCounter = 0;
171 if( ulLastCycleCounter == ulCycleCounter )
173 xQueueSetPollStatus = pdFAIL;
176 ulLastCycleCounter = ulCycleCounter;
178 return xQueueSetPollStatus;
180 /*-----------------------------------------------------------*/
183 #endif /* ( configUSE_QUEUE_SETS == 1 ) */