]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/QueueSet.c
809aff1ed416147fa9f666685eb48a046abdc38e
[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 the queue set receive.  A transmit task and (optionally) an\r
74  * interrupt repeatedly unblocks the receive task by sending messages to the\r
75  * queues in a pseudo random order.  The receive task removes the messages from\r
76  * the queues and flags an error if the received message does not match that\r
77  * expected.  The task sends values in the range 0 to\r
78  * queuesetINITIAL_ISR_TX_VALUE, and the ISR sends value in the range\r
79  * queuesetINITIAL_ISR_TX_VALUE to 0xffffffffUL;\r
80  */\r
81 \r
82 /* Standard includes. */\r
83 #include <stdlib.h>\r
84 \r
85 /* Kernel includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 #include "queue.h"\r
89 \r
90 /* Demo includes. */\r
91 #include "QueueSet.h"\r
92 \r
93 /* The number of queues that are created and added to the queue set. */\r
94 #define queuesetNUM_QUEUES_IN_SET 3\r
95 \r
96 /* The length of each created queue. */\r
97 #define queuesetQUEUE_LENGTH    3\r
98 \r
99 /* Block times used in this demo.  A block time or 0 means "don't block". */\r
100 #define queuesetSHORT_DELAY     200\r
101 #define queuesetDONT_BLOCK 0\r
102 \r
103 /* Messages are sent in incrementing order from both a task and an interrupt.\r
104 The task sends values in the range 0 to 0xfffe, and the interrupt sends values\r
105 in the range of 0xffff to 0xffffffff; */\r
106 #define queuesetINITIAL_ISR_TX_VALUE 0xffffUL\r
107 \r
108 /* The priorities used in this demo. */\r
109 #define queuesetLOW_PRIORITY    ( tskIDLE_PRIORITY )\r
110 #define queuesetMEDIUM_PRIORITY ( queuesetLOW_PRIORITY + 1 )\r
111 #define queuesetHIGH_PRIORITY   ( queuesetMEDIUM_PRIORITY + 1 )\r
112 \r
113 /* For test purposes the priority of the sending task is changed after every\r
114 queuesetPRIORITY_CHANGE_LOOPS number of values are sent to a queue. */\r
115 #define queuesetPRIORITY_CHANGE_LOOPS   100UL\r
116 \r
117 /* The ISR sends to the queue every queuesetISR_TX_PERIOD ticks. */\r
118 #define queuesetISR_TX_PERIOD   ( 100UL )\r
119 \r
120 /*\r
121  * The task that periodically sends to the queue set.\r
122  */\r
123 static void prvQueueSetSendingTask( void *pvParameters );\r
124 \r
125 /*\r
126  * The task that reads from the queue set.\r
127  */\r
128 static void prvQueueSetReceivingTask( void *pvParameters );\r
129 \r
130 /* The queues that are added to the set. */\r
131 static xQueueHandle xQueues[ queuesetNUM_QUEUES_IN_SET ] = { 0 };\r
132 \r
133 /* Counts how many times each queue in the set is used to ensure all the\r
134 queues are used. */\r
135 static unsigned long ulQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };\r
136 \r
137 /* The handle of the queue set to which the queues are added. */\r
138 static xQueueSetHandle xQueueSet;\r
139 \r
140 /* If the prvQueueSetReceivingTask() task has not detected any errors then\r
141 it increments ulCycleCounter on each iteration.\r
142 xAreQueueSetTasksStillRunning() returns pdPASS if the value of\r
143 ulCycleCounter has changed between consecutive calls, and pdFALSE if\r
144 ulCycleCounter has stopped incrementing (indicating an error condition). */\r
145 static volatile unsigned long ulCycleCounter = 0UL;\r
146 \r
147 /* Set to pdFAIL if an error is detected by any queue set task.\r
148 ulCycleCounter will only be incremented if xQueueSetTasksSatus equals pdPASS. */\r
149 static volatile portBASE_TYPE xQueueSetTasksStatus = pdPASS;\r
150 \r
151 /* Just a flag to let the function that writes to a queue from an ISR know that\r
152 the queues are setup and can be used. */\r
153 static volatile portBASE_TYPE xSetupComplete = pdFALSE;\r
154 \r
155 /* The value sent to the queue from the ISR is file scope so the\r
156 xAreQueeuSetTasksStillRunning() function can check it is incrementing as\r
157 expected. */\r
158 static volatile unsigned long ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;\r
159 \r
160 /*-----------------------------------------------------------*/\r
161 \r
162 void vStartQueueSetTasks( void )\r
163 {\r
164 xTaskHandle xQueueSetSendingTask;\r
165 \r
166         /* Create the two queues.  The handle of the sending task is passed into\r
167         the receiving task using the task parameter.  The receiving task uses the\r
168         handle to resume the sending task after it has created the queues. */\r
169         xTaskCreate( prvQueueSetSendingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, NULL, queuesetMEDIUM_PRIORITY, &xQueueSetSendingTask );\r
170         xTaskCreate( prvQueueSetReceivingTask, ( signed char * ) "Check", configMINIMAL_STACK_SIZE, ( void * ) xQueueSetSendingTask, queuesetMEDIUM_PRIORITY, NULL );\r
171 \r
172         /* It is important that the sending task does not attempt to write to a\r
173         queue before the queue has been created.  It is therefore placed into the\r
174         suspended state before the scheduler has started.  It is resumed by the\r
175         receiving task after the receiving task has created the queues and added the\r
176         queues to the queue set. */\r
177         vTaskSuspend( xQueueSetSendingTask );\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 portBASE_TYPE xAreQueueSetTasksStillRunning( void )\r
182 {\r
183 static unsigned long ulLastCycleCounter, ulLastISRTxValue = 0;\r
184 static unsigned long ulLastQueueUsedCounter[ queuesetNUM_QUEUES_IN_SET ] = { 0 };\r
185 portBASE_TYPE xReturn = pdPASS, x;\r
186 \r
187         if( ulLastCycleCounter == ulCycleCounter )\r
188         {\r
189                 /* The cycle counter is no longer being incremented.  Either one of the\r
190                 tasks is stalled or an error has been detected. */\r
191                 xReturn = pdFAIL;\r
192         }\r
193 \r
194         ulLastCycleCounter = ulCycleCounter;\r
195 \r
196         /* Ensure that all the queues in the set have been used.  This ensures the\r
197         test is working as intended and guards against the rand() in the Tx task\r
198         missing some values. */\r
199         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )\r
200         {\r
201                 if( ulLastQueueUsedCounter[ x ] == ulQueueUsedCounter[ x ] )\r
202                 {\r
203                         xReturn = pdFAIL;\r
204                 }\r
205 \r
206                 ulLastQueueUsedCounter[ x ] = ulQueueUsedCounter[ x ];\r
207         }\r
208 \r
209         /* Check the global status flag. */\r
210         if( xQueueSetTasksStatus != pdPASS )\r
211         {\r
212                 xReturn = pdFAIL;\r
213         }\r
214 \r
215         /* Check that the ISR is still sending values to the queues too. */\r
216         if( ulISRTxValue == ulLastISRTxValue )\r
217         {\r
218                 xReturn = pdFAIL;\r
219         }\r
220         else\r
221         {\r
222                 ulLastISRTxValue = ulISRTxValue;\r
223         }\r
224 \r
225         return xReturn;\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 void vQueueSetWriteToQueueFromISR( void )\r
230 {\r
231 portBASE_TYPE x = 0;\r
232 static unsigned long ulCallCount = 0;\r
233 \r
234         /* xSetupComplete is set to pdTRUE when the queues have been created and\r
235         are available for use. */\r
236         if( xSetupComplete == pdTRUE )\r
237         {\r
238                 /* It is intended that this function is called from the tick hook\r
239                 function, so each call is one tick period apart. */\r
240                 ulCallCount++;\r
241                 if( ulCallCount > queuesetISR_TX_PERIOD )\r
242                 {\r
243                         ulCallCount = 0;\r
244 \r
245                         /* Look for a queue that can be written to. */\r
246                         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )\r
247                         {\r
248                                 if( xQueues[ x ] != NULL )\r
249                                 {\r
250                                         /* xQueues[ x ] can be written to.  Send the next value. */\r
251                                         if( xQueueSendFromISR( xQueues[ x ], ( void * ) &ulISRTxValue, NULL ) == pdPASS )\r
252                                         {\r
253                                                 ulISRTxValue++;\r
254 \r
255                                                 /* If the Tx value has wrapped then set it back to its\r
256                                                 initial value. */\r
257                                                 if( ulISRTxValue == 0UL )\r
258                                                 {\r
259                                                         ulISRTxValue = queuesetINITIAL_ISR_TX_VALUE;\r
260                                                 }\r
261                                         }\r
262 \r
263                                         break;\r
264                                 }\r
265                         }\r
266                 }\r
267         }\r
268 }\r
269 /*-----------------------------------------------------------*/\r
270 \r
271 static void prvQueueSetSendingTask( void *pvParameters )\r
272 {\r
273 unsigned long ulTaskTxValue = 0;\r
274 portBASE_TYPE xQueueToWriteTo;\r
275 xQueueHandle xQueueInUse;\r
276 unsigned portBASE_TYPE uxPriority = queuesetMEDIUM_PRIORITY, ulLoops = 0;\r
277 \r
278         /* Remove compiler warning about the unused parameter. */\r
279         ( void ) pvParameters;\r
280 \r
281         srand( ( unsigned int ) &ulTaskTxValue );\r
282 \r
283         for( ;; )\r
284         {\r
285                 /* Generate the index for the queue to which a value is to be sent. */\r
286                 xQueueToWriteTo = rand() % queuesetNUM_QUEUES_IN_SET;\r
287                 xQueueInUse = xQueues[ xQueueToWriteTo ];\r
288 \r
289                 /* Note which index is being written to to ensure all the queues are\r
290                 used. */\r
291                 ( ulQueueUsedCounter[ xQueueToWriteTo ] )++;\r
292 \r
293                 /* Send to the queue to unblock the task that is waiting for data to\r
294                 arrive on a queue within the queue set to which this queue belongs. */\r
295                 if( xQueueSendToBack( xQueueInUse, &ulTaskTxValue, portMAX_DELAY ) != pdPASS )\r
296                 {\r
297                         /* The send should always pass as an infinite block time was\r
298                         used. */\r
299                         xQueueSetTasksStatus = pdFAIL;\r
300                 }\r
301 \r
302                 /* Attempt to remove the queue from a queue set it does not belong\r
303                 to (NULL being passed as the queue set in this case). */\r
304                 if( xQueueRemoveFromQueueSet( xQueueInUse, NULL ) != pdFAIL )\r
305                 {\r
306                         /* It is not possible to successfully remove a queue from a queue\r
307                         set it does not belong to. */\r
308                         xQueueSetTasksStatus = pdFAIL;\r
309                 }\r
310 \r
311                 /* Mark the space in the array of queues as being empty before actually\r
312                 removing the queue from the queue set.  This is to prevent the code\r
313                 that accesses the queue set from an interrupt from attempting to access\r
314                 a queue that is no longer in the set. */\r
315                 xQueues[ xQueueToWriteTo ] = 0;\r
316 \r
317                 /* Attempt to remove the queue from the queue set it does belong to. */\r
318                 if( xQueueRemoveFromQueueSet( xQueueInUse, xQueueSet ) != pdPASS )\r
319                 {\r
320                         /* It should be possible to remove the queue from the queue set it\r
321                         does belong to. */\r
322                         xQueueSetTasksStatus = pdFAIL;\r
323                 }\r
324 \r
325                 /* Add the queue back before cycling back to run again. */\r
326                 if( xQueueAddToQueueSet( xQueueInUse, xQueueSet ) != pdPASS )\r
327                 {\r
328                         /* If the queue was successfully removed from the queue set then it\r
329                         should be possible to add it back in again. */\r
330                         xQueueSetTasksStatus = pdFAIL;\r
331                 }\r
332 \r
333                 /* Now the queue is back in the set it is ok for the interrupt that\r
334                 writes to the queues to access it again. */\r
335                 xQueues[ xQueueToWriteTo ] = xQueueInUse;\r
336 \r
337                 ulTaskTxValue++;\r
338 \r
339                 /* If the Tx value has reached the range used by the ISR then set it\r
340                 back to 0. */\r
341                 if( ulTaskTxValue == queuesetINITIAL_ISR_TX_VALUE )\r
342                 {\r
343                         ulTaskTxValue = 0;\r
344                 }\r
345 \r
346                 /* Occasionally change the task priority relative to the priority of\r
347                 the receiving task. */\r
348                 ulLoops++;\r
349                 if( ulLoops >= queuesetPRIORITY_CHANGE_LOOPS )\r
350                 {\r
351                         ulLoops = 0;\r
352                         uxPriority++;\r
353                         if( uxPriority > queuesetHIGH_PRIORITY )\r
354                         {\r
355                                 uxPriority = queuesetLOW_PRIORITY;\r
356                         }\r
357 \r
358                         vTaskPrioritySet( NULL, uxPriority );\r
359                 }\r
360         }\r
361 }\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 static void prvQueueSetReceivingTask( void *pvParameters )\r
365 {\r
366 unsigned long ulReceived, ulExpectedReceivedFromTask = 0, ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;\r
367 xQueueHandle xActivatedQueue;\r
368 portBASE_TYPE x;\r
369 xTaskHandle xQueueSetSendingTask;\r
370 \r
371         /* The handle to the sending task is passed in using the task parameter. */\r
372         xQueueSetSendingTask = ( xTaskHandle ) pvParameters;\r
373 \r
374         /* Ensure the queues are created and the queue set configured before the\r
375         sending task is unsuspended.\r
376 \r
377         First Create the queue set such that it will be able to hold a message for\r
378         every space in every queue in the set. */\r
379         xQueueSet = xQueueSetCreate( queuesetNUM_QUEUES_IN_SET * queuesetQUEUE_LENGTH );\r
380 \r
381         for( x = 0; x < queuesetNUM_QUEUES_IN_SET; x++ )\r
382         {\r
383                 /* Create the queue and add it to the set.  The queue is just holding\r
384                 unsigned long value. */\r
385                 xQueues[ x ] = xQueueCreate( queuesetQUEUE_LENGTH, sizeof( unsigned long ) );\r
386                 configASSERT( xQueues[ x ] );\r
387                 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdPASS )\r
388                 {\r
389                         xQueueSetTasksStatus = pdFAIL;\r
390                 }\r
391 \r
392                 /* The queue has now been added to the queue set and cannot be added to\r
393                 another. */\r
394                 if( xQueueAddToQueueSet( xQueues[ x ], xQueueSet ) != pdFAIL )\r
395                 {\r
396                         xQueueSetTasksStatus = pdFAIL;\r
397                 }\r
398         }\r
399 \r
400         /* The task that sends to the queues is not running yet, so attempting to\r
401         read from the queue set should fail, resulting in xActivatedQueue being set\r
402         to NULL. */\r
403         xActivatedQueue = xQueueBlockMultiple( xQueueSet, queuesetSHORT_DELAY );\r
404         configASSERT( xActivatedQueue == NULL );\r
405 \r
406         /* Resume the task that writes to the queues. */\r
407         vTaskResume( xQueueSetSendingTask );\r
408 \r
409         /* Let the ISR access the queues also. */\r
410         xSetupComplete = pdTRUE;\r
411 \r
412         for( ;; )\r
413         {\r
414                 /* Wait for a message to arrive on one of the queues in the set. */\r
415                 xActivatedQueue = xQueueBlockMultiple( xQueueSet, portMAX_DELAY );\r
416                 configASSERT( xActivatedQueue );\r
417 \r
418                 if( xActivatedQueue == NULL )\r
419                 {\r
420                         /* This should not happen as an infinite delay was used. */\r
421                         xQueueSetTasksStatus = pdFAIL;\r
422                 }\r
423                 else\r
424                 {\r
425                         /* Reading from the queue should pass with a zero block time as\r
426                         this task will only run when something has been posted to a task\r
427                         in the queue set. */\r
428                         if( xQueueReceive( xActivatedQueue, &ulReceived, queuesetDONT_BLOCK ) != pdPASS )\r
429                         {\r
430                                 xQueueSetTasksStatus = pdFAIL;\r
431                         }\r
432 \r
433                         /* If the received value is equal to or greater than\r
434                         queuesetINITIAL_ISR_TX_VALUE then it was sent by an ISR. */\r
435                         if( ulReceived >= queuesetINITIAL_ISR_TX_VALUE )\r
436                         {\r
437                                 /* The value was sent from the ISR.  Check it against its\r
438                                 expected value. */\r
439                                 configASSERT( ulReceived == ulExpectedReceivedFromISR );\r
440                                 if( ulReceived != ulExpectedReceivedFromISR )\r
441                                 {\r
442                                         xQueueSetTasksStatus = pdFAIL;\r
443                                 }\r
444                                 else\r
445                                 {\r
446                                         /* It is expected to receive an incrementing value. */\r
447                                         ulExpectedReceivedFromISR++;\r
448 \r
449                                         /* If the expected value has wrapped then set it back to\r
450                                         its initial value. */\r
451                                         if( ulExpectedReceivedFromISR == 0 )\r
452                                         {\r
453                                                 ulExpectedReceivedFromISR = queuesetINITIAL_ISR_TX_VALUE;\r
454                                         }\r
455                                 }\r
456                         }\r
457                         else\r
458                         {\r
459                                 /* The value was sent from the Tx task.  Check it against its\r
460                                 expected value. */\r
461                                 configASSERT( ulReceived == ulExpectedReceivedFromTask );\r
462                                 if( ulReceived != ulExpectedReceivedFromTask )\r
463                                 {\r
464                                         xQueueSetTasksStatus = pdFAIL;\r
465                                 }\r
466                                 else\r
467                                 {\r
468                                         /* It is expected to receive an incrementing value. */\r
469                                         ulExpectedReceivedFromTask++;\r
470 \r
471                                         /* If the expected value has reached the range of values\r
472                                         used by the ISR then set it back to 0. */\r
473                                         if( ulExpectedReceivedFromTask >= queuesetINITIAL_ISR_TX_VALUE )\r
474                                         {\r
475                                                 ulExpectedReceivedFromTask = 0;\r
476                                         }\r
477                                 }\r
478                         }\r
479                 }\r
480 \r
481                 if( xQueueSetTasksStatus == pdPASS )\r
482                 {\r
483                         ulCycleCounter++;\r
484                 }\r
485         }\r
486 }\r
487 \r