Skip to main content
dchen
Associate III
January 23, 2023
Solved

In EXTI, how to know which port generates the interrupt?

  • January 23, 2023
  • 3 replies
  • 5287 views

Hi,

After reading the document I understand the interrupts are handled by pin#, and we set GPIOsel within EXTI_ConfigTypeDef.

My question is how do we determine which port generates the interrupt?

Suppose I use PA0 and PB0, and I only want to use one callback function for them.

void callback()

{

if(port == GPIOA)

{

do something

}

else if(port == GPIOB)

{

do other thing

}

}

Thank you

This topic has been closed for replies.
Best answer by PatrickF

Hi, unfortunately, EXTI cannot be programmed for PA0 and PB0 (you should have seen this restriction when you started your design with CubeMX).

Some possible solutions (just for your information, not tested):

  • move one pin to another GPIO (e.g. move PB0 to PB1)
  • If you know when you are expecting the interrupt for each pin (which is rarely the case), then you could reprogram EXTI whenever needed.
  • You could merge externally the two lanes to same pin (gate or diodes if slow enough) for interrupt purposes while you still connect the two to distinct port to check pin level in interrupt routine (risk of 'ghost' interrupt if disappear before been handled)
  • You could use IO expander with interrupt feature (usually control by I2C)
  • In the particular case of PA0, you might be lucky as you could probably use WKP1 feature (thru PWR, not EXTI) as workaround (and keep PB0 to EXTI).

Regards.

3 replies

dchen
dchenAuthor
Associate III
January 23, 2023

I know one way is to set EXTI_ConfigTypeDef exti_config as a global variable, and pass it into the callback function. Is there another way to do it?

PatrickF
Technical Moderator
January 23, 2023

Hi,

your example wont fly as you cannot have at same time EXTI0 set for PA0 and PB0.

If you MUXSEL is not static (i.e. dynamic SW changes from PA0 to PB0 and vice-versa), in the interrupt routine, you could check the content of EXTI_EXTICR1.EXTI0 (example for EXTI0) to know if interrupt was set to PA0, PB0, PC0, etc. This could save you some global variable.

Regards.

In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.NEW ! Sidekick STM32 AI agent, see here
dchen
dchenAuthor
Associate III
January 23, 2023

Thanks Patrick,

If I want PA0 and PB0 both can generate interrupt, is there a way to do it?

dchen
dchenAuthor
Associate III
January 23, 2023

These two interrupts won't be triggered at same time.

dchen
dchenAuthor
Associate III
January 23, 2023

@PatrickF​