Samer.M
Samer.M

Reputation: 131

using EXTI Line for software interrupt

I'm using STM32F4 and I want to generate a software interrupt. the question is how do I know in the interrupt handler if the interrupt was generated by software or by the pin connected to the EXTI line?

Upvotes: 4

Views: 8265

Answers (1)

Clifford
Clifford

Reputation: 93466

There are two ways of generating a software interrupt on STM32F4.

  • the generic Cortex-M4 method or writing to the Software Trigger Interrupt Register (STIR), or

  • the STM32 EXTI specific method of writing to the EXTI Software interrupt event register (EXTI_SWIER).

I don't think in the first method the interrupts are distinguishable because STIR is a write-only register. However EXTI_SWIER is r/w and the bit written to trigger the interrupt is not cleared until the corresponding bit in EXTI_PR is explicitly written. It is therefore possible to determine whether the interrupt is software triggered simply by reading EXTI_SWIER.

void EXTI0_IRQHandler(void) 
{
    // Detect SWI
    bool is_swi = (EXTI->SWIER & 0x00000001u) != 0 ;       

    // Clear interrupt flag
    EXTI_ClearITPendingBit(EXTI_Line0);

    if ( is_swi )
    {
        ...
    }
    else
    {
        ...
    }
}

For EXTI lines that share a single interrupt, you would first have to determine the active line by checking the PR register:

void EXTI15_10_IRQn( void )
{
    for( uint32_t exti = 10; exti < 15; exti++ )
    {
        bool is_swi = false ;
        if( EXTI_GetFlagStatus( exti ) == SET )
        {
            is_swi = (EXTI->SWIER & (0x1u << exti)) != 0 ;

            // Clear interrupt flag
            EXTI_ClearITPendingBit( exti ) ;

            if ( is_swi )
            {
                ...
            }
            else
            {
                ...
            }
        }
    }
}

Upvotes: 6

Related Questions