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 * Demonstrates and tests mutexes being used from an interrupt.
\r
102 #include <stdlib.h>
\r
104 /* Scheduler include files. */
\r
105 #include "FreeRTOS.h"
\r
107 #include "semphr.h"
\r
109 /* Demo program include files. */
\r
110 #include "IntSemTest.h"
\r
112 /*-----------------------------------------------------------*/
\r
114 /* The priorities of the test tasks. */
\r
115 #define intsemMASTER_PRIORITY ( tskIDLE_PRIORITY )
\r
116 #define intsemSLAVE_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
118 /* The rate at which the tick hook will give the mutex. */
\r
119 #define intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ( 100 )
\r
121 /* A block time of 0 means 'don't block'. */
\r
122 #define intsemNO_BLOCK 0
\r
124 /* The maximum count value for the counting semaphore given from an
\r
126 #define intsemMAX_COUNT 3
\r
128 /*-----------------------------------------------------------*/
\r
131 * The master is a task that receives a mutex that is given from an interrupt -
\r
132 * although generally mutexes should not be used given in interrupts (and
\r
133 * definitely never taken in an interrupt) there are some circumstances when it
\r
134 * may be desirable.
\r
136 * The slave task is just used by the master task to force priority inheritance
\r
137 * on a mutex that is shared between the master and the slave - which is a
\r
138 * separate mutex to that given by the interrupt.
\r
140 static void vInterruptMutexSlaveTask( void *pvParameters );
\r
141 static void vInterruptMutexMasterTask( void *pvParameters );
\r
144 * A test whereby the master takes the shared and interrupt mutexes in that
\r
145 * order, then gives them back in the same order, ensuring the priority
\r
146 * inheritance is behaving as expected at each step.
\r
148 static void prvTakeAndGiveInTheSameOrder( void );
\r
151 * A test whereby the master takes the shared and interrupt mutexes in that
\r
152 * order, then gives them back in the opposite order to which they were taken,
\r
153 * ensuring the priority inheritance is behaving as expected at each step.
\r
155 static void prvTakeAndGiveInTheOppositeOrder( void );
\r
158 * A simple task that interacts with an interrupt using a counting semaphore,
\r
159 * primarily for code coverage purposes.
\r
161 static void vInterruptCountingSemaphoreTask( void *pvParameters );
\r
163 /*-----------------------------------------------------------*/
\r
165 /* Flag that will be latched to pdTRUE should any unexpected behaviour be
\r
166 detected in any of the tasks. */
\r
167 static volatile BaseType_t xErrorDetected = pdFALSE;
\r
169 /* Counters that are incremented on each cycle of a test. This is used to
\r
170 detect a stalled task - a test that is no longer running. */
\r
171 static volatile uint32_t ulMasterLoops = 0, ulCountingSemaphoreLoops = 0;
\r
173 /* Handles of the test tasks that must be accessed from other test tasks. */
\r
174 static TaskHandle_t xSlaveHandle;
\r
176 /* A mutex which is given from an interrupt - although generally mutexes should
\r
177 not be used given in interrupts (and definitely never taken in an interrupt)
\r
178 there are some circumstances when it may be desirable. */
\r
179 static SemaphoreHandle_t xISRMutex = NULL;
\r
181 /* A counting semaphore which is given from an interrupt. */
\r
182 static SemaphoreHandle_t xISRCountingSemaphore = NULL;
\r
184 /* A mutex which is shared between the master and slave tasks - the master
\r
185 does both sharing of this mutex with the slave and receiving a mutex from the
\r
187 static SemaphoreHandle_t xMasterSlaveMutex = NULL;
\r
189 /* Flag that allows the master task to control when the interrupt gives or does
\r
190 not give the mutex. There is no mutual exclusion on this variable, but this is
\r
191 only test code and it should be fine in the 32=bit test environment. */
\r
192 static BaseType_t xOkToGiveMutex = pdFALSE, xOkToGiveCountingSemaphore = pdFALSE;
\r
194 /* Used to coordinate timing between tasks and the interrupt. */
\r
195 const TickType_t xInterruptGivePeriod = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
197 /*-----------------------------------------------------------*/
\r
199 void vStartInterruptSemaphoreTasks( void )
\r
201 /* Create the semaphores that are given from an interrupt. */
\r
202 xISRMutex = xSemaphoreCreateMutex();
\r
203 configASSERT( xISRMutex );
\r
204 xISRCountingSemaphore = xSemaphoreCreateCounting( intsemMAX_COUNT, 0 );
\r
205 configASSERT( xISRCountingSemaphore );
\r
207 /* Create the mutex that is shared between the master and slave tasks (the
\r
208 master receives a mutex from an interrupt and shares a mutex with the
\r
210 xMasterSlaveMutex = xSemaphoreCreateMutex();
\r
211 configASSERT( xMasterSlaveMutex );
\r
213 /* Create the tasks that share mutexes between then and with interrupts. */
\r
214 xTaskCreate( vInterruptMutexSlaveTask, "IntMuS", configMINIMAL_STACK_SIZE, NULL, intsemSLAVE_PRIORITY, &xSlaveHandle );
\r
215 xTaskCreate( vInterruptMutexMasterTask, "IntMuM", configMINIMAL_STACK_SIZE, NULL, intsemMASTER_PRIORITY, NULL );
\r
217 /* Create the task that blocks on the counting semaphore. */
\r
218 xTaskCreate( vInterruptCountingSemaphoreTask, "IntCnt", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
220 /*-----------------------------------------------------------*/
\r
222 static void vInterruptMutexMasterTask( void *pvParameters )
\r
224 /* Just to avoid compiler warnings. */
\r
225 ( void ) pvParameters;
\r
229 prvTakeAndGiveInTheSameOrder();
\r
231 /* Ensure not to starve out other tests. */
\r
233 vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
235 prvTakeAndGiveInTheOppositeOrder();
\r
237 /* Ensure not to starve out other tests. */
\r
239 vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );
\r
242 /*-----------------------------------------------------------*/
\r
244 static void prvTakeAndGiveInTheSameOrder( void )
\r
246 /* Ensure the slave is suspended, and that this task is running at the
\r
247 lower priority as expected as the start conditions. */
\r
248 #if( INCLUDE_eTaskGetState == 1 )
\r
250 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
252 #endif /* INCLUDE_eTaskGetState */
\r
254 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
256 xErrorDetected = pdTRUE;
\r
259 /* Take the semaphore that is shared with the slave. */
\r
260 if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )
\r
262 xErrorDetected = pdTRUE;
\r
265 /* This task now has the mutex. Unsuspend the slave so it too
\r
266 attempts to take the mutex. */
\r
267 vTaskResume( xSlaveHandle );
\r
269 /* The slave has the higher priority so should now have executed and
\r
270 blocked on the semaphore. */
\r
271 #if( INCLUDE_eTaskGetState == 1 )
\r
273 configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );
\r
275 #endif /* INCLUDE_eTaskGetState */
\r
277 /* This task should now have inherited the priority of the slave
\r
279 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
281 xErrorDetected = pdTRUE;
\r
284 /* Now wait a little longer than the time between ISR gives to also
\r
285 obtain the ISR mutex. */
\r
286 xOkToGiveMutex = pdTRUE;
\r
287 if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )
\r
289 xErrorDetected = pdTRUE;
\r
291 xOkToGiveMutex = pdFALSE;
\r
293 /* Attempting to take again immediately should fail as the mutex is
\r
295 if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )
\r
297 xErrorDetected = pdTRUE;
\r
300 /* Should still be at the priority of the slave task. */
\r
301 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
303 xErrorDetected = pdTRUE;
\r
306 /* Give back the ISR semaphore to ensure the priority is not
\r
307 disinherited as the shared mutex (which the higher priority task is
\r
308 attempting to obtain) is still held. */
\r
309 if( xSemaphoreGive( xISRMutex ) != pdPASS )
\r
311 xErrorDetected = pdTRUE;
\r
314 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
316 xErrorDetected = pdTRUE;
\r
319 /* Finally give back the shared mutex. This time the higher priority
\r
320 task should run before this task runs again - so this task should have
\r
321 disinherited the priority and the higher priority task should be in the
\r
322 suspended state again. */
\r
323 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
325 xErrorDetected = pdTRUE;
\r
328 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
330 xErrorDetected = pdTRUE;
\r
333 #if( INCLUDE_eTaskGetState == 1 )
\r
335 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
337 #endif /* INCLUDE_eTaskGetState */
\r
339 /* Reset the mutex ready for the next round. */
\r
340 xQueueReset( xISRMutex );
\r
342 /*-----------------------------------------------------------*/
\r
344 static void prvTakeAndGiveInTheOppositeOrder( void )
\r
346 /* Ensure the slave is suspended, and that this task is running at the
\r
347 lower priority as expected as the start conditions. */
\r
348 #if( INCLUDE_eTaskGetState == 1 )
\r
350 configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );
\r
352 #endif /* INCLUDE_eTaskGetState */
\r
354 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
356 xErrorDetected = pdTRUE;
\r
359 /* Take the semaphore that is shared with the slave. */
\r
360 if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )
\r
362 xErrorDetected = pdTRUE;
\r
365 /* This task now has the mutex. Unsuspend the slave so it too
\r
366 attempts to take the mutex. */
\r
367 vTaskResume( xSlaveHandle );
\r
369 /* The slave has the higher priority so should now have executed and
\r
370 blocked on the semaphore. */
\r
371 #if( INCLUDE_eTaskGetState == 1 )
\r
373 configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );
\r
375 #endif /* INCLUDE_eTaskGetState */
\r
377 /* This task should now have inherited the priority of the slave
\r
379 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
381 xErrorDetected = pdTRUE;
\r
384 /* Now wait a little longer than the time between ISR gives to also
\r
385 obtain the ISR mutex. */
\r
386 xOkToGiveMutex = pdTRUE;
\r
387 if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )
\r
389 xErrorDetected = pdTRUE;
\r
391 xOkToGiveMutex = pdFALSE;
\r
393 /* Attempting to take again immediately should fail as the mutex is
\r
395 if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )
\r
397 xErrorDetected = pdTRUE;
\r
400 /* Should still be at the priority of the slave task. */
\r
401 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
403 xErrorDetected = pdTRUE;
\r
406 /* Give back the shared semaphore to ensure the priority is not disinherited
\r
407 as the ISR mutex is still held. The higher priority slave task should run
\r
408 before this task runs again. */
\r
409 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
411 xErrorDetected = pdTRUE;
\r
414 /* Should still be at the priority of the slave task as this task still
\r
415 holds one semaphore (this is a simplification in the priority inheritance
\r
417 if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )
\r
419 xErrorDetected = pdTRUE;
\r
422 /* Give back the ISR semaphore, which should result in the priority being
\r
423 disinherited as it was the last mutex held. */
\r
424 if( xSemaphoreGive( xISRMutex ) != pdPASS )
\r
426 xErrorDetected = pdTRUE;
\r
429 if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )
\r
431 xErrorDetected = pdTRUE;
\r
434 /* Reset the mutex ready for the next round. */
\r
435 xQueueReset( xISRMutex );
\r
437 /*-----------------------------------------------------------*/
\r
439 static void vInterruptMutexSlaveTask( void *pvParameters )
\r
441 /* Just to avoid compiler warnings. */
\r
442 ( void ) pvParameters;
\r
446 /* This task starts by suspending itself so when it executes can be
\r
447 controlled by the master task. */
\r
448 vTaskSuspend( NULL );
\r
450 /* This task will execute when the master task already holds the mutex.
\r
451 Attempting to take the mutex will place this task in the Blocked
\r
453 if( xSemaphoreTake( xMasterSlaveMutex, portMAX_DELAY ) != pdPASS )
\r
455 xErrorDetected = pdTRUE;
\r
458 if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )
\r
460 xErrorDetected = pdTRUE;
\r
464 /*-----------------------------------------------------------*/
\r
466 static void vInterruptCountingSemaphoreTask( void *pvParameters )
\r
469 const TickType_t xDelay = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) * ( intsemMAX_COUNT + 1 );
\r
471 ( void ) pvParameters;
\r
475 /* Expect to start with the counting semaphore empty. */
\r
476 if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 )
\r
478 xErrorDetected = pdTRUE;
\r
481 /* Wait until it is expected that the interrupt will have filled the
\r
482 counting semaphore. */
\r
483 xOkToGiveCountingSemaphore = pdTRUE;
\r
484 vTaskDelay( xDelay );
\r
485 xOkToGiveCountingSemaphore = pdFALSE;
\r
487 /* Now it is expected that the counting semaphore is full. */
\r
488 if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != intsemMAX_COUNT )
\r
490 xErrorDetected = pdTRUE;
\r
493 if( uxQueueSpacesAvailable( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 )
\r
495 xErrorDetected = pdTRUE;
\r
498 ulCountingSemaphoreLoops++;
\r
500 /* Expect to be able to take the counting semaphore intsemMAX_COUNT
\r
501 times. A block time of 0 is used as the semaphore should already be
\r
504 while( xSemaphoreTake( xISRCountingSemaphore, 0 ) == pdPASS )
\r
509 if( xCount != intsemMAX_COUNT )
\r
511 xErrorDetected = pdTRUE;
\r
514 /* Now raise the priority of this task so it runs immediately that the
\r
515 semaphore is given from the interrupt. */
\r
516 vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );
\r
518 /* Block to wait for the semaphore to be given from the interrupt. */
\r
519 xOkToGiveCountingSemaphore = pdTRUE;
\r
520 xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY );
\r
521 xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY );
\r
522 xOkToGiveCountingSemaphore = pdFALSE;
\r
524 /* Reset the priority so as not to disturbe other tests too much. */
\r
525 vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
\r
527 ulCountingSemaphoreLoops++;
\r
530 /*-----------------------------------------------------------*/
\r
532 void vInterruptSemaphorePeriodicTest( void )
\r
534 static TickType_t xLastGiveTime = 0;
\r
535 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
536 TickType_t xTimeNow;
\r
538 /* No mutual exclusion on xOkToGiveMutex, but this is only test code (and
\r
539 only executed on a 32-bit architecture) so ignore that in this case. */
\r
540 xTimeNow = xTaskGetTickCountFromISR();
\r
541 if( ( xTimeNow - xLastGiveTime ) >= pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) )
\r
543 configASSERT( xISRMutex );
\r
544 if( xOkToGiveMutex != pdFALSE )
\r
546 /* Null is used as the second parameter in this give, and non-NULL
\r
547 in the other gives for code coverage reasons. */
\r
548 xSemaphoreGiveFromISR( xISRMutex, NULL );
\r
550 /* Second give attempt should fail. */
\r
551 configASSERT( xSemaphoreGiveFromISR( xISRMutex, &xHigherPriorityTaskWoken ) == pdFAIL );
\r
554 if( xOkToGiveCountingSemaphore != pdFALSE )
\r
556 xSemaphoreGiveFromISR( xISRCountingSemaphore, &xHigherPriorityTaskWoken );
\r
558 xLastGiveTime = xTimeNow;
\r
561 /* Remove compiler warnings about the value being set but not used. */
\r
562 ( void ) xHigherPriorityTaskWoken;
\r
564 /*-----------------------------------------------------------*/
\r
566 /* This is called to check that all the created tasks are still running. */
\r
567 BaseType_t xAreInterruptSemaphoreTasksStillRunning( void )
\r
569 static uint32_t ulLastMasterLoopCounter = 0, ulLastCountingSemaphoreLoops = 0;
\r
571 /* If the demo tasks are running then it is expected that the loop counters
\r
572 will have changed since this function was last called. */
\r
573 if( ulLastMasterLoopCounter == ulMasterLoops )
\r
575 xErrorDetected = pdTRUE;
\r
578 ulLastMasterLoopCounter = ulMasterLoops;
\r
580 if( ulLastCountingSemaphoreLoops == ulCountingSemaphoreLoops )
\r
582 xErrorDetected = pdTRUE;
\r
585 ulLastCountingSemaphoreLoops = ulCountingSemaphoreLoops++;
\r
587 /* Errors detected in the task itself will have latched xErrorDetected
\r
590 return ( BaseType_t ) !xErrorDetected;
\r