2 FreeRTOS V7.5.2 - 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 * This file implements the same demo and test as GenQTest.c, but uses the
\r
68 * light weight API in place of the fully featured API.
\r
70 * See the comments at the top of GenQTest.c for a description.
\r
76 /* Scheduler include files. */
\r
77 #include "FreeRTOS.h"
\r
82 /* Demo program include files. */
\r
83 #include "AltQTest.h"
\r
85 #define genqQUEUE_LENGTH ( 5 )
\r
86 #define genqNO_BLOCK ( 0 )
\r
88 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
89 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
90 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
91 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
93 /*-----------------------------------------------------------*/
\r
96 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
97 * macros by using both to fill a queue, then reading from the queue to
\r
98 * check the resultant queue order is as expected. Queue data is also
\r
101 static void prvSendFrontAndBackTest( void *pvParameters );
\r
104 * The following three tasks are used to demonstrate the mutex behaviour.
\r
105 * Each task is given a different priority to demonstrate the priority
\r
106 * inheritance mechanism.
\r
108 * The low priority task obtains a mutex. After this a high priority task
\r
109 * attempts to obtain the same mutex, causing its priority to be inherited
\r
110 * by the low priority task. The task with the inherited high priority then
\r
111 * resumes a medium priority task to ensure it is not blocked by the medium
\r
112 * priority task while it holds the inherited high priority. Once the mutex
\r
113 * is returned the task with the inherited priority returns to its original
\r
114 * low priority, and is therefore immediately preempted by first the high
\r
115 * priority task and then the medium prioroity task before it can continue.
\r
117 static void prvLowPriorityMutexTask( void *pvParameters );
\r
118 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
119 static void prvHighPriorityMutexTask( void *pvParameters );
\r
121 /*-----------------------------------------------------------*/
\r
123 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
124 detected in any of the tasks. */
\r
125 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
127 /* Counters that are incremented on each cycle of a test. This is used to
\r
128 detect a stalled task - a test that is no longer running. */
\r
129 static volatile unsigned portLONG ulLoopCounter = 0;
\r
130 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
132 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
133 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
135 /* Handles used in the mutext test to suspend and resume the high and medium
\r
136 priority mutex test tasks. */
\r
137 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
139 /*-----------------------------------------------------------*/
\r
141 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
143 xQueueHandle xQueue;
\r
144 xSemaphoreHandle xMutex;
\r
146 /* Create the queue that we are going to use for the
\r
147 prvSendFrontAndBackTest demo. */
\r
148 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
150 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
151 in use. The queue registry is provided as a means for kernel aware
\r
152 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
153 is not being used. The call to vQueueAddToRegistry() will be removed
\r
154 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
155 defined to be less than 1. */
\r
156 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Alt_Gen_Test_Queue" );
\r
158 /* Create the demo task and pass it the queue just created. We are
\r
159 passing the queue handle by value so it does not matter that it is
\r
160 declared on the stack here. */
\r
161 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * ) "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
163 /* Create the mutex used by the prvMutexTest task. */
\r
164 xMutex = xSemaphoreCreateMutex();
\r
166 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
167 in use. The registry is provided as a means for kernel aware
\r
168 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
169 is not being used. The call to vQueueAddToRegistry() will be removed
\r
170 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
171 defined to be less than 1. */
\r
172 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Alt_Q_Mutex" );
\r
174 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
175 passing the mutex handle by value so it does not matter that it is declared
\r
176 on the stack here. */
\r
177 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * ) "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
178 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * ) "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
179 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * ) "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
181 /*-----------------------------------------------------------*/
\r
183 static void prvSendFrontAndBackTest( void *pvParameters )
\r
185 unsigned portLONG ulData, ulData2;
\r
186 xQueueHandle xQueue;
\r
189 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
191 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
193 /* Queue a message for printing to say the task has started. */
\r
194 vPrintDisplayMessage( &pcTaskStartMsg );
\r
197 xQueue = ( xQueueHandle ) pvParameters;
\r
201 /* The queue is empty, so sending an item to the back of the queue
\r
202 should have the same efect as sending it to the front of the queue.
\r
204 First send to the front and check everything is as expected. */
\r
205 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
207 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
209 xErrorDetected = pdTRUE;
\r
212 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
214 xErrorDetected = pdTRUE;
\r
217 /* The data we sent to the queue should equal the data we just received
\r
219 if( ulLoopCounter != ulData )
\r
221 xErrorDetected = pdTRUE;
\r
224 /* Then do the same, sending the data to the back, checking everything
\r
226 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
228 xErrorDetected = pdTRUE;
\r
231 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
233 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
235 xErrorDetected = pdTRUE;
\r
238 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
240 xErrorDetected = pdTRUE;
\r
243 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
245 xErrorDetected = pdTRUE;
\r
248 /* The data we sent to the queue should equal the data we just received
\r
250 if( ulLoopCounter != ulData )
\r
252 xErrorDetected = pdTRUE;
\r
255 #if configUSE_PREEMPTION == 0
\r
261 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
262 for( ulData = 2; ulData < 5; ulData++ )
\r
264 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
267 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
268 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
269 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
271 xErrorDetected = pdTRUE;
\r
274 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
276 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
278 /* Now the queue should be full, and when we read the data out we
\r
279 should receive 0, 1, 2, 3, 4. */
\r
280 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
282 xErrorDetected = pdTRUE;
\r
285 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
287 xErrorDetected = pdTRUE;
\r
290 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
292 xErrorDetected = pdTRUE;
\r
295 #if configUSE_PREEMPTION == 0
\r
299 /* Check the data we read out is in the expected order. */
\r
300 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
302 /* Try peeking the data first. */
\r
303 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
305 xErrorDetected = pdTRUE;
\r
308 if( ulData != ulData2 )
\r
310 xErrorDetected = pdTRUE;
\r
314 /* Now try receiving the data for real. The value should be the
\r
315 same. Clobber the value first so we know we really received it. */
\r
316 ulData2 = ~ulData2;
\r
317 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
319 xErrorDetected = pdTRUE;
\r
322 if( ulData != ulData2 )
\r
324 xErrorDetected = pdTRUE;
\r
328 /* The queue should now be empty again. */
\r
329 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
331 xErrorDetected = pdTRUE;
\r
334 #if configUSE_PREEMPTION == 0
\r
339 /* Our queue is empty once more, add 10, 11 to the back. */
\r
341 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
343 xErrorDetected = pdTRUE;
\r
346 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
348 xErrorDetected = pdTRUE;
\r
351 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
353 xErrorDetected = pdTRUE;
\r
356 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
358 for( ulData = 9; ulData >= 7; ulData-- )
\r
360 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
362 xErrorDetected = pdTRUE;
\r
366 /* Now check that the queue is full, and that receiving data provides
\r
367 the expected sequence of 7, 8, 9, 10, 11. */
\r
368 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
370 xErrorDetected = pdTRUE;
\r
373 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
375 xErrorDetected = pdTRUE;
\r
378 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
380 xErrorDetected = pdTRUE;
\r
383 #if configUSE_PREEMPTION == 0
\r
387 /* Check the data we read out is in the expected order. */
\r
388 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
390 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
392 xErrorDetected = pdTRUE;
\r
395 if( ulData != ulData2 )
\r
397 xErrorDetected = pdTRUE;
\r
401 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
403 xErrorDetected = pdTRUE;
\r
409 /*-----------------------------------------------------------*/
\r
411 static void prvLowPriorityMutexTask( void *pvParameters )
\r
413 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
416 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
418 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
420 /* Queue a message for printing to say the task has started. */
\r
421 vPrintDisplayMessage( &pcTaskStartMsg );
\r
424 ( void ) pvParameters;
\r
429 /* Take the mutex. It should be available now. */
\r
430 if( xSemaphoreAltTake( 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 /* We should now have inherited the prioritoy of the high priority task,
\r
450 as by now it will have attempted to get the mutex. */
\r
451 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
453 xErrorDetected = pdTRUE;
\r
456 /* We can attempt to set our priority to the test priority - between the
\r
457 idle priority and the medium/high test priorities, but our actual
\r
458 prioroity should remain at the high priority. */
\r
459 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
460 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
462 xErrorDetected = pdTRUE;
\r
465 /* Now unsuspend the medium priority task. This should not run as our
\r
466 inherited priority is above that of the medium priority task. */
\r
467 vTaskResume( xMediumPriorityMutexTask );
\r
469 /* If the did run then it will have incremented our guarded variable. */
\r
470 if( ulGuardedVariable != 0 )
\r
472 xErrorDetected = pdTRUE;
\r
475 /* When we give back the semaphore our priority should be disinherited
\r
476 back to the priority to which we attempted to set ourselves. This means
\r
477 that when the high priority task next blocks, the medium priority task
\r
478 should execute and increment the guarded variable. When we next run
\r
479 both the high and medium priority tasks will have been suspended again. */
\r
480 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
482 xErrorDetected = pdTRUE;
\r
485 /* Check that the guarded variable did indeed increment... */
\r
486 if( ulGuardedVariable != 1 )
\r
488 xErrorDetected = pdTRUE;
\r
491 /* ... and that our priority has been disinherited to
\r
492 genqMUTEX_TEST_PRIORITY. */
\r
493 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
495 xErrorDetected = pdTRUE;
\r
498 /* Set our priority back to our original priority ready for the next
\r
499 loop around this test. */
\r
500 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
502 /* Just to show we are still running. */
\r
505 #if configUSE_PREEMPTION == 0
\r
510 /*-----------------------------------------------------------*/
\r
512 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
514 ( void ) pvParameters;
\r
518 /* The medium priority task starts by suspending itself. The low
\r
519 priority task will unsuspend this task when required. */
\r
520 vTaskSuspend( NULL );
\r
522 /* When this task unsuspends all it does is increment the guarded
\r
523 variable, this is so the low priority task knows that it has
\r
525 ulGuardedVariable++;
\r
528 /*-----------------------------------------------------------*/
\r
530 static void prvHighPriorityMutexTask( void *pvParameters )
\r
532 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
534 ( void ) pvParameters;
\r
538 /* The high priority task starts by suspending itself. The low
\r
539 priority task will unsuspend this task when required. */
\r
540 vTaskSuspend( NULL );
\r
542 /* When this task unsuspends all it does is attempt to obtain
\r
543 the mutex. It should find the mutex is not available so a
\r
544 block time is specified. */
\r
545 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
547 xErrorDetected = pdTRUE;
\r
550 /* When we eventually obtain the mutex we just give it back then
\r
551 return to suspend ready for the next test. */
\r
552 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
554 xErrorDetected = pdTRUE;
\r
558 /*-----------------------------------------------------------*/
\r
560 /* This is called to check that all the created tasks are still running. */
\r
561 portBASE_TYPE xAreAltGenericQueueTasksStillRunning( void )
\r
563 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
565 /* If the demo task is still running then we expect the loopcounters to
\r
566 have incremented since this function was last called. */
\r
567 if( ulLastLoopCounter == ulLoopCounter )
\r
569 xErrorDetected = pdTRUE;
\r
572 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
574 xErrorDetected = pdTRUE;
\r
577 ulLastLoopCounter = ulLoopCounter;
\r
578 ulLastLoopCounter2 = ulLoopCounter2;
\r
580 /* Errors detected in the task itself will have latched xErrorDetected
\r
583 return !xErrorDetected;
\r