2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
98 * This file implements the same demo and test as GenQTest.c, but uses the
\r
99 * light weight API in place of the fully featured API.
\r
101 * See the comments at the top of GenQTest.c for a description.
\r
105 #include <stdlib.h>
\r
107 /* Scheduler include files. */
\r
108 #include "FreeRTOS.h"
\r
111 #include "semphr.h"
\r
113 /* Demo program include files. */
\r
114 #include "AltQTest.h"
\r
116 #define genqQUEUE_LENGTH ( 5 )
\r
117 #define genqNO_BLOCK ( 0 )
\r
119 #define genqMUTEX_LOW_PRIORITY ( tskIDLE_PRIORITY )
\r
120 #define genqMUTEX_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
121 #define genqMUTEX_MEDIUM_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
122 #define genqMUTEX_HIGH_PRIORITY ( tskIDLE_PRIORITY + 3 )
\r
124 /*-----------------------------------------------------------*/
\r
127 * Tests the behaviour of the xQueueAltSendToFront() and xQueueAltSendToBack()
\r
128 * macros by using both to fill a queue, then reading from the queue to
\r
129 * check the resultant queue order is as expected. Queue data is also
\r
132 static void prvSendFrontAndBackTest( void *pvParameters );
\r
135 * The following three tasks are used to demonstrate the mutex behaviour.
\r
136 * Each task is given a different priority to demonstrate the priority
\r
137 * inheritance mechanism.
\r
139 * The low priority task obtains a mutex. After this a high priority task
\r
140 * attempts to obtain the same mutex, causing its priority to be inherited
\r
141 * by the low priority task. The task with the inherited high priority then
\r
142 * resumes a medium priority task to ensure it is not blocked by the medium
\r
143 * priority task while it holds the inherited high priority. Once the mutex
\r
144 * is returned the task with the inherited priority returns to its original
\r
145 * low priority, and is therefore immediately preempted by first the high
\r
146 * priority task and then the medium prioroity task before it can continue.
\r
148 static void prvLowPriorityMutexTask( void *pvParameters );
\r
149 static void prvMediumPriorityMutexTask( void *pvParameters );
\r
150 static void prvHighPriorityMutexTask( void *pvParameters );
\r
152 /*-----------------------------------------------------------*/
\r
154 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
155 detected in any of the tasks. */
\r
156 static BaseType_t xErrorDetected = pdFALSE;
\r
158 /* Counters that are incremented on each cycle of a test. This is used to
\r
159 detect a stalled task - a test that is no longer running. */
\r
160 static volatile uint32_t ulLoopCounter = 0;
\r
161 static volatile uint32_t ulLoopCounter2 = 0;
\r
163 /* The variable that is guarded by the mutex in the mutex demo tasks. */
\r
164 static volatile uint32_t ulGuardedVariable = 0;
\r
166 /* Handles used in the mutext test to suspend and resume the high and medium
\r
167 priority mutex test tasks. */
\r
168 static TaskHandle_t xHighPriorityMutexTask, xMediumPriorityMutexTask;
\r
170 /*-----------------------------------------------------------*/
\r
172 void vStartAltGenericQueueTasks( UBaseType_t uxPriority )
\r
174 QueueHandle_t xQueue;
\r
175 SemaphoreHandle_t xMutex;
\r
177 /* Create the queue that we are going to use for the
\r
178 prvSendFrontAndBackTest demo. */
\r
179 xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( uint32_t ) );
\r
181 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is
\r
182 in use. The queue registry is provided as a means for kernel aware
\r
183 debuggers to locate queues and has no purpose if a kernel aware debugger
\r
184 is not being used. The call to vQueueAddToRegistry() will be removed
\r
185 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
186 defined to be less than 1. */
\r
187 vQueueAddToRegistry( xQueue, "Alt_Gen_Test_Queue" );
\r
189 /* Create the demo task and pass it the queue just created. We are
\r
190 passing the queue handle by value so it does not matter that it is
\r
191 declared on the stack here. */
\r
192 xTaskCreate( prvSendFrontAndBackTest, "FGenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );
\r
194 /* Create the mutex used by the prvMutexTest task. */
\r
195 xMutex = xSemaphoreCreateMutex();
\r
197 /* vQueueAddToRegistry() adds the mutex to the registry, if one is
\r
198 in use. The registry is provided as a means for kernel aware
\r
199 debuggers to locate mutex and has no purpose if a kernel aware debugger
\r
200 is not being used. The call to vQueueAddToRegistry() will be removed
\r
201 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is
\r
202 defined to be less than 1. */
\r
203 vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Alt_Q_Mutex" );
\r
205 /* Create the mutex demo tasks and pass it the mutex just created. We are
\r
206 passing the mutex handle by value so it does not matter that it is declared
\r
207 on the stack here. */
\r
208 xTaskCreate( prvLowPriorityMutexTask, "FMuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );
\r
209 xTaskCreate( prvMediumPriorityMutexTask, "FMuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );
\r
210 xTaskCreate( prvHighPriorityMutexTask, "FMuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );
\r
212 /*-----------------------------------------------------------*/
\r
214 static void prvSendFrontAndBackTest( void *pvParameters )
\r
216 uint32_t ulData, ulData2;
\r
217 QueueHandle_t xQueue;
\r
220 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
222 const char * const pcTaskStartMsg = "Alt queue SendToFront/SendToBack/Peek test started.\r\n";
\r
224 /* Queue a message for printing to say the task has started. */
\r
225 vPrintDisplayMessage( &pcTaskStartMsg );
\r
228 xQueue = ( QueueHandle_t ) pvParameters;
\r
232 /* The queue is empty, so sending an item to the back of the queue
\r
233 should have the same efect as sending it to the front of the queue.
\r
235 First send to the front and check everything is as expected. */
\r
236 xQueueAltSendToFront( 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 /* 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 /* Then do the same, sending the data to the back, checking everything
\r
257 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
259 xErrorDetected = pdTRUE;
\r
262 xQueueAltSendToBack( xQueue, ( void * ) &ulLoopCounter, genqNO_BLOCK );
\r
264 if( uxQueueMessagesWaiting( xQueue ) != 1 )
\r
266 xErrorDetected = pdTRUE;
\r
269 if( xQueueAltReceive( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
271 xErrorDetected = pdTRUE;
\r
274 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
276 xErrorDetected = pdTRUE;
\r
279 /* The data we sent to the queue should equal the data we just received
\r
281 if( ulLoopCounter != ulData )
\r
283 xErrorDetected = pdTRUE;
\r
286 #if configUSE_PREEMPTION == 0
\r
292 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */
\r
293 for( ulData = 2; ulData < 5; ulData++ )
\r
295 xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
298 /* Now the order in the queue should be 2, 3, 4, with 2 being the first
\r
299 thing to be read out. Now add 1 then 0 to the front of the queue. */
\r
300 if( uxQueueMessagesWaiting( xQueue ) != 3 )
\r
302 xErrorDetected = pdTRUE;
\r
305 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
307 xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK );
\r
309 /* Now the queue should be full, and when we read the data out we
\r
310 should receive 0, 1, 2, 3, 4. */
\r
311 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
313 xErrorDetected = pdTRUE;
\r
316 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
318 xErrorDetected = pdTRUE;
\r
321 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
323 xErrorDetected = pdTRUE;
\r
326 #if configUSE_PREEMPTION == 0
\r
330 /* Check the data we read out is in the expected order. */
\r
331 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )
\r
333 /* Try peeking the data first. */
\r
334 if( xQueueAltPeek( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
336 xErrorDetected = pdTRUE;
\r
339 if( ulData != ulData2 )
\r
341 xErrorDetected = pdTRUE;
\r
345 /* Now try receiving the data for real. The value should be the
\r
346 same. Clobber the value first so we know we really received it. */
\r
347 ulData2 = ~ulData2;
\r
348 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
350 xErrorDetected = pdTRUE;
\r
353 if( ulData != ulData2 )
\r
355 xErrorDetected = pdTRUE;
\r
359 /* The queue should now be empty again. */
\r
360 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
362 xErrorDetected = pdTRUE;
\r
365 #if configUSE_PREEMPTION == 0
\r
370 /* Our queue is empty once more, add 10, 11 to the back. */
\r
372 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
374 xErrorDetected = pdTRUE;
\r
377 if( xQueueAltSendToBack( xQueue, &ulData, genqNO_BLOCK ) != pdPASS )
\r
379 xErrorDetected = pdTRUE;
\r
382 if( uxQueueMessagesWaiting( xQueue ) != 2 )
\r
384 xErrorDetected = pdTRUE;
\r
387 /* Now we should have 10, 11 in the queue. Add 7, 8, 9 to the
\r
389 for( ulData = 9; ulData >= 7; ulData-- )
\r
391 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != pdPASS )
\r
393 xErrorDetected = pdTRUE;
\r
397 /* Now check that the queue is full, and that receiving data provides
\r
398 the expected sequence of 7, 8, 9, 10, 11. */
\r
399 if( uxQueueMessagesWaiting( xQueue ) != 5 )
\r
401 xErrorDetected = pdTRUE;
\r
404 if( xQueueAltSendToFront( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
406 xErrorDetected = pdTRUE;
\r
409 if( xQueueAltSendToBack( xQueue, ( void * ) &ulData, genqNO_BLOCK ) != errQUEUE_FULL )
\r
411 xErrorDetected = pdTRUE;
\r
414 #if configUSE_PREEMPTION == 0
\r
418 /* Check the data we read out is in the expected order. */
\r
419 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )
\r
421 if( xQueueAltReceive( xQueue, &ulData2, genqNO_BLOCK ) != pdPASS )
\r
423 xErrorDetected = pdTRUE;
\r
426 if( ulData != ulData2 )
\r
428 xErrorDetected = pdTRUE;
\r
432 if( uxQueueMessagesWaiting( xQueue ) != 0 )
\r
434 xErrorDetected = pdTRUE;
\r
440 /*-----------------------------------------------------------*/
\r
442 static void prvLowPriorityMutexTask( void *pvParameters )
\r
444 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;
\r
447 void vPrintDisplayMessage( const char * const * ppcMessageToSend );
\r
449 const char * const pcTaskStartMsg = "Fast mutex with priority inheritance test started.\r\n";
\r
451 /* Queue a message for printing to say the task has started. */
\r
452 vPrintDisplayMessage( &pcTaskStartMsg );
\r
455 ( void ) pvParameters;
\r
460 /* Take the mutex. It should be available now. */
\r
461 if( xSemaphoreAltTake( xMutex, genqNO_BLOCK ) != pdPASS )
\r
463 xErrorDetected = pdTRUE;
\r
466 /* Set our guarded variable to a known start value. */
\r
467 ulGuardedVariable = 0;
\r
469 /* Our priority should be as per that assigned when the task was
\r
471 if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )
\r
473 xErrorDetected = pdTRUE;
\r
476 /* Now unsuspend the high priority task. This will attempt to take the
\r
477 mutex, and block when it finds it cannot obtain it. */
\r
478 vTaskResume( xHighPriorityMutexTask );
\r
480 /* We should now have inherited the prioritoy of the high priority task,
\r
481 as by now it will have attempted to get the mutex. */
\r
482 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
484 xErrorDetected = pdTRUE;
\r
487 /* We can attempt to set our priority to the test priority - between the
\r
488 idle priority and the medium/high test priorities, but our actual
\r
489 prioroity should remain at the high priority. */
\r
490 vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );
\r
491 if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )
\r
493 xErrorDetected = pdTRUE;
\r
496 /* Now unsuspend the medium priority task. This should not run as our
\r
497 inherited priority is above that of the medium priority task. */
\r
498 vTaskResume( xMediumPriorityMutexTask );
\r
500 /* If the did run then it will have incremented our guarded variable. */
\r
501 if( ulGuardedVariable != 0 )
\r
503 xErrorDetected = pdTRUE;
\r
506 /* When we give back the semaphore our priority should be disinherited
\r
507 back to the priority to which we attempted to set ourselves. This means
\r
508 that when the high priority task next blocks, the medium priority task
\r
509 should execute and increment the guarded variable. When we next run
\r
510 both the high and medium priority tasks will have been suspended again. */
\r
511 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
513 xErrorDetected = pdTRUE;
\r
516 /* Check that the guarded variable did indeed increment... */
\r
517 if( ulGuardedVariable != 1 )
\r
519 xErrorDetected = pdTRUE;
\r
522 /* ... and that our priority has been disinherited to
\r
523 genqMUTEX_TEST_PRIORITY. */
\r
524 if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )
\r
526 xErrorDetected = pdTRUE;
\r
529 /* Set our priority back to our original priority ready for the next
\r
530 loop around this test. */
\r
531 vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );
\r
533 /* Just to show we are still running. */
\r
536 #if configUSE_PREEMPTION == 0
\r
541 /*-----------------------------------------------------------*/
\r
543 static void prvMediumPriorityMutexTask( void *pvParameters )
\r
545 ( void ) pvParameters;
\r
549 /* The medium priority task starts by suspending itself. The low
\r
550 priority task will unsuspend this task when required. */
\r
551 vTaskSuspend( NULL );
\r
553 /* When this task unsuspends all it does is increment the guarded
\r
554 variable, this is so the low priority task knows that it has
\r
556 ulGuardedVariable++;
\r
559 /*-----------------------------------------------------------*/
\r
561 static void prvHighPriorityMutexTask( void *pvParameters )
\r
563 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;
\r
565 ( void ) pvParameters;
\r
569 /* The high priority task starts by suspending itself. The low
\r
570 priority task will unsuspend this task when required. */
\r
571 vTaskSuspend( NULL );
\r
573 /* When this task unsuspends all it does is attempt to obtain
\r
574 the mutex. It should find the mutex is not available so a
\r
575 block time is specified. */
\r
576 if( xSemaphoreAltTake( xMutex, portMAX_DELAY ) != pdPASS )
\r
578 xErrorDetected = pdTRUE;
\r
581 /* When we eventually obtain the mutex we just give it back then
\r
582 return to suspend ready for the next test. */
\r
583 if( xSemaphoreAltGive( xMutex ) != pdPASS )
\r
585 xErrorDetected = pdTRUE;
\r
589 /*-----------------------------------------------------------*/
\r
591 /* This is called to check that all the created tasks are still running. */
\r
592 BaseType_t xAreAltGenericQueueTasksStillRunning( void )
\r
594 static uint32_t ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;
\r
596 /* If the demo task is still running then we expect the loopcounters to
\r
597 have incremented since this function was last called. */
\r
598 if( ulLastLoopCounter == ulLoopCounter )
\r
600 xErrorDetected = pdTRUE;
\r
603 if( ulLastLoopCounter2 == ulLoopCounter2 )
\r
605 xErrorDetected = pdTRUE;
\r
608 ulLastLoopCounter = ulLoopCounter;
\r
609 ulLastLoopCounter2 = ulLoopCounter2;
\r
611 /* Errors detected in the task itself will have latched xErrorDetected
\r
614 return !xErrorDetected;
\r