]> git.sur5r.net Git - freertos/blob - Demo/Common/Minimal/GenQTest.c
906fea42c5506ac8036afb15491b7555a8fdc500
[freertos] / Demo / Common / Minimal / GenQTest.c
1 /*\r
2         FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com a version that has been certified for use\r
32         in safety critical systems, plus commercial licensing, development and\r
33         support options.\r
34         ***************************************************************************\r
35 */\r
36 \r
37 \r
38 /* \r
39  * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 - \r
40  * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and \r
41  * mutex behaviour. \r
42  *\r
43  * See the comments above the prvSendFrontAndBackTest() and \r
44  * prvLowPriorityMutexTask() prototypes below for more information.\r
45  */\r
46 \r
47 \r
48 #include <stdlib.h>\r
49 \r
50 /* Scheduler include files. */\r
51 #include "FreeRTOS.h"\r
52 #include "task.h"\r
53 #include "queue.h"\r
54 #include "semphr.h"\r
55 \r
56 /* Demo program include files. */\r
57 #include "GenQTest.h"\r
58 \r
59 #define genqQUEUE_LENGTH                ( 5 )\r
60 #define genqNO_BLOCK                    ( 0 )\r
61 \r
62 #define genqMUTEX_LOW_PRIORITY          ( tskIDLE_PRIORITY )\r
63 #define genqMUTEX_TEST_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
64 #define genqMUTEX_MEDIUM_PRIORITY       ( tskIDLE_PRIORITY + 2 )\r
65 #define genqMUTEX_HIGH_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /*\r
70  * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()\r
71  * macros by using both to fill a queue, then reading from the queue to\r
72  * check the resultant queue order is as expected.  Queue data is also\r
73  * peeked.\r
74  */\r
75 static void prvSendFrontAndBackTest( void *pvParameters );\r
76 \r
77 /*\r
78  * The following three tasks are used to demonstrate the mutex behaviour.\r
79  * Each task is given a different priority to demonstrate the priority\r
80  * inheritance mechanism.\r
81  *\r
82  * The low priority task obtains a mutex.  After this a high priority task\r
83  * attempts to obtain the same mutex, causing its priority to be inherited\r
84  * by the low priority task.  The task with the inherited high priority then\r
85  * resumes a medium priority task to ensure it is not blocked by the medium\r
86  * priority task while it holds the inherited high priority.  Once the mutex\r
87  * is returned the task with the inherited priority returns to its original\r
88  * low priority, and is therefore immediately preempted by first the high\r
89  * priority task and then the medium prioroity task before it can continue.\r
90  */\r
91 static void prvLowPriorityMutexTask( void *pvParameters );\r
92 static void prvMediumPriorityMutexTask( void *pvParameters );\r
93 static void prvHighPriorityMutexTask( void *pvParameters );\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
98 detected in any of the tasks. */\r
99 static portBASE_TYPE xErrorDetected = pdFALSE;\r
100 \r
101 /* Counters that are incremented on each cycle of a test.  This is used to\r
102 detect a stalled task - a test that is no longer running. */\r
103 static volatile unsigned portLONG ulLoopCounter = 0;\r
104 static volatile unsigned portLONG ulLoopCounter2 = 0;\r
105 \r
106 /* The variable that is guarded by the mutex in the mutex demo tasks. */\r
107 static volatile unsigned portLONG ulGuardedVariable = 0;\r
108 \r
109 /* Handles used in the mutext test to suspend and resume the high and medium\r
110 priority mutex test tasks. */\r
111 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 void vStartGenericQueueTasks( unsigned portBASE_TYPE uxPriority )\r
116 {\r
117 xQueueHandle xQueue;\r
118 xSemaphoreHandle xMutex;\r
119 \r
120         /* Create the queue that we are going to use for the\r
121         prvSendFrontAndBackTest demo. */\r
122         xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );\r
123 \r
124         /* Create the demo task and pass it the queue just created.  We are\r
125         passing the queue handle by value so it does not matter that it is\r
126         declared on the stack here. */\r
127         xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * )"GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );\r
128 \r
129         /* Create the mutex used by the prvMutexTest task. */\r
130         xMutex = xSemaphoreCreateMutex();\r
131 \r
132         /* Create the mutex demo tasks and pass it the mutex just created.  We are\r
133         passing the mutex handle by value so it does not matter that it is declared\r
134         on the stack here. */\r
135         xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * )"MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );\r
136         xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * )"MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );\r
137         xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * )"MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 static void prvSendFrontAndBackTest( void *pvParameters )\r
142 {\r
143 unsigned portLONG ulData, ulData2;\r
144 xQueueHandle xQueue;\r
145 \r
146         #ifdef USE_STDIO\r
147         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
148         \r
149                 const portCHAR * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";\r
150 \r
151                 /* Queue a message for printing to say the task has started. */\r
152                 vPrintDisplayMessage( &pcTaskStartMsg );\r
153         #endif\r
154 \r
155         xQueue = ( xQueueHandle ) pvParameters;\r
156 \r
157         for( ;; )\r
158         {\r
159                 /* The queue is empty, so sending an item to the back of the queue\r
160                 should have the same efect as sending it to the front of the queue.\r
161 \r
162                 First send to the front and check everything is as expected. */\r
163                 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );\r
164 \r
165                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
166                 {\r
167                         xErrorDetected = pdTRUE;\r
168                 }\r
169 \r
170                 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )\r
171                 {\r
172                         xErrorDetected = pdTRUE;\r
173                 }\r
174 \r
175                 /* The data we sent to the queue should equal the data we just received\r
176                 from the queue. */\r
177                 if( ulLoopCounter != ulData )\r
178                 {\r
179                         xErrorDetected = pdTRUE;\r
180                 }\r
181 \r
182                 /* Then do the same, sending the data to the back, checking everything\r
183                 is as expected. */\r
184                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
185                 {\r
186                         xErrorDetected = pdTRUE;\r
187                 }\r
188 \r
189                 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );\r
190 \r
191                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
192                 {\r
193                         xErrorDetected = pdTRUE;\r
194                 }\r
195 \r
196                 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )\r
197                 {\r
198                         xErrorDetected = pdTRUE;\r
199                 }\r
200 \r
201                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
202                 {\r
203                         xErrorDetected = pdTRUE;\r
204                 }\r
205 \r
206                 /* The data we sent to the queue should equal the data we just received\r
207                 from the queue. */\r
208                 if( ulLoopCounter != ulData )\r
209                 {\r
210                         xErrorDetected = pdTRUE;\r
211                 }\r
212 \r
213                 #if configUSE_PREEMPTION == 0\r
214                         taskYIELD();\r
215                 #endif\r
216 \r
217 \r
218 \r
219                 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */\r
220                 for( ulData = 2; ulData < 5; ulData++ )\r
221                 {\r
222                         xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );\r
223                 }\r
224 \r
225                 /* Now the order in the queue should be 2, 3, 4, with 2 being the first\r
226                 thing to be read out.  Now add 1 then 0 to the front of the queue. */\r
227                 if( uxQueueMessagesWaiting( xQueue ) != 3 )\r
228                 {\r
229                         xErrorDetected = pdTRUE;\r
230                 }\r
231                 ulData = 1;\r
232                 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );\r
233                 ulData = 0;\r
234                 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );\r
235 \r
236                 /* Now the queue should be full, and when we read the data out we\r
237                 should receive 0, 1, 2, 3, 4. */\r
238                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
239                 {\r
240                         xErrorDetected = pdTRUE;\r
241                 }\r
242 \r
243                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
244                 {\r
245                         xErrorDetected = pdTRUE;\r
246                 }\r
247 \r
248                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
249                 {\r
250                         xErrorDetected = pdTRUE;\r
251                 }\r
252 \r
253                 #if configUSE_PREEMPTION == 0\r
254                         taskYIELD();\r
255                 #endif\r
256 \r
257                 /* Check the data we read out is in the expected order. */\r
258                 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )\r
259                 {\r
260                         /* Try peeking the data first. */\r
261                         if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )\r
262                         {\r
263                                 xErrorDetected = pdTRUE;\r
264                         }\r
265 \r
266                         if( ulData != ulData2 )\r
267                         {\r
268                                 xErrorDetected = pdTRUE;\r
269                         }\r
270                         \r
271 \r
272                         /* Now try receiving the data for real.  The value should be the\r
273                         same.  Clobber the value first so we know we really received it. */\r
274                         ulData2 = ~ulData2;\r
275                         if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )\r
276                         {\r
277                                 xErrorDetected = pdTRUE;\r
278                         }\r
279 \r
280                         if( ulData != ulData2 )\r
281                         {\r
282                                 xErrorDetected = pdTRUE;\r
283                         }\r
284                 }\r
285 \r
286                 /* The queue should now be empty again. */\r
287                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
288                 {\r
289                         xErrorDetected = pdTRUE;\r
290                 }\r
291 \r
292                 #if configUSE_PREEMPTION == 0\r
293                         taskYIELD();\r
294                 #endif\r
295 \r
296 \r
297                 /* Our queue is empty once more, add 10, 11 to the back. */\r
298                 ulData = 10;\r
299                 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )\r
300                 {\r
301                         xErrorDetected = pdTRUE;\r
302                 }\r
303                 ulData = 11;\r
304                 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )\r
305                 {\r
306                         xErrorDetected = pdTRUE;\r
307                 }\r
308 \r
309                 if( uxQueueMessagesWaiting( xQueue ) != 2 )\r
310                 {\r
311                         xErrorDetected = pdTRUE;\r
312                 }\r
313 \r
314                 /* Now we should have 10, 11 in the queue.  Add 7, 8, 9 to the\r
315                 front. */\r
316                 for( ulData = 9; ulData >= 7; ulData-- )\r
317                 {\r
318                         if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )\r
319                         {\r
320                                 xErrorDetected = pdTRUE;\r
321                         }\r
322                 }\r
323 \r
324                 /* Now check that the queue is full, and that receiving data provides\r
325                 the expected sequence of 7, 8, 9, 10, 11. */\r
326                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
327                 {\r
328                         xErrorDetected = pdTRUE;\r
329                 }\r
330 \r
331                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
332                 {\r
333                         xErrorDetected = pdTRUE;\r
334                 }\r
335 \r
336                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )\r
337                 {\r
338                         xErrorDetected = pdTRUE;\r
339                 }\r
340 \r
341                 #if configUSE_PREEMPTION == 0\r
342                         taskYIELD();\r
343                 #endif\r
344 \r
345                 /* Check the data we read out is in the expected order. */\r
346                 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )\r
347                 {\r
348                         if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )\r
349                         {\r
350                                 xErrorDetected = pdTRUE;\r
351                         }\r
352 \r
353                         if( ulData != ulData2 )\r
354                         {\r
355                                 xErrorDetected = pdTRUE;\r
356                         }\r
357                 }\r
358 \r
359                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
360                 {\r
361                         xErrorDetected = pdTRUE;\r
362                 }\r
363 \r
364                 ulLoopCounter++;\r
365         }\r
366 }\r
367 /*-----------------------------------------------------------*/\r
368 \r
369 static void prvLowPriorityMutexTask( void *pvParameters )\r
370 {\r
371 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;\r
372 \r
373         #ifdef USE_STDIO\r
374         void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );\r
375         \r
376                 const portCHAR * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";\r
377 \r
378                 /* Queue a message for printing to say the task has started. */\r
379                 vPrintDisplayMessage( &pcTaskStartMsg );\r
380         #endif\r
381 \r
382         for( ;; )\r
383         {\r
384                 /* Take the mutex.  It should be available now. */\r
385                 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )\r
386                 {\r
387                         xErrorDetected = pdTRUE;\r
388                 }\r
389 \r
390                 /* Set our guarded variable to a known start value. */\r
391                 ulGuardedVariable = 0;\r
392 \r
393                 /* Our priority should be as per that assigned when the task was\r
394                 created. */\r
395                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
396                 {\r
397                         xErrorDetected = pdTRUE;\r
398                 }\r
399 \r
400                 /* Now unsuspend the high priority task.  This will attempt to take the\r
401                 mutex, and block when it finds it cannot obtain it. */\r
402                 vTaskResume( xHighPriorityMutexTask );\r
403 \r
404                 /* We should now have inherited the prioritoy of the high priority task,\r
405                 as by now it will have attempted to get the mutex. */\r
406                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
407                 {\r
408                         xErrorDetected = pdTRUE;\r
409                 }\r
410 \r
411                 /* We can attempt to set our priority to the test priority - between the\r
412                 idle priority and the medium/high test priorities, but our actual\r
413                 prioroity should remain at the high priority. */\r
414                 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );\r
415                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
416                 {\r
417                         xErrorDetected = pdTRUE;\r
418                 }\r
419 \r
420                 /* Now unsuspend the medium priority task.  This should not run as our\r
421                 inherited priority is above that of the medium priority task. */\r
422                 vTaskResume( xMediumPriorityMutexTask );\r
423 \r
424                 /* If the did run then it will have incremented our guarded variable. */\r
425                 if( ulGuardedVariable != 0 )\r
426                 {\r
427                         xErrorDetected = pdTRUE;\r
428                 }\r
429 \r
430                 /* When we give back the semaphore our priority should be disinherited\r
431                 back to the priority to which we attempted to set ourselves.  This means\r
432                 that when the high priority task next blocks, the medium priority task\r
433                 should execute and increment the guarded variable.   When we next run\r
434                 both the high and medium priority tasks will have been suspended again. */\r
435                 if( xSemaphoreGive( xMutex ) != pdPASS )\r
436                 {\r
437                         xErrorDetected = pdTRUE;\r
438                 }\r
439 \r
440                 /* Check that the guarded variable did indeed increment... */\r
441                 if( ulGuardedVariable != 1 )\r
442                 {\r
443                         xErrorDetected = pdTRUE;\r
444                 }\r
445 \r
446                 /* ... and that our priority has been disinherited to\r
447                 genqMUTEX_TEST_PRIORITY. */\r
448                 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )\r
449                 {\r
450                         xErrorDetected = pdTRUE;\r
451                 }\r
452 \r
453                 /* Set our priority back to our original priority ready for the next\r
454                 loop around this test. */\r
455                 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );\r
456 \r
457                 /* Just to show we are still running. */\r
458                 ulLoopCounter2++;\r
459 \r
460                 #if configUSE_PREEMPTION == 0\r
461                         taskYIELD();\r
462                 #endif          \r
463         }\r
464 }\r
465 /*-----------------------------------------------------------*/\r
466 \r
467 static void prvMediumPriorityMutexTask( void *pvParameters )\r
468 {\r
469         ( void ) pvParameters;\r
470 \r
471         for( ;; )\r
472         {\r
473                 /* The medium priority task starts by suspending itself.  The low\r
474                 priority task will unsuspend this task when required. */\r
475                 vTaskSuspend( NULL );\r
476 \r
477                 /* When this task unsuspends all it does is increment the guarded\r
478                 variable, this is so the low priority task knows that it has\r
479                 executed. */\r
480                 ulGuardedVariable++;\r
481         }\r
482 }\r
483 /*-----------------------------------------------------------*/\r
484 \r
485 static void prvHighPriorityMutexTask( void *pvParameters )\r
486 {\r
487 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;\r
488 \r
489         for( ;; )\r
490         {\r
491                 /* The high priority task starts by suspending itself.  The low\r
492                 priority task will unsuspend this task when required. */\r
493                 vTaskSuspend( NULL );\r
494 \r
495                 /* When this task unsuspends all it does is attempt to obtain\r
496                 the mutex.  It should find the mutex is not available so a\r
497                 block time is specified. */\r
498                 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )\r
499                 {\r
500                         xErrorDetected = pdTRUE;\r
501                 }\r
502 \r
503                 /* When we eventually obtain the mutex we just give it back then\r
504                 return to suspend ready for the next test. */\r
505                 if( xSemaphoreGive( xMutex ) != pdPASS )\r
506                 {\r
507                         xErrorDetected = pdTRUE;\r
508                 }               \r
509         }\r
510 }\r
511 /*-----------------------------------------------------------*/\r
512 \r
513 /* This is called to check that all the created tasks are still running. */\r
514 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )\r
515 {\r
516 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;\r
517 \r
518         /* If the demo task is still running then we expect the loopcounters to\r
519         have incremented since this function was last called. */\r
520         if( ulLastLoopCounter == ulLoopCounter )\r
521         {\r
522                 xErrorDetected = pdTRUE;\r
523         }\r
524 \r
525         if( ulLastLoopCounter2 == ulLoopCounter2 )\r
526         {\r
527                 xErrorDetected = pdTRUE;\r
528         }\r
529 \r
530         ulLastLoopCounter = ulLoopCounter;\r
531         ulLastLoopCounter2 = ulLoopCounter2;    \r
532 \r
533         /* Errors detected in the task itself will have latched xErrorDetected\r
534         to true. */\r
535 \r
536         return !xErrorDetected;\r
537 }\r
538 \r
539 \r