2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
77 * This file implements the same demo and test as GenQTest.c, but uses the
\r
78 * light weight API in place of the fully featured API.
\r
80 * See the comments at the top of GenQTest.c for a description.
\r
86 /* Scheduler include files. */
\r
87 #include "FreeRTOS.h"
\r
92 /* Demo program include files. */
\r
93 #include "AltQTest.h"
\r
95 #define genqQUEUE_LENGTH ( 5 )
\r
96 #define genqNO_BLOCK ( 0 )
\r
98 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
99 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
100 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
101 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
103 /*-----------------------------------------------------------*/
\r
106 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
107 * macros by using both to fill a queue, then reading from the queue to
\r
108 * check the resultant queue order is as expected. Queue data is also
\r
111 static void prvSendFrontAndBackTest( void *pvParameters );
\r
114 * The following three tasks are used to demonstrate the mutex behaviour.
\r
115 * Each task is given a different priority to demonstrate the priority
\r
116 * inheritance mechanism.
\r
118 * The low priority task obtains a mutex. After this a high priority task
\r
119 * attempts to obtain the same mutex, causing its priority to be inherited
\r
120 * by the low priority task. The task with the inherited high priority then
\r
121 * resumes a medium priority task to ensure it is not blocked by the medium
\r
122 * priority task while it holds the inherited high priority. Once the mutex
\r
123 * is returned the task with the inherited priority returns to its original
\r
124 * low priority, and is therefore immediately preempted by first the high
\r
125 * priority task and then the medium prioroity task before it can continue.
\r
127 static void prvLowPriorityMutexTask( void *pvParameters );
\r
128 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
129 static void prvHighPriorityMutexTask( void *pvParameters );
\r
131 /*-----------------------------------------------------------*/
\r
133 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
134 detected in any of the tasks. */
\r
135 static portBASE_TYPE xErrorDetected = pdFALSE;
\r
137 /* Counters that are incremented on each cycle of a test. This is used to
\r
138 detect a stalled task - a test that is no longer running. */
\r
139 static volatile unsigned portLONG ulLoopCounter = 0;
\r
140 static volatile unsigned portLONG ulLoopCounter2 = 0;
\r
142 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
143 static volatile unsigned portLONG ulGuardedVariable = 0;
\r
145 /* Handles used in the mutext test to suspend and resume the high and medium
\r
146 priority mutex test tasks. */
\r
147 static xTaskHandle xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
149 /*-----------------------------------------------------------*/
\r
151 void vStartAltGenericQueueTasks( unsigned portBASE_TYPE uxPriority )
\r
153 xQueueHandle xQueue;
\r
154 xSemaphoreHandle xMutex;
\r
156 /* Create the queue that we are going to use for the
\r
157 prvSendFrontAndBackTest demo. */
\r
158 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( unsigned portLONG ) );
\r
160 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
161 in use. The queue registry is provided as a means for kernel aware
\r
162 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
163 is not being used. The call to vQueueAddToRegistry() will be removed
\r
164 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
165 defined to be less than 1. */
\r
166 vQueueAddToRegistry( xQueue, ( signed portCHAR * ) "Alt_Gen_Test_Queue" );
\r
168 /* Create the demo task and pass it the queue just created. We are
\r
169 passing the queue handle by value so it does not matter that it is
\r
170 declared on the stack here. */
\r
171 xTaskCreate( prvSendFrontAndBackTest, ( signed portCHAR * ) "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
173 /* Create the mutex used by the prvMutexTest task. */
\r
174 xMutex = xSemaphoreCreateMutex();
\r
176 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
177 in use. The registry is provided as a means for kernel aware
\r
178 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
179 is not being used. The call to vQueueAddToRegistry() will be removed
\r
180 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
181 defined to be less than 1. */
\r
182 vQueueAddToRegistry( ( xQueueHandle ) xMutex, ( signed portCHAR * ) "Alt_Q_Mutex" );
\r
184 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
185 passing the mutex handle by value so it does not matter that it is declared
\r
186 on the stack here. */
\r
187 xTaskCreate( prvLowPriorityMutexTask, ( signed portCHAR * ) "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
188 xTaskCreate( prvMediumPriorityMutexTask, ( signed portCHAR * ) "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
189 xTaskCreate( prvHighPriorityMutexTask, ( signed portCHAR * ) "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
191 /*-----------------------------------------------------------*/
\r
193 static void prvSendFrontAndBackTest( void *pvParameters )
\r
195 unsigned portLONG ulData, ulData2;
\r
196 xQueueHandle xQueue;
\r
199 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
201 const portCHAR * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
203 /* Queue a message for printing to say the task has started. */
\r
204 vPrintDisplayMessage( &pcTaskStartMsg );
\r
207 xQueue = ( xQueueHandle ) pvParameters;
\r
211 /* The queue is empty, so sending an item to the back of the queue
\r
212 should have the same efect as sending it to the front of the queue.
\r
214 First send to the front and check everything is as expected. */
\r
215 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
217 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
219 xErrorDetected = pdTRUE;
\r
222 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
224 xErrorDetected = pdTRUE;
\r
227 /* The data we sent to the queue should equal the data we just received
\r
229 if( ulLoopCounter != ulData )
\r
231 xErrorDetected = pdTRUE;
\r
234 /* Then do the same, sending the data to the back, checking everything
\r
236 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
238 xErrorDetected = pdTRUE;
\r
241 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
243 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
245 xErrorDetected = pdTRUE;
\r
248 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
250 xErrorDetected = pdTRUE;
\r
253 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
255 xErrorDetected = pdTRUE;
\r
258 /* The data we sent to the queue should equal the data we just received
\r
260 if( ulLoopCounter != ulData )
\r
262 xErrorDetected = pdTRUE;
\r
265 #if configUSE_PREEMPTION == 0
\r
271 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
272 for( ulData = 2; ulData < 5; ulData++ )
\r
274 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
277 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
278 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
279 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
281 xErrorDetected = pdTRUE;
\r
284 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
286 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
288 /* Now the queue should be full, and when we read the data out we
\r
289 should receive 0, 1, 2, 3, 4. */
\r
290 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
292 xErrorDetected = pdTRUE;
\r
295 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
297 xErrorDetected = pdTRUE;
\r
300 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
302 xErrorDetected = pdTRUE;
\r
305 #if configUSE_PREEMPTION == 0
\r
309 /* Check the data we read out is in the expected order. */
\r
310 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
312 /* Try peeking the data first. */
\r
313 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
315 xErrorDetected = pdTRUE;
\r
318 if( ulData != ulData2 )
\r
320 xErrorDetected = pdTRUE;
\r
324 /* Now try receiving the data for real. The value should be the
\r
325 same. Clobber the value first so we know we really received it. */
\r
326 ulData2 = ~ulData2;
\r
327 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
329 xErrorDetected = pdTRUE;
\r
332 if( ulData != ulData2 )
\r
334 xErrorDetected = pdTRUE;
\r
338 /* The queue should now be empty again. */
\r
339 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
341 xErrorDetected = pdTRUE;
\r
344 #if configUSE_PREEMPTION == 0
\r
349 /* Our queue is empty once more, add 10, 11 to the back. */
\r
351 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
353 xErrorDetected = pdTRUE;
\r
356 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
358 xErrorDetected = pdTRUE;
\r
361 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
363 xErrorDetected = pdTRUE;
\r
366 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
368 for( ulData = 9; ulData >= 7; ulData-- )
\r
370 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
372 xErrorDetected = pdTRUE;
\r
376 /* Now check that the queue is full, and that receiving data provides
\r
377 the expected sequence of 7, 8, 9, 10, 11. */
\r
378 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
380 xErrorDetected = pdTRUE;
\r
383 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
385 xErrorDetected = pdTRUE;
\r
388 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
390 xErrorDetected = pdTRUE;
\r
393 #if configUSE_PREEMPTION == 0
\r
397 /* Check the data we read out is in the expected order. */
\r
398 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
400 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
402 xErrorDetected = pdTRUE;
\r
405 if( ulData != ulData2 )
\r
407 xErrorDetected = pdTRUE;
\r
411 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
413 xErrorDetected = pdTRUE;
\r
419 /*-----------------------------------------------------------*/
\r
421 static void prvLowPriorityMutexTask( void *pvParameters )
\r
423 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
426 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend );
\r
428 const portCHAR * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
430 /* Queue a message for printing to say the task has started. */
\r
431 vPrintDisplayMessage( &pcTaskStartMsg );
\r
434 ( void ) pvParameters;
\r
439 /* Take the mutex. It should be available now. */
\r
440 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
442 xErrorDetected = pdTRUE;
\r
445 /* Set our guarded variable to a known start value. */
\r
446 ulGuardedVariable = 0;
\r
448 /* Our priority should be as per that assigned when the task was
\r
450 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
452 xErrorDetected = pdTRUE;
\r
455 /* Now unsuspend the high priority task. This will attempt to take the
\r
456 mutex, and block when it finds it cannot obtain it. */
\r
457 vTaskResume( xHighPriorityMutexTask );
\r
459 /* We should now have inherited the prioritoy of the high priority task,
\r
460 as by now it will have attempted to get the mutex. */
\r
461 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
463 xErrorDetected = pdTRUE;
\r
466 /* We can attempt to set our priority to the test priority - between the
\r
467 idle priority and the medium/high test priorities, but our actual
\r
468 prioroity should remain at the high priority. */
\r
469 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
470 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
472 xErrorDetected = pdTRUE;
\r
475 /* Now unsuspend the medium priority task. This should not run as our
\r
476 inherited priority is above that of the medium priority task. */
\r
477 vTaskResume( xMediumPriorityMutexTask );
\r
479 /* If the did run then it will have incremented our guarded variable. */
\r
480 if( ulGuardedVariable != 0 )
\r
482 xErrorDetected = pdTRUE;
\r
485 /* When we give back the semaphore our priority should be disinherited
\r
486 back to the priority to which we attempted to set ourselves. This means
\r
487 that when the high priority task next blocks, the medium priority task
\r
488 should execute and increment the guarded variable. When we next run
\r
489 both the high and medium priority tasks will have been suspended again. */
\r
490 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
492 xErrorDetected = pdTRUE;
\r
495 /* Check that the guarded variable did indeed increment... */
\r
496 if( ulGuardedVariable != 1 )
\r
498 xErrorDetected = pdTRUE;
\r
501 /* ... and that our priority has been disinherited to
\r
502 genqMUTEX_TEST_PRIORITY. */
\r
503 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
505 xErrorDetected = pdTRUE;
\r
508 /* Set our priority back to our original priority ready for the next
\r
509 loop around this test. */
\r
510 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
512 /* Just to show we are still running. */
\r
515 #if configUSE_PREEMPTION == 0
\r
520 /*-----------------------------------------------------------*/
\r
522 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
524 ( void ) pvParameters;
\r
528 /* The medium priority task starts by suspending itself. The low
\r
529 priority task will unsuspend this task when required. */
\r
530 vTaskSuspend( NULL );
\r
532 /* When this task unsuspends all it does is increment the guarded
\r
533 variable, this is so the low priority task knows that it has
\r
535 ulGuardedVariable++;
\r
538 /*-----------------------------------------------------------*/
\r
540 static void prvHighPriorityMutexTask( void *pvParameters )
\r
542 xSemaphoreHandle xMutex = ( xSemaphoreHandle ) pvParameters;
\r
544 ( void ) pvParameters;
\r
548 /* The high priority task starts by suspending itself. The low
\r
549 priority task will unsuspend this task when required. */
\r
550 vTaskSuspend( NULL );
\r
552 /* When this task unsuspends all it does is attempt to obtain
\r
553 the mutex. It should find the mutex is not available so a
\r
554 block time is specified. */
\r
555 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
557 xErrorDetected = pdTRUE;
\r
560 /* When we eventually obtain the mutex we just give it back then
\r
561 return to suspend ready for the next test. */
\r
562 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
564 xErrorDetected = pdTRUE;
\r
568 /*-----------------------------------------------------------*/
\r
570 /* This is called to check that all the created tasks are still running. */
\r
571 portBASE_TYPE xAreAltGenericQueueTasksStillRunning( void )
\r
573 static unsigned portLONG ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
575 /* If the demo task is still running then we expect the loopcounters to
\r
576 have incremented since this function was last called. */
\r
577 if( ulLastLoopCounter == ulLoopCounter )
\r
579 xErrorDetected = pdTRUE;
\r
582 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
584 xErrorDetected = pdTRUE;
\r
587 ulLastLoopCounter = ulLoopCounter;
\r
588 ulLastLoopCounter2 = ulLoopCounter2;
\r
590 /* Errors detected in the task itself will have latched xErrorDetected
\r
593 return !xErrorDetected;
\r