4

I'm trying to interface a few modules to my STM32L476 board for which I need to enable two GPIO interrupts from the same port (portA, pin 5 and portA, pin 6), but the interrupt handler for these pins are handled by an external line common for pins 5 to 9 (EXTI9_5_IRQHandler).

I need to do a different task on both these interrupts, but how would I know which interrupt has occurred as both are handled by same handler? Are there any flags that I could check to know this?

Peter Mortensen
  • 1,676
  • 3
  • 17
  • 23
Arun Joe Cheriyan
  • 707
  • 2
  • 8
  • 22

1 Answers1

8

Read the EXTI->PR1 register to decide

void EXTI9_5_IRQHandler(void) {
    uint32_t pending = EXTI->PR1;
    if(pending & (1 << 5)) {
        EXTI->PR1 = 1 << 5; // clear pending flag, otherwise we'd get endless interrupts
        // handle pin 5 here
    }
    if(pending & (1 << 6)) {
        EXTI->PR1 = 1 << 6;
        // handle pin 6 here
    }
}
  • 1
    What if the pins are from 2 different ports? like 5A and 5B – Arun Joe Cheriyan Apr 10 '17 at 14:43
  • 4
    You can't do that. See the description of `SYSCFG->EXTICR`, `EXTI5` comes either from `PA5` or `PB5`, both is not possible. – followed Monica to Codidact Apr 10 '17 at 14:50
  • 1
    You can store previous (last) states for GPIO_A5 and GPIO_B5, in the ISR check which one was changed. Surely, update the variable with last state upon exiting. Might not work for very short pulse, when pin state is changed back before ISR samples it. If period between pulses are known to be big enough, you can "stretch" very short pulse width with C & R. – Flanker Apr 11 '17 at 15:16
  • 1
    @Flanker, what's C&R? – scriptsNgiggles Feb 16 '19 at 12:46