2 FreeRTOS V7.6.0 - Copyright (C) 2013 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 * This file implements the same demo and test as GenQTest.c, but uses the
\r
69 * light weight API in place of the fully featured API.
\r
71 * See the comments at the top of GenQTest.c for a description.
\r
77 /* Scheduler include files. */
\r
78 #include "FreeRTOS.h"
\r
83 /* Demo program include files. */
\r
84 #include "AltQTest.h"
\r
86 #define genqQUEUE_LENGTH ( 5 )
\r
87 #define genqNO_BLOCK ( 0 )
\r
89 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
90 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
91 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
92 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
94 /*-----------------------------------------------------------*/
\r
97 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
98 * macros by using both to fill a queue, then reading from the queue to
\r
99 * check the resultant queue order is as expected. Queue data is also
\r
102 static void prvSendFrontAndBackTest( void *pvParameters );
\r
105 * The following three tasks are used to demonstrate the mutex behaviour.
\r
106 * Each task is given a different priority to demonstrate the priority
\r
107 * inheritance mechanism.
\r
109 * The low priority task obtains a mutex. After this a high priority task
\r
110 * attempts to obtain the same mutex, causing its priority to be inherited
\r
111 * by the low priority task. The task with the inherited high priority then
\r
112 * resumes a medium priority task to ensure it is not blocked by the medium
\r
113 * priority task while it holds the inherited high priority. Once the mutex
\r
114 * is returned the task with the inherited priority returns to its original
\r
115 * low priority, and is therefore immediately preempted by first the high
\r
116 * priority task and then the medium prioroity task before it can continue.
\r
118 static void prvLowPriorityMutexTask( void *pvParameters );
\r
119 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
120 static void prvHighPriorityMutexTask( void *pvParameters );
\r
122 /*-----------------------------------------------------------*/
\r
124 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
125 detected in any of the tasks. */
\r
126 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
128 /* Counters that are incremented on each cycle of a test. This is used to
\r
129 detect a stalled task - a test that is no longer running. */
\r
130 static volatile unsigned portLONG ulLoopCounter = 0;
\r
131 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
133 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
134 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
136 /* Handles used in the mutext test to suspend and resume the high and medium
\r
137 priority mutex test tasks. */
\r
138 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
140 /*-----------------------------------------------------------*/
\r
142 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
144 xQueueHandle xQueue;
\r
145 xSemaphoreHandle xMutex;
\r
147 /* Create the queue that we are going to use for the
\r
148 prvSendFrontAndBackTest demo. */
\r
149 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
151 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
152 in use. The queue registry is provided as a means for kernel aware
\r
153 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
154 is not being used. The call to vQueueAddToRegistry() will be removed
\r
155 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
156 defined to be less than 1. */
\r
157 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Alt_Gen_Test_Queue" );
\r
159 /* Create the demo task and pass it the queue just created. We are
\r
160 passing the queue handle by value so it does not matter that it is
\r
161 declared on the stack here. */
\r
162 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * ) "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
164 /* Create the mutex used by the prvMutexTest task. */
\r
165 xMutex = xSemaphoreCreateMutex();
\r
167 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
168 in use. The registry is provided as a means for kernel aware
\r
169 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
170 is not being used. The call to vQueueAddToRegistry() will be removed
\r
171 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
172 defined to be less than 1. */
\r
173 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Alt_Q_Mutex" );
\r
175 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
176 passing the mutex handle by value so it does not matter that it is declared
\r
177 on the stack here. */
\r
178 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * ) "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
179 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * ) "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
180 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * ) "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
182 /*-----------------------------------------------------------*/
\r
184 static void prvSendFrontAndBackTest( void *pvParameters )
\r
186 unsigned portLONG ulData, ulData2;
\r
187 xQueueHandle xQueue;
\r
190 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
192 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
194 /* Queue a message for printing to say the task has started. */
\r
195 vPrintDisplayMessage( &pcTaskStartMsg );
\r
198 xQueue = ( xQueueHandle ) pvParameters;
\r
202 /* The queue is empty, so sending an item to the back of the queue
\r
203 should have the same efect as sending it to the front of the queue.
\r
205 First send to the front and check everything is as expected. */
\r
206 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
208 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
210 xErrorDetected = pdTRUE;
\r
213 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
215 xErrorDetected = pdTRUE;
\r
218 /* The data we sent to the queue should equal the data we just received
\r
220 if( ulLoopCounter != ulData )
\r
222 xErrorDetected = pdTRUE;
\r
225 /* Then do the same, sending the data to the back, checking everything
\r
227 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
229 xErrorDetected = pdTRUE;
\r
232 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
234 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
236 xErrorDetected = pdTRUE;
\r
239 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
241 xErrorDetected = pdTRUE;
\r
244 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
246 xErrorDetected = pdTRUE;
\r
249 /* The data we sent to the queue should equal the data we just received
\r
251 if( ulLoopCounter != ulData )
\r
253 xErrorDetected = pdTRUE;
\r
256 #if configUSE_PREEMPTION == 0
\r
262 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
263 for( ulData = 2; ulData < 5; ulData++ )
\r
265 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
268 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
269 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
270 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
272 xErrorDetected = pdTRUE;
\r
275 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
277 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
279 /* Now the queue should be full, and when we read the data out we
\r
280 should receive 0, 1, 2, 3, 4. */
\r
281 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
283 xErrorDetected = pdTRUE;
\r
286 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
288 xErrorDetected = pdTRUE;
\r
291 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
293 xErrorDetected = pdTRUE;
\r
296 #if configUSE_PREEMPTION == 0
\r
300 /* Check the data we read out is in the expected order. */
\r
301 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
303 /* Try peeking the data first. */
\r
304 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
306 xErrorDetected = pdTRUE;
\r
309 if( ulData != ulData2 )
\r
311 xErrorDetected = pdTRUE;
\r
315 /* Now try receiving the data for real. The value should be the
\r
316 same. Clobber the value first so we know we really received it. */
\r
317 ulData2 = ~ulData2;
\r
318 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
320 xErrorDetected = pdTRUE;
\r
323 if( ulData != ulData2 )
\r
325 xErrorDetected = pdTRUE;
\r
329 /* The queue should now be empty again. */
\r
330 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
332 xErrorDetected = pdTRUE;
\r
335 #if configUSE_PREEMPTION == 0
\r
340 /* Our queue is empty once more, add 10, 11 to the back. */
\r
342 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
344 xErrorDetected = pdTRUE;
\r
347 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
349 xErrorDetected = pdTRUE;
\r
352 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
354 xErrorDetected = pdTRUE;
\r
357 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
359 for( ulData = 9; ulData >= 7; ulData-- )
\r
361 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
363 xErrorDetected = pdTRUE;
\r
367 /* Now check that the queue is full, and that receiving data provides
\r
368 the expected sequence of 7, 8, 9, 10, 11. */
\r
369 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
371 xErrorDetected = pdTRUE;
\r
374 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
376 xErrorDetected = pdTRUE;
\r
379 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
381 xErrorDetected = pdTRUE;
\r
384 #if configUSE_PREEMPTION == 0
\r
388 /* Check the data we read out is in the expected order. */
\r
389 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
391 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
393 xErrorDetected = pdTRUE;
\r
396 if( ulData != ulData2 )
\r
398 xErrorDetected = pdTRUE;
\r
402 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
404 xErrorDetected = pdTRUE;
\r
410 /*-----------------------------------------------------------*/
\r
412 static void prvLowPriorityMutexTask( void *pvParameters )
\r
414 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
417 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
419 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
421 /* Queue a message for printing to say the task has started. */
\r
422 vPrintDisplayMessage( &pcTaskStartMsg );
\r
425 ( void ) pvParameters;
\r
430 /* Take the mutex. It should be available now. */
\r
431 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
433 xErrorDetected = pdTRUE;
\r
436 /* Set our guarded variable to a known start value. */
\r
437 ulGuardedVariable = 0;
\r
439 /* Our priority should be as per that assigned when the task was
\r
441 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
443 xErrorDetected = pdTRUE;
\r
446 /* Now unsuspend the high priority task. This will attempt to take the
\r
447 mutex, and block when it finds it cannot obtain it. */
\r
448 vTaskResume( xHighPriorityMutexTask );
\r
450 /* We should now have inherited the prioritoy of the high priority task,
\r
451 as by now it will have attempted to get the mutex. */
\r
452 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
454 xErrorDetected = pdTRUE;
\r
457 /* We can attempt to set our priority to the test priority - between the
\r
458 idle priority and the medium/high test priorities, but our actual
\r
459 prioroity should remain at the high priority. */
\r
460 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
461 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
463 xErrorDetected = pdTRUE;
\r
466 /* Now unsuspend the medium priority task. This should not run as our
\r
467 inherited priority is above that of the medium priority task. */
\r
468 vTaskResume( xMediumPriorityMutexTask );
\r
470 /* If the did run then it will have incremented our guarded variable. */
\r
471 if( ulGuardedVariable != 0 )
\r
473 xErrorDetected = pdTRUE;
\r
476 /* When we give back the semaphore our priority should be disinherited
\r
477 back to the priority to which we attempted to set ourselves. This means
\r
478 that when the high priority task next blocks, the medium priority task
\r
479 should execute and increment the guarded variable. When we next run
\r
480 both the high and medium priority tasks will have been suspended again. */
\r
481 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
483 xErrorDetected = pdTRUE;
\r
486 /* Check that the guarded variable did indeed increment... */
\r
487 if( ulGuardedVariable != 1 )
\r
489 xErrorDetected = pdTRUE;
\r
492 /* ... and that our priority has been disinherited to
\r
493 genqMUTEX_TEST_PRIORITY. */
\r
494 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
496 xErrorDetected = pdTRUE;
\r
499 /* Set our priority back to our original priority ready for the next
\r
500 loop around this test. */
\r
501 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
503 /* Just to show we are still running. */
\r
506 #if configUSE_PREEMPTION == 0
\r
511 /*-----------------------------------------------------------*/
\r
513 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
515 ( void ) pvParameters;
\r
519 /* The medium priority task starts by suspending itself. The low
\r
520 priority task will unsuspend this task when required. */
\r
521 vTaskSuspend( NULL );
\r
523 /* When this task unsuspends all it does is increment the guarded
\r
524 variable, this is so the low priority task knows that it has
\r
526 ulGuardedVariable++;
\r
529 /*-----------------------------------------------------------*/
\r
531 static void prvHighPriorityMutexTask( void *pvParameters )
\r
533 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
535 ( void ) 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( xSemaphoreAltTake( 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( xSemaphoreAltGive( 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 xAreAltGenericQueueTasksStillRunning( 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