2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
67 * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -
\r
68 * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and
\r
71 * See the comments above the prvSendFrontAndBackTest() and
\r
72 * prvLowPriorityMutexTask() prototypes below for more information.
\r
78 /* Scheduler include files. */
\r
79 #include "FreeRTOS.h"
\r
84 /* Demo program include files. */
\r
85 #include "GenQTest.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 xQueueSendToFront() and xQueueSendToBack()
\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 volatile 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 vStartGenericQueueTasks( 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 * ) "Gen_Queue_Test" );
\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 * )"GenQ", 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 mutexes 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 * ) "Gen_Queue_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 * )"MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
180 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * )"MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
181 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * )"MuHigh", 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 = "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 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
209 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
211 xErrorDetected = pdTRUE;
\r
214 if( xQueueReceive( 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 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
235 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
237 xErrorDetected = pdTRUE;
\r
240 if( xQueueReceive( 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 xQueueSendToBack( 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 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
278 xQueueSendToFront( 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( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
289 xErrorDetected = pdTRUE;
\r
292 if( xQueueSendToBack( 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( xQueuePeek( 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( xQueueReceive( 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( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
345 xErrorDetected = pdTRUE;
\r
348 if( xQueueSend( 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( xQueueSendToFront( 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( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
377 xErrorDetected = pdTRUE;
\r
380 if( xQueueSendToBack( 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( xQueueReceive( 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 = "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
428 /* Take the mutex. It should be available now. */
\r
429 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
431 xErrorDetected = pdTRUE;
\r
434 /* Set our guarded variable to a known start value. */
\r
435 ulGuardedVariable = 0;
\r
437 /* Our priority should be as per that assigned when the task was
\r
439 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
441 xErrorDetected = pdTRUE;
\r
444 /* Now unsuspend the high priority task. This will attempt to take the
\r
445 mutex, and block when it finds it cannot obtain it. */
\r
446 vTaskResume( xHighPriorityMutexTask );
\r
448 /* We should now have inherited the prioritoy of the high priority task,
\r
449 as by now it will have attempted to get the mutex. */
\r
450 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
452 xErrorDetected = pdTRUE;
\r
455 /* We can attempt to set our priority to the test priority - between the
\r
456 idle priority and the medium/high test priorities, but our actual
\r
457 prioroity should remain at the high priority. */
\r
458 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
459 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
461 xErrorDetected = pdTRUE;
\r
464 /* Now unsuspend the medium priority task. This should not run as our
\r
465 inherited priority is above that of the medium priority task. */
\r
466 vTaskResume( xMediumPriorityMutexTask );
\r
468 /* If the did run then it will have incremented our guarded variable. */
\r
469 if( ulGuardedVariable != 0 )
\r
471 xErrorDetected = pdTRUE;
\r
474 /* When we give back the semaphore our priority should be disinherited
\r
475 back to the priority to which we attempted to set ourselves. This means
\r
476 that when the high priority task next blocks, the medium priority task
\r
477 should execute and increment the guarded variable. When we next run
\r
478 both the high and medium priority tasks will have been suspended again. */
\r
479 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
481 xErrorDetected = pdTRUE;
\r
484 /* Check that the guarded variable did indeed increment... */
\r
485 if( ulGuardedVariable != 1 )
\r
487 xErrorDetected = pdTRUE;
\r
490 /* ... and that our priority has been disinherited to
\r
491 genqMUTEX_TEST_PRIORITY. */
\r
492 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
494 xErrorDetected = pdTRUE;
\r
497 /* Set our priority back to our original priority ready for the next
\r
498 loop around this test. */
\r
499 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
501 /* Just to show we are still running. */
\r
504 #if configUSE_PREEMPTION == 0
\r
509 /*-----------------------------------------------------------*/
\r
511 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
513 ( void ) pvParameters;
\r
517 /* The medium priority task starts by suspending itself. The low
\r
518 priority task will unsuspend this task when required. */
\r
519 vTaskSuspend( NULL );
\r
521 /* When this task unsuspends all it does is increment the guarded
\r
522 variable, this is so the low priority task knows that it has
\r
524 ulGuardedVariable++;
\r
527 /*-----------------------------------------------------------*/
\r
529 static void prvHighPriorityMutexTask( void *pvParameters )
\r
531 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
535 /* The high priority task starts by suspending itself. The low
\r
536 priority task will unsuspend this task when required. */
\r
537 vTaskSuspend( NULL );
\r
539 /* When this task unsuspends all it does is attempt to obtain
\r
540 the mutex. It should find the mutex is not available so a
\r
541 block time is specified. */
\r
542 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
544 xErrorDetected = pdTRUE;
\r
547 /* When we eventually obtain the mutex we just give it back then
\r
548 return to suspend ready for the next test. */
\r
549 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
551 xErrorDetected = pdTRUE;
\r
555 /*-----------------------------------------------------------*/
\r
557 /* This is called to check that all the created tasks are still running. */
\r
558 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )
\r
560 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
562 /* If the demo task is still running then we expect the loopcounters to
\r
563 have incremented since this function was last called. */
\r
564 if( ulLastLoopCounter == ulLoopCounter )
\r
566 xErrorDetected = pdTRUE;
\r
569 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
571 xErrorDetected = pdTRUE;
\r
574 ulLastLoopCounter = ulLoopCounter;
\r
575 ulLastLoopCounter2 = ulLoopCounter2;
\r
577 /* Errors detected in the task itself will have latched xErrorDetected
\r
580 return ( portBASE_TYPE ) !xErrorDetected;
\r