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