2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * This file implements the same demo and test as GenQTest.c, but uses the
\r
73 * light weight API in place of the fully featured API.
\r
75 * See the comments at the top of GenQTest.c for a description.
\r
81 /* Scheduler include files. */
\r
82 #include "FreeRTOS.h"
\r
87 /* Demo program include files. */
\r
88 #include "AltQTest.h"
\r
90 #define genqQUEUE_LENGTH ( 5 )
\r
91 #define genqNO_BLOCK ( 0 )
\r
93 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
94 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
95 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
96 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
98 /*-----------------------------------------------------------*/
\r
101 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
102 * macros by using both to fill a queue, then reading from the queue to
\r
103 * check the resultant queue order is as expected. Queue data is also
\r
106 static void prvSendFrontAndBackTest( void *pvParameters );
\r
109 * The following three tasks are used to demonstrate the mutex behaviour.
\r
110 * Each task is given a different priority to demonstrate the priority
\r
111 * inheritance mechanism.
\r
113 * The low priority task obtains a mutex. After this a high priority task
\r
114 * attempts to obtain the same mutex, causing its priority to be inherited
\r
115 * by the low priority task. The task with the inherited high priority then
\r
116 * resumes a medium priority task to ensure it is not blocked by the medium
\r
117 * priority task while it holds the inherited high priority. Once the mutex
\r
118 * is returned the task with the inherited priority returns to its original
\r
119 * low priority, and is therefore immediately preempted by first the high
\r
120 * priority task and then the medium prioroity task before it can continue.
\r
122 static void prvLowPriorityMutexTask( void *pvParameters );
\r
123 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
124 static void prvHighPriorityMutexTask( void *pvParameters );
\r
126 /*-----------------------------------------------------------*/
\r
128 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
129 detected in any of the tasks. */
\r
130 static BaseType_t xErrorDetected = pdFALSE;
\r
132 /* Counters that are incremented on each cycle of a test. This is used to
\r
133 detect a stalled task - a test that is no longer running. */
\r
134 static volatile uint32_t ulLoopCounter = 0;
\r
135 static volatile uint32_t ulLoopCounter2 = 0;
\r
137 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
138 static volatile uint32_t ulGuardedVariable = 0;
\r
140 /* Handles used in the mutext test to suspend and resume the high and medium
\r
141 priority mutex test tasks. */
\r
142 static TaskHandle_t xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
144 /*-----------------------------------------------------------*/
\r
146 void vStartAltGenericQueueTasks( UBaseType_t uxPriority )
\r
148 QueueHandle_t xQueue;
\r
149 SemaphoreHandle_t xMutex;
\r
151 /* Create the queue that we are going to use for the
\r
152 prvSendFrontAndBackTest demo. */
\r
153 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( uint32_t ) );
\r
155 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
156 in use. The queue registry is provided as a means for kernel aware
\r
157 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
158 is not being used. The call to vQueueAddToRegistry() will be removed
\r
159 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
160 defined to be less than 1. */
\r
161 vQueueAddToRegistry( xQueue, "Alt_Gen_Test_Queue" );
\r
163 /* Create the demo task and pass it the queue just created. We are
\r
164 passing the queue handle by value so it does not matter that it is
\r
165 declared on the stack here. */
\r
166 xTaskCreate( prvSendFrontAndBackTest, "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
168 /* Create the mutex used by the prvMutexTest task. */
\r
169 xMutex = xSemaphoreCreateMutex();
\r
171 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
172 in use. The registry is provided as a means for kernel aware
\r
173 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
174 is not being used. The call to vQueueAddToRegistry() will be removed
\r
175 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
176 defined to be less than 1. */
\r
177 vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Alt_Q_Mutex" );
\r
179 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
180 passing the mutex handle by value so it does not matter that it is declared
\r
181 on the stack here. */
\r
182 xTaskCreate( prvLowPriorityMutexTask, "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
183 xTaskCreate( prvMediumPriorityMutexTask, "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
184 xTaskCreate( prvHighPriorityMutexTask, "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
186 /*-----------------------------------------------------------*/
\r
188 static void prvSendFrontAndBackTest( void *pvParameters )
\r
190 uint32_t ulData, ulData2;
\r
191 QueueHandle_t xQueue;
\r
194 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
196 const char * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
198 /* Queue a message for printing to say the task has started. */
\r
199 vPrintDisplayMessage( &pcTaskStartMsg );
\r
202 xQueue = ( QueueHandle_t ) pvParameters;
\r
206 /* The queue is empty, so sending an item to the back of the queue
\r
207 should have the same efect as sending it to the front of the queue.
\r
209 First send to the front and check everything is as expected. */
\r
210 xQueueAltSendToFront( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
212 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
214 xErrorDetected = pdTRUE;
\r
217 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
219 xErrorDetected = pdTRUE;
\r
222 /* The data we sent to the queue should equal the data we just received
\r
224 if( ulLoopCounter != ulData )
\r
226 xErrorDetected = pdTRUE;
\r
229 /* Then do the same, sending the data to the back, checking everything
\r
231 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
233 xErrorDetected = pdTRUE;
\r
236 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
238 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
240 xErrorDetected = pdTRUE;
\r
243 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
245 xErrorDetected = pdTRUE;
\r
248 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
250 xErrorDetected = pdTRUE;
\r
253 /* The data we sent to the queue should equal the data we just received
\r
255 if( ulLoopCounter != ulData )
\r
257 xErrorDetected = pdTRUE;
\r
260 #if configUSE_PREEMPTION == 0
\r
266 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
267 for( ulData = 2; ulData < 5; ulData++ )
\r
269 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
272 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
273 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
274 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
276 xErrorDetected = pdTRUE;
\r
279 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
281 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
283 /* Now the queue should be full, and when we read the data out we
\r
284 should receive 0, 1, 2, 3, 4. */
\r
285 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
287 xErrorDetected = pdTRUE;
\r
290 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
292 xErrorDetected = pdTRUE;
\r
295 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
297 xErrorDetected = pdTRUE;
\r
300 #if configUSE_PREEMPTION == 0
\r
304 /* Check the data we read out is in the expected order. */
\r
305 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
307 /* Try peeking the data first. */
\r
308 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
310 xErrorDetected = pdTRUE;
\r
313 if( ulData != ulData2 )
\r
315 xErrorDetected = pdTRUE;
\r
319 /* Now try receiving the data for real. The value should be the
\r
320 same. Clobber the value first so we know we really received it. */
\r
321 ulData2 = ~ulData2;
\r
322 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
324 xErrorDetected = pdTRUE;
\r
327 if( ulData != ulData2 )
\r
329 xErrorDetected = pdTRUE;
\r
333 /* The queue should now be empty again. */
\r
334 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
336 xErrorDetected = pdTRUE;
\r
339 #if configUSE_PREEMPTION == 0
\r
344 /* Our queue is empty once more, add 10, 11 to the back. */
\r
346 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
348 xErrorDetected = pdTRUE;
\r
351 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
353 xErrorDetected = pdTRUE;
\r
356 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
358 xErrorDetected = pdTRUE;
\r
361 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
363 for( ulData = 9; ulData >= 7; ulData-- )
\r
365 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
367 xErrorDetected = pdTRUE;
\r
371 /* Now check that the queue is full, and that receiving data provides
\r
372 the expected sequence of 7, 8, 9, 10, 11. */
\r
373 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
375 xErrorDetected = pdTRUE;
\r
378 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
380 xErrorDetected = pdTRUE;
\r
383 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
385 xErrorDetected = pdTRUE;
\r
388 #if configUSE_PREEMPTION == 0
\r
392 /* Check the data we read out is in the expected order. */
\r
393 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
395 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
397 xErrorDetected = pdTRUE;
\r
400 if( ulData != ulData2 )
\r
402 xErrorDetected = pdTRUE;
\r
406 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
408 xErrorDetected = pdTRUE;
\r
414 /*-----------------------------------------------------------*/
\r
416 static void prvLowPriorityMutexTask( void *pvParameters )
\r
418 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;
\r
421 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
423 const char * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
425 /* Queue a message for printing to say the task has started. */
\r
426 vPrintDisplayMessage( &pcTaskStartMsg );
\r
429 ( void ) pvParameters;
\r
434 /* Take the mutex. It should be available now. */
\r
435 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
437 xErrorDetected = pdTRUE;
\r
440 /* Set our guarded variable to a known start value. */
\r
441 ulGuardedVariable = 0;
\r
443 /* Our priority should be as per that assigned when the task was
\r
445 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
447 xErrorDetected = pdTRUE;
\r
450 /* Now unsuspend the high priority task. This will attempt to take the
\r
451 mutex, and block when it finds it cannot obtain it. */
\r
452 vTaskResume( xHighPriorityMutexTask );
\r
454 /* We should now have inherited the prioritoy of the high priority task,
\r
455 as by now it will have attempted to get the mutex. */
\r
456 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
458 xErrorDetected = pdTRUE;
\r
461 /* We can attempt to set our priority to the test priority - between the
\r
462 idle priority and the medium/high test priorities, but our actual
\r
463 prioroity should remain at the high priority. */
\r
464 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
465 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
467 xErrorDetected = pdTRUE;
\r
470 /* Now unsuspend the medium priority task. This should not run as our
\r
471 inherited priority is above that of the medium priority task. */
\r
472 vTaskResume( xMediumPriorityMutexTask );
\r
474 /* If the did run then it will have incremented our guarded variable. */
\r
475 if( ulGuardedVariable != 0 )
\r
477 xErrorDetected = pdTRUE;
\r
480 /* When we give back the semaphore our priority should be disinherited
\r
481 back to the priority to which we attempted to set ourselves. This means
\r
482 that when the high priority task next blocks, the medium priority task
\r
483 should execute and increment the guarded variable. When we next run
\r
484 both the high and medium priority tasks will have been suspended again. */
\r
485 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
487 xErrorDetected = pdTRUE;
\r
490 /* Check that the guarded variable did indeed increment... */
\r
491 if( ulGuardedVariable != 1 )
\r
493 xErrorDetected = pdTRUE;
\r
496 /* ... and that our priority has been disinherited to
\r
497 genqMUTEX_TEST_PRIORITY. */
\r
498 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
500 xErrorDetected = pdTRUE;
\r
503 /* Set our priority back to our original priority ready for the next
\r
504 loop around this test. */
\r
505 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
507 /* Just to show we are still running. */
\r
510 #if configUSE_PREEMPTION == 0
\r
515 /*-----------------------------------------------------------*/
\r
517 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
519 ( void ) pvParameters;
\r
523 /* The medium priority task starts by suspending itself. The low
\r
524 priority task will unsuspend this task when required. */
\r
525 vTaskSuspend( NULL );
\r
527 /* When this task unsuspends all it does is increment the guarded
\r
528 variable, this is so the low priority task knows that it has
\r
530 ulGuardedVariable++;
\r
533 /*-----------------------------------------------------------*/
\r
535 static void prvHighPriorityMutexTask( void *pvParameters )
\r
537 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;
\r
539 ( void ) pvParameters;
\r
543 /* The high priority task starts by suspending itself. The low
\r
544 priority task will unsuspend this task when required. */
\r
545 vTaskSuspend( NULL );
\r
547 /* When this task unsuspends all it does is attempt to obtain
\r
548 the mutex. It should find the mutex is not available so a
\r
549 block time is specified. */
\r
550 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
552 xErrorDetected = pdTRUE;
\r
555 /* When we eventually obtain the mutex we just give it back then
\r
556 return to suspend ready for the next test. */
\r
557 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
559 xErrorDetected = pdTRUE;
\r
563 /*-----------------------------------------------------------*/
\r
565 /* This is called to check that all the created tasks are still running. */
\r
566 BaseType_t xAreAltGenericQueueTasksStillRunning( void )
\r
568 static uint32_t ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
570 /* If the demo task is still running then we expect the loopcounters to
\r
571 have incremented since this function was last called. */
\r
572 if( ulLastLoopCounter == ulLoopCounter )
\r
574 xErrorDetected = pdTRUE;
\r
577 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
579 xErrorDetected = pdTRUE;
\r
582 ulLastLoopCounter = ulLoopCounter;
\r
583 ulLastLoopCounter2 = ulLoopCounter2;
\r
585 /* Errors detected in the task itself will have latched xErrorDetected
\r
588 return !xErrorDetected;
\r