2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
68 * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -
\r
69 * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and
\r
72 * See the comments above the prvSendFrontAndBackTest() and
\r
73 * prvLowPriorityMutexTask() prototypes below for more information.
\r
79 /* Scheduler include files. */
\r
80 #include "FreeRTOS.h"
\r
85 /* Demo program include files. */
\r
86 #include "GenQTest.h"
\r
88 #define genqQUEUE_LENGTH ( 5 )
\r
89 #define genqNO_BLOCK ( 0 )
\r
91 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
92 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
93 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
94 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
96 /*-----------------------------------------------------------*/
\r
99 * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()
\r
100 * macros by using both to fill a queue, then reading from the queue to
\r
101 * check the resultant queue order is as expected. Queue data is also
\r
104 static void prvSendFrontAndBackTest( void *pvParameters );
\r
107 * The following three tasks are used to demonstrate the mutex behaviour.
\r
108 * Each task is given a different priority to demonstrate the priority
\r
109 * inheritance mechanism.
\r
111 * The low priority task obtains a mutex. After this a high priority task
\r
112 * attempts to obtain the same mutex, causing its priority to be inherited
\r
113 * by the low priority task. The task with the inherited high priority then
\r
114 * resumes a medium priority task to ensure it is not blocked by the medium
\r
115 * priority task while it holds the inherited high priority. Once the mutex
\r
116 * is returned the task with the inherited priority returns to its original
\r
117 * low priority, and is therefore immediately preempted by first the high
\r
118 * priority task and then the medium prioroity task before it can continue.
\r
120 static void prvLowPriorityMutexTask( void *pvParameters );
\r
121 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
122 static void prvHighPriorityMutexTask( void *pvParameters );
\r
124 /*-----------------------------------------------------------*/
\r
126 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
127 detected in any of the tasks. */
\r
128 static volatile portBASE_TYPE xErrorDetected = pdFALSE;
\r
130 /* Counters that are incremented on each cycle of a test. This is used to
\r
131 detect a stalled task - a test that is no longer running. */
\r
132 static volatile unsigned long ulLoopCounter = 0;
\r
133 static volatile unsigned long ulLoopCounter2 = 0;
\r
135 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
136 static volatile unsigned long ulGuardedVariable = 0;
\r
138 /* Handles used in the mutext test to suspend and resume the high and medium
\r
139 priority mutex test tasks. */
\r
140 static TaskHandle_t xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
142 /*-----------------------------------------------------------*/
\r
144 void vStartGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
146 QueueHandle_t xQueue;
\r
147 SemaphoreHandle_t xMutex;
\r
149 /* Create the queue that we are going to use for the
\r
150 prvSendFrontAndBackTest demo. */
\r
151 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned long ) );
\r
153 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
154 in use. The queue registry is provided as a means for kernel aware
\r
155 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
156 is not being used. The call to vQueueAddToRegistry() will be removed
\r
157 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
158 defined to be less than 1. */
\r
159 vQueueAddToRegistry( xQueue, "Gen_Queue_Test" );
\r
161 /* Create the demo task and pass it the queue just created. We are
\r
162 passing the queue handle by value so it does not matter that it is
\r
163 declared on the stack here. */
\r
164 xTaskCreate( prvSendFrontAndBackTest, "GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
166 /* Create the mutex used by the prvMutexTest task. */
\r
167 xMutex = xSemaphoreCreateMutex();
\r
169 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
170 in use. The registry is provided as a means for kernel aware
\r
171 debuggers to locate mutexes and has no purpose if a kernel aware debugger
\r
172 is not being used. The call to vQueueAddToRegistry() will be removed
\r
173 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
174 defined to be less than 1. */
\r
175 vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Gen_Queue_Mutex" );
\r
177 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
178 passing the mutex handle by value so it does not matter that it is declared
\r
179 on the stack here. */
\r
180 xTaskCreate( prvLowPriorityMutexTask, "MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
181 xTaskCreate( prvMediumPriorityMutexTask, "MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
182 xTaskCreate( prvHighPriorityMutexTask, "MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
184 /*-----------------------------------------------------------*/
\r
186 static void prvSendFrontAndBackTest( void *pvParameters )
\r
188 unsigned long ulData, ulData2;
\r
189 QueueHandle_t xQueue;
\r
192 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
194 const char * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";
\r
196 /* Queue a message for printing to say the task has started. */
\r
197 vPrintDisplayMessage( &pcTaskStartMsg );
\r
200 xQueue = ( QueueHandle_t ) pvParameters;
\r
204 /* The queue is empty, so sending an item to the back of the queue
\r
205 should have the same efect as sending it to the front of the queue.
\r
207 First send to the front and check everything is as expected. */
\r
208 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
210 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
212 xErrorDetected = pdTRUE;
\r
215 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
217 xErrorDetected = pdTRUE;
\r
220 /* The data we sent to the queue should equal the data we just received
\r
222 if( ulLoopCounter != ulData )
\r
224 xErrorDetected = pdTRUE;
\r
227 /* Then do the same, sending the data to the back, checking everything
\r
229 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
231 xErrorDetected = pdTRUE;
\r
234 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
236 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
238 xErrorDetected = pdTRUE;
\r
241 if( xQueueReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
243 xErrorDetected = pdTRUE;
\r
246 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
248 xErrorDetected = pdTRUE;
\r
251 /* The data we sent to the queue should equal the data we just received
\r
253 if( ulLoopCounter != ulData )
\r
255 xErrorDetected = pdTRUE;
\r
258 #if configUSE_PREEMPTION == 0
\r
264 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
265 for( ulData = 2; ulData < 5; ulData++ )
\r
267 xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
270 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
271 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
272 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
274 xErrorDetected = pdTRUE;
\r
277 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
279 xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
281 /* Now the queue should be full, and when we read the data out we
\r
282 should receive 0, 1, 2, 3, 4. */
\r
283 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
285 xErrorDetected = pdTRUE;
\r
288 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
290 xErrorDetected = pdTRUE;
\r
293 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
295 xErrorDetected = pdTRUE;
\r
298 #if configUSE_PREEMPTION == 0
\r
302 /* Check the data we read out is in the expected order. */
\r
303 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
305 /* Try peeking the data first. */
\r
306 if( xQueuePeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
308 xErrorDetected = pdTRUE;
\r
311 if( ulData != ulData2 )
\r
313 xErrorDetected = pdTRUE;
\r
317 /* Now try receiving the data for real. The value should be the
\r
318 same. Clobber the value first so we know we really received it. */
\r
319 ulData2 = ~ulData2;
\r
320 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
322 xErrorDetected = pdTRUE;
\r
325 if( ulData != ulData2 )
\r
327 xErrorDetected = pdTRUE;
\r
331 /* The queue should now be empty again. */
\r
332 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
334 xErrorDetected = pdTRUE;
\r
337 #if configUSE_PREEMPTION == 0
\r
342 /* Our queue is empty once more, add 10, 11 to the back. */
\r
344 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
346 xErrorDetected = pdTRUE;
\r
349 if( xQueueSend( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
351 xErrorDetected = pdTRUE;
\r
354 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
356 xErrorDetected = pdTRUE;
\r
359 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
361 for( ulData = 9; ulData >= 7; ulData-- )
\r
363 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
365 xErrorDetected = pdTRUE;
\r
369 /* Now check that the queue is full, and that receiving data provides
\r
370 the expected sequence of 7, 8, 9, 10, 11. */
\r
371 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
373 xErrorDetected = pdTRUE;
\r
376 if( xQueueSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
378 xErrorDetected = pdTRUE;
\r
381 if( xQueueSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
383 xErrorDetected = pdTRUE;
\r
386 #if configUSE_PREEMPTION == 0
\r
390 /* Check the data we read out is in the expected order. */
\r
391 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
393 if( xQueueReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
395 xErrorDetected = pdTRUE;
\r
398 if( ulData != ulData2 )
\r
400 xErrorDetected = pdTRUE;
\r
404 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
406 xErrorDetected = pdTRUE;
\r
412 /*-----------------------------------------------------------*/
\r
414 static void prvLowPriorityMutexTask( void *pvParameters )
\r
416 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;
\r
419 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
421 const char * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";
\r
423 /* Queue a message for printing to say the task has started. */
\r
424 vPrintDisplayMessage( &pcTaskStartMsg );
\r
429 /* Take the mutex. It should be available now. */
\r
430 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
432 xErrorDetected = pdTRUE;
\r
435 /* Set our guarded variable to a known start value. */
\r
436 ulGuardedVariable = 0;
\r
438 /* Our priority should be as per that assigned when the task was
\r
440 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
442 xErrorDetected = pdTRUE;
\r
445 /* Now unsuspend the high priority task. This will attempt to take the
\r
446 mutex, and block when it finds it cannot obtain it. */
\r
447 vTaskResume( xHighPriorityMutexTask );
\r
449 #if configUSE_PREEMPTION == 0
\r
453 /* Ensure the task is reporting it priority as blocked and not
\r
454 suspended (as it would have done in versions up to V7.5.3). */
\r
455 #if( INCLUDE_eTaskGetState == 1 )
\r
457 configASSERT( eTaskGetState( xHighPriorityMutexTask ) == eBlocked );
\r
459 #endif /* INCLUDE_eTaskGetState */
\r
461 /* We should now have inherited the prioritoy of the high priority task,
\r
462 as by now it will have attempted to get the mutex. */
\r
463 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
465 xErrorDetected = pdTRUE;
\r
468 /* We can attempt to set our priority to the test priority - between the
\r
469 idle priority and the medium/high test priorities, but our actual
\r
470 prioroity should remain at the high priority. */
\r
471 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
472 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
474 xErrorDetected = pdTRUE;
\r
477 /* Now unsuspend the medium priority task. This should not run as our
\r
478 inherited priority is above that of the medium priority task. */
\r
479 vTaskResume( xMediumPriorityMutexTask );
\r
481 /* If the did run then it will have incremented our guarded variable. */
\r
482 if( ulGuardedVariable != 0 )
\r
484 xErrorDetected = pdTRUE;
\r
487 /* When we give back the semaphore our priority should be disinherited
\r
488 back to the priority to which we attempted to set ourselves. This means
\r
489 that when the high priority task next blocks, the medium priority task
\r
490 should execute and increment the guarded variable. When we next run
\r
491 both the high and medium priority tasks will have been suspended again. */
\r
492 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
494 xErrorDetected = pdTRUE;
\r
497 #if configUSE_PREEMPTION == 0
\r
501 /* Check that the guarded variable did indeed increment... */
\r
502 if( ulGuardedVariable != 1 )
\r
504 xErrorDetected = pdTRUE;
\r
507 /* ... and that our priority has been disinherited to
\r
508 genqMUTEX_TEST_PRIORITY. */
\r
509 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
511 xErrorDetected = pdTRUE;
\r
514 /* Set our priority back to our original priority ready for the next
\r
515 loop around this test. */
\r
516 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
518 /* Just to show we are still running. */
\r
521 #if configUSE_PREEMPTION == 0
\r
526 /*-----------------------------------------------------------*/
\r
528 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
530 ( void ) pvParameters;
\r
534 /* The medium priority task starts by suspending itself. The low
\r
535 priority task will unsuspend this task when required. */
\r
536 vTaskSuspend( NULL );
\r
538 /* When this task unsuspends all it does is increment the guarded
\r
539 variable, this is so the low priority task knows that it has
\r
541 ulGuardedVariable++;
\r
544 /*-----------------------------------------------------------*/
\r
546 static void prvHighPriorityMutexTask( void *pvParameters )
\r
548 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;
\r
552 /* The high priority task starts by suspending itself. The low
\r
553 priority task will unsuspend this task when required. */
\r
554 vTaskSuspend( NULL );
\r
556 /* When this task unsuspends all it does is attempt to obtain
\r
557 the mutex. It should find the mutex is not available so a
\r
558 block time is specified. */
\r
559 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
561 xErrorDetected = pdTRUE;
\r
564 /* When we eventually obtain the mutex we just give it back then
\r
565 return to suspend ready for the next test. */
\r
566 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
568 xErrorDetected = pdTRUE;
\r
572 /*-----------------------------------------------------------*/
\r
574 /* This is called to check that all the created tasks are still running. */
\r
575 portBASE_TYPE xAreGenericQueueTasksStillRunning( void )
\r
577 static unsigned long ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
579 /* If the demo task is still running then we expect the loopcounters to
\r
580 have incremented since this function was last called. */
\r
581 if( ulLastLoopCounter == ulLoopCounter )
\r
583 xErrorDetected = pdTRUE;
\r
586 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
588 xErrorDetected = pdTRUE;
\r
591 ulLastLoopCounter = ulLoopCounter;
\r
592 ulLastLoopCounter2 = ulLoopCounter2;
\r
594 /* Errors detected in the task itself will have latched xErrorDetected
\r
597 return ( portBASE_TYPE ) !xErrorDetected;
\r