2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
71 * This file implements the same demo and test as GenQTest.c, but uses the
\r
72 * light weight API in place of the fully featured API.
\r
74 * See the comments at the top of GenQTest.c for a description.
\r
80 /* Scheduler include files. */
\r
81 #include "FreeRTOS.h"
\r
86 /* Demo program include files. */
\r
87 #include "AltQTest.h"
\r
89 #define genqQUEUE_LENGTH ( 5 )
\r
90 #define genqNO_BLOCK ( 0 )
\r
92 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
93 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
94 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
95 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
97 /*-----------------------------------------------------------*/
\r
100 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
101 * macros by using both to fill a queue, then reading from the queue to
\r
102 * check the resultant queue order is as expected. Queue data is also
\r
105 static void prvSendFrontAndBackTest( void *pvParameters );
\r
108 * The following three tasks are used to demonstrate the mutex behaviour.
\r
109 * Each task is given a different priority to demonstrate the priority
\r
110 * inheritance mechanism.
\r
112 * The low priority task obtains a mutex. After this a high priority task
\r
113 * attempts to obtain the same mutex, causing its priority to be inherited
\r
114 * by the low priority task. The task with the inherited high priority then
\r
115 * resumes a medium priority task to ensure it is not blocked by the medium
\r
116 * priority task while it holds the inherited high priority. Once the mutex
\r
117 * is returned the task with the inherited priority returns to its original
\r
118 * low priority, and is therefore immediately preempted by first the high
\r
119 * priority task and then the medium prioroity task before it can continue.
\r
121 static void prvLowPriorityMutexTask( void *pvParameters );
\r
122 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
123 static void prvHighPriorityMutexTask( void *pvParameters );
\r
125 /*-----------------------------------------------------------*/
\r
127 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
128 detected in any of the tasks. */
\r
129 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
131 /* Counters that are incremented on each cycle of a test. This is used to
\r
132 detect a stalled task - a test that is no longer running. */
\r
133 static volatile unsigned portLONG ulLoopCounter = 0;
\r
134 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
136 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
137 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
139 /* Handles used in the mutext test to suspend and resume the high and medium
\r
140 priority mutex test tasks. */
\r
141 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
143 /*-----------------------------------------------------------*/
\r
145 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
147 xQueueHandle xQueue;
\r
148 xSemaphoreHandle xMutex;
\r
150 /* Create the queue that we are going to use for the
\r
151 prvSendFrontAndBackTest demo. */
\r
152 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
154 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
155 in use. The queue registry is provided as a means for kernel aware
\r
156 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
157 is not being used. The call to vQueueAddToRegistry() will be removed
\r
158 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
159 defined to be less than 1. */
\r
160 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Alt_Gen_Test_Queue" );
\r
162 /* Create the demo task and pass it the queue just created. We are
\r
163 passing the queue handle by value so it does not matter that it is
\r
164 declared on the stack here. */
\r
165 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * ) "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
167 /* Create the mutex used by the prvMutexTest task. */
\r
168 xMutex = xSemaphoreCreateMutex();
\r
170 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
171 in use. The registry is provided as a means for kernel aware
\r
172 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
173 is not being used. The call to vQueueAddToRegistry() will be removed
\r
174 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
175 defined to be less than 1. */
\r
176 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Alt_Q_Mutex" );
\r
178 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
179 passing the mutex handle by value so it does not matter that it is declared
\r
180 on the stack here. */
\r
181 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * ) "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
182 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * ) "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
183 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * ) "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
185 /*-----------------------------------------------------------*/
\r
187 static void prvSendFrontAndBackTest( void *pvParameters )
\r
189 unsigned portLONG ulData, ulData2;
\r
190 xQueueHandle xQueue;
\r
193 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
195 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
197 /* Queue a message for printing to say the task has started. */
\r
198 vPrintDisplayMessage( &pcTaskStartMsg );
\r
201 xQueue = ( xQueueHandle ) pvParameters;
\r
205 /* The queue is empty, so sending an item to the back of the queue
\r
206 should have the same efect as sending it to the front of the queue.
\r
208 First send to the front and check everything is as expected. */
\r
209 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
211 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
213 xErrorDetected = pdTRUE;
\r
216 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
218 xErrorDetected = pdTRUE;
\r
221 /* The data we sent to the queue should equal the data we just received
\r
223 if( ulLoopCounter != ulData )
\r
225 xErrorDetected = pdTRUE;
\r
228 /* Then do the same, sending the data to the back, checking everything
\r
230 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
232 xErrorDetected = pdTRUE;
\r
235 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
237 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
239 xErrorDetected = pdTRUE;
\r
242 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
244 xErrorDetected = pdTRUE;
\r
247 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
249 xErrorDetected = pdTRUE;
\r
252 /* The data we sent to the queue should equal the data we just received
\r
254 if( ulLoopCounter != ulData )
\r
256 xErrorDetected = pdTRUE;
\r
259 #if configUSE_PREEMPTION == 0
\r
265 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
266 for( ulData = 2; ulData < 5; ulData++ )
\r
268 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
271 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
272 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
273 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
275 xErrorDetected = pdTRUE;
\r
278 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
280 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
282 /* Now the queue should be full, and when we read the data out we
\r
283 should receive 0, 1, 2, 3, 4. */
\r
284 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
286 xErrorDetected = pdTRUE;
\r
289 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
291 xErrorDetected = pdTRUE;
\r
294 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
296 xErrorDetected = pdTRUE;
\r
299 #if configUSE_PREEMPTION == 0
\r
303 /* Check the data we read out is in the expected order. */
\r
304 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
306 /* Try peeking the data first. */
\r
307 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
309 xErrorDetected = pdTRUE;
\r
312 if( ulData != ulData2 )
\r
314 xErrorDetected = pdTRUE;
\r
318 /* Now try receiving the data for real. The value should be the
\r
319 same. Clobber the value first so we know we really received it. */
\r
320 ulData2 = ~ulData2;
\r
321 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
323 xErrorDetected = pdTRUE;
\r
326 if( ulData != ulData2 )
\r
328 xErrorDetected = pdTRUE;
\r
332 /* The queue should now be empty again. */
\r
333 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
335 xErrorDetected = pdTRUE;
\r
338 #if configUSE_PREEMPTION == 0
\r
343 /* Our queue is empty once more, add 10, 11 to the back. */
\r
345 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
347 xErrorDetected = pdTRUE;
\r
350 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
352 xErrorDetected = pdTRUE;
\r
355 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
357 xErrorDetected = pdTRUE;
\r
360 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
362 for( ulData = 9; ulData >= 7; ulData-- )
\r
364 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
366 xErrorDetected = pdTRUE;
\r
370 /* Now check that the queue is full, and that receiving data provides
\r
371 the expected sequence of 7, 8, 9, 10, 11. */
\r
372 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
374 xErrorDetected = pdTRUE;
\r
377 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
379 xErrorDetected = pdTRUE;
\r
382 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
384 xErrorDetected = pdTRUE;
\r
387 #if configUSE_PREEMPTION == 0
\r
391 /* Check the data we read out is in the expected order. */
\r
392 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
394 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
396 xErrorDetected = pdTRUE;
\r
399 if( ulData != ulData2 )
\r
401 xErrorDetected = pdTRUE;
\r
405 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
407 xErrorDetected = pdTRUE;
\r
413 /*-----------------------------------------------------------*/
\r
415 static void prvLowPriorityMutexTask( void *pvParameters )
\r
417 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
420 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
422 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
424 /* Queue a message for printing to say the task has started. */
\r
425 vPrintDisplayMessage( &pcTaskStartMsg );
\r
428 ( void ) pvParameters;
\r
433 /* Take the mutex. It should be available now. */
\r
434 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
436 xErrorDetected = pdTRUE;
\r
439 /* Set our guarded variable to a known start value. */
\r
440 ulGuardedVariable = 0;
\r
442 /* Our 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 /* We should now have inherited the prioritoy of the high priority task,
\r
454 as by now it will have attempted to get the mutex. */
\r
455 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
457 xErrorDetected = pdTRUE;
\r
460 /* We can attempt to set our priority to the test priority - between the
\r
461 idle priority and the medium/high test priorities, but our actual
\r
462 prioroity should remain at the high priority. */
\r
463 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
464 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
466 xErrorDetected = pdTRUE;
\r
469 /* Now unsuspend the medium priority task. This should not run as our
\r
470 inherited priority is above that of the medium priority task. */
\r
471 vTaskResume( xMediumPriorityMutexTask );
\r
473 /* If the did run then it will have incremented our guarded variable. */
\r
474 if( ulGuardedVariable != 0 )
\r
476 xErrorDetected = pdTRUE;
\r
479 /* When we give back the semaphore our priority should be disinherited
\r
480 back to the priority to which we attempted to set ourselves. This means
\r
481 that when the high priority task next blocks, the medium priority task
\r
482 should execute and increment the guarded variable. When we next run
\r
483 both the high and medium priority tasks will have been suspended again. */
\r
484 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
486 xErrorDetected = pdTRUE;
\r
489 /* Check that the guarded variable did indeed increment... */
\r
490 if( ulGuardedVariable != 1 )
\r
492 xErrorDetected = pdTRUE;
\r
495 /* ... and that our priority has been disinherited to
\r
496 genqMUTEX_TEST_PRIORITY. */
\r
497 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
499 xErrorDetected = pdTRUE;
\r
502 /* Set our priority back to our original priority ready for the next
\r
503 loop around this test. */
\r
504 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
506 /* Just to show we are still running. */
\r
509 #if configUSE_PREEMPTION == 0
\r
514 /*-----------------------------------------------------------*/
\r
516 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
518 ( void ) pvParameters;
\r
522 /* The medium priority task starts by suspending itself. The low
\r
523 priority task will unsuspend this task when required. */
\r
524 vTaskSuspend( NULL );
\r
526 /* When this task unsuspends all it does is increment the guarded
\r
527 variable, this is so the low priority task knows that it has
\r
529 ulGuardedVariable++;
\r
532 /*-----------------------------------------------------------*/
\r
534 static void prvHighPriorityMutexTask( void *pvParameters )
\r
536 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
538 ( void ) pvParameters;
\r
542 /* The high priority task starts by suspending itself. The low
\r
543 priority task will unsuspend this task when required. */
\r
544 vTaskSuspend( NULL );
\r
546 /* When this task unsuspends all it does is attempt to obtain
\r
547 the mutex. It should find the mutex is not available so a
\r
548 block time is specified. */
\r
549 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
551 xErrorDetected = pdTRUE;
\r
554 /* When we eventually obtain the mutex we just give it back then
\r
555 return to suspend ready for the next test. */
\r
556 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
558 xErrorDetected = pdTRUE;
\r
562 /*-----------------------------------------------------------*/
\r
564 /* This is called to check that all the created tasks are still running. */
\r
565 portBASE_TYPE xAreAltGenericQueueTasksStillRunning( void )
\r
567 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
569 /* If the demo task is still running then we expect the loopcounters to
\r
570 have incremented since this function was last called. */
\r
571 if( ulLastLoopCounter == ulLoopCounter )
\r
573 xErrorDetected = pdTRUE;
\r
576 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
578 xErrorDetected = pdTRUE;
\r
581 ulLastLoopCounter = ulLoopCounter;
\r
582 ulLastLoopCounter2 = ulLoopCounter2;
\r
584 /* Errors detected in the task itself will have latched xErrorDetected
\r
587 return !xErrorDetected;
\r