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 * 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
48 * See the comments at the top of GenQTest.c for a description.
\r
54 /* Scheduler include files. */
\r
55 #include "FreeRTOS.h"
\r
60 /* Demo program include files. */
\r
61 #include "AltQTest.h"
\r
63 #define genqQUEUE_LENGTH ( 5 )
\r
64 #define genqNO_BLOCK ( 0 )
\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
71 /*-----------------------------------------------------------*/
\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
79 static void prvSendFrontAndBackTest( void *pvParameters );
\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
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
95 static void prvLowPriorityMutexTask( void *pvParameters );
\r
96 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
97 static void prvHighPriorityMutexTask( void *pvParameters );
\r
99 /*-----------------------------------------------------------*/
\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
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
110 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
111 static volatile unsigned portLONG ulGuardedVariable = 0;
\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
117 /*-----------------------------------------------------------*/
\r
119 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
121 xQueueHandle xQueue;
\r
122 xSemaphoreHandle xMutex;
\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
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
133 /* Create the mutex used by the prvMutexTest task. */
\r
134 xMutex = xSemaphoreCreateMutex();
\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
143 /*-----------------------------------------------------------*/
\r
145 static void prvSendFrontAndBackTest( void *pvParameters )
\r
147 unsigned portLONG ulData, ulData2;
\r
148 xQueueHandle xQueue;
\r
151 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
153 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
155 /* Queue a message for printing to say the task has started. */
\r
156 vPrintDisplayMessage( &pcTaskStartMsg );
\r
159 xQueue = ( xQueueHandle ) pvParameters;
\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
166 First send to the front and check everything is as expected. */
\r
167 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
169 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
171 xErrorDetected = pdTRUE;
\r
174 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
176 xErrorDetected = pdTRUE;
\r
179 /* The data we sent to the queue should equal the data we just received
\r
181 if( ulLoopCounter != ulData )
\r
183 xErrorDetected = pdTRUE;
\r
186 /* Then do the same, sending the data to the back, checking everything
\r
188 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
190 xErrorDetected = pdTRUE;
\r
193 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
195 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
197 xErrorDetected = pdTRUE;
\r
200 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
202 xErrorDetected = pdTRUE;
\r
205 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
207 xErrorDetected = pdTRUE;
\r
210 /* The data we sent to the queue should equal the data we just received
\r
212 if( ulLoopCounter != ulData )
\r
214 xErrorDetected = pdTRUE;
\r
217 #if configUSE_PREEMPTION == 0
\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
226 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\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
233 xErrorDetected = pdTRUE;
\r
236 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
238 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\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
244 xErrorDetected = pdTRUE;
\r
247 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
249 xErrorDetected = pdTRUE;
\r
252 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
254 xErrorDetected = pdTRUE;
\r
257 #if configUSE_PREEMPTION == 0
\r
261 /* Check the data we read out is in the expected order. */
\r
262 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
264 /* Try peeking the data first. */
\r
265 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
267 xErrorDetected = pdTRUE;
\r
270 if( ulData != ulData2 )
\r
272 xErrorDetected = pdTRUE;
\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
281 xErrorDetected = pdTRUE;
\r
284 if( ulData != ulData2 )
\r
286 xErrorDetected = pdTRUE;
\r
290 /* The queue should now be empty again. */
\r
291 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
293 xErrorDetected = pdTRUE;
\r
296 #if configUSE_PREEMPTION == 0
\r
301 /* Our queue is empty once more, add 10, 11 to the back. */
\r
303 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
305 xErrorDetected = pdTRUE;
\r
308 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
310 xErrorDetected = pdTRUE;
\r
313 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
315 xErrorDetected = pdTRUE;
\r
318 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
320 for( ulData = 9; ulData >= 7; ulData-- )
\r
322 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
324 xErrorDetected = pdTRUE;
\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
332 xErrorDetected = pdTRUE;
\r
335 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
337 xErrorDetected = pdTRUE;
\r
340 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
342 xErrorDetected = pdTRUE;
\r
345 #if configUSE_PREEMPTION == 0
\r
349 /* Check the data we read out is in the expected order. */
\r
350 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
352 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
354 xErrorDetected = pdTRUE;
\r
357 if( ulData != ulData2 )
\r
359 xErrorDetected = pdTRUE;
\r
363 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
365 xErrorDetected = pdTRUE;
\r
371 /*-----------------------------------------------------------*/
\r
373 static void prvLowPriorityMutexTask( void *pvParameters )
\r
375 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
378 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
380 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
382 /* Queue a message for printing to say the task has started. */
\r
383 vPrintDisplayMessage( &pcTaskStartMsg );
\r
386 ( void ) pvParameters;
\r
391 /* Take the mutex. It should be available now. */
\r
392 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
394 xErrorDetected = pdTRUE;
\r
397 /* Set our guarded variable to a known start value. */
\r
398 ulGuardedVariable = 0;
\r
400 /* Our priority should be as per that assigned when the task was
\r
402 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
404 xErrorDetected = pdTRUE;
\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
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
415 xErrorDetected = pdTRUE;
\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
424 xErrorDetected = pdTRUE;
\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
431 /* If the did run then it will have incremented our guarded variable. */
\r
432 if( ulGuardedVariable != 0 )
\r
434 xErrorDetected = pdTRUE;
\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
444 xErrorDetected = pdTRUE;
\r
447 /* Check that the guarded variable did indeed increment... */
\r
448 if( ulGuardedVariable != 1 )
\r
450 xErrorDetected = pdTRUE;
\r
453 /* ... and that our priority has been disinherited to
\r
454 genqMUTEX_TEST_PRIORITY. */
\r
455 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
457 xErrorDetected = pdTRUE;
\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
464 /* Just to show we are still running. */
\r
467 #if configUSE_PREEMPTION == 0
\r
472 /*-----------------------------------------------------------*/
\r
474 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
476 ( void ) pvParameters;
\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
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
487 ulGuardedVariable++;
\r
490 /*-----------------------------------------------------------*/
\r
492 static void prvHighPriorityMutexTask( void *pvParameters )
\r
494 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
496 ( void ) pvParameters;
\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
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
509 xErrorDetected = pdTRUE;
\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
516 xErrorDetected = pdTRUE;
\r
520 /*-----------------------------------------------------------*/
\r
522 /* This is called to check that all the created tasks are still running. */
\r
523 portBASE_TYPE xAreAltGenericQueueTasksStillRunning( void )
\r
525 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\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
531 xErrorDetected = pdTRUE;
\r
534 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
536 xErrorDetected = pdTRUE;
\r
539 ulLastLoopCounter = ulLoopCounter;
\r
540 ulLastLoopCounter2 = ulLoopCounter2;
\r
542 /* Errors detected in the task itself will have latched xErrorDetected
\r
545 return !xErrorDetected;
\r