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?


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
    \$\begingroup\$ What if the pins are from 2 different ports? like 5A and 5B \$\endgroup\$ – Arun Joe Apr 10 '17 at 14:43
  • 4
    \$\begingroup\$ You can't do that. See the description of SYSCFG->EXTICR, EXTI5 comes either from PA5 or PB5, both is not possible. \$\endgroup\$ – berendi Apr 10 '17 at 14:50
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Flanker Apr 11 '17 at 15:16
  • \$\begingroup\$ @Flanker, what's C&R? \$\endgroup\$ – pokrface Feb 16 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.