]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/GenQTest.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / Common / Minimal / GenQTest.c
1 /*\r
2     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \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
12 \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
19 \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
24 \r
25     ***************************************************************************\r
26      *                                                                       *\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
31      *                                                                       *\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
36      *                                                                       *\r
37     ***************************************************************************\r
38 \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
42 \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
46 \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
51 \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
55 \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
58 \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
62 \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
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /*\r
72  * Tests the extra queue functionality introduced in FreeRTOS.org V4.5.0 -\r
73  * including xQueueSendToFront(), xQueueSendToBack(), xQueuePeek() and\r
74  * mutex behaviour.\r
75  *\r
76  * See the comments above the prvSendFrontAndBackTest() and\r
77  * prvLowPriorityMutexTask() prototypes below for more information.\r
78  */\r
79 \r
80 /* Standard includes. */\r
81 #include <stdlib.h>\r
82 \r
83 /* Scheduler include files. */\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 #include "queue.h"\r
87 #include "semphr.h"\r
88 \r
89 /* Demo program include files. */\r
90 #include "GenQTest.h"\r
91 \r
92 #define genqQUEUE_LENGTH                ( 5 )\r
93 #define intsemNO_BLOCK                  ( 0 )\r
94 \r
95 #define genqMUTEX_LOW_PRIORITY          ( tskIDLE_PRIORITY )\r
96 #define genqMUTEX_TEST_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
97 #define genqMUTEX_MEDIUM_PRIORITY       ( tskIDLE_PRIORITY + 2 )\r
98 #define genqMUTEX_HIGH_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /*\r
103  * Tests the behaviour of the xQueueSendToFront() and xQueueSendToBack()\r
104  * macros by using both to fill a queue, then reading from the queue to\r
105  * check the resultant queue order is as expected.  Queue data is also\r
106  * peeked.\r
107  */\r
108 static void prvSendFrontAndBackTest( void *pvParameters );\r
109 \r
110 /*\r
111  * The following three tasks are used to demonstrate the mutex behaviour.\r
112  * Each task is given a different priority to demonstrate the priority\r
113  * inheritance mechanism.\r
114  *\r
115  * The low priority task obtains a mutex.  After this a high priority task\r
116  * attempts to obtain the same mutex, causing its priority to be inherited\r
117  * by the low priority task.  The task with the inherited high priority then\r
118  * resumes a medium priority task to ensure it is not blocked by the medium\r
119  * priority task while it holds the inherited high priority.  Once the mutex\r
120  * is returned the task with the inherited priority returns to its original\r
121  * low priority, and is therefore immediately preempted by first the high\r
122  * priority task and then the medium priority task before it can continue.\r
123  */\r
124 static void prvLowPriorityMutexTask( void *pvParameters );\r
125 static void prvMediumPriorityMutexTask( void *pvParameters );\r
126 static void prvHighPriorityMutexTask( void *pvParameters );\r
127 \r
128 /*-----------------------------------------------------------*/\r
129 \r
130 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
131 detected in any of the tasks. */\r
132 static volatile BaseType_t xErrorDetected = pdFALSE;\r
133 \r
134 /* Counters that are incremented on each cycle of a test.  This is used to\r
135 detect a stalled task - a test that is no longer running. */\r
136 static volatile uint32_t ulLoopCounter = 0;\r
137 static volatile uint32_t ulLoopCounter2 = 0;\r
138 \r
139 /* The variable that is guarded by the mutex in the mutex demo tasks. */\r
140 static volatile uint32_t ulGuardedVariable = 0;\r
141 \r
142 /* Handles used in the mutex test to suspend and resume the high and medium\r
143 priority mutex test tasks. */\r
144 static TaskHandle_t xHighPriorityMutexTask, xMediumPriorityMutexTask;\r
145 \r
146 /*-----------------------------------------------------------*/\r
147 \r
148 void vStartGenericQueueTasks( UBaseType_t uxPriority )\r
149 {\r
150 QueueHandle_t xQueue;\r
151 SemaphoreHandle_t xMutex;\r
152 \r
153         /* Create the queue that we are going to use for the\r
154         prvSendFrontAndBackTest demo. */\r
155         xQueue = xQueueCreate( genqQUEUE_LENGTH, sizeof( uint32_t ) );\r
156 \r
157         if( xQueue != NULL )\r
158         {\r
159                 /* vQueueAddToRegistry() adds the queue to the queue registry, if one\r
160                 is in use.  The queue registry is provided as a means for kernel aware\r
161                 debuggers to locate queues and has no purpose if a kernel aware debugger\r
162                 is not being used.  The call to vQueueAddToRegistry() will be removed\r
163                 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is\r
164                 defined to be less than 1. */\r
165                 vQueueAddToRegistry( xQueue, "Gen_Queue_Test" );\r
166 \r
167                 /* Create the demo task and pass it the queue just created.  We are\r
168                 passing the queue handle by value so it does not matter that it is\r
169                 declared on the stack here. */\r
170                 xTaskCreate( prvSendFrontAndBackTest, "GenQ", configMINIMAL_STACK_SIZE, ( void * ) xQueue, uxPriority, NULL );\r
171         }\r
172 \r
173         /* Create the mutex used by the prvMutexTest task. */\r
174         xMutex = xSemaphoreCreateMutex();\r
175 \r
176         if( xMutex != NULL )\r
177         {\r
178                 /* vQueueAddToRegistry() adds the mutex to the registry, if one is\r
179                 in use.  The registry is provided as a means for kernel aware\r
180                 debuggers to locate mutexes and has no purpose if a kernel aware\r
181                 debugger is not being used.  The call to vQueueAddToRegistry() will be\r
182                 removed by the pre-processor if configQUEUE_REGISTRY_SIZE is not\r
183                 defined or is defined to be less than 1. */\r
184                 vQueueAddToRegistry( ( QueueHandle_t ) xMutex, "Gen_Queue_Mutex" );\r
185 \r
186                 /* Create the mutex demo tasks and pass it the mutex just created.  We\r
187                 are passing the mutex handle by value so it does not matter that it is\r
188                 declared on the stack here. */\r
189                 xTaskCreate( prvLowPriorityMutexTask, "MuLow", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_LOW_PRIORITY, NULL );\r
190                 xTaskCreate( prvMediumPriorityMutexTask, "MuMed", configMINIMAL_STACK_SIZE, NULL, genqMUTEX_MEDIUM_PRIORITY, &xMediumPriorityMutexTask );\r
191                 xTaskCreate( prvHighPriorityMutexTask, "MuHigh", configMINIMAL_STACK_SIZE, ( void * ) xMutex, genqMUTEX_HIGH_PRIORITY, &xHighPriorityMutexTask );\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void prvSendFrontAndBackTest( void *pvParameters )\r
197 {\r
198 uint32_t ulData, ulData2;\r
199 QueueHandle_t xQueue;\r
200 \r
201         #ifdef USE_STDIO\r
202         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
203 \r
204                 const char * const pcTaskStartMsg = "Queue SendToFront/SendToBack/Peek test started.\r\n";\r
205 \r
206                 /* Queue a message for printing to say the task has started. */\r
207                 vPrintDisplayMessage( &pcTaskStartMsg );\r
208         #endif\r
209 \r
210         xQueue = ( QueueHandle_t ) pvParameters;\r
211 \r
212         for( ;; )\r
213         {\r
214                 /* The queue is empty, so sending an item to the back of the queue\r
215                 should have the same efect as sending it to the front of the queue.\r
216 \r
217                 First send to the front and check everything is as expected. */\r
218                 xQueueSendToFront( xQueue, ( void * ) &ulLoopCounter, intsemNO_BLOCK );\r
219 \r
220                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
221                 {\r
222                         xErrorDetected = pdTRUE;\r
223                 }\r
224 \r
225                 if( xQueueReceive( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != pdPASS )\r
226                 {\r
227                         xErrorDetected = pdTRUE;\r
228                 }\r
229 \r
230                 /* The data we sent to the queue should equal the data we just received\r
231                 from the queue. */\r
232                 if( ulLoopCounter != ulData )\r
233                 {\r
234                         xErrorDetected = pdTRUE;\r
235                 }\r
236 \r
237                 /* Then do the same, sending the data to the back, checking everything\r
238                 is as expected. */\r
239                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
240                 {\r
241                         xErrorDetected = pdTRUE;\r
242                 }\r
243 \r
244                 xQueueSendToBack( xQueue, ( void * ) &ulLoopCounter, intsemNO_BLOCK );\r
245 \r
246                 if( uxQueueMessagesWaiting( xQueue ) != 1 )\r
247                 {\r
248                         xErrorDetected = pdTRUE;\r
249                 }\r
250 \r
251                 if( xQueueReceive( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != pdPASS )\r
252                 {\r
253                         xErrorDetected = pdTRUE;\r
254                 }\r
255 \r
256                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
257                 {\r
258                         xErrorDetected = pdTRUE;\r
259                 }\r
260 \r
261                 /* The data we sent to the queue should equal the data we just received\r
262                 from the queue. */\r
263                 if( ulLoopCounter != ulData )\r
264                 {\r
265                         xErrorDetected = pdTRUE;\r
266                 }\r
267 \r
268                 #if configUSE_PREEMPTION == 0\r
269                         taskYIELD();\r
270                 #endif\r
271 \r
272 \r
273 \r
274                 /* Place 2, 3, 4 into the queue, adding items to the back of the queue. */\r
275                 for( ulData = 2; ulData < 5; ulData++ )\r
276                 {\r
277                         xQueueSendToBack( xQueue, ( void * ) &ulData, intsemNO_BLOCK );\r
278                 }\r
279 \r
280                 /* Now the order in the queue should be 2, 3, 4, with 2 being the first\r
281                 thing to be read out.  Now add 1 then 0 to the front of the queue. */\r
282                 if( uxQueueMessagesWaiting( xQueue ) != 3 )\r
283                 {\r
284                         xErrorDetected = pdTRUE;\r
285                 }\r
286                 ulData = 1;\r
287                 xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK );\r
288                 ulData = 0;\r
289                 xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK );\r
290 \r
291                 /* Now the queue should be full, and when we read the data out we\r
292                 should receive 0, 1, 2, 3, 4. */\r
293                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
294                 {\r
295                         xErrorDetected = pdTRUE;\r
296                 }\r
297 \r
298                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
299                 {\r
300                         xErrorDetected = pdTRUE;\r
301                 }\r
302 \r
303                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
304                 {\r
305                         xErrorDetected = pdTRUE;\r
306                 }\r
307 \r
308                 #if configUSE_PREEMPTION == 0\r
309                         taskYIELD();\r
310                 #endif\r
311 \r
312                 /* Check the data we read out is in the expected order. */\r
313                 for( ulData = 0; ulData < genqQUEUE_LENGTH; ulData++ )\r
314                 {\r
315                         /* Try peeking the data first. */\r
316                         if( xQueuePeek( xQueue, &ulData2, intsemNO_BLOCK ) != pdPASS )\r
317                         {\r
318                                 xErrorDetected = pdTRUE;\r
319                         }\r
320 \r
321                         if( ulData != ulData2 )\r
322                         {\r
323                                 xErrorDetected = pdTRUE;\r
324                         }\r
325 \r
326 \r
327                         /* Now try receiving the data for real.  The value should be the\r
328                         same.  Clobber the value first so we know we really received it. */\r
329                         ulData2 = ~ulData2;\r
330                         if( xQueueReceive( xQueue, &ulData2, intsemNO_BLOCK ) != pdPASS )\r
331                         {\r
332                                 xErrorDetected = pdTRUE;\r
333                         }\r
334 \r
335                         if( ulData != ulData2 )\r
336                         {\r
337                                 xErrorDetected = pdTRUE;\r
338                         }\r
339                 }\r
340 \r
341                 /* The queue should now be empty again. */\r
342                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
343                 {\r
344                         xErrorDetected = pdTRUE;\r
345                 }\r
346 \r
347                 #if configUSE_PREEMPTION == 0\r
348                         taskYIELD();\r
349                 #endif\r
350 \r
351 \r
352                 /* Our queue is empty once more, add 10, 11 to the back. */\r
353                 ulData = 10;\r
354                 if( xQueueSend( xQueue, &ulData, intsemNO_BLOCK ) != pdPASS )\r
355                 {\r
356                         xErrorDetected = pdTRUE;\r
357                 }\r
358                 ulData = 11;\r
359                 if( xQueueSend( xQueue, &ulData, intsemNO_BLOCK ) != pdPASS )\r
360                 {\r
361                         xErrorDetected = pdTRUE;\r
362                 }\r
363 \r
364                 if( uxQueueMessagesWaiting( xQueue ) != 2 )\r
365                 {\r
366                         xErrorDetected = pdTRUE;\r
367                 }\r
368 \r
369                 /* Now we should have 10, 11 in the queue.  Add 7, 8, 9 to the\r
370                 front. */\r
371                 for( ulData = 9; ulData >= 7; ulData-- )\r
372                 {\r
373                         if( xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != pdPASS )\r
374                         {\r
375                                 xErrorDetected = pdTRUE;\r
376                         }\r
377                 }\r
378 \r
379                 /* Now check that the queue is full, and that receiving data provides\r
380                 the expected sequence of 7, 8, 9, 10, 11. */\r
381                 if( uxQueueMessagesWaiting( xQueue ) != 5 )\r
382                 {\r
383                         xErrorDetected = pdTRUE;\r
384                 }\r
385 \r
386                 if( xQueueSendToFront( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
387                 {\r
388                         xErrorDetected = pdTRUE;\r
389                 }\r
390 \r
391                 if( xQueueSendToBack( xQueue, ( void * ) &ulData, intsemNO_BLOCK ) != errQUEUE_FULL )\r
392                 {\r
393                         xErrorDetected = pdTRUE;\r
394                 }\r
395 \r
396                 #if configUSE_PREEMPTION == 0\r
397                         taskYIELD();\r
398                 #endif\r
399 \r
400                 /* Check the data we read out is in the expected order. */\r
401                 for( ulData = 7; ulData < ( 7 + genqQUEUE_LENGTH ); ulData++ )\r
402                 {\r
403                         if( xQueueReceive( xQueue, &ulData2, intsemNO_BLOCK ) != pdPASS )\r
404                         {\r
405                                 xErrorDetected = pdTRUE;\r
406                         }\r
407 \r
408                         if( ulData != ulData2 )\r
409                         {\r
410                                 xErrorDetected = pdTRUE;\r
411                         }\r
412                 }\r
413 \r
414                 if( uxQueueMessagesWaiting( xQueue ) != 0 )\r
415                 {\r
416                         xErrorDetected = pdTRUE;\r
417                 }\r
418 \r
419                 ulLoopCounter++;\r
420         }\r
421 }\r
422 /*-----------------------------------------------------------*/\r
423 \r
424 static void prvTakeTwoMutexesReturnInDifferentOrder( SemaphoreHandle_t xMutex, SemaphoreHandle_t xLocalMutex )\r
425 {\r
426         /* Take the mutex.  It should be available now. */\r
427         if( xSemaphoreTake( xMutex, intsemNO_BLOCK ) != pdPASS )\r
428         {\r
429                 xErrorDetected = pdTRUE;\r
430         }\r
431 \r
432         /* Set the guarded variable to a known start value. */\r
433         ulGuardedVariable = 0;\r
434 \r
435         /* This task's priority should be as per that assigned when the task was\r
436         created. */\r
437         if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
438         {\r
439                 xErrorDetected = pdTRUE;\r
440         }\r
441 \r
442         /* Now unsuspend the high priority task.  This will attempt to take the\r
443         mutex, and block when it finds it cannot obtain it. */\r
444         vTaskResume( xHighPriorityMutexTask );\r
445 \r
446         #if configUSE_PREEMPTION == 0\r
447                 taskYIELD();\r
448         #endif\r
449 \r
450         /* Ensure the task is reporting its priority as blocked and not\r
451         suspended (as it would have done in versions up to V7.5.3). */\r
452         #if( INCLUDE_eTaskGetState == 1 )\r
453         {\r
454                 configASSERT( eTaskGetState( xHighPriorityMutexTask ) == eBlocked );\r
455         }\r
456         #endif /* INCLUDE_eTaskGetState */\r
457 \r
458         /* The priority of the high priority task should now have been inherited\r
459         as by now it will have attempted to get the mutex. */\r
460         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
461         {\r
462                 xErrorDetected = pdTRUE;\r
463         }\r
464 \r
465         /* Attempt to set the priority of this task to the test priority -\r
466         between the     idle priority and the medium/high test priorities, but the\r
467         actual priority should remain at the high priority. */\r
468         vTaskPrioritySet( NULL, genqMUTEX_TEST_PRIORITY );\r
469         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
470         {\r
471                 xErrorDetected = pdTRUE;\r
472         }\r
473 \r
474         /* Now unsuspend the medium priority task.  This should not run as the\r
475         inherited priority of this task is above that of the medium priority\r
476         task. */\r
477         vTaskResume( xMediumPriorityMutexTask );\r
478 \r
479         /* If the medium priority task did run then it will have incremented the\r
480         guarded variable. */\r
481         if( ulGuardedVariable != 0 )\r
482         {\r
483                 xErrorDetected = pdTRUE;\r
484         }\r
485 \r
486         /* Take the local mutex too, so two mutexes are now held. */\r
487         if( xSemaphoreTake( xLocalMutex, intsemNO_BLOCK ) != pdPASS )\r
488         {\r
489                 xErrorDetected = pdTRUE;\r
490         }\r
491 \r
492         /* When the semaphore is given back the priority of this task should not\r
493         yet be disinherited because the local mutex is still held.  This is a\r
494         simplification to allow FreeRTOS to be integrated with middleware that\r
495         attempts to hold multiple mutexes without bloating the code with complex\r
496         algorithms.  It is possible that the high priority mutex task will\r
497         execute as it shares a priority with this task. */\r
498         if( xSemaphoreGive( xMutex ) != pdPASS )\r
499         {\r
500                 xErrorDetected = pdTRUE;\r
501         }\r
502 \r
503         #if configUSE_PREEMPTION == 0\r
504                 taskYIELD();\r
505         #endif\r
506 \r
507         /* The guarded variable is only incremented by the medium priority task,\r
508         which still should not have executed as this task should remain at the\r
509         higher priority, ensure this is the case. */\r
510         if( ulGuardedVariable != 0 )\r
511         {\r
512                 xErrorDetected = pdTRUE;\r
513         }\r
514 \r
515         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
516         {\r
517                 xErrorDetected = pdTRUE;\r
518         }\r
519 \r
520         /* Now also give back the local mutex, taking the held count back to 0.\r
521         This time the priority of this task should be disinherited back to the\r
522         priority to which it was set while the mutex was held.  This means\r
523         the medium priority task should execute and increment the guarded\r
524         variable.   When this task next runs both the high and medium priority\r
525         tasks will have been suspended again. */\r
526         if( xSemaphoreGive( xLocalMutex ) != pdPASS )\r
527         {\r
528                 xErrorDetected = pdTRUE;\r
529         }\r
530 \r
531         #if configUSE_PREEMPTION == 0\r
532                 taskYIELD();\r
533         #endif\r
534 \r
535         /* Check the guarded variable did indeed increment... */\r
536         if( ulGuardedVariable != 1 )\r
537         {\r
538                 xErrorDetected = pdTRUE;\r
539         }\r
540 \r
541         /* ... and that the priority of this task has been disinherited to\r
542         genqMUTEX_TEST_PRIORITY. */\r
543         if( uxTaskPriorityGet( NULL ) != genqMUTEX_TEST_PRIORITY )\r
544         {\r
545                 xErrorDetected = pdTRUE;\r
546         }\r
547 \r
548         /* Set the priority of this task back to its original value, ready for\r
549         the next loop around this test. */\r
550         vTaskPrioritySet( NULL, genqMUTEX_LOW_PRIORITY );\r
551 }\r
552 /*-----------------------------------------------------------*/\r
553 \r
554 static void prvTakeTwoMutexesReturnInSameOrder( SemaphoreHandle_t xMutex, SemaphoreHandle_t xLocalMutex )\r
555 {\r
556         /* Take the mutex.  It should be available now. */\r
557         if( xSemaphoreTake( xMutex, intsemNO_BLOCK ) != pdPASS )\r
558         {\r
559                 xErrorDetected = pdTRUE;\r
560         }\r
561 \r
562         /* Set the guarded variable to a known start value. */\r
563         ulGuardedVariable = 0;\r
564 \r
565         /* This task's priority should be as per that assigned when the task was\r
566         created. */\r
567         if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
568         {\r
569                 xErrorDetected = pdTRUE;\r
570         }\r
571 \r
572         /* Now unsuspend the high priority task.  This will attempt to take the\r
573         mutex, and block when it finds it cannot obtain it. */\r
574         vTaskResume( xHighPriorityMutexTask );\r
575 \r
576         #if configUSE_PREEMPTION == 0\r
577                 taskYIELD();\r
578         #endif\r
579 \r
580         /* Ensure the task is reporting its priority as blocked and not\r
581         suspended (as it would have done in versions up to V7.5.3). */\r
582         #if( INCLUDE_eTaskGetState == 1 )\r
583         {\r
584                 configASSERT( eTaskGetState( xHighPriorityMutexTask ) == eBlocked );\r
585         }\r
586         #endif /* INCLUDE_eTaskGetState */\r
587 \r
588         /* The priority of the high priority task should now have been inherited\r
589         as by now it will have attempted to get the mutex. */\r
590         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
591         {\r
592                 xErrorDetected = pdTRUE;\r
593         }\r
594 \r
595         /* Now unsuspend the medium priority task.  This should not run as the\r
596         inherited priority of this task is above that of the medium priority\r
597         task. */\r
598         vTaskResume( xMediumPriorityMutexTask );\r
599 \r
600         /* If the medium priority task did run then it will have incremented the\r
601         guarded variable. */\r
602         if( ulGuardedVariable != 0 )\r
603         {\r
604                 xErrorDetected = pdTRUE;\r
605         }\r
606 \r
607         /* Take the local mutex too, so two mutexes are now held. */\r
608         if( xSemaphoreTake( xLocalMutex, intsemNO_BLOCK ) != pdPASS )\r
609         {\r
610                 xErrorDetected = pdTRUE;\r
611         }\r
612 \r
613         /* When the local semaphore is given back the priority of this task should\r
614         not     yet be disinherited because the shared mutex is still held.  This is a\r
615         simplification to allow FreeRTOS to be integrated with middleware that\r
616         attempts to hold multiple mutexes without bloating the code with complex\r
617         algorithms.  It is possible that the high priority mutex task will\r
618         execute as it shares a priority with this task. */\r
619         if( xSemaphoreGive( xLocalMutex ) != pdPASS )\r
620         {\r
621                 xErrorDetected = pdTRUE;\r
622         }\r
623 \r
624         #if configUSE_PREEMPTION == 0\r
625                 taskYIELD();\r
626         #endif\r
627 \r
628         /* The guarded variable is only incremented by the medium priority task,\r
629         which still should not have executed as this task should remain at the\r
630         higher priority, ensure this is the case. */\r
631         if( ulGuardedVariable != 0 )\r
632         {\r
633                 xErrorDetected = pdTRUE;\r
634         }\r
635 \r
636         if( uxTaskPriorityGet( NULL ) != genqMUTEX_HIGH_PRIORITY )\r
637         {\r
638                 xErrorDetected = pdTRUE;\r
639         }\r
640 \r
641         /* Now also give back the shared mutex, taking the held count back to 0.\r
642         This time the priority of this task should be disinherited back to the\r
643         priority at which it was created.  This means the medium priority task\r
644         should execute and increment the guarded variable.  When this task next runs\r
645         both the high and medium priority tasks will have been suspended again. */\r
646         if( xSemaphoreGive( xMutex ) != pdPASS )\r
647         {\r
648                 xErrorDetected = pdTRUE;\r
649         }\r
650 \r
651         #if configUSE_PREEMPTION == 0\r
652                 taskYIELD();\r
653         #endif\r
654 \r
655         /* Check the guarded variable did indeed increment... */\r
656         if( ulGuardedVariable != 1 )\r
657         {\r
658                 xErrorDetected = pdTRUE;\r
659         }\r
660 \r
661         /* ... and that the priority of this task has been disinherited to\r
662         genqMUTEX_LOW_PRIORITY. */\r
663         if( uxTaskPriorityGet( NULL ) != genqMUTEX_LOW_PRIORITY )\r
664         {\r
665                 xErrorDetected = pdTRUE;\r
666         }\r
667 }\r
668 /*-----------------------------------------------------------*/\r
669 \r
670 static void prvLowPriorityMutexTask( void *pvParameters )\r
671 {\r
672 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters, xLocalMutex;\r
673 \r
674         #ifdef USE_STDIO\r
675         void vPrintDisplayMessage( const char * const * ppcMessageToSend );\r
676 \r
677                 const char * const pcTaskStartMsg = "Mutex with priority inheritance test started.\r\n";\r
678 \r
679                 /* Queue a message for printing to say the task has started. */\r
680                 vPrintDisplayMessage( &pcTaskStartMsg );\r
681         #endif\r
682 \r
683         /* The local mutex is used to check the 'mutexs held' count. */\r
684         xLocalMutex = xSemaphoreCreateMutex();\r
685         configASSERT( xLocalMutex );\r
686 \r
687         for( ;; )\r
688         {\r
689                 /* The first tests exercise the priority inheritance when two mutexes\r
690                 are taken then returned in a different order to which they were\r
691                 taken. */\r
692                 prvTakeTwoMutexesReturnInDifferentOrder( xMutex, xLocalMutex );\r
693 \r
694                 /* Just to show this task is still running. */\r
695                 ulLoopCounter2++;\r
696 \r
697                 #if configUSE_PREEMPTION == 0\r
698                         taskYIELD();\r
699                 #endif\r
700 \r
701                 /* The second tests exercise the priority inheritance when two mutexes\r
702                 are taken then returned in the same order in which they were taken. */\r
703                 prvTakeTwoMutexesReturnInSameOrder( xMutex, xLocalMutex );\r
704 \r
705                 /* Just to show this task is still running. */\r
706                 ulLoopCounter2++;\r
707 \r
708                 #if configUSE_PREEMPTION == 0\r
709                         taskYIELD();\r
710                 #endif\r
711         }\r
712 }\r
713 /*-----------------------------------------------------------*/\r
714 \r
715 static void prvMediumPriorityMutexTask( void *pvParameters )\r
716 {\r
717         ( void ) pvParameters;\r
718 \r
719         for( ;; )\r
720         {\r
721                 /* The medium priority task starts by suspending itself.  The low\r
722                 priority task will unsuspend this task when required. */\r
723                 vTaskSuspend( NULL );\r
724 \r
725                 /* When this task unsuspends all it does is increment the guarded\r
726                 variable, this is so the low priority task knows that it has\r
727                 executed. */\r
728                 ulGuardedVariable++;\r
729         }\r
730 }\r
731 /*-----------------------------------------------------------*/\r
732 \r
733 static void prvHighPriorityMutexTask( void *pvParameters )\r
734 {\r
735 SemaphoreHandle_t xMutex = ( SemaphoreHandle_t ) pvParameters;\r
736 \r
737         for( ;; )\r
738         {\r
739                 /* The high priority task starts by suspending itself.  The low\r
740                 priority task will unsuspend this task when required. */\r
741                 vTaskSuspend( NULL );\r
742 \r
743                 /* When this task unsuspends all it does is attempt to obtain\r
744                 the mutex.  It should find the mutex is not available so a\r
745                 block time is specified. */\r
746                 if( xSemaphoreTake( xMutex, portMAX_DELAY ) != pdPASS )\r
747                 {\r
748                         xErrorDetected = pdTRUE;\r
749                 }\r
750 \r
751                 /* When the mutex is eventually obtained it is just given back before\r
752                 returning to suspend ready for the next cycle. */\r
753                 if( xSemaphoreGive( xMutex ) != pdPASS )\r
754                 {\r
755                         xErrorDetected = pdTRUE;\r
756                 }\r
757         }\r
758 }\r
759 /*-----------------------------------------------------------*/\r
760 \r
761 \r
762 /* This is called to check that all the created tasks are still running. */\r
763 BaseType_t xAreGenericQueueTasksStillRunning( void )\r
764 {\r
765 static uint32_t ulLastLoopCounter = 0, ulLastLoopCounter2 = 0;\r
766 \r
767         /* If the demo task is still running then we expect the loop counters to\r
768         have incremented since this function was last called. */\r
769         if( ulLastLoopCounter == ulLoopCounter )\r
770         {\r
771                 xErrorDetected = pdTRUE;\r
772         }\r
773 \r
774         if( ulLastLoopCounter2 == ulLoopCounter2 )\r
775         {\r
776                 xErrorDetected = pdTRUE;\r
777         }\r
778 \r
779         ulLastLoopCounter = ulLoopCounter;\r
780         ulLastLoopCounter2 = ulLoopCounter2;\r
781 \r
782         /* Errors detected in the task itself will have latched xErrorDetected\r
783         to true. */\r
784 \r
785         return ( BaseType_t ) !xErrorDetected;\r
786 }\r
787 \r
788 \r