2 FreeRTOS.org V5.0.0 - 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
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
52 * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -
\r
53 * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and
\r
56 * See the comments above the prvSendFrontAndBackTest() and
\r
57 * prvLowPriorityMutexTask() prototypes below for more information.
\r
63 /* Scheduler include files. */
\r
64 #include "FreeRTOS.h"
\r
69 /* Demo program include files. */
\r
70 #include "GenQTest.h"
\r
72 #define genqQUEUE_LENGTH ( 5 )
\r
73 #define genqNO_BLOCK ( 0 )
\r
75 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
76 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
77 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
78 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
80 /*-----------------------------------------------------------*/
\r
83 * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()
\r
84 * macros by using both to fill a queue, then reading from the queue to
\r
85 * check the resultant queue order is as expected. Queue data is also
\r
88 static void prvSendFrontAndBackTest( void *pvParameters );
\r
91 * The following three tasks are used to demonstrate the mutex behaviour.
\r
92 * Each task is given a different priority to demonstrate the priority
\r
93 * inheritance mechanism.
\r
95 * The low priority task obtains a mutex. After this a high priority task
\r
96 * attempts to obtain the same mutex, causing its priority to be inherited
\r
97 * by the low priority task. The task with the inherited high priority then
\r
98 * resumes a medium priority task to ensure it is not blocked by the medium
\r
99 * priority task while it holds the inherited high priority. Once the mutex
\r
100 * is returned the task with the inherited priority returns to its original
\r
101 * low priority, and is therefore immediately preempted by first the high
\r
102 * priority task and then the medium prioroity task before it can continue.
\r
104 static void prvLowPriorityMutexTask( void *pvParameters );
\r
105 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
106 static void prvHighPriorityMutexTask( void *pvParameters );
\r
108 /*-----------------------------------------------------------*/
\r
110 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
111 detected in any of the tasks. */
\r
112 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
114 /* Counters that are incremented on each cycle of a test. This is used to
\r
115 detect a stalled task - a test that is no longer running. */
\r
116 static volatile unsigned portLONG ulLoopCounter = 0;
\r
117 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
119 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
120 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
122 /* Handles used in the mutext test to suspend and resume the high and medium
\r
123 priority mutex test tasks. */
\r
124 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
126 /*-----------------------------------------------------------*/
\r
128 void vStartGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
130 xQueueHandle xQueue;
\r
131 xSemaphoreHandle xMutex;
\r
133 /* Create the queue that we are going to use for the
\r
134 prvSendFrontAndBackTest demo. */
\r
135 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
137 /* Create the demo task and pass it the queue just created. We are
\r
138 passing the queue handle by value so it does not matter that it is
\r
139 declared on the stack here. */
\r
140 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * )"GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
142 /* Create the mutex used by the prvMutexTest task. */
\r
143 xMutex = xSemaphoreCreateMutex();
\r
145 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
146 passing the mutex handle by value so it does not matter that it is declared
\r
147 on the stack here. */
\r
148 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * )"MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
149 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * )"MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
150 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * )"MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
152 /*-----------------------------------------------------------*/
\r
154 static void prvSendFrontAndBackTest( void *pvParameters )
\r
156 unsigned portLONG ulData, ulData2;
\r
157 xQueueHandle xQueue;
\r
160 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
162 const portCHAR * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";
\r
164 /* Queue a message for printing to say the task has started. */
\r
165 vPrintDisplayMessage( &pcTaskStartMsg );
\r
168 xQueue = ( xQueueHandle ) pvParameters;
\r
172 /* The queue is empty, so sending an item to the back of the queue
\r
173 should have the same efect as sending it to the front of the queue.
\r
175 First send to the front and check everything is as expected. */
\r
176 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
178 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
180 xErrorDetected = pdTRUE;
\r
183 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
185 xErrorDetected = pdTRUE;
\r
188 /* The data we sent to the queue should equal the data we just received
\r
190 if( ulLoopCounter != ulData )
\r
192 xErrorDetected = pdTRUE;
\r
195 /* Then do the same, sending the data to the back, checking everything
\r
197 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
199 xErrorDetected = pdTRUE;
\r
202 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
204 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
206 xErrorDetected = pdTRUE;
\r
209 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
211 xErrorDetected = pdTRUE;
\r
214 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
216 xErrorDetected = pdTRUE;
\r
219 /* The data we sent to the queue should equal the data we just received
\r
221 if( ulLoopCounter != ulData )
\r
223 xErrorDetected = pdTRUE;
\r
226 #if configUSE_PREEMPTION == 0
\r
232 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
233 for( ulData = 2; ulData < 5; ulData++ )
\r
235 xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
238 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
239 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
240 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
242 xErrorDetected = pdTRUE;
\r
245 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
247 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
249 /* Now the queue should be full, and when we read the data out we
\r
250 should receive 0, 1, 2, 3, 4. */
\r
251 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
253 xErrorDetected = pdTRUE;
\r
256 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
258 xErrorDetected = pdTRUE;
\r
261 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
263 xErrorDetected = pdTRUE;
\r
266 #if configUSE_PREEMPTION == 0
\r
270 /* Check the data we read out is in the expected order. */
\r
271 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
273 /* Try peeking the data first. */
\r
274 if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
276 xErrorDetected = pdTRUE;
\r
279 if( ulData != ulData2 )
\r
281 xErrorDetected = pdTRUE;
\r
285 /* Now try receiving the data for real. The value should be the
\r
286 same. Clobber the value first so we know we really received it. */
\r
287 ulData2 = ~ulData2;
\r
288 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
290 xErrorDetected = pdTRUE;
\r
293 if( ulData != ulData2 )
\r
295 xErrorDetected = pdTRUE;
\r
299 /* The queue should now be empty again. */
\r
300 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
302 xErrorDetected = pdTRUE;
\r
305 #if configUSE_PREEMPTION == 0
\r
310 /* Our queue is empty once more, add 10, 11 to the back. */
\r
312 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
314 xErrorDetected = pdTRUE;
\r
317 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
319 xErrorDetected = pdTRUE;
\r
322 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
324 xErrorDetected = pdTRUE;
\r
327 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
329 for( ulData = 9; ulData >= 7; ulData-- )
\r
331 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
333 xErrorDetected = pdTRUE;
\r
337 /* Now check that the queue is full, and that receiving data provides
\r
338 the expected sequence of 7, 8, 9, 10, 11. */
\r
339 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
341 xErrorDetected = pdTRUE;
\r
344 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
346 xErrorDetected = pdTRUE;
\r
349 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
351 xErrorDetected = pdTRUE;
\r
354 #if configUSE_PREEMPTION == 0
\r
358 /* Check the data we read out is in the expected order. */
\r
359 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
361 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
363 xErrorDetected = pdTRUE;
\r
366 if( ulData != ulData2 )
\r
368 xErrorDetected = pdTRUE;
\r
372 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
374 xErrorDetected = pdTRUE;
\r
380 /*-----------------------------------------------------------*/
\r
382 static void prvLowPriorityMutexTask( void *pvParameters )
\r
384 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
387 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
389 const portCHAR * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";
\r
391 /* Queue a message for printing to say the task has started. */
\r
392 vPrintDisplayMessage( &pcTaskStartMsg );
\r
397 /* Take the mutex. It should be available now. */
\r
398 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
400 xErrorDetected = pdTRUE;
\r
403 /* Set our guarded variable to a known start value. */
\r
404 ulGuardedVariable = 0;
\r
406 /* Our priority should be as per that assigned when the task was
\r
408 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
410 xErrorDetected = pdTRUE;
\r
413 /* Now unsuspend the high priority task. This will attempt to take the
\r
414 mutex, and block when it finds it cannot obtain it. */
\r
415 vTaskResume( xHighPriorityMutexTask );
\r
417 /* We should now have inherited the prioritoy of the high priority task,
\r
418 as by now it will have attempted to get the mutex. */
\r
419 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
421 xErrorDetected = pdTRUE;
\r
424 /* We can attempt to set our priority to the test priority - between the
\r
425 idle priority and the medium/high test priorities, but our actual
\r
426 prioroity should remain at the high priority. */
\r
427 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
428 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
430 xErrorDetected = pdTRUE;
\r
433 /* Now unsuspend the medium priority task. This should not run as our
\r
434 inherited priority is above that of the medium priority task. */
\r
435 vTaskResume( xMediumPriorityMutexTask );
\r
437 /* If the did run then it will have incremented our guarded variable. */
\r
438 if( ulGuardedVariable != 0 )
\r
440 xErrorDetected = pdTRUE;
\r
443 /* When we give back the semaphore our priority should be disinherited
\r
444 back to the priority to which we attempted to set ourselves. This means
\r
445 that when the high priority task next blocks, the medium priority task
\r
446 should execute and increment the guarded variable. When we next run
\r
447 both the high and medium priority tasks will have been suspended again. */
\r
448 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
450 xErrorDetected = pdTRUE;
\r
453 /* Check that the guarded variable did indeed increment... */
\r
454 if( ulGuardedVariable != 1 )
\r
456 xErrorDetected = pdTRUE;
\r
459 /* ... and that our priority has been disinherited to
\r
460 genqMUTEX_TEST_PRIORITY. */
\r
461 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
463 xErrorDetected = pdTRUE;
\r
466 /* Set our priority back to our original priority ready for the next
\r
467 loop around this test. */
\r
468 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
470 /* Just to show we are still running. */
\r
473 #if configUSE_PREEMPTION == 0
\r
478 /*-----------------------------------------------------------*/
\r
480 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
482 ( void ) pvParameters;
\r
486 /* The medium priority task starts by suspending itself. The low
\r
487 priority task will unsuspend this task when required. */
\r
488 vTaskSuspend( NULL );
\r
490 /* When this task unsuspends all it does is increment the guarded
\r
491 variable, this is so the low priority task knows that it has
\r
493 ulGuardedVariable++;
\r
496 /*-----------------------------------------------------------*/
\r
498 static void prvHighPriorityMutexTask( void *pvParameters )
\r
500 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
504 /* The high priority task starts by suspending itself. The low
\r
505 priority task will unsuspend this task when required. */
\r
506 vTaskSuspend( NULL );
\r
508 /* When this task unsuspends all it does is attempt to obtain
\r
509 the mutex. It should find the mutex is not available so a
\r
510 block time is specified. */
\r
511 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
513 xErrorDetected = pdTRUE;
\r
516 /* When we eventually obtain the mutex we just give it back then
\r
517 return to suspend ready for the next test. */
\r
518 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
520 xErrorDetected = pdTRUE;
\r
524 /*-----------------------------------------------------------*/
\r
526 /* This is called to check that all the created tasks are still running. */
\r
527 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )
\r
529 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
531 /* If the demo task is still running then we expect the loopcounters to
\r
532 have incremented since this function was last called. */
\r
533 if( ulLastLoopCounter == ulLoopCounter )
\r
535 xErrorDetected = pdTRUE;
\r
538 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
540 xErrorDetected = pdTRUE;
\r
543 ulLastLoopCounter = ulLoopCounter;
\r
544 ulLastLoopCounter2 = ulLoopCounter2;
\r
546 /* Errors detected in the task itself will have latched xErrorDetected
\r
549 return !xErrorDetected;
\r