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