]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/Common/Minimal/IntSemTest.c
Prepare for V9.0.0 release:
[freertos] / FreeRTOS / Demo / Common / Minimal / IntSemTest.c
index 310047a2b62070ce0e3239574d4391b83419d5e6..40517c87aa0e55413471080af54b305b0c3c4aae 100644 (file)
@@ -1,60 +1,64 @@
 /*\r
-    FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.\r
+    FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.\r
     All rights reserved\r
 \r
     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
 \r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    FreeRTOS provides completely free yet professionally developed,    *\r
-     *    robust, strictly quality controlled, supported, and cross          *\r
-     *    platform software that has become a de facto standard.             *\r
-     *                                                                       *\r
-     *    Help yourself get started quickly and support the FreeRTOS         *\r
-     *    project by purchasing a FreeRTOS tutorial book, reference          *\r
-     *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
-     *                                                                       *\r
-     *    Thank you!                                                         *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
     This file is part of the FreeRTOS distribution.\r
 \r
     FreeRTOS is free software; you can redistribute it and/or modify it under\r
     the terms of the GNU General Public License (version 2) as published by the\r
-    Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
+    Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
 \r
+    ***************************************************************************\r
     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
     >>!   obliged to provide the source code for proprietary components     !<<\r
     >>!   outside of the FreeRTOS kernel.                                   !<<\r
+    ***************************************************************************\r
 \r
     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
-    FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
+    FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
     link: http://www.freertos.org/a00114.html\r
 \r
-    1 tab == 4 spaces!\r
-\r
     ***************************************************************************\r
      *                                                                       *\r
-     *    Having a problem?  Start by reading the FAQ "My application does   *\r
-     *    not run, what could be wrong?"                                     *\r
+     *    FreeRTOS provides completely free yet professionally developed,    *\r
+     *    robust, strictly quality controlled, supported, and cross          *\r
+     *    platform software that is more than just the market leader, it     *\r
+     *    is the industry's de facto standard.                               *\r
      *                                                                       *\r
-     *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
+     *    Help yourself get started quickly while simultaneously helping     *\r
+     *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
+     *    tutorial book, reference manual, or both:                          *\r
+     *    http://www.FreeRTOS.org/Documentation                              *\r
      *                                                                       *\r
     ***************************************************************************\r
 \r
-    http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
-    license and Real Time Engineers Ltd. contact details.\r
+    http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
+    the FAQ page "My application does not run, what could be wrong?".  Have you\r
+    defined configASSERT()?\r
+\r
+    http://www.FreeRTOS.org/support - In return for receiving this top quality\r
+    embedded software for free we request you assist our global community by\r
+    participating in the support forum.\r
+\r
+    http://www.FreeRTOS.org/training - Investing in training allows your team to\r
+    be as productive as possible as early as possible.  Now you can receive\r
+    FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
+    Ltd, and the world's leading authority on the world's leading RTOS.\r
 \r
     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
 \r
-    http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
-    Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
-    licenses offer ticketed support, indemnification and middleware.\r
+    http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
+    Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
+\r
+    http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
+    Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
+    licenses offer ticketed support, indemnification and commercial middleware.\r
 \r
     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
     engineered and independently SIL3 certified version for use in safety and\r
 /* A block time of 0 means 'don't block'. */\r
 #define intsemNO_BLOCK                         0\r
 \r
+/* The maximum count value for the counting semaphore given from an\r
+interrupt. */\r
+#define intsemMAX_COUNT                                3\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
 static void vInterruptMutexSlaveTask( void *pvParameters );\r
 static void vInterruptMutexMasterTask( void *pvParameters );\r
 \r
+/*\r
+ * A test whereby the master takes the shared and interrupt mutexes in that\r
+ * order, then gives them back in the same order, ensuring the priority\r
+ * inheritance is behaving as expected at each step.\r
+ */\r
+static void prvTakeAndGiveInTheSameOrder( void );\r
+\r
+/*\r
+ * A test whereby the master takes the shared and interrupt mutexes in that\r
+ * order, then gives them back in the opposite order to which they were taken,\r
+ * ensuring the priority inheritance is behaving as expected at each step.\r
+ */\r
+static void prvTakeAndGiveInTheOppositeOrder( void );\r
+\r
+/*\r
+ * A simple task that interacts with an interrupt using a counting semaphore,\r
+ * primarily for code coverage purposes.\r
+ */\r
+static void vInterruptCountingSemaphoreTask( void *pvParameters );\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 /* Flag that will be latched to pdTRUE should any unexpected behaviour be\r
@@ -114,7 +142,7 @@ static volatile BaseType_t xErrorDetected = pdFALSE;
 \r
 /* Counters that are incremented on each cycle of a test.  This is used to\r
 detect a stalled task - a test that is no longer running. */\r
-static volatile uint32_t ulMasterLoops = 0;\r
+static volatile uint32_t ulMasterLoops = 0, ulCountingSemaphoreLoops = 0;\r
 \r
 /* Handles of the test tasks that must be accessed from other test tasks. */\r
 static TaskHandle_t xSlaveHandle;\r
@@ -124,6 +152,9 @@ not be used given in interrupts (and definitely never taken in an interrupt)
 there are some circumstances when it may be desirable. */\r
 static SemaphoreHandle_t xISRMutex = NULL;\r
 \r
+/* A counting semaphore which is given from an interrupt. */\r
+static SemaphoreHandle_t xISRCountingSemaphore = NULL;\r
+\r
 /* A mutex which is shared between the master and slave tasks - the master\r
 does both sharing of this mutex with the slave and receiving a mutex from the\r
 interrupt. */\r
@@ -132,15 +163,20 @@ static SemaphoreHandle_t xMasterSlaveMutex = NULL;
 /* Flag that allows the master task to control when the interrupt gives or does\r
 not give the mutex.  There is no mutual exclusion on this variable, but this is\r
 only test code and it should be fine in the 32=bit test environment. */\r
-static BaseType_t xOkToGiveMutex = pdFALSE;\r
+static BaseType_t xOkToGiveMutex = pdFALSE, xOkToGiveCountingSemaphore = pdFALSE;\r
+\r
+/* Used to coordinate timing between tasks and the interrupt. */\r
+const TickType_t xInterruptGivePeriod = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
 void vStartInterruptSemaphoreTasks( void )\r
 {\r
-       /* Create the mutex that is given from an interrupt. */\r
+       /* Create the semaphores that are given from an interrupt. */\r
        xISRMutex = xSemaphoreCreateMutex();\r
        configASSERT( xISRMutex );\r
+       xISRCountingSemaphore = xSemaphoreCreateCounting( intsemMAX_COUNT, 0 );\r
+       configASSERT( xISRCountingSemaphore );\r
 \r
        /* Create the mutex that is shared between the master and slave tasks (the\r
        master receives a mutex from an interrupt and shares a mutex with the\r
@@ -151,168 +187,226 @@ void vStartInterruptSemaphoreTasks( void )
        /* Create the tasks that share mutexes between then and with interrupts. */\r
        xTaskCreate( vInterruptMutexSlaveTask, "IntMuS", configMINIMAL_STACK_SIZE, NULL, intsemSLAVE_PRIORITY, &xSlaveHandle );\r
        xTaskCreate( vInterruptMutexMasterTask, "IntMuM", configMINIMAL_STACK_SIZE, NULL, intsemMASTER_PRIORITY, NULL );\r
+\r
+       /* Create the task that blocks on the counting semaphore. */\r
+       xTaskCreate( vInterruptCountingSemaphoreTask, "IntCnt", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 static void vInterruptMutexMasterTask( void *pvParameters )\r
 {\r
-const TickType_t xInterruptGivePeriod = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );\r
-\r
        /* Just to avoid compiler warnings. */\r
        ( void ) pvParameters;\r
 \r
        for( ;; )\r
        {\r
-               /* Ensure the slave is suspended, and that this task is running at the\r
-               lower priority as expected as the start conditions. */\r
-               #if( INCLUDE_eTaskGetState == 1 )\r
-               {\r
-                       configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );\r
-               }\r
-               #endif /* INCLUDE_eTaskGetState */\r
+               prvTakeAndGiveInTheSameOrder();\r
 \r
-               if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+               /* Ensure not to starve out other tests. */\r
+               ulMasterLoops++;\r
+               vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );\r
 \r
-               /* Take the semaphore that is shared with the slave. */\r
-               if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+               prvTakeAndGiveInTheOppositeOrder();\r
 \r
-               /* This task now has the mutex.  Unsuspend the slave so it too\r
-               attempts to take the mutex. */\r
-               vTaskResume( xSlaveHandle );\r
+               /* Ensure not to starve out other tests. */\r
+               ulMasterLoops++;\r
+               vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
 \r
-               /* The slave has the higher priority so should now have executed and\r
-               blocked on the semaphore. */\r
-               #if( INCLUDE_eTaskGetState == 1 )\r
-               {\r
-                       configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );\r
-               }\r
-               #endif /* INCLUDE_eTaskGetState */\r
+static void prvTakeAndGiveInTheSameOrder( void )\r
+{\r
+       /* Ensure the slave is suspended, and that this task is running at the\r
+       lower priority as expected as the start conditions. */\r
+       #if( INCLUDE_eTaskGetState == 1 )\r
+       {\r
+               configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );\r
+       }\r
+       #endif /* INCLUDE_eTaskGetState */\r
 \r
-               /* This task should now have inherited the priority of the slave\r
-               task. */\r
-               if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               /* Now wait a little longer than the time between ISR gives to also\r
-               obtain the ISR mutex. */\r
-               xOkToGiveMutex = pdTRUE;\r
-               if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
-               xOkToGiveMutex = pdFALSE;\r
+       /* Take the semaphore that is shared with the slave. */\r
+       if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               /* Attempting to take again immediately should fail as the mutex is\r
-               already held. */\r
-               if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* This task now has the mutex.  Unsuspend the slave so it too\r
+       attempts to take the mutex. */\r
+       vTaskResume( xSlaveHandle );\r
 \r
-               /* Should still be at the priority of the slave task. */\r
-               if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* The slave has the higher priority so should now have executed and\r
+       blocked on the semaphore. */\r
+       #if( INCLUDE_eTaskGetState == 1 )\r
+       {\r
+               configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );\r
+       }\r
+       #endif /* INCLUDE_eTaskGetState */\r
 \r
-               /* Give back the ISR semaphore to ensure the priority is not\r
-               disinherited as the shared mutex (which the higher priority task is\r
-               attempting to obtain) is still held. */\r
-               if( xSemaphoreGive( xISRMutex ) != pdPASS )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* This task should now have inherited the priority of the slave\r
+       task. */\r
+       if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* Now wait a little longer than the time between ISR gives to also\r
+       obtain the ISR mutex. */\r
+       xOkToGiveMutex = pdTRUE;\r
+       if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+       xOkToGiveMutex = pdFALSE;\r
 \r
-               /* Finally give back the shared mutex.  This time the higher priority\r
-               task should run before this task runs again - so this task should have\r
-               disinherited the priority and the higher priority task should be in the\r
-               suspended state again. */\r
-               if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* Attempting to take again immediately should fail as the mutex is\r
+       already held. */\r
+       if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* Should still be at the priority of the slave task. */\r
+       if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               #if( INCLUDE_eTaskGetState == 1 )\r
-               {\r
-                       configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );\r
-               }\r
-               #endif /* INCLUDE_eTaskGetState */\r
+       /* Give back the ISR semaphore to ensure the priority is not\r
+       disinherited as the shared mutex (which the higher priority task is\r
+       attempting to obtain) is still held. */\r
+       if( xSemaphoreGive( xISRMutex ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               /* Ensure not to starve out other tests. */\r
-               ulMasterLoops++;\r
-               vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );\r
+       if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
+       /* Finally give back the shared mutex.  This time the higher priority\r
+       task should run before this task runs again - so this task should have\r
+       disinherited the priority and the higher priority task should be in the\r
+       suspended state again. */\r
+       if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               /* Repeat exactly up to the point where the mutexes are given back.\r
-               This time the shared mutex is given back first. */\r
-               if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               vTaskResume( xSlaveHandle );\r
+       #if( INCLUDE_eTaskGetState == 1 )\r
+       {\r
+               configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );\r
+       }\r
+       #endif /* INCLUDE_eTaskGetState */\r
 \r
-               #if( INCLUDE_eTaskGetState == 1 )\r
-               {\r
-                       configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );\r
-               }\r
-               #endif /* INCLUDE_eTaskGetState */\r
+       /* Reset the mutex ready for the next round. */\r
+       xQueueReset( xISRMutex );\r
+}\r
+/*-----------------------------------------------------------*/\r
 \r
-               if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+static void prvTakeAndGiveInTheOppositeOrder( void )\r
+{\r
+       /* Ensure the slave is suspended, and that this task is running at the\r
+       lower priority as expected as the start conditions. */\r
+       #if( INCLUDE_eTaskGetState == 1 )\r
+       {\r
+               configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended );\r
+       }\r
+       #endif /* INCLUDE_eTaskGetState */\r
 \r
-               xOkToGiveMutex = pdTRUE;\r
-               if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
-               xOkToGiveMutex = pdFALSE;\r
+       if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* Take the semaphore that is shared with the slave. */\r
+       if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
 \r
-               /* This is where the differences start as this time the shared mutex is\r
-               given back first.  This time to the higher priority task should run\r
-               before this task gets to the point of releasing the interrupt mutex - so\r
-               this task should have disinherited the priority and the higher priority\r
-               task should be in the suspended state again. */\r
-               if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )\r
-               {\r
-                       xErrorDetected = pdTRUE;\r
-               }\r
+       /* This task now has the mutex.  Unsuspend the slave so it too\r
+       attempts to take the mutex. */\r
+       vTaskResume( xSlaveHandle );\r
 \r
-               /* Give back the interrupt semaphore too, so the mutex held count goes\r
-               back to 0.  The mutex will then have to be reset so the ISR can give it\r
-               in the next cycle. */\r
-               xSemaphoreGive( xISRMutex );\r
-               xQueueReset( ( QueueHandle_t ) xISRMutex );\r
+       /* The slave has the higher priority so should now have executed and\r
+       blocked on the semaphore. */\r
+       #if( INCLUDE_eTaskGetState == 1 )\r
+       {\r
+               configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked );\r
+       }\r
+       #endif /* INCLUDE_eTaskGetState */\r
 \r
-               /* Ensure not to starve out other tests. */\r
-               ulMasterLoops++;\r
-               vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS );\r
+       /* This task should now have inherited the priority of the slave\r
+       task. */\r
+       if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+\r
+       /* Now wait a little longer than the time between ISR gives to also\r
+       obtain the ISR mutex. */\r
+       xOkToGiveMutex = pdTRUE;\r
+       if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+       xOkToGiveMutex = pdFALSE;\r
+\r
+       /* Attempting to take again immediately should fail as the mutex is\r
+       already held. */\r
+       if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+\r
+       /* Should still be at the priority of the slave task. */\r
+       if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+\r
+       /* Give back the shared semaphore to ensure the priority is not disinherited\r
+       as the ISR mutex is still held.  The higher priority slave task should run\r
+       before this task runs again. */\r
+       if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+\r
+       /* Should still be at the priority of the slave task as this task still\r
+       holds one semaphore (this is a simplification in the priority inheritance\r
+       mechanism. */\r
+       if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
        }\r
+\r
+       /* Give back the ISR semaphore, which should result in the priority being\r
+       disinherited as it was the last mutex held. */\r
+       if( xSemaphoreGive( xISRMutex ) != pdPASS )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+\r
+       if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+\r
+       /* Reset the mutex ready for the next round. */\r
+       xQueueReset( xISRMutex );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -343,33 +437,110 @@ static void vInterruptMutexSlaveTask( void *pvParameters )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+static void vInterruptCountingSemaphoreTask( void *pvParameters )\r
+{\r
+BaseType_t xCount;\r
+const TickType_t xDelay = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) * ( intsemMAX_COUNT + 1 );\r
+\r
+       ( void ) pvParameters;\r
+\r
+       for( ;; )\r
+       {\r
+               /* Expect to start with the counting semaphore empty. */\r
+               if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 )\r
+               {\r
+                       xErrorDetected = pdTRUE;\r
+               }\r
+\r
+               /* Wait until it is expected that the interrupt will have filled the\r
+               counting semaphore. */\r
+               xOkToGiveCountingSemaphore = pdTRUE;\r
+               vTaskDelay( xDelay );\r
+               xOkToGiveCountingSemaphore = pdFALSE;\r
+\r
+               /* Now it is expected that the counting semaphore is full. */\r
+               if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != intsemMAX_COUNT )\r
+               {\r
+                       xErrorDetected = pdTRUE;\r
+               }\r
+\r
+               if( uxQueueSpacesAvailable( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 )\r
+               {\r
+                       xErrorDetected = pdTRUE;\r
+               }\r
+\r
+               ulCountingSemaphoreLoops++;\r
+\r
+               /* Expect to be able to take the counting semaphore intsemMAX_COUNT\r
+               times.  A block time of 0 is used as the semaphore should already be\r
+               there. */\r
+               xCount = 0;\r
+               while( xSemaphoreTake( xISRCountingSemaphore, 0 ) == pdPASS )\r
+               {\r
+                       xCount++;\r
+               }\r
+\r
+               if( xCount != intsemMAX_COUNT )\r
+               {\r
+                       xErrorDetected = pdTRUE;\r
+               }\r
+\r
+               /* Now raise the priority of this task so it runs immediately that the\r
+               semaphore is given from the interrupt. */\r
+               vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 );\r
+\r
+               /* Block to wait for the semaphore to be given from the interrupt. */\r
+               xOkToGiveCountingSemaphore = pdTRUE;\r
+               xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY );\r
+               xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY );\r
+               xOkToGiveCountingSemaphore = pdFALSE;\r
+\r
+               /* Reset the priority so as not to disturbe other tests too much. */\r
+               vTaskPrioritySet( NULL, tskIDLE_PRIORITY );\r
+\r
+               ulCountingSemaphoreLoops++;\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 void vInterruptSemaphorePeriodicTest( void )\r
 {\r
 static TickType_t xLastGiveTime = 0;\r
+BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
 TickType_t xTimeNow;\r
 \r
        /* No mutual exclusion on xOkToGiveMutex, but this is only test code (and\r
        only executed on a 32-bit architecture) so ignore that in this case. */\r
        xTimeNow = xTaskGetTickCountFromISR();\r
-       if( ( xTimeNow - xLastGiveTime ) >= pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) )\r
+       if( ( ( TickType_t ) ( xTimeNow - xLastGiveTime ) ) >= pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) )\r
        {\r
                configASSERT( xISRMutex );\r
                if( xOkToGiveMutex != pdFALSE )\r
                {\r
+                       /* Null is used as the second parameter in this give, and non-NULL\r
+                       in the other gives for code coverage reasons. */\r
                        xSemaphoreGiveFromISR( xISRMutex, NULL );\r
 \r
                        /* Second give attempt should fail. */\r
-                       configASSERT( xSemaphoreGiveFromISR( xISRMutex, NULL ) == pdFAIL );\r
+                       configASSERT( xSemaphoreGiveFromISR( xISRMutex, &xHigherPriorityTaskWoken ) == pdFAIL );\r
+               }\r
+\r
+               if( xOkToGiveCountingSemaphore != pdFALSE )\r
+               {\r
+                       xSemaphoreGiveFromISR( xISRCountingSemaphore, &xHigherPriorityTaskWoken );\r
                }\r
                xLastGiveTime = xTimeNow;\r
        }\r
+\r
+       /* Remove compiler warnings about the value being set but not used. */\r
+       ( void ) xHigherPriorityTaskWoken;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 /* This is called to check that all the created tasks are still running. */\r
 BaseType_t xAreInterruptSemaphoreTasksStillRunning( void )\r
 {\r
-static uint32_t ulLastMasterLoopCounter = 0;\r
+static uint32_t ulLastMasterLoopCounter = 0, ulLastCountingSemaphoreLoops = 0;\r
 \r
        /* If the demo tasks are running then it is expected that the loop counters\r
        will have changed since this function was last called. */\r
@@ -380,6 +551,13 @@ static uint32_t ulLastMasterLoopCounter = 0;
 \r
        ulLastMasterLoopCounter = ulMasterLoops;\r
 \r
+       if( ulLastCountingSemaphoreLoops == ulCountingSemaphoreLoops )\r
+       {\r
+               xErrorDetected = pdTRUE;\r
+       }\r
+\r
+       ulLastCountingSemaphoreLoops = ulCountingSemaphoreLoops++;\r
+\r
        /* Errors detected in the task itself will have latched xErrorDetected\r
        to true. */\r
 \r