2 FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\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
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
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
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
26 ***************************************************************************
\r
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
45 * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -
\r
46 * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and
\r
49 * See the comments above the prvSendFrontAndBackTest() and
\r
50 * prvLowPriorityMutexTask() prototypes below for more information.
\r
56 /* Scheduler include files. */
\r
57 #include "FreeRTOS.h"
\r
62 /* Demo program include files. */
\r
63 #include "GenQTest.h"
\r
65 #define genqQUEUE_LENGTH ( 5 )
\r
66 #define genqNO_BLOCK ( 0 )
\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
73 /*-----------------------------------------------------------*/
\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
81 static void prvSendFrontAndBackTest( void *pvParameters );
\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
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
97 static void prvLowPriorityMutexTask( void *pvParameters );
\r
98 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
99 static void prvHighPriorityMutexTask( void *pvParameters );
\r
101 /*-----------------------------------------------------------*/
\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
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
112 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
113 static volatile unsigned portLONG ulGuardedVariable = 0;
\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
119 /*-----------------------------------------------------------*/
\r
121 void vStartGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
123 xQueueHandle xQueue;
\r
124 xSemaphoreHandle xMutex;
\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
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
135 /* Create the mutex used by the prvMutexTest task. */
\r
136 xMutex = xSemaphoreCreateMutex();
\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
145 /*-----------------------------------------------------------*/
\r
147 static void prvSendFrontAndBackTest( void *pvParameters )
\r
149 unsigned portLONG ulData, ulData2;
\r
150 xQueueHandle xQueue;
\r
153 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
155 const portCHAR * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";
\r
157 /* Queue a message for printing to say the task has started. */
\r
158 vPrintDisplayMessage( &pcTaskStartMsg );
\r
161 xQueue = ( xQueueHandle ) pvParameters;
\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
168 First send to the front and check everything is as expected. */
\r
169 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
171 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
173 xErrorDetected = pdTRUE;
\r
176 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
178 xErrorDetected = pdTRUE;
\r
181 /* The data we sent to the queue should equal the data we just received
\r
183 if( ulLoopCounter != ulData )
\r
185 xErrorDetected = pdTRUE;
\r
188 /* Then do the same, sending the data to the back, checking everything
\r
190 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
192 xErrorDetected = pdTRUE;
\r
195 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
197 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
199 xErrorDetected = pdTRUE;
\r
202 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
204 xErrorDetected = pdTRUE;
\r
207 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
209 xErrorDetected = pdTRUE;
\r
212 /* The data we sent to the queue should equal the data we just received
\r
214 if( ulLoopCounter != ulData )
\r
216 xErrorDetected = pdTRUE;
\r
219 #if configUSE_PREEMPTION == 0
\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
228 xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\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
235 xErrorDetected = pdTRUE;
\r
238 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
240 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\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
246 xErrorDetected = pdTRUE;
\r
249 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
251 xErrorDetected = pdTRUE;
\r
254 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
256 xErrorDetected = pdTRUE;
\r
259 #if configUSE_PREEMPTION == 0
\r
263 /* Check the data we read out is in the expected order. */
\r
264 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
266 /* Try peeking the data first. */
\r
267 if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
269 xErrorDetected = pdTRUE;
\r
272 if( ulData != ulData2 )
\r
274 xErrorDetected = pdTRUE;
\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
283 xErrorDetected = pdTRUE;
\r
286 if( ulData != ulData2 )
\r
288 xErrorDetected = pdTRUE;
\r
292 /* The queue should now be empty again. */
\r
293 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
295 xErrorDetected = pdTRUE;
\r
298 #if configUSE_PREEMPTION == 0
\r
303 /* Our queue is empty once more, add 10, 11 to the back. */
\r
305 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
307 xErrorDetected = pdTRUE;
\r
310 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
312 xErrorDetected = pdTRUE;
\r
315 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
317 xErrorDetected = pdTRUE;
\r
320 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
322 for( ulData = 9; ulData >= 7; ulData-- )
\r
324 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
326 xErrorDetected = pdTRUE;
\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
334 xErrorDetected = pdTRUE;
\r
337 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
339 xErrorDetected = pdTRUE;
\r
342 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
344 xErrorDetected = pdTRUE;
\r
347 #if configUSE_PREEMPTION == 0
\r
351 /* Check the data we read out is in the expected order. */
\r
352 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
354 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
356 xErrorDetected = pdTRUE;
\r
359 if( ulData != ulData2 )
\r
361 xErrorDetected = pdTRUE;
\r
365 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
367 xErrorDetected = pdTRUE;
\r
373 /*-----------------------------------------------------------*/
\r
375 static void prvLowPriorityMutexTask( void *pvParameters )
\r
377 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
380 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
382 const portCHAR * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";
\r
384 /* Queue a message for printing to say the task has started. */
\r
385 vPrintDisplayMessage( &pcTaskStartMsg );
\r
390 /* Take the mutex. It should be available now. */
\r
391 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
393 xErrorDetected = pdTRUE;
\r
396 /* Set our guarded variable to a known start value. */
\r
397 ulGuardedVariable = 0;
\r
399 /* Our priority should be as per that assigned when the task was
\r
401 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
403 xErrorDetected = pdTRUE;
\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
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
414 xErrorDetected = pdTRUE;
\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
423 xErrorDetected = pdTRUE;
\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
430 /* If the did run then it will have incremented our guarded variable. */
\r
431 if( ulGuardedVariable != 0 )
\r
433 xErrorDetected = pdTRUE;
\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
443 xErrorDetected = pdTRUE;
\r
446 /* Check that the guarded variable did indeed increment... */
\r
447 if( ulGuardedVariable != 1 )
\r
449 xErrorDetected = pdTRUE;
\r
452 /* ... and that our priority has been disinherited to
\r
453 genqMUTEX_TEST_PRIORITY. */
\r
454 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
456 xErrorDetected = pdTRUE;
\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
463 /* Just to show we are still running. */
\r
466 #if configUSE_PREEMPTION == 0
\r
471 /*-----------------------------------------------------------*/
\r
473 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
475 ( void ) pvParameters;
\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
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
486 ulGuardedVariable++;
\r
489 /*-----------------------------------------------------------*/
\r
491 static void prvHighPriorityMutexTask( void *pvParameters )
\r
493 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\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
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
506 xErrorDetected = pdTRUE;
\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
513 xErrorDetected = pdTRUE;
\r
517 /*-----------------------------------------------------------*/
\r
519 /* This is called to check that all the created tasks are still running. */
\r
520 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )
\r
522 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\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
528 xErrorDetected = pdTRUE;
\r
531 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
533 xErrorDetected = pdTRUE;
\r
536 ulLastLoopCounter = ulLoopCounter;
\r
537 ulLastLoopCounter2 = ulLoopCounter2;
\r
539 /* Errors detected in the task itself will have latched xErrorDetected
\r
542 return !xErrorDetected;
\r