]> begriffs open source - cmsis-freertos/blob - Demo/Common/Minimal/QueueSetPolling.c
Demo examples updated.
[cmsis-freertos] / Demo / Common / Minimal / QueueSetPolling.c
1 /*
2  * FreeRTOS Kernel V10.2.0
3  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
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.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29  * Tests the use of queue sets.
30  *
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.
39  */
40
41
42 /* Standard includes. */
43 #include <stdlib.h>
44 #include <limits.h>
45
46 /* Kernel includes. */
47 #include "FreeRTOS.h"
48 #include "task.h"
49 #include "queue.h"
50
51 /* Demo includes. */
52 #include "QueueSetPolling.h"
53
54 /* The length of each created queue. */
55 #define setpollQUEUE_LENGTH     10
56
57 /* Block times used in this demo.  A block time or 0 means "don't block". */
58 #define setpollDONT_BLOCK 0
59
60 /* The ISR sends to the queue every setpollISR_TX_PERIOD ticks. */
61 #define queuesetISR_TX_PERIOD   ( 50UL )
62
63 /*
64  * The task that reads from the queue set.
65  */
66 static void prvQueueSetReceivingTask( void *pvParameters );
67
68 /*-----------------------------------------------------------*/
69
70 /* The queue that is added to the set. */
71 static QueueHandle_t xQueue = NULL;
72
73 /* The handle of the queue set to which the queue is added. */
74 static QueueSetHandle_t xQueueSet = NULL;
75
76 /* Set to pdFAIL if an error is detected by any queue set task.
77 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */
78 static volatile BaseType_t xQueueSetPollStatus = pdPASS;
79
80 /* Counter used to ensure the task is still running. */
81 static uint32_t ulCycleCounter = 0;
82
83 /*-----------------------------------------------------------*/
84
85 void vStartQueueSetPollingTask( void )
86 {
87         /* Create the queue that is added to the set, the set, and add the queue to
88         the set. */
89         xQueue = xQueueCreate( setpollQUEUE_LENGTH, sizeof( uint32_t ) );
90         xQueueSet = xQueueCreateSet( setpollQUEUE_LENGTH );
91
92         if( ( xQueue != NULL ) && ( xQueueSet != NULL ) )
93         {
94                 xQueueAddToSet( xQueue, xQueueSet );
95
96                 /* Create the task. */
97                 xTaskCreate( prvQueueSetReceivingTask, "SetPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
98         }
99 }
100 /*-----------------------------------------------------------*/
101
102 static void prvQueueSetReceivingTask( void *pvParameters )
103 {
104 uint32_t ulReceived, ulExpected = 0;
105 QueueHandle_t xActivatedQueue;
106
107         /* Remove compiler warnings. */
108         ( void ) pvParameters;
109
110         for( ;; )
111         {
112                 /* Is a message waiting?  A block time is not used to ensure the queue
113                 set is polled while it is being written to from an interrupt. */
114                 xActivatedQueue = xQueueSelectFromSet( xQueueSet, setpollDONT_BLOCK );
115
116                 if( xActivatedQueue != NULL )
117                 {
118                         /* Reading from the queue should pass with a zero block time as
119                         this task will only run when something has been posted to a task
120                         in the queue set. */
121                         if( xQueueReceive( xActivatedQueue, &ulReceived, setpollDONT_BLOCK ) != pdPASS )
122                         {
123                                 xQueueSetPollStatus = pdFAIL;
124                         }
125
126                         if( ulReceived == ulExpected )
127                         {
128                                 ulExpected++;
129                         }
130                         else
131                         {
132                                 xQueueSetPollStatus = pdFAIL;
133                         }
134
135                         if( xQueueSetPollStatus == pdPASS )
136                         {
137                                 ulCycleCounter++;
138                         }
139                 }
140         }
141 }
142 /*-----------------------------------------------------------*/
143
144 void vQueueSetPollingInterruptAccess( void )
145 {
146 static uint32_t ulCallCount = 0, ulValueToSend = 0;
147
148         /* It is intended that this function is called from the tick hook
149         function, so each call is one tick period apart. */
150         ulCallCount++;
151         if( ulCallCount > queuesetISR_TX_PERIOD )
152         {
153                 ulCallCount = 0;
154
155                 if( xQueueSendFromISR( xQueue, ( void * ) &ulValueToSend, NULL ) == pdPASS )
156                 {
157                         /* Send the next value next time. */
158                         ulValueToSend++;
159                 }
160         }
161 }
162 /*-----------------------------------------------------------*/
163
164 BaseType_t xAreQueueSetPollTasksStillRunning( void )
165 {
166 static uint32_t ulLastCycleCounter = 0;
167
168         if( ulLastCycleCounter == ulCycleCounter )
169         {
170                 xQueueSetPollStatus = pdFAIL;
171         }
172
173         ulLastCycleCounter = ulCycleCounter;
174
175         return xQueueSetPollStatus;
176 }
177 /*-----------------------------------------------------------*/
178
179