]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueSet.c
ef06c9be04d87503421d97d188e0e82297bb04f3
[freertos] / FreeRTOS / Demo / Common / Minimal / QueueSet.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45 \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55 \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license\r
57     and contact details.\r
58 \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell\r
63     the code with commercial support, indemnification, and middleware, under\r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under\r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /*\r
70  * Demonstrates the creation an use of queue sets.\r
71  *\r
72  * A receive task creates a number of queues and adds them to a queue set before\r
73  * blocking on a queue set receive.  A transmit task repeatedly unblocks the\r
74  * receive task by sending messages to the queues in a pseudo random order.\r
75  * The receive task removes the messages from the queues and flags an error if\r
76  * the received message does not match that expected.\r
77  */\r
78 \r
79 /* Kernel includes. */\r
80 #include <FreeRTOS.h>\r
81 #include "task.h"\r
82 #include "queue.h"\r
83 \r
84 /* Demo includes. */\r
85 #include "QueueSet.h"\r
86 \r
87 /* The number of queues that are created and added to the queue set. */\r
88 #define queuesetNUM_QUEUES_IN_SET 3\r
89 \r
90 /* The length of each created queue. */\r
91 #define queuesetQUEUE_LENGTH    3\r
92 \r
93 /* Block times used in this demo.  A block time or 0 means "don't block". */\r
94 #define queuesetSHORT_DELAY     200\r
95 #define queuesetDONT_BLOCK 0\r
96 \r
97 /*\r
98  * The task that periodically sends to the queue set.\r
99  */\r
100 static void prvQueueSetSendingTask( void *pvParameters );\r
101 \r
102 /*\r
103  * The task that reads from the queue set.\r
104  */\r
105 static void prvQueueSetReceivingTask( void *pvParameters );\r
106 \r
107 /* The queues that are added to the set. */\r
108 static xQueueHandle xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };\r
109 \r
110 /* The handle of the queue set to which the queues are added. */\r
111 static xQueueSetHandle xQueueSet;\r
112 \r
113 /* If the prvQueueSetReceivingTask() task has not detected any errors then\r
114 it increments ulCycleCounter on each iteration.\r
115 xAreQueueSetTasksStillRunning() returns pdPASS if the value of\r
116 ulCycleCounter has changed between consecutive calls, and pdFALSE if\r
117 ulCycleCounter has stopped incrementing (indicating an error condition). */\r
118 volatile unsigned long ulCycleCounter = 0UL;\r
119 \r
120 /* Set to pdFAIL if an error is detected by any queue set task.\r
121 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */\r
122 volatile portBASE_TYPE xQueueSetTasksStatus = pdPASS;\r
123 \r
124 \r
125 /*-----------------------------------------------------------*/\r
126 \r
127 void vStartQueueSetTasks( unsigned portBASE_TYPE uxPriority )\r
128 {\r
129 xTaskHandle xQueueSetSendingTask;\r
130 \r
131         /* Create the two queues.  The handle of the sending task is passed into\r
132         the receiving task using the task parameter.  The receiving task uses the\r
133         handle to resume the sending task after it has created the queues. */\r
134         xTaskCreate( prvQueueSetSendingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, uxPriority, &xQueueSetSendingTask );\r
135         xTaskCreate( prvQueueSetReceivingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, uxPriority, NULL );\r
136 \r
137         /* It is important that the sending task does not attempt to write to a\r
138         queue before the queue has been created.  It is therefore placed into the\r
139         suspended state before the scheduler has started.  It is resumed by the\r
140         receiving task after the receiving task has created the queues and added the\r
141         queues to the queue set. */\r
142         vTaskSuspend( xQueueSetSendingTask );\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 portBASE_TYPE xAreQueueSetTasksStillRunning( void )\r
147 {\r
148 static unsigned long ulLastCycleCounter;\r
149 portBASE_TYPE xReturn;\r
150 \r
151         if( ulLastCycleCounter == ulCycleCounter )\r
152         {\r
153                 /* The cycle counter is no longer being incremented.  Either one of the\r
154                 tasks is stalled or an error has been detected. */\r
155                 xReturn = pdFAIL;\r
156         }\r
157         else\r
158         {\r
159                 xReturn = pdPASS;\r
160         }\r
161 \r
162         return xReturn;\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 static void prvQueueSetSendingTask( void *pvParameters )\r
167 {\r
168 unsigned long ulTxValue = 0;\r
169 portBASE_TYPE xQueueToWriteTo;\r
170 \r
171         /* Remove compiler warning about the unused parameter. */\r
172         ( void ) pvParameters;\r
173 \r
174         srand( ( unsigned int ) &ulTxValue );\r
175 \r
176         for( ;; )\r
177         {\r
178                 /* Generate the index for the queue to which a value is to be sent. */\r
179                 xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;\r
180                 if( xQueueSendToBack( xQueues[ xQueueToWriteTo ], &ulTxValue, portMAX_DELAY ) != pdPASS )\r
181                 {\r
182                         /* The send should always pass as an infinite block time was\r
183                         used. */\r
184                         xQueueSetTasksStatus = pdFAIL;\r
185                 }\r
186 \r
187                 ulTxValue++;\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 static void prvQueueSetReceivingTask( void *pvParameters )\r
193 {\r
194 unsigned long ulReceived, ulLastReceived = ~0UL;\r
195 xQueueHandle xActivatedQueue;\r
196 portBASE_TYPE x;\r
197 xTaskHandle xQueueSetSendingTask;\r
198 \r
199         /* The handle to the sending task is passed in using the task parameter. */\r
200         xQueueSetSendingTask = ( xTaskHandle ) pvParameters;\r
201 \r
202         /* Ensure the queues are created and the queue set configured before the\r
203         sending task is unsuspended.\r
204 \r
205         First Create the queue set such that it will be able to hold a message for\r
206         every space in every queue in the set. */\r
207         xQueueSet = xQueueSetCreate( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );\r
208 \r
209         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )\r
210         {\r
211                 /* Create the queue and add it to the set. */\r
212                 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( unsigned long ) );\r
213                 configASSERT( xQueues[ x ] );\r
214                 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdPASS )\r
215                 {\r
216                         xQueueSetTasksStatus = pdFAIL;\r
217                 }\r
218 \r
219                 /* The queue has now been added to the queue set and cannot be added to\r
220                 another. */\r
221                 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdFAIL )\r
222                 {\r
223                         xQueueSetTasksStatus = pdFAIL;\r
224                 }\r
225         }\r
226 \r
227         /* The task that sends to the queues is not running yet, so attempting to\r
228         read from the queue set should fail, resulting in xActivatedQueue being set\r
229         to NULL. */\r
230         xActivatedQueue = xQueueReadMultiple( xQueueSet, queuesetSHORT_DELAY );\r
231         configASSERT( xActivatedQueue == NULL );\r
232 \r
233         /* Resume the task that writes to the queues. */\r
234         vTaskResume( xQueueSetSendingTask );\r
235 \r
236         for( ;; )\r
237         {\r
238                 /* Wait for a message to arrive on one of the queues in the set. */\r
239                 xActivatedQueue = xQueueReadMultiple( xQueueSet, portMAX_DELAY );\r
240                 configASSERT( xActivatedQueue );\r
241 \r
242                 if( xActivatedQueue == NULL )\r
243                 {\r
244                         /* This should not happen as an infinite delay was used. */\r
245                         xQueueSetTasksStatus = pdFAIL;\r
246                 }\r
247                 else\r
248                 {\r
249                         /* Reading from the queue should pass with a zero block time as\r
250                         this task will only run when something has been posted to a task\r
251                         in the queue set. */\r
252                         if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )\r
253                         {\r
254                                 xQueueSetTasksStatus = pdFAIL;\r
255                         }\r
256 \r
257                         /* It is always expected that the received value will be one\r
258                         greater than the previously received value. */\r
259                         configASSERT( ulReceived == ( ulLastReceived + 1 ) );\r
260                         if( ulReceived != ( ulLastReceived + 1 ) )\r
261                         {\r
262                                 xQueueSetTasksStatus = pdFAIL;\r
263                         }\r
264                         else\r
265                         {\r
266                                 ulLastReceived = ulReceived;\r
267                         }\r
268                 }\r
269 \r
270                 if( xQueueSetTasksStatus == pdPASS )\r
271                 {\r
272                         ulCycleCounter++;\r
273                 }\r
274         }\r
275 }\r
276 \r