From: rtel Date: Tue, 16 Sep 2014 14:54:32 +0000 (+0000) Subject: Core kernel code: X-Git-Tag: V8.2.0rc1~14 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=f333c34b87b7cd60c58d792f70aa61bbab7947f8;p=freertos Core kernel code: + Introduce xSemaphoreGenericGiveFromISR() as an optimisation when giving semaphores and mutexes from an interrupt. Demo applications: + Update IntSemTest.c to provide more code coverage in xSemaphoreGenericGiveFromISR(). + Ensure the MMU is turned on in the RZ IAR demo. It was already on in the RZ ARM demo. git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2306 1d2547de-c912-0410-9cb9-b8ca96c0e9e2 --- diff --git a/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dbgdt b/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dbgdt index bc1b04294..175b58bf4 100644 --- a/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dbgdt +++ b/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dbgdt @@ -39,7 +39,7 @@ - + TabID-6824-27546 @@ -55,7 +55,7 @@ TabID-11794-23690Find in FilesFind-in-Files - 0 + 0 TabID-17573-27549 @@ -67,20 +67,20 @@ - 0TabID-8721-7680DisassemblyDisassembly0 + 0TabID-8721-7680DisassemblyDisassembly0 - TextEditor$WS_DIR$\..\Source\Full-Demo\main_full.c000006481238123TextEditor$WS_DIR$\..\Source\Blinky-Demo\main_blinky.c000006499799979TextEditor$WS_DIR$\..\Source\main.c0000011260906090TextEditor$WS_DIR$\cstartup.s0000013254565456TextEditor$WS_DIR$\LowLevelInitialise.c0000024134113414TextEditor$WS_DIR$\modules\armv7a_cp15_drv.c0000072720604206040100000010000001 + TextEditor$WS_DIR$\..\Source\Blinky-Demo\main_blinky.c000006400TextEditor$WS_DIR$\..\Source\main.c0000012263486348TextEditor$WS_DIR$\cstartup.s0000012036723672TextEditor$WS_DIR$\LowLevelInitialise.c0000014108010803TextEditor$WS_DIR$\modules\armv7a_cp15_drv.c000005731678016780TextEditor$WS_DIR$\..\Source\LEDs.c000006442474247TextEditor$WS_DIR$\..\Source\RenesasFiles\board_settings\siochar.c000006346694669TextEditor$WS_DIR$\..\Source\Full-Demo\main_full.c0000034017554175540100000010000001 - iaridepm.enu1debuggergui.enu1-2-2718302-2-2200200119048203252180952731707-2-2718699-2-2200200119048203252417262731707-2-21981682-2-216842001002381203252119048203252 + iaridepm.enu1debuggergui.enu1-2-2718302-2-2200200119048203252180952731707-2-2718699-2-2200200119048203252417262731707-2-21981682-2-216842001002381203252119048203252 diff --git a/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dni b/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dni index c22cb97f8..4f7098e1e 100644 --- a/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dni +++ b/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.dni @@ -14,7 +14,7 @@ Watch0=_ 0 "" 0 "" 0 "" 0 "" 0 0 0 0 Watch1=_ 0 "" 0 "" 0 "" 0 "" 0 0 0 0 CStepIntDis=_ 0 [DebugChecksum] -Checksum=-1045357403 +Checksum=72368941 [Jet] JetConnSerialNo=73866 JetConnFoundProbes= @@ -109,7 +109,8 @@ Exclusions= [Disassemble mode] mode=0 [Breakpoints2] -Count=0 +Bp0=_ 1 "EMUL_CODE" "{$PROJ_DIR$\..\Source\main.c}.136.2" 0 0 1 "" 0 "" 0 +Count=1 [Aliases] Count=0 SuppressDialog=0 diff --git a/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.wsdt b/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.wsdt index f5a6e5bd5..1757579d8 100644 --- a/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.wsdt +++ b/FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/IAR/settings/RTOSDemo.wsdt @@ -38,7 +38,7 @@ Workspace - RTOSDemoRTOSDemo/Blinky-Demo + RTOSDemo @@ -58,20 +58,20 @@ TabID-959-438Ambiguous DefinitionsSelect-Ambiguous-DefinitionsTabID-21579-10611Find All DeclarationsFind-All-Declarations - 1 + 3 - TextEditor$WS_DIR$\..\Source\Full-Demo\main_full.c000006481238123TextEditor$WS_DIR$\..\Source\Blinky-Demo\main_blinky.c000006400TextEditor$WS_DIR$\..\Source\main.c0000011200TextEditor$WS_DIR$\cstartup.s0000013254565456TextEditor$WS_DIR$\LowLevelInitialise.c0000014134113414TextEditor$WS_DIR$\modules\armv7a_cp15_drv.c0000072720604206040100000010000001 + TextEditor$WS_DIR$\..\Source\Blinky-Demo\main_blinky.c000006400TextEditor$WS_DIR$\..\Source\main.c0000012263486348TextEditor$WS_DIR$\cstartup.s0000012036723672TextEditor$WS_DIR$\LowLevelInitialise.c0000023106710823TextEditor$WS_DIR$\modules\armv7a_cp15_drv.c0000028385238523TextEditor$WS_DIR$\..\Source\LEDs.c000006442474247TextEditor$WS_DIR$\..\Source\RenesasFiles\board_settings\siochar.c000006346694669TextEditor$WS_DIR$\..\Source\Full-Demo\main_full.c0000034017554175540100000010000001 - iaridepm.enu1-2-2693380-2-2200200119048203252227381706301-2-22471682-2-216842491002381253049119048203252 + iaridepm.enu1-2-2693380-2-2200200119048203252227381706301-2-22471682-2-216842491002381253049119048203252 diff --git a/FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/RTOSDemo.atsuo b/FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/RTOSDemo.atsuo index f2ad135f4..e72f99e07 100644 Binary files a/FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/RTOSDemo.atsuo and b/FreeRTOS/Demo/CORTEX_M4_ATSAM4S_Atmel_Studio/RTOSDemo.atsuo differ diff --git a/FreeRTOS/Demo/Common/Minimal/IntSemTest.c b/FreeRTOS/Demo/Common/Minimal/IntSemTest.c index 310047a2b..973673178 100644 --- a/FreeRTOS/Demo/Common/Minimal/IntSemTest.c +++ b/FreeRTOS/Demo/Common/Minimal/IntSemTest.c @@ -91,6 +91,10 @@ /* A block time of 0 means 'don't block'. */ #define intsemNO_BLOCK 0 +/* The maximum count value for the counting semaphore given from an +interrupt. */ +#define intsemMAX_COUNT 3 + /*-----------------------------------------------------------*/ /* @@ -106,6 +110,26 @@ static void vInterruptMutexSlaveTask( void *pvParameters ); static void vInterruptMutexMasterTask( void *pvParameters ); +/* + * A test whereby the master takes the shared and interrupt mutexes in that + * order, then gives them back in the same order, ensuring the priority + * inheritance is behaving as expected at each step. + */ +static void prvTakeAndGiveInTheSameOrder( void ); + +/* + * A test whereby the master takes the shared and interrupt mutexes in that + * order, then gives them back in the opposite order to which they were taken, + * ensuring the priority inheritance is behaving as expected at each step. + */ +static void prvTakeAndGiveInTheOppositeOrder( void ); + +/* + * A simple task that interacts with an interrupt using a counting semaphore, + * primarily for code coverage purposes. + */ +static void vInterruptCountingSemaphoreTask( void *pvParameters ); + /*-----------------------------------------------------------*/ /* Flag that will be latched to pdTRUE should any unexpected behaviour be @@ -114,7 +138,7 @@ static volatile BaseType_t xErrorDetected = pdFALSE; /* Counters that are incremented on each cycle of a test. This is used to detect a stalled task - a test that is no longer running. */ -static volatile uint32_t ulMasterLoops = 0; +static volatile uint32_t ulMasterLoops = 0, ulCountingSemaphoreLoops = 0; /* Handles of the test tasks that must be accessed from other test tasks. */ static TaskHandle_t xSlaveHandle; @@ -124,6 +148,9 @@ not be used given in interrupts (and definitely never taken in an interrupt) there are some circumstances when it may be desirable. */ static SemaphoreHandle_t xISRMutex = NULL; +/* A counting semaphore which is given from an interrupt. */ +static SemaphoreHandle_t xISRCountingSemaphore = NULL; + /* A mutex which is shared between the master and slave tasks - the master does both sharing of this mutex with the slave and receiving a mutex from the interrupt. */ @@ -132,15 +159,20 @@ static SemaphoreHandle_t xMasterSlaveMutex = NULL; /* Flag that allows the master task to control when the interrupt gives or does not give the mutex. There is no mutual exclusion on this variable, but this is only test code and it should be fine in the 32=bit test environment. */ -static BaseType_t xOkToGiveMutex = pdFALSE; +static BaseType_t xOkToGiveMutex = pdFALSE, xOkToGiveCountingSemaphore = pdFALSE; + +/* Used to coordinate timing between tasks and the interrupt. */ +const TickType_t xInterruptGivePeriod = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ); /*-----------------------------------------------------------*/ void vStartInterruptSemaphoreTasks( void ) { - /* Create the mutex that is given from an interrupt. */ + /* Create the semaphores that are given from an interrupt. */ xISRMutex = xSemaphoreCreateMutex(); configASSERT( xISRMutex ); + xISRCountingSemaphore = xSemaphoreCreateCounting( intsemMAX_COUNT, 0 ); + configASSERT( xISRCountingSemaphore ); /* Create the mutex that is shared between the master and slave tasks (the master receives a mutex from an interrupt and shares a mutex with the @@ -151,168 +183,226 @@ void vStartInterruptSemaphoreTasks( void ) /* Create the tasks that share mutexes between then and with interrupts. */ xTaskCreate( vInterruptMutexSlaveTask, "IntMuS", configMINIMAL_STACK_SIZE, NULL, intsemSLAVE_PRIORITY, &xSlaveHandle ); xTaskCreate( vInterruptMutexMasterTask, "IntMuM", configMINIMAL_STACK_SIZE, NULL, intsemMASTER_PRIORITY, NULL ); + + /* Create the task that blocks on the counting semaphore. */ + xTaskCreate( vInterruptCountingSemaphoreTask, "IntCnt", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL ); } /*-----------------------------------------------------------*/ static void vInterruptMutexMasterTask( void *pvParameters ) { -const TickType_t xInterruptGivePeriod = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ); - /* Just to avoid compiler warnings. */ ( void ) pvParameters; for( ;; ) { - /* Ensure the slave is suspended, and that this task is running at the - lower priority as expected as the start conditions. */ - #if( INCLUDE_eTaskGetState == 1 ) - { - configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended ); - } - #endif /* INCLUDE_eTaskGetState */ + prvTakeAndGiveInTheSameOrder(); - if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY ) - { - xErrorDetected = pdTRUE; - } + /* Ensure not to starve out other tests. */ + ulMasterLoops++; + vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ); - /* Take the semaphore that is shared with the slave. */ - if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS ) - { - xErrorDetected = pdTRUE; - } + prvTakeAndGiveInTheOppositeOrder(); - /* This task now has the mutex. Unsuspend the slave so it too - attempts to take the mutex. */ - vTaskResume( xSlaveHandle ); + /* Ensure not to starve out other tests. */ + ulMasterLoops++; + vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ); + } +} +/*-----------------------------------------------------------*/ - /* The slave has the higher priority so should now have executed and - blocked on the semaphore. */ - #if( INCLUDE_eTaskGetState == 1 ) - { - configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked ); - } - #endif /* INCLUDE_eTaskGetState */ +static void prvTakeAndGiveInTheSameOrder( void ) +{ + /* Ensure the slave is suspended, and that this task is running at the + lower priority as expected as the start conditions. */ + #if( INCLUDE_eTaskGetState == 1 ) + { + configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended ); + } + #endif /* INCLUDE_eTaskGetState */ - /* This task should now have inherited the priority of the slave - task. */ - if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) - { - xErrorDetected = pdTRUE; - } + if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY ) + { + xErrorDetected = pdTRUE; + } - /* Now wait a little longer than the time between ISR gives to also - obtain the ISR mutex. */ - xOkToGiveMutex = pdTRUE; - if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS ) - { - xErrorDetected = pdTRUE; - } - xOkToGiveMutex = pdFALSE; + /* Take the semaphore that is shared with the slave. */ + if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } - /* Attempting to take again immediately should fail as the mutex is - already held. */ - if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL ) - { - xErrorDetected = pdTRUE; - } + /* This task now has the mutex. Unsuspend the slave so it too + attempts to take the mutex. */ + vTaskResume( xSlaveHandle ); - /* Should still be at the priority of the slave task. */ - if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) - { - xErrorDetected = pdTRUE; - } + /* The slave has the higher priority so should now have executed and + blocked on the semaphore. */ + #if( INCLUDE_eTaskGetState == 1 ) + { + configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked ); + } + #endif /* INCLUDE_eTaskGetState */ - /* Give back the ISR semaphore to ensure the priority is not - disinherited as the shared mutex (which the higher priority task is - attempting to obtain) is still held. */ - if( xSemaphoreGive( xISRMutex ) != pdPASS ) - { - xErrorDetected = pdTRUE; - } + /* This task should now have inherited the priority of the slave + task. */ + if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) + { + xErrorDetected = pdTRUE; + } - if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) - { - xErrorDetected = pdTRUE; - } + /* Now wait a little longer than the time between ISR gives to also + obtain the ISR mutex. */ + xOkToGiveMutex = pdTRUE; + if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } + xOkToGiveMutex = pdFALSE; - /* Finally give back the shared mutex. This time the higher priority - task should run before this task runs again - so this task should have - disinherited the priority and the higher priority task should be in the - suspended state again. */ - if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS ) - { - xErrorDetected = pdTRUE; - } + /* Attempting to take again immediately should fail as the mutex is + already held. */ + if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL ) + { + xErrorDetected = pdTRUE; + } - if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY ) - { - xErrorDetected = pdTRUE; - } + /* Should still be at the priority of the slave task. */ + if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) + { + xErrorDetected = pdTRUE; + } - #if( INCLUDE_eTaskGetState == 1 ) - { - configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended ); - } - #endif /* INCLUDE_eTaskGetState */ + /* Give back the ISR semaphore to ensure the priority is not + disinherited as the shared mutex (which the higher priority task is + attempting to obtain) is still held. */ + if( xSemaphoreGive( xISRMutex ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } - /* Ensure not to starve out other tests. */ - ulMasterLoops++; - vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ); + if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) + { + xErrorDetected = pdTRUE; + } + /* Finally give back the shared mutex. This time the higher priority + task should run before this task runs again - so this task should have + disinherited the priority and the higher priority task should be in the + suspended state again. */ + if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } - /* Repeat exactly up to the point where the mutexes are given back. - This time the shared mutex is given back first. */ - if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS ) - { - xErrorDetected = pdTRUE; - } + if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY ) + { + xErrorDetected = pdTRUE; + } - vTaskResume( xSlaveHandle ); + #if( INCLUDE_eTaskGetState == 1 ) + { + configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended ); + } + #endif /* INCLUDE_eTaskGetState */ - #if( INCLUDE_eTaskGetState == 1 ) - { - configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked ); - } - #endif /* INCLUDE_eTaskGetState */ + /* Reset the mutex ready for the next round. */ + xQueueReset( xISRMutex ); +} +/*-----------------------------------------------------------*/ - if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) - { - xErrorDetected = pdTRUE; - } +static void prvTakeAndGiveInTheOppositeOrder( void ) +{ + /* Ensure the slave is suspended, and that this task is running at the + lower priority as expected as the start conditions. */ + #if( INCLUDE_eTaskGetState == 1 ) + { + configASSERT( eTaskGetState( xSlaveHandle ) == eSuspended ); + } + #endif /* INCLUDE_eTaskGetState */ - xOkToGiveMutex = pdTRUE; - if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS ) - { - xErrorDetected = pdTRUE; - } - xOkToGiveMutex = pdFALSE; + if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY ) + { + xErrorDetected = pdTRUE; + } - if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) - { - xErrorDetected = pdTRUE; - } + /* Take the semaphore that is shared with the slave. */ + if( xSemaphoreTake( xMasterSlaveMutex, intsemNO_BLOCK ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } - /* This is where the differences start as this time the shared mutex is - given back first. This time to the higher priority task should run - before this task gets to the point of releasing the interrupt mutex - so - this task should have disinherited the priority and the higher priority - task should be in the suspended state again. */ - if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS ) - { - xErrorDetected = pdTRUE; - } + /* This task now has the mutex. Unsuspend the slave so it too + attempts to take the mutex. */ + vTaskResume( xSlaveHandle ); - /* Give back the interrupt semaphore too, so the mutex held count goes - back to 0. The mutex will then have to be reset so the ISR can give it - in the next cycle. */ - xSemaphoreGive( xISRMutex ); - xQueueReset( ( QueueHandle_t ) xISRMutex ); + /* The slave has the higher priority so should now have executed and + blocked on the semaphore. */ + #if( INCLUDE_eTaskGetState == 1 ) + { + configASSERT( eTaskGetState( xSlaveHandle ) == eBlocked ); + } + #endif /* INCLUDE_eTaskGetState */ - /* Ensure not to starve out other tests. */ - ulMasterLoops++; - vTaskDelay( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ); + /* This task should now have inherited the priority of the slave + task. */ + if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) + { + xErrorDetected = pdTRUE; + } + + /* Now wait a little longer than the time between ISR gives to also + obtain the ISR mutex. */ + xOkToGiveMutex = pdTRUE; + if( xSemaphoreTake( xISRMutex, ( xInterruptGivePeriod * 2 ) ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } + xOkToGiveMutex = pdFALSE; + + /* Attempting to take again immediately should fail as the mutex is + already held. */ + if( xSemaphoreTake( xISRMutex, intsemNO_BLOCK ) != pdFAIL ) + { + xErrorDetected = pdTRUE; + } + + /* Should still be at the priority of the slave task. */ + if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) + { + xErrorDetected = pdTRUE; + } + + /* Give back the shared semaphore to ensure the priority is not disinherited + as the ISR mutex is still held. The higher priority slave task should run + before this task runs again. */ + if( xSemaphoreGive( xMasterSlaveMutex ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } + + /* Should still be at the priority of the slave task as this task still + holds one semaphore (this is a simplification in the priority inheritance + mechanism. */ + if( uxTaskPriorityGet( NULL ) != intsemSLAVE_PRIORITY ) + { + xErrorDetected = pdTRUE; } + + /* Give back the ISR semaphore, which should result in the priority being + disinherited as it was the last mutex held. */ + if( xSemaphoreGive( xISRMutex ) != pdPASS ) + { + xErrorDetected = pdTRUE; + } + + if( uxTaskPriorityGet( NULL ) != intsemMASTER_PRIORITY ) + { + xErrorDetected = pdTRUE; + } + + /* Reset the mutex ready for the next round. */ + xQueueReset( xISRMutex ); } /*-----------------------------------------------------------*/ @@ -343,9 +433,76 @@ static void vInterruptMutexSlaveTask( void *pvParameters ) } /*-----------------------------------------------------------*/ +static void vInterruptCountingSemaphoreTask( void *pvParameters ) +{ +BaseType_t xCount; +const TickType_t xDelay = pdMS_TO_TICKS( intsemINTERRUPT_MUTEX_GIVE_PERIOD_MS ) * ( intsemMAX_COUNT + 1 ); + + ( void ) pvParameters; + + for( ;; ) + { + /* Expect to start with the counting semaphore empty. */ + if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 ) + { + xErrorDetected = pdTRUE; + } + + /* Wait until it is expected that the interrupt will have filled the + counting semaphore. */ + xOkToGiveCountingSemaphore = pdTRUE; + vTaskDelay( xDelay ); + xOkToGiveCountingSemaphore = pdFALSE; + + /* Now it is expected that the counting semaphore is full. */ + if( uxQueueMessagesWaiting( ( QueueHandle_t ) xISRCountingSemaphore ) != intsemMAX_COUNT ) + { + xErrorDetected = pdTRUE; + } + + if( uxQueueSpacesAvailable( ( QueueHandle_t ) xISRCountingSemaphore ) != 0 ) + { + xErrorDetected = pdTRUE; + } + + ulCountingSemaphoreLoops++; + + /* Expect to be able to take the counting semaphore intsemMAX_COUNT + times. A block time of 0 is used as the semaphore should already be + there. */ + xCount = 0; + while( xSemaphoreTake( xISRCountingSemaphore, 0 ) == pdPASS ) + { + xCount++; + } + + if( xCount != intsemMAX_COUNT ) + { + xErrorDetected = pdTRUE; + } + + /* Now raise the priority of this task so it runs immediately that the + semaphore is given from the interrupt. */ + vTaskPrioritySet( NULL, configMAX_PRIORITIES - 1 ); + + /* Block to wait for the semaphore to be given from the interrupt. */ + xOkToGiveCountingSemaphore = pdTRUE; + xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY ); + xSemaphoreTake( xISRCountingSemaphore, portMAX_DELAY ); + xOkToGiveCountingSemaphore = pdFALSE; + + /* Reset the priority so as not to disturbe other tests too much. */ + vTaskPrioritySet( NULL, tskIDLE_PRIORITY ); + + ulCountingSemaphoreLoops++; + } +} +/*-----------------------------------------------------------*/ + void vInterruptSemaphorePeriodicTest( void ) { static TickType_t xLastGiveTime = 0; +BaseType_t xHigherPriorityTaskWoken = pdFALSE; TickType_t xTimeNow; /* No mutual exclusion on xOkToGiveMutex, but this is only test code (and @@ -356,20 +513,30 @@ TickType_t xTimeNow; configASSERT( xISRMutex ); if( xOkToGiveMutex != pdFALSE ) { + /* Null is used as the second parameter in this give, and non-NULL + in the other gives for code coverage reasons. */ xSemaphoreGiveFromISR( xISRMutex, NULL ); /* Second give attempt should fail. */ - configASSERT( xSemaphoreGiveFromISR( xISRMutex, NULL ) == pdFAIL ); + configASSERT( xSemaphoreGiveFromISR( xISRMutex, &xHigherPriorityTaskWoken ) == pdFAIL ); + } + + if( xOkToGiveCountingSemaphore != pdFALSE ) + { + xSemaphoreGiveFromISR( xISRCountingSemaphore, &xHigherPriorityTaskWoken ); } xLastGiveTime = xTimeNow; } + + /* Remove compiler warnings about the value being set but not used. */ + ( void ) xHigherPriorityTaskWoken; } /*-----------------------------------------------------------*/ /* This is called to check that all the created tasks are still running. */ BaseType_t xAreInterruptSemaphoreTasksStillRunning( void ) { -static uint32_t ulLastMasterLoopCounter = 0; +static uint32_t ulLastMasterLoopCounter = 0, ulLastCountingSemaphoreLoops = 0; /* If the demo tasks are running then it is expected that the loop counters will have changed since this function was last called. */ @@ -380,6 +547,13 @@ static uint32_t ulLastMasterLoopCounter = 0; ulLastMasterLoopCounter = ulMasterLoops; + if( ulLastCountingSemaphoreLoops == ulCountingSemaphoreLoops ) + { + xErrorDetected = pdTRUE; + } + + ulLastCountingSemaphoreLoops = ulCountingSemaphoreLoops++; + /* Errors detected in the task itself will have latched xErrorDetected to true. */ diff --git a/FreeRTOS/Source/include/queue.h b/FreeRTOS/Source/include/queue.h index b4553b310..b62f1853a 100644 --- a/FreeRTOS/Source/include/queue.h +++ b/FreeRTOS/Source/include/queue.h @@ -1269,7 +1269,8 @@ uint32_t ulVarToSend, ulValReceived; * * It is preferred that the macros xQueueSendFromISR(), * xQueueSendToFrontFromISR() and xQueueSendToBackFromISR() be used in place - * of calling this function directly. + * of calling this function directly. xQueueGenericGiveFromISR() is an + * equivalent for use by semaphores that don't actually copy any data. * * Post an item on a queue. It is safe to use this function from within an * interrupt service routine. @@ -1333,6 +1334,7 @@ uint32_t ulVarToSend, ulValReceived; * \ingroup QueueManagement */ BaseType_t xQueueGenericSendFromISR( QueueHandle_t xQueue, const void * const pvItemToQueue, BaseType_t * const pxHigherPriorityTaskWoken, const BaseType_t xCopyPosition ) PRIVILEGED_FUNCTION; +BaseType_t xQueueGenericGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken ) PRIVILEGED_FUNCTION; /** * queue. h diff --git a/FreeRTOS/Source/include/semphr.h b/FreeRTOS/Source/include/semphr.h index 96d483c6e..612aaaf75 100644 --- a/FreeRTOS/Source/include/semphr.h +++ b/FreeRTOS/Source/include/semphr.h @@ -605,7 +605,7 @@ typedef QueueHandle_t SemaphoreHandle_t; * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR * \ingroup Semaphores */ -#define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK ) +#define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGenericGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) ) /** * semphr. h diff --git a/FreeRTOS/Source/queue.c b/FreeRTOS/Source/queue.c index 12a79f93c..2bdbdcf2b 100644 --- a/FreeRTOS/Source/queue.c +++ b/FreeRTOS/Source/queue.c @@ -1074,20 +1074,166 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue; { traceQUEUE_SEND_FROM_ISR( pxQueue ); - if( prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition ) != pdFALSE ) + /* A task can only have an inherited priority if it is a mutex + holder - and if there is a mutex holder then the mutex cannot be + given from an ISR. Therefore, unlike the xQueueGenericGive() + function, there is no need to determine the need for priority + disinheritance here or to clear the mutex holder TCB member. */ + ( void ) prvCopyDataToQueue( pxQueue, pvItemToQueue, xCopyPosition ); + + /* The event list is not altered if the queue is locked. This will + be done when the queue is unlocked later. */ + if( pxQueue->xTxLock == queueUNLOCKED ) { - /* This is a special case that can only be executed if a task - holds multiple mutexes and then gives the mutexes back in an - order that is different to that in which they were taken. */ - if( pxHigherPriorityTaskWoken != NULL ) + #if ( configUSE_QUEUE_SETS == 1 ) { - *pxHigherPriorityTaskWoken = pdTRUE; + if( pxQueue->pxQueueSetContainer != NULL ) + { + if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE ) + { + /* The queue is a member of a queue set, and posting + to the queue set caused a higher priority task to + unblock. A context switch is required. */ + if( pxHigherPriorityTaskWoken != NULL ) + { + *pxHigherPriorityTaskWoken = pdTRUE; + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + } + else + { + if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE ) + { + if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE ) + { + /* The task waiting has a higher priority so + record that a context switch is required. */ + if( pxHigherPriorityTaskWoken != NULL ) + { + *pxHigherPriorityTaskWoken = pdTRUE; + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + } } - else + #else /* configUSE_QUEUE_SETS */ { - mtCOVERAGE_TEST_MARKER(); + if( listLIST_IS_EMPTY( &( pxQueue->xTasksWaitingToReceive ) ) == pdFALSE ) + { + if( xTaskRemoveFromEventList( &( pxQueue->xTasksWaitingToReceive ) ) != pdFALSE ) + { + /* The task waiting has a higher priority so record that a + context switch is required. */ + if( pxHigherPriorityTaskWoken != NULL ) + { + *pxHigherPriorityTaskWoken = pdTRUE; + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + } + else + { + mtCOVERAGE_TEST_MARKER(); + } + } + else + { + mtCOVERAGE_TEST_MARKER(); + } } + #endif /* configUSE_QUEUE_SETS */ } + else + { + /* Increment the lock count so the task that unlocks the queue + knows that data was posted while it was locked. */ + ++( pxQueue->xTxLock ); + } + + xReturn = pdPASS; + } + else + { + traceQUEUE_SEND_FROM_ISR_FAILED( pxQueue ); + xReturn = errQUEUE_FULL; + } + } + portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); + + return xReturn; +} +/*-----------------------------------------------------------*/ + +BaseType_t xQueueGenericGiveFromISR( QueueHandle_t xQueue, BaseType_t * const pxHigherPriorityTaskWoken ) +{ +BaseType_t xReturn; +UBaseType_t uxSavedInterruptStatus; +Queue_t * const pxQueue = ( Queue_t * ) xQueue; + + configASSERT( pxQueue ); + + /* xQueueGenericSendFromISR() should be used in the item size is not 0. */ + configASSERT( pxQueue->uxItemSize == 0 ); + + /* RTOS ports that support interrupt nesting have the concept of a maximum + system call (or maximum API call) interrupt priority. Interrupts that are + above the maximum system call priority are kept permanently enabled, even + when the RTOS kernel is in a critical section, but cannot make any calls to + FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h + then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion + failure if a FreeRTOS API function is called from an interrupt that has been + assigned a priority above the configured maximum system call priority. + Only FreeRTOS functions that end in FromISR can be called from interrupts + that have been assigned a priority at or (logically) below the maximum + system call interrupt priority. FreeRTOS maintains a separate interrupt + safe API to ensure interrupt entry is as fast and as simple as possible. + More information (albeit Cortex-M specific) is provided on the following + link: http://www.freertos.org/RTOS-Cortex-M3-M4.html */ + portASSERT_IF_INTERRUPT_PRIORITY_INVALID(); + + /* Similar to xQueueGenericSendFromISR() but used with semaphores where the + item size is 0. Don't directly wake a task that was blocked on a queue + read, instead return a flag to say whether a context switch is required or + not (i.e. has a task with a higher priority than us been woken by this + post). */ + uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR(); + { + /* When the queue is used to implement a semaphore no data is ever + moved through the queue but it is still valid to see if the queue 'has + space'. */ + if( pxQueue->uxMessagesWaiting < pxQueue->uxLength ) + { + traceQUEUE_SEND_FROM_ISR( pxQueue ); + + /* A task can only have an inherited priority if it is a mutex + holder - and if there is a mutex holder then the mutex cannot be + given from an ISR. Therefore, unlike the xQueueGenericGive() + function, there is no need to determine the need for priority + disinheritance here or to clear the mutex holder TCB member. */ + + ++( pxQueue->uxMessagesWaiting ); /* The event list is not altered if the queue is locked. This will be done when the queue is unlocked later. */ @@ -1097,11 +1243,11 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue; { if( pxQueue->pxQueueSetContainer != NULL ) { - if( prvNotifyQueueSetContainer( pxQueue, xCopyPosition ) == pdTRUE ) + if( prvNotifyQueueSetContainer( pxQueue, queueSEND_TO_BACK ) == pdTRUE ) { - /* The queue is a member of a queue set, and posting - to the queue set caused a higher priority task to - unblock. A context switch is required. */ + /* The semaphore is a member of a queue set, and + posting to the queue set caused a higher priority + task to unblock. A context switch is required. */ if( pxHigherPriorityTaskWoken != NULL ) { *pxHigherPriorityTaskWoken = pdTRUE; @@ -1188,7 +1334,7 @@ Queue_t * const pxQueue = ( Queue_t * ) xQueue; xReturn = errQUEUE_FULL; } } - portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); + portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus ); //0.36 return xReturn; } diff --git a/FreeRTOS/Source/tasks.c b/FreeRTOS/Source/tasks.c index 010a05baf..565b65c34 100644 --- a/FreeRTOS/Source/tasks.c +++ b/FreeRTOS/Source/tasks.c @@ -3254,8 +3254,11 @@ TCB_t *pxTCB; /* Only disinherit if no other mutexes are held. */ if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 ) { - /* The holding task must be the running task to be able to give - the mutex back. Remove the holding task from the ready list. */ + /* A task can only have an inhertied priority if it holds + the mutex. If the mutex is held by a task then it cannot be + given from an interrupt, and if a mutex is given by the + holding task then it must be the running state task. Remove + the holding task from the ready list. */ if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 ) { taskRESET_READY_PRIORITY( pxTCB->uxPriority ); @@ -3265,8 +3268,8 @@ TCB_t *pxTCB; mtCOVERAGE_TEST_MARKER(); } - /* Disinherit the priority before adding the task into the new - ready list. */ + /* Disinherit the priority before adding the task into the + new ready list. */ traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority ); pxTCB->uxPriority = pxTCB->uxBasePriority; @@ -3279,7 +3282,11 @@ TCB_t *pxTCB; /* Return true to indicate that a context switch is required. This is only actually required in the corner case whereby multiple mutexes were held and the mutexes were given back - in an order different to that in which they were taken. */ + in an order different to that in which they were taken. + If a context switch did not occur when the first mutex was + returned, even if a task was waiting on it, then a context + switch should occur when the last mutex is returned whether + a task is waiting on it or not. */ xReturn = pdTRUE; } else