STM32F4Discovery External Interrupt

To configure an external interrupt one must configure the external interrupt (EXTI) peripheral as well as the NVIC peripheral. The general procedure is as follows:

  • Configure the EXTIxx bits in the SYSCFG_EXTICRx registers to map the GPIO pin(s) of interest to the appropriate external interrupt lines (EXTI0-EXTI15).





  • For the external interrupt lines (EXTIxx) of interest, choose a signal change that will trigger the external interrupt. The signal change can be a rising edge, a falling edge or both. These can be set via the EXTI_RTSR (rising) and the EXTI_FTSR (falling) registers.

  • Unmask the external interrupt line(s) of interest, by setting the bit corresponding to the EXTI line of interest in the EXT_IMR register.

  • Set the priority for the interrupt vector in question in the NVIC either via the CMSIS based  “NVIC_SetPriority()” function or through the IPR0-IPR7 registers.

  • Enable the interrupt in the NVIC either via the CMSIS based “NVIC_EnableIRQ()” function or via the ISER register. 

  •   Note: EXTI1_IRQn to EXTI4_IRQn is used for EXT1 to EXT4
                EXTI9_5_IRQn is used for EXT5 to EXT9
                EXTI15_10_IRQn is used for EXT10 to EXT15


  • Write your interrupt service routine (ISR). 

  • Inside your interrupt service routine, check the source of the interrupt…either the GPIO pin directly or the external interrupt line. Once you figure out which one triggered the interrupt, perform the interrupt processing scheme associated with it. Make sure that you clear the corresponding pending bit of the external interrupt lines of interest in the EXT_PR (external interrupt pending register) register by writing a '1' to it. 


  • Example code: configure PB1 as interrupt pin
    Note: this code is used in uVsion which can be downloaded in https://www.keil.com/demo/eval/armv4.htm
    -------------------------------------------------------------------------------------------------
    #include <stdio.h>
    #include <stm32f4xx.h>
    
    uint32_t i=0;
    
    void EXTI1_IRQHandler(void)
    {
     if(EXTI->PR & (1<<1))
        {
            i++; //increase value if interrupt happen
        }
     EXTI->PR=(1<<1); //clear interrupt flag for EXTI1
    }
    
    void exticonf(void)
    {
     //Config PB1 as interrupt pin
     NVIC_EnableIRQ(EXTI1_IRQn); // Enable IRQ for ext. signals, line EXTI1_IRQn
     //NVIC_EnableIRQ(EXTI9_5_IRQn); //External Line[9:5] Interrupts 
     //NVIC_EnableIRQ(EXTI15_10_IRQn); //External Line[15:10] Interrupts
     NVIC_SetPriority(EXTI1_IRQn, 15);
     SYSCFG->EXTICR[0] = SYSCFG_EXTICR1_EXTI1_PB; // select PB to make IRQ EXTI1
     EXTI->RTSR = 0x00000002; // allow positive edge interrupt for EXTI1
     EXTI->IMR = 0x00000002; // enable interrupt on EXTI1
    }
    
    int main (void) {
     RCC->APB2ENR |= 0x00004000; // Clock SYSCFG - system configuration controller, necessary for interrupt
     exticonf();
            while(1) { };
    }
    ------------------------------------------------------------------------------------------------------




    Reference site:
    http://www.fmf.uni-lj.si/
    http://homepage.cem.itesm.mx/


    0 Comments: