2 FreeRTOS V7.2.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\r
60 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
61 the code with commercial support, indemnification, and middleware, under
\r
62 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
63 provide a safety engineered and independently SIL3 certified version under
\r
64 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 * This file implements the same demo and test as GenQTest.c, but uses the
\r
70 * light weight API in place of the fully featured API.
\r
72 * See the comments at the top of GenQTest.c for a description.
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
84 /* Demo program include files. */
\r
85 #include "AltQTest.h"
\r
87 #define genqQUEUE_LENGTH ( 5 )
\r
88 #define genqNO_BLOCK ( 0 )
\r
90 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
91 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
92 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
93 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
95 /*-----------------------------------------------------------*/
\r
98 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
99 * macros by using both to fill a queue, then reading from the queue to
\r
100 * check the resultant queue order is as expected. Queue data is also
\r
103 static void prvSendFrontAndBackTest( void *pvParameters );
\r
106 * The following three tasks are used to demonstrate the mutex behaviour.
\r
107 * Each task is given a different priority to demonstrate the priority
\r
108 * inheritance mechanism.
\r
110 * The low priority task obtains a mutex. After this a high priority task
\r
111 * attempts to obtain the same mutex, causing its priority to be inherited
\r
112 * by the low priority task. The task with the inherited high priority then
\r
113 * resumes a medium priority task to ensure it is not blocked by the medium
\r
114 * priority task while it holds the inherited high priority. Once the mutex
\r
115 * is returned the task with the inherited priority returns to its original
\r
116 * low priority, and is therefore immediately preempted by first the high
\r
117 * priority task and then the medium prioroity task before it can continue.
\r
119 static void prvLowPriorityMutexTask( void *pvParameters );
\r
120 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
121 static void prvHighPriorityMutexTask( void *pvParameters );
\r
123 /*-----------------------------------------------------------*/
\r
125 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
126 detected in any of the tasks. */
\r
127 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
129 /* Counters that are incremented on each cycle of a test. This is used to
\r
130 detect a stalled task - a test that is no longer running. */
\r
131 static volatile unsigned portLONG ulLoopCounter = 0;
\r
132 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
134 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
135 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
137 /* Handles used in the mutext test to suspend and resume the high and medium
\r
138 priority mutex test tasks. */
\r
139 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
141 /*-----------------------------------------------------------*/
\r
143 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
145 xQueueHandle xQueue;
\r
146 xSemaphoreHandle xMutex;
\r
148 /* Create the queue that we are going to use for the
\r
149 prvSendFrontAndBackTest demo. */
\r
150 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
152 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
153 in use. The queue registry is provided as a means for kernel aware
\r
154 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
155 is not being used. The call to vQueueAddToRegistry() will be removed
\r
156 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
157 defined to be less than 1. */
\r
158 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Alt_Gen_Test_Queue" );
\r
160 /* Create the demo task and pass it the queue just created. We are
\r
161 passing the queue handle by value so it does not matter that it is
\r
162 declared on the stack here. */
\r
163 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * ) "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
165 /* Create the mutex used by the prvMutexTest task. */
\r
166 xMutex = xSemaphoreCreateMutex();
\r
168 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
169 in use. The registry is provided as a means for kernel aware
\r
170 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
171 is not being used. The call to vQueueAddToRegistry() will be removed
\r
172 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
173 defined to be less than 1. */
\r
174 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Alt_Q_Mutex" );
\r
176 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
177 passing the mutex handle by value so it does not matter that it is declared
\r
178 on the stack here. */
\r
179 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * ) "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
180 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * ) "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
181 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * ) "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
183 /*-----------------------------------------------------------*/
\r
185 static void prvSendFrontAndBackTest( void *pvParameters )
\r
187 unsigned portLONG ulData, ulData2;
\r
188 xQueueHandle xQueue;
\r
191 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
193 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
195 /* Queue a message for printing to say the task has started. */
\r
196 vPrintDisplayMessage( &pcTaskStartMsg );
\r
199 xQueue = ( xQueueHandle ) pvParameters;
\r
203 /* The queue is empty, so sending an item to the back of the queue
\r
204 should have the same efect as sending it to the front of the queue.
\r
206 First send to the front and check everything is as expected. */
\r
207 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
209 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
211 xErrorDetected = pdTRUE;
\r
214 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\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 /* Then do the same, sending the data to the back, checking everything
\r
228 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
230 xErrorDetected = pdTRUE;
\r
233 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
235 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
237 xErrorDetected = pdTRUE;
\r
240 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
242 xErrorDetected = pdTRUE;
\r
245 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
247 xErrorDetected = pdTRUE;
\r
250 /* The data we sent to the queue should equal the data we just received
\r
252 if( ulLoopCounter != ulData )
\r
254 xErrorDetected = pdTRUE;
\r
257 #if configUSE_PREEMPTION == 0
\r
263 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
264 for( ulData = 2; ulData < 5; ulData++ )
\r
266 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
269 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
270 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
271 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
273 xErrorDetected = pdTRUE;
\r
276 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
278 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
280 /* Now the queue should be full, and when we read the data out we
\r
281 should receive 0, 1, 2, 3, 4. */
\r
282 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
284 xErrorDetected = pdTRUE;
\r
287 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
289 xErrorDetected = pdTRUE;
\r
292 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
294 xErrorDetected = pdTRUE;
\r
297 #if configUSE_PREEMPTION == 0
\r
301 /* Check the data we read out is in the expected order. */
\r
302 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
304 /* Try peeking the data first. */
\r
305 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
307 xErrorDetected = pdTRUE;
\r
310 if( ulData != ulData2 )
\r
312 xErrorDetected = pdTRUE;
\r
316 /* Now try receiving the data for real. The value should be the
\r
317 same. Clobber the value first so we know we really received it. */
\r
318 ulData2 = ~ulData2;
\r
319 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
321 xErrorDetected = pdTRUE;
\r
324 if( ulData != ulData2 )
\r
326 xErrorDetected = pdTRUE;
\r
330 /* The queue should now be empty again. */
\r
331 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
333 xErrorDetected = pdTRUE;
\r
336 #if configUSE_PREEMPTION == 0
\r
341 /* Our queue is empty once more, add 10, 11 to the back. */
\r
343 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
345 xErrorDetected = pdTRUE;
\r
348 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
350 xErrorDetected = pdTRUE;
\r
353 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
355 xErrorDetected = pdTRUE;
\r
358 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
360 for( ulData = 9; ulData >= 7; ulData-- )
\r
362 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
364 xErrorDetected = pdTRUE;
\r
368 /* Now check that the queue is full, and that receiving data provides
\r
369 the expected sequence of 7, 8, 9, 10, 11. */
\r
370 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
372 xErrorDetected = pdTRUE;
\r
375 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
377 xErrorDetected = pdTRUE;
\r
380 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
382 xErrorDetected = pdTRUE;
\r
385 #if configUSE_PREEMPTION == 0
\r
389 /* Check the data we read out is in the expected order. */
\r
390 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
392 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
394 xErrorDetected = pdTRUE;
\r
397 if( ulData != ulData2 )
\r
399 xErrorDetected = pdTRUE;
\r
403 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
405 xErrorDetected = pdTRUE;
\r
411 /*-----------------------------------------------------------*/
\r
413 static void prvLowPriorityMutexTask( void *pvParameters )
\r
415 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
418 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
420 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
422 /* Queue a message for printing to say the task has started. */
\r
423 vPrintDisplayMessage( &pcTaskStartMsg );
\r
426 ( void ) pvParameters;
\r
431 /* Take the mutex. It should be available now. */
\r
432 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
434 xErrorDetected = pdTRUE;
\r
437 /* Set our guarded variable to a known start value. */
\r
438 ulGuardedVariable = 0;
\r
440 /* Our priority should be as per that assigned when the task was
\r
442 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
444 xErrorDetected = pdTRUE;
\r
447 /* Now unsuspend the high priority task. This will attempt to take the
\r
448 mutex, and block when it finds it cannot obtain it. */
\r
449 vTaskResume( xHighPriorityMutexTask );
\r
451 /* We should now have inherited the prioritoy of the high priority task,
\r
452 as by now it will have attempted to get the mutex. */
\r
453 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
455 xErrorDetected = pdTRUE;
\r
458 /* We can attempt to set our priority to the test priority - between the
\r
459 idle priority and the medium/high test priorities, but our actual
\r
460 prioroity should remain at the high priority. */
\r
461 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
462 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
464 xErrorDetected = pdTRUE;
\r
467 /* Now unsuspend the medium priority task. This should not run as our
\r
468 inherited priority is above that of the medium priority task. */
\r
469 vTaskResume( xMediumPriorityMutexTask );
\r
471 /* If the did run then it will have incremented our guarded variable. */
\r
472 if( ulGuardedVariable != 0 )
\r
474 xErrorDetected = pdTRUE;
\r
477 /* When we give back the semaphore our priority should be disinherited
\r
478 back to the priority to which we attempted to set ourselves. This means
\r
479 that when the high priority task next blocks, the medium priority task
\r
480 should execute and increment the guarded variable. When we next run
\r
481 both the high and medium priority tasks will have been suspended again. */
\r
482 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
484 xErrorDetected = pdTRUE;
\r
487 /* Check that the guarded variable did indeed increment... */
\r
488 if( ulGuardedVariable != 1 )
\r
490 xErrorDetected = pdTRUE;
\r
493 /* ... and that our priority has been disinherited to
\r
494 genqMUTEX_TEST_PRIORITY. */
\r
495 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
497 xErrorDetected = pdTRUE;
\r
500 /* Set our priority back to our original priority ready for the next
\r
501 loop around this test. */
\r
502 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
504 /* Just to show we are still running. */
\r
507 #if configUSE_PREEMPTION == 0
\r
512 /*-----------------------------------------------------------*/
\r
514 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
516 ( void ) pvParameters;
\r
520 /* The medium priority task starts by suspending itself. The low
\r
521 priority task will unsuspend this task when required. */
\r
522 vTaskSuspend( NULL );
\r
524 /* When this task unsuspends all it does is increment the guarded
\r
525 variable, this is so the low priority task knows that it has
\r
527 ulGuardedVariable++;
\r
530 /*-----------------------------------------------------------*/
\r
532 static void prvHighPriorityMutexTask( void *pvParameters )
\r
534 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
536 ( void ) pvParameters;
\r
540 /* The high priority task starts by suspending itself. The low
\r
541 priority task will unsuspend this task when required. */
\r
542 vTaskSuspend( NULL );
\r
544 /* When this task unsuspends all it does is attempt to obtain
\r
545 the mutex. It should find the mutex is not available so a
\r
546 block time is specified. */
\r
547 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
549 xErrorDetected = pdTRUE;
\r
552 /* When we eventually obtain the mutex we just give it back then
\r
553 return to suspend ready for the next test. */
\r
554 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
556 xErrorDetected = pdTRUE;
\r
560 /*-----------------------------------------------------------*/
\r
562 /* This is called to check that all the created tasks are still running. */
\r
563 portBASE_TYPE xAreAltGenericQueueTasksStillRunning( void )
\r
565 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
567 /* If the demo task is still running then we expect the loopcounters to
\r
568 have incremented since this function was last called. */
\r
569 if( ulLastLoopCounter == ulLoopCounter )
\r
571 xErrorDetected = pdTRUE;
\r
574 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
576 xErrorDetected = pdTRUE;
\r
579 ulLastLoopCounter = ulLoopCounter;
\r
580 ulLastLoopCounter2 = ulLoopCounter2;
\r
582 /* Errors detected in the task itself will have latched xErrorDetected
\r
585 return !xErrorDetected;
\r