embeddedinterruptstm32f4

Using the EXTI line for a software interrupt


I'm using STM32F4, and I want to generate a software interrupt.

How do I know in the interrupt handler if the interrupt was generated by software or by the pin connected to the EXTI line?


Solution

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

    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
                {
                    ...
                }
            }
        }
    }