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