2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
71 * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -
\r
72 * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and
\r
75 * See the comments above the prvSendFrontAndBackTest() and
\r
76 * prvLowPriorityMutexTask() prototypes below for more information.
\r
82 /* Scheduler include files. */
\r
83 #include "FreeRTOS.h"
\r
88 /* Demo program include files. */
\r
89 #include "GenQTest.h"
\r
91 #define genqQUEUE_LENGTH ( 5 )
\r
92 #define genqNO_BLOCK ( 0 )
\r
94 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
95 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
96 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
97 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
99 /*-----------------------------------------------------------*/
\r
102 * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()
\r
103 * macros by using both to fill a queue, then reading from the queue to
\r
104 * check the resultant queue order is as expected. Queue data is also
\r
107 static void prvSendFrontAndBackTest( void *pvParameters );
\r
110 * The following three tasks are used to demonstrate the mutex behaviour.
\r
111 * Each task is given a different priority to demonstrate the priority
\r
112 * inheritance mechanism.
\r
114 * The low priority task obtains a mutex. After this a high priority task
\r
115 * attempts to obtain the same mutex, causing its priority to be inherited
\r
116 * by the low priority task. The task with the inherited high priority then
\r
117 * resumes a medium priority task to ensure it is not blocked by the medium
\r
118 * priority task while it holds the inherited high priority. Once the mutex
\r
119 * is returned the task with the inherited priority returns to its original
\r
120 * low priority, and is therefore immediately preempted by first the high
\r
121 * priority task and then the medium prioroity task before it can continue.
\r
123 static void prvLowPriorityMutexTask( void *pvParameters );
\r
124 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
125 static void prvHighPriorityMutexTask( void *pvParameters );
\r
127 /*-----------------------------------------------------------*/
\r
129 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
130 detected in any of the tasks. */
\r
131 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
133 /* Counters that are incremented on each cycle of a test. This is used to
\r
134 detect a stalled task - a test that is no longer running. */
\r
135 static volatile unsigned portLONG ulLoopCounter = 0;
\r
136 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
138 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
139 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
141 /* Handles used in the mutext test to suspend and resume the high and medium
\r
142 priority mutex test tasks. */
\r
143 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
145 /*-----------------------------------------------------------*/
\r
147 void vStartGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
149 xQueueHandle xQueue;
\r
150 xSemaphoreHandle xMutex;
\r
152 /* Create the queue that we are going to use for the
\r
153 prvSendFrontAndBackTest demo. */
\r
154 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
156 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
157 in use. The queue registry is provided as a means for kernel aware
\r
158 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
159 is not being used. The call to vQueueAddToRegistry() will be removed
\r
160 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
161 defined to be less than 1. */
\r
162 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Gen_Queue_Test" );
\r
164 /* Create the demo task and pass it the queue just created. We are
\r
165 passing the queue handle by value so it does not matter that it is
\r
166 declared on the stack here. */
\r
167 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * )"GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
169 /* Create the mutex used by the prvMutexTest task. */
\r
170 xMutex = xSemaphoreCreateMutex();
\r
172 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
173 in use. The registry is provided as a means for kernel aware
\r
174 debuggers to locate mutexes and has no purpose if a kernel aware debugger
\r
175 is not being used. The call to vQueueAddToRegistry() will be removed
\r
176 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
177 defined to be less than 1. */
\r
178 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Gen_Queue_Mutex" );
\r
180 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
181 passing the mutex handle by value so it does not matter that it is declared
\r
182 on the stack here. */
\r
183 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * )"MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
184 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * )"MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
185 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * )"MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
187 /*-----------------------------------------------------------*/
\r
189 static void prvSendFrontAndBackTest( void *pvParameters )
\r
191 unsigned portLONG ulData, ulData2;
\r
192 xQueueHandle xQueue;
\r
195 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
197 const portCHAR * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";
\r
199 /* Queue a message for printing to say the task has started. */
\r
200 vPrintDisplayMessage( &pcTaskStartMsg );
\r
203 xQueue = ( xQueueHandle ) pvParameters;
\r
207 /* The queue is empty, so sending an item to the back of the queue
\r
208 should have the same efect as sending it to the front of the queue.
\r
210 First send to the front and check everything is as expected. */
\r
211 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
213 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
215 xErrorDetected = pdTRUE;
\r
218 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
220 xErrorDetected = pdTRUE;
\r
223 /* The data we sent to the queue should equal the data we just received
\r
225 if( ulLoopCounter != ulData )
\r
227 xErrorDetected = pdTRUE;
\r
230 /* Then do the same, sending the data to the back, checking everything
\r
232 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
234 xErrorDetected = pdTRUE;
\r
237 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
239 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
241 xErrorDetected = pdTRUE;
\r
244 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
246 xErrorDetected = pdTRUE;
\r
249 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
251 xErrorDetected = pdTRUE;
\r
254 /* The data we sent to the queue should equal the data we just received
\r
256 if( ulLoopCounter != ulData )
\r
258 xErrorDetected = pdTRUE;
\r
261 #if configUSE_PREEMPTION == 0
\r
267 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
268 for( ulData = 2; ulData < 5; ulData++ )
\r
270 xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
273 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
274 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
275 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
277 xErrorDetected = pdTRUE;
\r
280 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
282 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
284 /* Now the queue should be full, and when we read the data out we
\r
285 should receive 0, 1, 2, 3, 4. */
\r
286 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
288 xErrorDetected = pdTRUE;
\r
291 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
293 xErrorDetected = pdTRUE;
\r
296 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
298 xErrorDetected = pdTRUE;
\r
301 #if configUSE_PREEMPTION == 0
\r
305 /* Check the data we read out is in the expected order. */
\r
306 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
308 /* Try peeking the data first. */
\r
309 if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
311 xErrorDetected = pdTRUE;
\r
314 if( ulData != ulData2 )
\r
316 xErrorDetected = pdTRUE;
\r
320 /* Now try receiving the data for real. The value should be the
\r
321 same. Clobber the value first so we know we really received it. */
\r
322 ulData2 = ~ulData2;
\r
323 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
325 xErrorDetected = pdTRUE;
\r
328 if( ulData != ulData2 )
\r
330 xErrorDetected = pdTRUE;
\r
334 /* The queue should now be empty again. */
\r
335 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
337 xErrorDetected = pdTRUE;
\r
340 #if configUSE_PREEMPTION == 0
\r
345 /* Our queue is empty once more, add 10, 11 to the back. */
\r
347 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
349 xErrorDetected = pdTRUE;
\r
352 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
354 xErrorDetected = pdTRUE;
\r
357 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
359 xErrorDetected = pdTRUE;
\r
362 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
364 for( ulData = 9; ulData >= 7; ulData-- )
\r
366 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
368 xErrorDetected = pdTRUE;
\r
372 /* Now check that the queue is full, and that receiving data provides
\r
373 the expected sequence of 7, 8, 9, 10, 11. */
\r
374 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
376 xErrorDetected = pdTRUE;
\r
379 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
381 xErrorDetected = pdTRUE;
\r
384 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
386 xErrorDetected = pdTRUE;
\r
389 #if configUSE_PREEMPTION == 0
\r
393 /* Check the data we read out is in the expected order. */
\r
394 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
396 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
398 xErrorDetected = pdTRUE;
\r
401 if( ulData != ulData2 )
\r
403 xErrorDetected = pdTRUE;
\r
407 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
409 xErrorDetected = pdTRUE;
\r
415 /*-----------------------------------------------------------*/
\r
417 static void prvLowPriorityMutexTask( void *pvParameters )
\r
419 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
422 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
424 const portCHAR * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";
\r
426 /* Queue a message for printing to say the task has started. */
\r
427 vPrintDisplayMessage( &pcTaskStartMsg );
\r
432 /* Take the mutex. It should be available now. */
\r
433 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
435 xErrorDetected = pdTRUE;
\r
438 /* Set our guarded variable to a known start value. */
\r
439 ulGuardedVariable = 0;
\r
441 /* Our priority should be as per that assigned when the task was
\r
443 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
445 xErrorDetected = pdTRUE;
\r
448 /* Now unsuspend the high priority task. This will attempt to take the
\r
449 mutex, and block when it finds it cannot obtain it. */
\r
450 vTaskResume( xHighPriorityMutexTask );
\r
452 /* We should now have inherited the prioritoy of the high priority task,
\r
453 as by now it will have attempted to get the mutex. */
\r
454 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
456 xErrorDetected = pdTRUE;
\r
459 /* We can attempt to set our priority to the test priority - between the
\r
460 idle priority and the medium/high test priorities, but our actual
\r
461 prioroity should remain at the high priority. */
\r
462 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
463 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
465 xErrorDetected = pdTRUE;
\r
468 /* Now unsuspend the medium priority task. This should not run as our
\r
469 inherited priority is above that of the medium priority task. */
\r
470 vTaskResume( xMediumPriorityMutexTask );
\r
472 /* If the did run then it will have incremented our guarded variable. */
\r
473 if( ulGuardedVariable != 0 )
\r
475 xErrorDetected = pdTRUE;
\r
478 /* When we give back the semaphore our priority should be disinherited
\r
479 back to the priority to which we attempted to set ourselves. This means
\r
480 that when the high priority task next blocks, the medium priority task
\r
481 should execute and increment the guarded variable. When we next run
\r
482 both the high and medium priority tasks will have been suspended again. */
\r
483 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
485 xErrorDetected = pdTRUE;
\r
488 /* Check that the guarded variable did indeed increment... */
\r
489 if( ulGuardedVariable != 1 )
\r
491 xErrorDetected = pdTRUE;
\r
494 /* ... and that our priority has been disinherited to
\r
495 genqMUTEX_TEST_PRIORITY. */
\r
496 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
498 xErrorDetected = pdTRUE;
\r
501 /* Set our priority back to our original priority ready for the next
\r
502 loop around this test. */
\r
503 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
505 /* Just to show we are still running. */
\r
508 #if configUSE_PREEMPTION == 0
\r
513 /*-----------------------------------------------------------*/
\r
515 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
517 ( void ) pvParameters;
\r
521 /* The medium priority task starts by suspending itself. The low
\r
522 priority task will unsuspend this task when required. */
\r
523 vTaskSuspend( NULL );
\r
525 /* When this task unsuspends all it does is increment the guarded
\r
526 variable, this is so the low priority task knows that it has
\r
528 ulGuardedVariable++;
\r
531 /*-----------------------------------------------------------*/
\r
533 static void prvHighPriorityMutexTask( void *pvParameters )
\r
535 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
539 /* The high priority task starts by suspending itself. The low
\r
540 priority task will unsuspend this task when required. */
\r
541 vTaskSuspend( NULL );
\r
543 /* When this task unsuspends all it does is attempt to obtain
\r
544 the mutex. It should find the mutex is not available so a
\r
545 block time is specified. */
\r
546 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
548 xErrorDetected = pdTRUE;
\r
551 /* When we eventually obtain the mutex we just give it back then
\r
552 return to suspend ready for the next test. */
\r
553 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
555 xErrorDetected = pdTRUE;
\r
559 /*-----------------------------------------------------------*/
\r
561 /* This is called to check that all the created tasks are still running. */
\r
562 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )
\r
564 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
566 /* If the demo task is still running then we expect the loopcounters to
\r
567 have incremented since this function was last called. */
\r
568 if( ulLastLoopCounter == ulLoopCounter )
\r
570 xErrorDetected = pdTRUE;
\r
573 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
575 xErrorDetected = pdTRUE;
\r
578 ulLastLoopCounter = ulLoopCounter;
\r
579 ulLastLoopCounter2 = ulLoopCounter2;
\r
581 /* Errors detected in the task itself will have latched xErrorDetected
\r
584 return !xErrorDetected;
\r