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