2 FreeRTOS V8.0.1 - 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 !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\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 BaseType_t 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 uint32_t ulLoopCounter = 0;
\r
133 static volatile uint32_t ulLoopCounter2 = 0;
\r
135 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
136 static volatile uint32_t 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( UBaseType_t 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( uint32_t ) );
\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 uint32_t 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, xLocalMutex;
\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
427 /* The local mutex is used to check the 'mutexs held' count. */
\r
428 xLocalMutex = xSemaphoreCreateMutex();
\r
429 configASSERT( xLocalMutex );
\r
433 /* Take the mutex. It should be available now. */
\r
434 if( xSemaphoreTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
436 xErrorDetected = pdTRUE;
\r
439 /* Set the guarded variable to a known start value. */
\r
440 ulGuardedVariable = 0;
\r
442 /* This task's priority should be as per that assigned when the task was
\r
444 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
446 xErrorDetected = pdTRUE;
\r
449 /* Now unsuspend the high priority task. This will attempt to take the
\r
450 mutex, and block when it finds it cannot obtain it. */
\r
451 vTaskResume( xHighPriorityMutexTask );
\r
453 #if configUSE_PREEMPTION == 0
\r
457 /* Ensure the task is reporting its priority as blocked and not
\r
458 suspended (as it would have done in versions up to V7.5.3). */
\r
459 #if( INCLUDE_eTaskGetState == 1 )
\r
461 configASSERT( eTaskGetState( xHighPriorityMutexTask ) == eBlocked );
\r
463 #endif /* INCLUDE_eTaskGetState */
\r
465 /* The priority of the high priority task should now have been inherited
\r
466 as by now it will have attempted to get the mutex. */
\r
467 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
469 xErrorDetected = pdTRUE;
\r
472 /* Attempt to set the priority of this task to the test priority -
\r
473 between the idle priority and the medium/high test priorities, but the
\r
474 actual priority should remain at the high priority. */
\r
475 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
476 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
478 xErrorDetected = pdTRUE;
\r
481 /* Now unsuspend the medium priority task. This should not run as the
\r
482 inherited priority of this task is above that of the medium priority
\r
484 vTaskResume( xMediumPriorityMutexTask );
\r
486 /* If the medium priority task did run then it will have incremented the
\r
487 guarded variable. */
\r
488 if( ulGuardedVariable != 0 )
\r
490 xErrorDetected = pdTRUE;
\r
493 /* Take the local mutex too, so two mutexes are now held. */
\r
494 if( xSemaphoreTake( xLocalMutex, genqNO_BLOCK ) != pdPASS )
\r
496 xErrorDetected = pdTRUE;
\r
499 /* When the semaphore is given back the priority of this task should not
\r
500 yet be disinherited because the local mutex is still held. This is a
\r
501 simplification to allow FreeRTOS to be integrated with middleware that
\r
502 attempts to hold multiple mutexes without bloating the code with complex
\r
503 algorithms. It is possible that the high priority mutex task will
\r
504 execute as it shares a priority with this task. */
\r
505 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
507 xErrorDetected = pdTRUE;
\r
510 #if configUSE_PREEMPTION == 0
\r
514 /* The guarded variable is only incremented by the medium priority task,
\r
515 which still should not have executed as this task should remain at the
\r
516 higher priority, ensure this is the case. */
\r
517 if( ulGuardedVariable != 0 )
\r
519 xErrorDetected = pdTRUE;
\r
522 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
524 xErrorDetected = pdTRUE;
\r
527 /* Now also give back the local mutex, taking the held count back to 0.
\r
528 This time the priority of this task should be disinherited back to the
\r
529 priority to which it was set while the mutex was held. This means
\r
530 the medium priority task should execute and increment the guarded
\r
531 variable. When this task next runs both the high and medium priority
\r
532 tasks will have been suspended again. */
\r
533 if( xSemaphoreGive( xLocalMutex ) != pdPASS )
\r
535 xErrorDetected = pdTRUE;
\r
538 #if configUSE_PREEMPTION == 0
\r
542 /* Check the guarded variable did indeed increment... */
\r
543 if( ulGuardedVariable != 1 )
\r
545 xErrorDetected = pdTRUE;
\r
548 /* ... and that the priority of this task has been disinherited to
\r
549 genqMUTEX_TEST_PRIORITY. */
\r
550 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
552 xErrorDetected = pdTRUE;
\r
555 /* Set the priority of this task back to its original value, ready for
\r
556 the next loop around this test. */
\r
557 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
559 /* Just to show this task is still running. */
\r
562 #if configUSE_PREEMPTION == 0
\r
567 /*-----------------------------------------------------------*/
\r
569 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
571 ( void ) pvParameters;
\r
575 /* The medium priority task starts by suspending itself. The low
\r
576 priority task will unsuspend this task when required. */
\r
577 vTaskSuspend( NULL );
\r
579 /* When this task unsuspends all it does is increment the guarded
\r
580 variable, this is so the low priority task knows that it has
\r
582 ulGuardedVariable++;
\r
585 /*-----------------------------------------------------------*/
\r
587 static void prvHighPriorityMutexTask( void *pvParameters )
\r
589 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;
\r
593 /* The high priority task starts by suspending itself. The low
\r
594 priority task will unsuspend this task when required. */
\r
595 vTaskSuspend( NULL );
\r
597 /* When this task unsuspends all it does is attempt to obtain
\r
598 the mutex. It should find the mutex is not available so a
\r
599 block time is specified. */
\r
600 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
602 xErrorDetected = pdTRUE;
\r
605 /* When the mutex is eventually obtained it is just given back before
\r
606 returning to suspend ready for the next cycle. */
\r
607 if( xSemaphoreGive( xMutex ) != pdPASS )
\r
609 xErrorDetected = pdTRUE;
\r
613 /*-----------------------------------------------------------*/
\r
615 /* This is called to check that all the created tasks are still running. */
\r
616 BaseType_t xAreGenericQueueTasksStillRunning( void )
\r
618 static uint32_t ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
620 /* If the demo task is still running then we expect the loopcounters to
\r
621 have incremented since this function was last called. */
\r
622 if( ulLastLoopCounter == ulLoopCounter )
\r
624 xErrorDetected = pdTRUE;
\r
627 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
629 xErrorDetected = pdTRUE;
\r
632 ulLastLoopCounter = ulLoopCounter;
\r
633 ulLastLoopCounter2 = ulLoopCounter2;
\r
635 /* Errors detected in the task itself will have latched xErrorDetected
\r
638 return ( BaseType_t ) !xErrorDetected;
\r