2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 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 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
55 * This file implements the same demo and test as GenQTest.c, but uses the
\r
56 * light weight API in place of the fully featured API.
\r
58 * See the comments at the top of GenQTest.c for a description.
\r
64 /* Scheduler include files. */
\r
65 #include "FreeRTOS.h"
\r
70 /* Demo program include files. */
\r
71 #include "AltQTest.h"
\r
73 #define genqQUEUE_LENGTH ( 5 )
\r
74 #define genqNO_BLOCK ( 0 )
\r
76 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
77 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
78 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
79 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
81 /*-----------------------------------------------------------*/
\r
84 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
85 * macros by using both to fill a queue, then reading from the queue to
\r
86 * check the resultant queue order is as expected. Queue data is also
\r
89 static void prvSendFrontAndBackTest( void *pvParameters );
\r
92 * The following three tasks are used to demonstrate the mutex behaviour.
\r
93 * Each task is given a different priority to demonstrate the priority
\r
94 * inheritance mechanism.
\r
96 * The low priority task obtains a mutex. After this a high priority task
\r
97 * attempts to obtain the same mutex, causing its priority to be inherited
\r
98 * by the low priority task. The task with the inherited high priority then
\r
99 * resumes a medium priority task to ensure it is not blocked by the medium
\r
100 * priority task while it holds the inherited high priority. Once the mutex
\r
101 * is returned the task with the inherited priority returns to its original
\r
102 * low priority, and is therefore immediately preempted by first the high
\r
103 * priority task and then the medium prioroity task before it can continue.
\r
105 static void prvLowPriorityMutexTask( void *pvParameters );
\r
106 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
107 static void prvHighPriorityMutexTask( void *pvParameters );
\r
109 /*-----------------------------------------------------------*/
\r
111 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
112 detected in any of the tasks. */
\r
113 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
115 /* Counters that are incremented on each cycle of a test. This is used to
\r
116 detect a stalled task - a test that is no longer running. */
\r
117 static volatile unsigned portLONG ulLoopCounter = 0;
\r
118 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
120 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
121 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
123 /* Handles used in the mutext test to suspend and resume the high and medium
\r
124 priority mutex test tasks. */
\r
125 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
127 /*-----------------------------------------------------------*/
\r
129 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
131 xQueueHandle xQueue;
\r
132 xSemaphoreHandle xMutex;
\r
134 /* Create the queue that we are going to use for the
\r
135 prvSendFrontAndBackTest demo. */
\r
136 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
138 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
139 in use. The queue registry is provided as a means for kernel aware
\r
140 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
141 is not being used. The call to vQueueAddToRegistry() will be removed
\r
142 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
143 defined to be less than 1. */
\r
144 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Alt_Gen_Test_Queue" );
\r
146 /* Create the demo task and pass it the queue just created. We are
\r
147 passing the queue handle by value so it does not matter that it is
\r
148 declared on the stack here. */
\r
149 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * ) "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
151 /* Create the mutex used by the prvMutexTest task. */
\r
152 xMutex = xSemaphoreCreateMutex();
\r
154 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
155 in use. The registry is provided as a means for kernel aware
\r
156 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
157 is not being used. The call to vQueueAddToRegistry() will be removed
\r
158 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
159 defined to be less than 1. */
\r
160 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Alt_Q_Mutex" );
\r
162 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
163 passing the mutex handle by value so it does not matter that it is declared
\r
164 on the stack here. */
\r
165 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * ) "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
166 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * ) "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
167 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * ) "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
169 /*-----------------------------------------------------------*/
\r
171 static void prvSendFrontAndBackTest( void *pvParameters )
\r
173 unsigned portLONG ulData, ulData2;
\r
174 xQueueHandle xQueue;
\r
177 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
179 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
181 /* Queue a message for printing to say the task has started. */
\r
182 vPrintDisplayMessage( &pcTaskStartMsg );
\r
185 xQueue = ( xQueueHandle ) pvParameters;
\r
189 /* The queue is empty, so sending an item to the back of the queue
\r
190 should have the same efect as sending it to the front of the queue.
\r
192 First send to the front and check everything is as expected. */
\r
193 xQueueAltSendToFront( 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 /* The data we sent to the queue should equal the data we just received
\r
207 if( ulLoopCounter != ulData )
\r
209 xErrorDetected = pdTRUE;
\r
212 /* Then do the same, sending the data to the back, checking everything
\r
214 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
216 xErrorDetected = pdTRUE;
\r
219 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
221 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
223 xErrorDetected = pdTRUE;
\r
226 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
228 xErrorDetected = pdTRUE;
\r
231 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
233 xErrorDetected = pdTRUE;
\r
236 /* The data we sent to the queue should equal the data we just received
\r
238 if( ulLoopCounter != ulData )
\r
240 xErrorDetected = pdTRUE;
\r
243 #if configUSE_PREEMPTION == 0
\r
249 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
250 for( ulData = 2; ulData < 5; ulData++ )
\r
252 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
255 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
256 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
257 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
259 xErrorDetected = pdTRUE;
\r
262 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
264 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
266 /* Now the queue should be full, and when we read the data out we
\r
267 should receive 0, 1, 2, 3, 4. */
\r
268 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
270 xErrorDetected = pdTRUE;
\r
273 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
275 xErrorDetected = pdTRUE;
\r
278 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
280 xErrorDetected = pdTRUE;
\r
283 #if configUSE_PREEMPTION == 0
\r
287 /* Check the data we read out is in the expected order. */
\r
288 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
290 /* Try peeking the data first. */
\r
291 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
293 xErrorDetected = pdTRUE;
\r
296 if( ulData != ulData2 )
\r
298 xErrorDetected = pdTRUE;
\r
302 /* Now try receiving the data for real. The value should be the
\r
303 same. Clobber the value first so we know we really received it. */
\r
304 ulData2 = ~ulData2;
\r
305 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
307 xErrorDetected = pdTRUE;
\r
310 if( ulData != ulData2 )
\r
312 xErrorDetected = pdTRUE;
\r
316 /* The queue should now be empty again. */
\r
317 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
319 xErrorDetected = pdTRUE;
\r
322 #if configUSE_PREEMPTION == 0
\r
327 /* Our queue is empty once more, add 10, 11 to the back. */
\r
329 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
331 xErrorDetected = pdTRUE;
\r
334 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
336 xErrorDetected = pdTRUE;
\r
339 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
341 xErrorDetected = pdTRUE;
\r
344 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
346 for( ulData = 9; ulData >= 7; ulData-- )
\r
348 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
350 xErrorDetected = pdTRUE;
\r
354 /* Now check that the queue is full, and that receiving data provides
\r
355 the expected sequence of 7, 8, 9, 10, 11. */
\r
356 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
358 xErrorDetected = pdTRUE;
\r
361 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
363 xErrorDetected = pdTRUE;
\r
366 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
368 xErrorDetected = pdTRUE;
\r
371 #if configUSE_PREEMPTION == 0
\r
375 /* Check the data we read out is in the expected order. */
\r
376 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
378 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
380 xErrorDetected = pdTRUE;
\r
383 if( ulData != ulData2 )
\r
385 xErrorDetected = pdTRUE;
\r
389 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
391 xErrorDetected = pdTRUE;
\r
397 /*-----------------------------------------------------------*/
\r
399 static void prvLowPriorityMutexTask( void *pvParameters )
\r
401 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
404 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
406 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
408 /* Queue a message for printing to say the task has started. */
\r
409 vPrintDisplayMessage( &pcTaskStartMsg );
\r
412 ( void ) pvParameters;
\r
417 /* Take the mutex. It should be available now. */
\r
418 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
420 xErrorDetected = pdTRUE;
\r
423 /* Set our guarded variable to a known start value. */
\r
424 ulGuardedVariable = 0;
\r
426 /* Our priority should be as per that assigned when the task was
\r
428 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
430 xErrorDetected = pdTRUE;
\r
433 /* Now unsuspend the high priority task. This will attempt to take the
\r
434 mutex, and block when it finds it cannot obtain it. */
\r
435 vTaskResume( xHighPriorityMutexTask );
\r
437 /* We should now have inherited the prioritoy of the high priority task,
\r
438 as by now it will have attempted to get the mutex. */
\r
439 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
441 xErrorDetected = pdTRUE;
\r
444 /* We can attempt to set our priority to the test priority - between the
\r
445 idle priority and the medium/high test priorities, but our actual
\r
446 prioroity should remain at the high priority. */
\r
447 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
448 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
450 xErrorDetected = pdTRUE;
\r
453 /* Now unsuspend the medium priority task. This should not run as our
\r
454 inherited priority is above that of the medium priority task. */
\r
455 vTaskResume( xMediumPriorityMutexTask );
\r
457 /* If the did run then it will have incremented our guarded variable. */
\r
458 if( ulGuardedVariable != 0 )
\r
460 xErrorDetected = pdTRUE;
\r
463 /* When we give back the semaphore our priority should be disinherited
\r
464 back to the priority to which we attempted to set ourselves. This means
\r
465 that when the high priority task next blocks, the medium priority task
\r
466 should execute and increment the guarded variable. When we next run
\r
467 both the high and medium priority tasks will have been suspended again. */
\r
468 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
470 xErrorDetected = pdTRUE;
\r
473 /* Check that the guarded variable did indeed increment... */
\r
474 if( ulGuardedVariable != 1 )
\r
476 xErrorDetected = pdTRUE;
\r
479 /* ... and that our priority has been disinherited to
\r
480 genqMUTEX_TEST_PRIORITY. */
\r
481 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
483 xErrorDetected = pdTRUE;
\r
486 /* Set our priority back to our original priority ready for the next
\r
487 loop around this test. */
\r
488 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
490 /* Just to show we are still running. */
\r
493 #if configUSE_PREEMPTION == 0
\r
498 /*-----------------------------------------------------------*/
\r
500 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
502 ( void ) pvParameters;
\r
506 /* The medium priority task starts by suspending itself. The low
\r
507 priority task will unsuspend this task when required. */
\r
508 vTaskSuspend( NULL );
\r
510 /* When this task unsuspends all it does is increment the guarded
\r
511 variable, this is so the low priority task knows that it has
\r
513 ulGuardedVariable++;
\r
516 /*-----------------------------------------------------------*/
\r
518 static void prvHighPriorityMutexTask( void *pvParameters )
\r
520 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
522 ( void ) pvParameters;
\r
526 /* The high priority task starts by suspending itself. The low
\r
527 priority task will unsuspend this task when required. */
\r
528 vTaskSuspend( NULL );
\r
530 /* When this task unsuspends all it does is attempt to obtain
\r
531 the mutex. It should find the mutex is not available so a
\r
532 block time is specified. */
\r
533 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
535 xErrorDetected = pdTRUE;
\r
538 /* When we eventually obtain the mutex we just give it back then
\r
539 return to suspend ready for the next test. */
\r
540 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
542 xErrorDetected = pdTRUE;
\r
546 /*-----------------------------------------------------------*/
\r
548 /* This is called to check that all the created tasks are still running. */
\r
549 portBASE_TYPE xAreAltGenericQueueTasksStillRunning( void )
\r
551 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
553 /* If the demo task is still running then we expect the loopcounters to
\r
554 have incremented since this function was last called. */
\r
555 if( ulLastLoopCounter == ulLoopCounter )
\r
557 xErrorDetected = pdTRUE;
\r
560 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
562 xErrorDetected = pdTRUE;
\r
565 ulLastLoopCounter = ulLoopCounter;
\r
566 ulLastLoopCounter2 = ulLoopCounter2;
\r
568 /* Errors detected in the task itself will have latched xErrorDetected
\r
571 return !xErrorDetected;
\r