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 * This file implements the same demo and test as GenQTest.c, but uses the
\r
53 * light weight API in place of the fully featured API.
\r
55 * See the comments at the top of GenQTest.c for a description.
\r
61 /* Scheduler include files. */
\r
62 #include "FreeRTOS.h"
\r
67 /* Demo program include files. */
\r
68 #include "AltQTest.h"
\r
70 #define genqQUEUE_LENGTH ( 5 )
\r
71 #define genqNO_BLOCK ( 0 )
\r
73 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
74 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
75 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
76 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
78 /*-----------------------------------------------------------*/
\r
81 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
82 * macros by using both to fill a queue, then reading from the queue to
\r
83 * check the resultant queue order is as expected. Queue data is also
\r
86 static void prvSendFrontAndBackTest( void *pvParameters );
\r
89 * The following three tasks are used to demonstrate the mutex behaviour.
\r
90 * Each task is given a different priority to demonstrate the priority
\r
91 * inheritance mechanism.
\r
93 * The low priority task obtains a mutex. After this a high priority task
\r
94 * attempts to obtain the same mutex, causing its priority to be inherited
\r
95 * by the low priority task. The task with the inherited high priority then
\r
96 * resumes a medium priority task to ensure it is not blocked by the medium
\r
97 * priority task while it holds the inherited high priority. Once the mutex
\r
98 * is returned the task with the inherited priority returns to its original
\r
99 * low priority, and is therefore immediately preempted by first the high
\r
100 * priority task and then the medium prioroity task before it can continue.
\r
102 static void prvLowPriorityMutexTask( void *pvParameters );
\r
103 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
104 static void prvHighPriorityMutexTask( void *pvParameters );
\r
106 /*-----------------------------------------------------------*/
\r
108 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
109 detected in any of the tasks. */
\r
110 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
112 /* Counters that are incremented on each cycle of a test. This is used to
\r
113 detect a stalled task - a test that is no longer running. */
\r
114 static volatile unsigned portLONG ulLoopCounter = 0;
\r
115 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
117 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
118 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
120 /* Handles used in the mutext test to suspend and resume the high and medium
\r
121 priority mutex test tasks. */
\r
122 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
124 /*-----------------------------------------------------------*/
\r
126 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
128 xQueueHandle xQueue;
\r
129 xSemaphoreHandle xMutex;
\r
131 /* Create the queue that we are going to use for the
\r
132 prvSendFrontAndBackTest demo. */
\r
133 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
135 /* Create the demo task and pass it the queue just created. We are
\r
136 passing the queue handle by value so it does not matter that it is
\r
137 declared on the stack here. */
\r
138 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * ) "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
140 /* Create the mutex used by the prvMutexTest task. */
\r
141 xMutex = xSemaphoreCreateMutex();
\r
143 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
144 passing the mutex handle by value so it does not matter that it is declared
\r
145 on the stack here. */
\r
146 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * ) "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
147 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * ) "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
148 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * ) "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
150 /*-----------------------------------------------------------*/
\r
152 static void prvSendFrontAndBackTest( void *pvParameters )
\r
154 unsigned portLONG ulData, ulData2;
\r
155 xQueueHandle xQueue;
\r
158 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
160 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
162 /* Queue a message for printing to say the task has started. */
\r
163 vPrintDisplayMessage( &pcTaskStartMsg );
\r
166 xQueue = ( xQueueHandle ) pvParameters;
\r
170 /* The queue is empty, so sending an item to the back of the queue
\r
171 should have the same efect as sending it to the front of the queue.
\r
173 First send to the front and check everything is as expected. */
\r
174 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
176 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
178 xErrorDetected = pdTRUE;
\r
181 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
183 xErrorDetected = pdTRUE;
\r
186 /* The data we sent to the queue should equal the data we just received
\r
188 if( ulLoopCounter != ulData )
\r
190 xErrorDetected = pdTRUE;
\r
193 /* Then do the same, sending the data to the back, checking everything
\r
195 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
197 xErrorDetected = pdTRUE;
\r
200 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
202 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
204 xErrorDetected = pdTRUE;
\r
207 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
209 xErrorDetected = pdTRUE;
\r
212 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
214 xErrorDetected = pdTRUE;
\r
217 /* The data we sent to the queue should equal the data we just received
\r
219 if( ulLoopCounter != ulData )
\r
221 xErrorDetected = pdTRUE;
\r
224 #if configUSE_PREEMPTION == 0
\r
230 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
231 for( ulData = 2; ulData < 5; ulData++ )
\r
233 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
236 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
237 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
238 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
240 xErrorDetected = pdTRUE;
\r
243 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
245 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
247 /* Now the queue should be full, and when we read the data out we
\r
248 should receive 0, 1, 2, 3, 4. */
\r
249 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
251 xErrorDetected = pdTRUE;
\r
254 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
256 xErrorDetected = pdTRUE;
\r
259 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
261 xErrorDetected = pdTRUE;
\r
264 #if configUSE_PREEMPTION == 0
\r
268 /* Check the data we read out is in the expected order. */
\r
269 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
271 /* Try peeking the data first. */
\r
272 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
274 xErrorDetected = pdTRUE;
\r
277 if( ulData != ulData2 )
\r
279 xErrorDetected = pdTRUE;
\r
283 /* Now try receiving the data for real. The value should be the
\r
284 same. Clobber the value first so we know we really received it. */
\r
285 ulData2 = ~ulData2;
\r
286 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
288 xErrorDetected = pdTRUE;
\r
291 if( ulData != ulData2 )
\r
293 xErrorDetected = pdTRUE;
\r
297 /* The queue should now be empty again. */
\r
298 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
300 xErrorDetected = pdTRUE;
\r
303 #if configUSE_PREEMPTION == 0
\r
308 /* Our queue is empty once more, add 10, 11 to the back. */
\r
310 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
312 xErrorDetected = pdTRUE;
\r
315 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
317 xErrorDetected = pdTRUE;
\r
320 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
322 xErrorDetected = pdTRUE;
\r
325 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
327 for( ulData = 9; ulData >= 7; ulData-- )
\r
329 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
331 xErrorDetected = pdTRUE;
\r
335 /* Now check that the queue is full, and that receiving data provides
\r
336 the expected sequence of 7, 8, 9, 10, 11. */
\r
337 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
339 xErrorDetected = pdTRUE;
\r
342 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
344 xErrorDetected = pdTRUE;
\r
347 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
349 xErrorDetected = pdTRUE;
\r
352 #if configUSE_PREEMPTION == 0
\r
356 /* Check the data we read out is in the expected order. */
\r
357 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
359 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
361 xErrorDetected = pdTRUE;
\r
364 if( ulData != ulData2 )
\r
366 xErrorDetected = pdTRUE;
\r
370 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
372 xErrorDetected = pdTRUE;
\r
378 /*-----------------------------------------------------------*/
\r
380 static void prvLowPriorityMutexTask( void *pvParameters )
\r
382 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
385 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
387 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
389 /* Queue a message for printing to say the task has started. */
\r
390 vPrintDisplayMessage( &pcTaskStartMsg );
\r
393 ( void ) pvParameters;
\r
398 /* Take the mutex. It should be available now. */
\r
399 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
401 xErrorDetected = pdTRUE;
\r
404 /* Set our guarded variable to a known start value. */
\r
405 ulGuardedVariable = 0;
\r
407 /* Our priority should be as per that assigned when the task was
\r
409 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
411 xErrorDetected = pdTRUE;
\r
414 /* Now unsuspend the high priority task. This will attempt to take the
\r
415 mutex, and block when it finds it cannot obtain it. */
\r
416 vTaskResume( xHighPriorityMutexTask );
\r
418 /* We should now have inherited the prioritoy of the high priority task,
\r
419 as by now it will have attempted to get the mutex. */
\r
420 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
422 xErrorDetected = pdTRUE;
\r
425 /* We can attempt to set our priority to the test priority - between the
\r
426 idle priority and the medium/high test priorities, but our actual
\r
427 prioroity should remain at the high priority. */
\r
428 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
429 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
431 xErrorDetected = pdTRUE;
\r
434 /* Now unsuspend the medium priority task. This should not run as our
\r
435 inherited priority is above that of the medium priority task. */
\r
436 vTaskResume( xMediumPriorityMutexTask );
\r
438 /* If the did run then it will have incremented our guarded variable. */
\r
439 if( ulGuardedVariable != 0 )
\r
441 xErrorDetected = pdTRUE;
\r
444 /* When we give back the semaphore our priority should be disinherited
\r
445 back to the priority to which we attempted to set ourselves. This means
\r
446 that when the high priority task next blocks, the medium priority task
\r
447 should execute and increment the guarded variable. When we next run
\r
448 both the high and medium priority tasks will have been suspended again. */
\r
449 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
451 xErrorDetected = pdTRUE;
\r
454 /* Check that the guarded variable did indeed increment... */
\r
455 if( ulGuardedVariable != 1 )
\r
457 xErrorDetected = pdTRUE;
\r
460 /* ... and that our priority has been disinherited to
\r
461 genqMUTEX_TEST_PRIORITY. */
\r
462 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
464 xErrorDetected = pdTRUE;
\r
467 /* Set our priority back to our original priority ready for the next
\r
468 loop around this test. */
\r
469 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
471 /* Just to show we are still running. */
\r
474 #if configUSE_PREEMPTION == 0
\r
479 /*-----------------------------------------------------------*/
\r
481 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
483 ( void ) pvParameters;
\r
487 /* The medium priority task starts by suspending itself. The low
\r
488 priority task will unsuspend this task when required. */
\r
489 vTaskSuspend( NULL );
\r
491 /* When this task unsuspends all it does is increment the guarded
\r
492 variable, this is so the low priority task knows that it has
\r
494 ulGuardedVariable++;
\r
497 /*-----------------------------------------------------------*/
\r
499 static void prvHighPriorityMutexTask( void *pvParameters )
\r
501 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
503 ( void ) pvParameters;
\r
507 /* The high priority task starts by suspending itself. The low
\r
508 priority task will unsuspend this task when required. */
\r
509 vTaskSuspend( NULL );
\r
511 /* When this task unsuspends all it does is attempt to obtain
\r
512 the mutex. It should find the mutex is not available so a
\r
513 block time is specified. */
\r
514 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
516 xErrorDetected = pdTRUE;
\r
519 /* When we eventually obtain the mutex we just give it back then
\r
520 return to suspend ready for the next test. */
\r
521 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
523 xErrorDetected = pdTRUE;
\r
527 /*-----------------------------------------------------------*/
\r
529 /* This is called to check that all the created tasks are still running. */
\r
530 portBASE_TYPE xAreAltGenericQueueTasksStillRunning( void )
\r
532 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
534 /* If the demo task is still running then we expect the loopcounters to
\r
535 have incremented since this function was last called. */
\r
536 if( ulLastLoopCounter == ulLoopCounter )
\r
538 xErrorDetected = pdTRUE;
\r
541 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
543 xErrorDetected = pdTRUE;
\r
546 ulLastLoopCounter = ulLoopCounter;
\r
547 ulLastLoopCounter2 = ulLoopCounter2;
\r
549 /* Errors detected in the task itself will have latched xErrorDetected
\r
552 return !xErrorDetected;
\r