2 FreeRTOS V7.1.1 - 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 * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -
\r
70 * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and
\r
73 * See the comments above the prvSendFrontAndBackTest() and
\r
74 * prvLowPriorityMutexTask() prototypes below for more information.
\r
80 /* Scheduler include files. */
\r
81 #include "FreeRTOS.h"
\r
86 /* Demo program include files. */
\r
87 #include "GenQTest.h"
\r
89 #define genqQUEUE_LENGTH ( 5 )
\r
90 #define genqNO_BLOCK ( 0 )
\r
92 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
93 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
94 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
95 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
97 /*-----------------------------------------------------------*/
\r
100 * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()
\r
101 * macros by using both to fill a queue, then reading from the queue to
\r
102 * check the resultant queue order is as expected. Queue data is also
\r
105 static void prvSendFrontAndBackTest( void *pvParameters );
\r
108 * The following three tasks are used to demonstrate the mutex behaviour.
\r
109 * Each task is given a different priority to demonstrate the priority
\r
110 * inheritance mechanism.
\r
112 * The low priority task obtains a mutex. After this a high priority task
\r
113 * attempts to obtain the same mutex, causing its priority to be inherited
\r
114 * by the low priority task. The task with the inherited high priority then
\r
115 * resumes a medium priority task to ensure it is not blocked by the medium
\r
116 * priority task while it holds the inherited high priority. Once the mutex
\r
117 * is returned the task with the inherited priority returns to its original
\r
118 * low priority, and is therefore immediately preempted by first the high
\r
119 * priority task and then the medium prioroity task before it can continue.
\r
121 static void prvLowPriorityMutexTask( void *pvParameters );
\r
122 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
123 static void prvHighPriorityMutexTask( void *pvParameters );
\r
125 /*-----------------------------------------------------------*/
\r
127 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
128 detected in any of the tasks. */
\r
129 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
131 /* Counters that are incremented on each cycle of a test. This is used to
\r
132 detect a stalled task - a test that is no longer running. */
\r
133 static volatile unsigned portLONG ulLoopCounter = 0;
\r
134 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
136 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
137 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
139 /* Handles used in the mutext test to suspend and resume the high and medium
\r
140 priority mutex test tasks. */
\r
141 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
143 /*-----------------------------------------------------------*/
\r
145 void vStartGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
147 xQueueHandle xQueue;
\r
148 xSemaphoreHandle xMutex;
\r
150 /* Create the queue that we are going to use for the
\r
151 prvSendFrontAndBackTest demo. */
\r
152 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
154 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
155 in use. The queue registry is provided as a means for kernel aware
\r
156 debuggers to locate queues 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( xQueue, ( signed portCHAR * ) "Gen_Queue_Test" );
\r
162 /* Create the demo task and pass it the queue just created. We are
\r
163 passing the queue handle by value so it does not matter that it is
\r
164 declared on the stack here. */
\r
165 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * )"GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
167 /* Create the mutex used by the prvMutexTest task. */
\r
168 xMutex = xSemaphoreCreateMutex();
\r
170 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
171 in use. The registry is provided as a means for kernel aware
\r
172 debuggers to locate mutexes and has no purpose if a kernel aware debugger
\r
173 is not being used. The call to vQueueAddToRegistry() will be removed
\r
174 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
175 defined to be less than 1. */
\r
176 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Gen_Queue_Mutex" );
\r
178 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
179 passing the mutex handle by value so it does not matter that it is declared
\r
180 on the stack here. */
\r
181 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * )"MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
182 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * )"MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
183 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * )"MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
185 /*-----------------------------------------------------------*/
\r
187 static void prvSendFrontAndBackTest( void *pvParameters )
\r
189 unsigned portLONG ulData, ulData2;
\r
190 xQueueHandle xQueue;
\r
193 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
195 const portCHAR * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";
\r
197 /* Queue a message for printing to say the task has started. */
\r
198 vPrintDisplayMessage( &pcTaskStartMsg );
\r
201 xQueue = ( xQueueHandle ) pvParameters;
\r
205 /* The queue is empty, so sending an item to the back of the queue
\r
206 should have the same efect as sending it to the front of the queue.
\r
208 First send to the front and check everything is as expected. */
\r
209 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
211 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
213 xErrorDetected = pdTRUE;
\r
216 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
218 xErrorDetected = pdTRUE;
\r
221 /* The data we sent to the queue should equal the data we just received
\r
223 if( ulLoopCounter != ulData )
\r
225 xErrorDetected = pdTRUE;
\r
228 /* Then do the same, sending the data to the back, checking everything
\r
230 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
232 xErrorDetected = pdTRUE;
\r
235 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
237 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
239 xErrorDetected = pdTRUE;
\r
242 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
244 xErrorDetected = pdTRUE;
\r
247 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
249 xErrorDetected = pdTRUE;
\r
252 /* The data we sent to the queue should equal the data we just received
\r
254 if( ulLoopCounter != ulData )
\r
256 xErrorDetected = pdTRUE;
\r
259 #if configUSE_PREEMPTION == 0
\r
265 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
266 for( ulData = 2; ulData < 5; ulData++ )
\r
268 xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
271 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
272 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
273 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
275 xErrorDetected = pdTRUE;
\r
278 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
280 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
282 /* Now the queue should be full, and when we read the data out we
\r
283 should receive 0, 1, 2, 3, 4. */
\r
284 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
286 xErrorDetected = pdTRUE;
\r
289 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
291 xErrorDetected = pdTRUE;
\r
294 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
296 xErrorDetected = pdTRUE;
\r
299 #if configUSE_PREEMPTION == 0
\r
303 /* Check the data we read out is in the expected order. */
\r
304 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
306 /* Try peeking the data first. */
\r
307 if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
309 xErrorDetected = pdTRUE;
\r
312 if( ulData != ulData2 )
\r
314 xErrorDetected = pdTRUE;
\r
318 /* Now try receiving the data for real. The value should be the
\r
319 same. Clobber the value first so we know we really received it. */
\r
320 ulData2 = ~ulData2;
\r
321 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
323 xErrorDetected = pdTRUE;
\r
326 if( ulData != ulData2 )
\r
328 xErrorDetected = pdTRUE;
\r
332 /* The queue should now be empty again. */
\r
333 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
335 xErrorDetected = pdTRUE;
\r
338 #if configUSE_PREEMPTION == 0
\r
343 /* Our queue is empty once more, add 10, 11 to the back. */
\r
345 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
347 xErrorDetected = pdTRUE;
\r
350 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
352 xErrorDetected = pdTRUE;
\r
355 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
357 xErrorDetected = pdTRUE;
\r
360 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
362 for( ulData = 9; ulData >= 7; ulData-- )
\r
364 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
366 xErrorDetected = pdTRUE;
\r
370 /* Now check that the queue is full, and that receiving data provides
\r
371 the expected sequence of 7, 8, 9, 10, 11. */
\r
372 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
374 xErrorDetected = pdTRUE;
\r
377 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
379 xErrorDetected = pdTRUE;
\r
382 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
384 xErrorDetected = pdTRUE;
\r
387 #if configUSE_PREEMPTION == 0
\r
391 /* Check the data we read out is in the expected order. */
\r
392 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
394 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
396 xErrorDetected = pdTRUE;
\r
399 if( ulData != ulData2 )
\r
401 xErrorDetected = pdTRUE;
\r
405 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
407 xErrorDetected = pdTRUE;
\r
413 /*-----------------------------------------------------------*/
\r
415 static void prvLowPriorityMutexTask( void *pvParameters )
\r
417 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
420 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
422 const portCHAR * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";
\r
424 /* Queue a message for printing to say the task has started. */
\r
425 vPrintDisplayMessage( &pcTaskStartMsg );
\r
430 /* Take the mutex. It should be available now. */
\r
431 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
433 xErrorDetected = pdTRUE;
\r
436 /* Set our guarded variable to a known start value. */
\r
437 ulGuardedVariable = 0;
\r
439 /* Our priority should be as per that assigned when the task was
\r
441 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
443 xErrorDetected = pdTRUE;
\r
446 /* Now unsuspend the high priority task. This will attempt to take the
\r
447 mutex, and block when it finds it cannot obtain it. */
\r
448 vTaskResume( xHighPriorityMutexTask );
\r
450 /* We should now have inherited the prioritoy of the high priority task,
\r
451 as by now it will have attempted to get the mutex. */
\r
452 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
454 xErrorDetected = pdTRUE;
\r
457 /* We can attempt to set our priority to the test priority - between the
\r
458 idle priority and the medium/high test priorities, but our actual
\r
459 prioroity should remain at the high priority. */
\r
460 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
461 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
463 xErrorDetected = pdTRUE;
\r
466 /* Now unsuspend the medium priority task. This should not run as our
\r
467 inherited priority is above that of the medium priority task. */
\r
468 vTaskResume( xMediumPriorityMutexTask );
\r
470 /* If the did run then it will have incremented our guarded variable. */
\r
471 if( ulGuardedVariable != 0 )
\r
473 xErrorDetected = pdTRUE;
\r
476 /* When we give back the semaphore our priority should be disinherited
\r
477 back to the priority to which we attempted to set ourselves. This means
\r
478 that when the high priority task next blocks, the medium priority task
\r
479 should execute and increment the guarded variable. When we next run
\r
480 both the high and medium priority tasks will have been suspended again. */
\r
481 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
483 xErrorDetected = pdTRUE;
\r
486 /* Check that the guarded variable did indeed increment... */
\r
487 if( ulGuardedVariable != 1 )
\r
489 xErrorDetected = pdTRUE;
\r
492 /* ... and that our priority has been disinherited to
\r
493 genqMUTEX_TEST_PRIORITY. */
\r
494 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
496 xErrorDetected = pdTRUE;
\r
499 /* Set our priority back to our original priority ready for the next
\r
500 loop around this test. */
\r
501 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
503 /* Just to show we are still running. */
\r
506 #if configUSE_PREEMPTION == 0
\r
511 /*-----------------------------------------------------------*/
\r
513 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
515 ( void ) pvParameters;
\r
519 /* The medium priority task starts by suspending itself. The low
\r
520 priority task will unsuspend this task when required. */
\r
521 vTaskSuspend( NULL );
\r
523 /* When this task unsuspends all it does is increment the guarded
\r
524 variable, this is so the low priority task knows that it has
\r
526 ulGuardedVariable++;
\r
529 /*-----------------------------------------------------------*/
\r
531 static void prvHighPriorityMutexTask( void *pvParameters )
\r
533 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
537 /* The high priority task starts by suspending itself. The low
\r
538 priority task will unsuspend this task when required. */
\r
539 vTaskSuspend( NULL );
\r
541 /* When this task unsuspends all it does is attempt to obtain
\r
542 the mutex. It should find the mutex is not available so a
\r
543 block time is specified. */
\r
544 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
546 xErrorDetected = pdTRUE;
\r
549 /* When we eventually obtain the mutex we just give it back then
\r
550 return to suspend ready for the next test. */
\r
551 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
553 xErrorDetected = pdTRUE;
\r
557 /*-----------------------------------------------------------*/
\r
559 /* This is called to check that all the created tasks are still running. */
\r
560 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )
\r
562 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
564 /* If the demo task is still running then we expect the loopcounters to
\r
565 have incremented since this function was last called. */
\r
566 if( ulLastLoopCounter == ulLoopCounter )
\r
568 xErrorDetected = pdTRUE;
\r
571 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
573 xErrorDetected = pdTRUE;
\r
576 ulLastLoopCounter = ulLoopCounter;
\r
577 ulLastLoopCounter2 = ulLoopCounter2;
\r
579 /* Errors detected in the task itself will have latched xErrorDetected
\r
582 return !xErrorDetected;
\r