]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueSetPolling.c
4126e2430ad3860d8269cbe99621b3be9730ce92
[freertos] / FreeRTOS / Demo / Common / Minimal / QueueSetPolling.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13         ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18         ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40         the FAQ page "My application does not run, what could be wrong?".  Have you\r
41         defined configASSERT()?\r
42 \r
43         http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44         embedded software for free we request you assist our global community by\r
45         participating in the support forum.\r
46 \r
47         http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48         be as productive as possible as early as possible.  Now you can receive\r
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50         Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * Tests the use of queue sets.\r
72  *\r
73  * A receive task creates a number of queues and adds them to a queue set before\r
74  * blocking on the queue set receive.  A transmit task and (optionally) an\r
75  * interrupt repeatedly unblocks the receive task by sending messages to the\r
76  * queues in a pseudo random order.  The receive task removes the messages from\r
77  * the queues and flags an error if the received message does not match that\r
78  * expected.  The task sends values in the range 0 to\r
79  * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range\r
80  * queuesetINITIAL_ISR_TX_VALUE to ULONG_MAX.\r
81  */\r
82 \r
83 \r
84 /* Standard includes. */\r
85 #include <stdlib.h>\r
86 #include <limits.h>\r
87 \r
88 /* Kernel includes. */\r
89 #include "FreeRTOS.h"\r
90 #include "task.h"\r
91 #include "queue.h"\r
92 \r
93 /* Demo includes. */\r
94 #include "QueueSetPolling.h"\r
95 \r
96 /* The length of each created queue. */\r
97 #define setpollQUEUE_LENGTH     10\r
98 \r
99 /* Block times used in this demo.  A block time or 0 means "don't block". */\r
100 #define setpollDONT_BLOCK 0\r
101 \r
102 /* The ISR sends to the queue every setpollISR_TX_PERIOD ticks. */\r
103 #define queuesetISR_TX_PERIOD   ( 50UL )\r
104 \r
105 /*\r
106  * The task that reads from the queue set.\r
107  */\r
108 static void prvQueueSetReceivingTask( void *pvParameters );\r
109 \r
110 /*-----------------------------------------------------------*/\r
111 \r
112 /* The queue that is added to the set. */\r
113 static QueueHandle_t xQueue = NULL;\r
114 \r
115 /* The handle of the queue set to which the queue is added. */\r
116 static QueueSetHandle_t xQueueSet = NULL;\r
117 \r
118 /* Set to pdFAIL if an error is detected by any queue set task.\r
119 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */\r
120 static volatile BaseType_t xQueueSetPollStatus = pdPASS;\r
121 \r
122 /* Counter used to ensure the task is still running. */\r
123 static uint32_t ulCycleCounter = 0;\r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vStartQueueSetPollingTask( void )\r
128 {\r
129         /* Create the queue that is added to the set, the set, and add the queue to\r
130         the set. */\r
131         xQueue = xQueueCreate( setpollQUEUE_LENGTH, sizeof( uint32_t ) );\r
132         xQueueSet = xQueueCreateSet( setpollQUEUE_LENGTH );\r
133         xQueueAddToSet( xQueue, xQueueSet );\r
134 \r
135         /* Create the task. */\r
136         xTaskCreate( prvQueueSetReceivingTask, "SetPoll", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
137 }\r
138 /*-----------------------------------------------------------*/\r
139 \r
140 static void prvQueueSetReceivingTask( void *pvParameters )\r
141 {\r
142 uint32_t ulReceived, ulExpected = 0;\r
143 QueueHandle_t xActivatedQueue;\r
144 \r
145         /* Remove compiler warnings. */\r
146         ( void ) pvParameters;\r
147 \r
148         for( ;; )\r
149         {\r
150                 /* Is a message waiting?  A block time is not used to ensure the queue\r
151                 set is polled while it is being written to from an interrupt. */\r
152                 xActivatedQueue = xQueueSelectFromSet( xQueueSet, setpollDONT_BLOCK );\r
153 \r
154                 if( xActivatedQueue != NULL )\r
155                 {\r
156                         /* Reading from the queue should pass with a zero block time as\r
157                         this task will only run when something has been posted to a task\r
158                         in the queue set. */\r
159                         if( xQueueReceive( xActivatedQueue, &ulReceived, setpollDONT_BLOCK ) != pdPASS )\r
160                         {\r
161                                 xQueueSetPollStatus = pdFAIL;\r
162                         }\r
163 \r
164                         if( ulReceived == ulExpected )\r
165                         {\r
166                                 ulExpected++;\r
167                         }\r
168                         else\r
169                         {\r
170                                 xQueueSetPollStatus = pdFAIL;\r
171                         }\r
172 \r
173                         if( xQueueSetPollStatus == pdPASS )\r
174                         {\r
175                                 ulCycleCounter++;\r
176                         }\r
177                 }\r
178         }\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vQueueSetPollingInterruptAccess( void )\r
183 {\r
184 static uint32_t ulCallCount = 0, ulValueToSend = 0;\r
185 \r
186         /* It is intended that this function is called from the tick hook\r
187         function, so each call is one tick period apart. */\r
188         ulCallCount++;\r
189         if( ulCallCount > queuesetISR_TX_PERIOD )\r
190         {\r
191                 ulCallCount = 0;\r
192 \r
193                 if( xQueueSendFromISR( xQueue, ( void * ) &ulValueToSend, NULL ) == pdPASS )\r
194                 {\r
195                         /* Send the next value next time. */\r
196                         ulValueToSend++;\r
197                 }\r
198         }\r
199 }\r
200 /*-----------------------------------------------------------*/\r
201 \r
202 BaseType_t xAreQueueSetPollTasksStillRunning( void )\r
203 {\r
204 static uint32_t ulLastCycleCounter = 0;\r
205 \r
206         if( ulLastCycleCounter == ulCycleCounter )\r
207         {\r
208                 xQueueSetPollStatus = pdFAIL;\r
209         }\r
210 \r
211         ulLastCycleCounter = ulCycleCounter;\r
212 \r
213         return xQueueSetPollStatus;\r
214 }\r
215 /*-----------------------------------------------------------*/\r
216 \r
217 \r