STM32 External Interrupts.
The advantage of external interrupts is that they detect a transition from one logic level to another, not the level itself.
Suppose we need to detect when the voltage on some pin changes and urgently execute a certain set of instructions. The simplest algorithm looks like this: at certain intervals we poll the pin and check whether its state has changed. If the time between polls is long, we may miss a transition; if it's short, most of the processor time will be spent polling the pin and waiting for the event. Clearly, this algorithm doesn't suit us. What should we do? The solution to this problem was invented long ago by the engineers who design microcontrollers, and it's called the external interrupt.
How does it work? When the logic level on a microcontroller pin changes, it drops everything and runs to execute the instructions located at a given address; our job is to place the required code at that address.
Now let's move from the general theory to specific implementations. On AVR, specific pins are dedicated to external interrupts; the Atmega16 has 4 of them. On STM32, external interrupts are more interesting: they are managed by the EXTI controller. Any port pin can be configured for external interrupts, but the number of GPIO pins configured for external interrupts at the same time can't exceed sixteen. These pins are connected to EXTI via multiplexers.

If you look at the picture with the multiplexers, you can see that the multiplexer number matches the pin number, and we can't configure two pins with the same number for interrupts, i.e. PB0 and PA0 can't be configured for external interrupts at the same time.
There are 4 more lines that are connected not to GPIO but to peripherals:
Besides ordinary interrupts, the EXTI controller can generate events and software interrupts.
Event — a flag is set without jumping to the interrupt handler.
Software interrupt — the same interrupt, but triggered manually by writing '1' to the corresponding register.
There are only seven external interrupt handlers: EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI9_5, EXTI15_10. You can see that not every external interrupt from GPIO has a separate handler; some of them are combined.
Let's look at the registers needed to configure external interrupts.
First, you need to enable clocking of the port and alternate functions, since the interrupt is an alternate function of the port.
A pin configured for external interrupts can be configured as a floating input or an input with pull-up/pull-down.
Four AFIO_EXTICRx registers are provided for configuring the multiplexers, each divided into 4 sections of 4 bits; let's look at one of them.

To configure pin zero for external interrupts, you need to write the port code to the EXTI0 section.
EXTI_IMR - the interrupt mask register; writing '1' to the corresponding bit enables the interrupt; by default it contains zeros.
EXTI_EMR – the event mask register; writing '1' to the corresponding bit enables the event; by default it contains zeros.
EXTI_FTSR and EXTI_RTSR – determine on which edge the interrupt occurs. Writing '1' to the corresponding bit of EXTI_FTSR selects the falling edge, of EXTI_RTSR the rising edge.
EXTI_SWIER – software triggering of an event/interrupt; writing '1' to the corresponding bit generates the event/interrupt.
EXTI_PR – the event/interrupt pending flag; it is cleared manually by writing '1'.
Now let's look at a simple example in which pressing a button connected to pin zero of port E generates an interrupt.
Suppose we need to detect when the voltage on some pin changes and urgently execute a certain set of instructions. The simplest algorithm looks like this: at certain intervals we poll the pin and check whether its state has changed. If the time between polls is long, we may miss a transition; if it's short, most of the processor time will be spent polling the pin and waiting for the event. Clearly, this algorithm doesn't suit us. What should we do? The solution to this problem was invented long ago by the engineers who design microcontrollers, and it's called the external interrupt.
How does it work? When the logic level on a microcontroller pin changes, it drops everything and runs to execute the instructions located at a given address; our job is to place the required code at that address.
Now let's move from the general theory to specific implementations. On AVR, specific pins are dedicated to external interrupts; the Atmega16 has 4 of them. On STM32, external interrupts are more interesting: they are managed by the EXTI controller. Any port pin can be configured for external interrupts, but the number of GPIO pins configured for external interrupts at the same time can't exceed sixteen. These pins are connected to EXTI via multiplexers.

If you look at the picture with the multiplexers, you can see that the multiplexer number matches the pin number, and we can't configure two pins with the same number for interrupts, i.e. PB0 and PA0 can't be configured for external interrupts at the same time.
There are 4 more lines that are connected not to GPIO but to peripherals:
- EXTI 17 – PVD output
- EXTI 18 – RTC_Alarm event
- EXTI 19 – USB_Wakeup event
- EXTI 20 – Ethernet_Wakeup event
Besides ordinary interrupts, the EXTI controller can generate events and software interrupts.
Event — a flag is set without jumping to the interrupt handler.
Software interrupt — the same interrupt, but triggered manually by writing '1' to the corresponding register.
There are only seven external interrupt handlers: EXTI0, EXTI1, EXTI2, EXTI3, EXTI4, EXTI9_5, EXTI15_10. You can see that not every external interrupt from GPIO has a separate handler; some of them are combined.
Let's look at the registers needed to configure external interrupts.
First, you need to enable clocking of the port and alternate functions, since the interrupt is an alternate function of the port.
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN | RCC_APB2ENR_IOPEEN;
A pin configured for external interrupts can be configured as a floating input or an input with pull-up/pull-down.
Four AFIO_EXTICRx registers are provided for configuring the multiplexers, each divided into 4 sections of 4 bits; let's look at one of them.

To configure pin zero for external interrupts, you need to write the port code to the EXTI0 section.
//select the port and pin for the external interrupt
AFIO->EXTICR [0] |= AFIO_EXTICR1_EXTI0_PE;
//if we want to select the first pin of port B
AFIO->EXTICR [0] |= AFIO_EXTICR1_EXTI1_PB;
EXTI_IMR - the interrupt mask register; writing '1' to the corresponding bit enables the interrupt; by default it contains zeros.
EXTI_EMR – the event mask register; writing '1' to the corresponding bit enables the event; by default it contains zeros.
EXTI_FTSR and EXTI_RTSR – determine on which edge the interrupt occurs. Writing '1' to the corresponding bit of EXTI_FTSR selects the falling edge, of EXTI_RTSR the rising edge.
EXTI_SWIER – software triggering of an event/interrupt; writing '1' to the corresponding bit generates the event/interrupt.
EXTI_PR – the event/interrupt pending flag; it is cleared manually by writing '1'.
Now let's look at a simple example in which pressing a button connected to pin zero of port E generates an interrupt.
#include "stm32f10x.h"
void EXTI0_IRQHandler(void)
{
//clear the interrupt flag
EXTI->PR |= EXTI_PR_PR0;
}
int main(void)
{
//enable clocking of the port and alternate functions
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN | RCC_APB2ENR_IOPEEN;
//configure the pin as an input with pull-up/pull-down
GPIOE->CRL &= ~GPIO_CRL_CNF0_0;
GPIOE->CRL |= GPIO_CRL_CNF0_1;
GPIOE->CRL &= ~GPIO_CRL_MODE0 ;
//pull up to the supply
GPIOE->BSRR |= GPIO_BSRR_BR0;
//select the port and pin for the external interrupt
AFIO->EXTICR [0] |= AFIO_EXTICR1_EXTI0_PE;
//on the falling edge
EXTI->FTSR |= EXTI_FTSR_TR0;
//set the mask
EXTI->IMR |= EXTI_IMR_MR0;
//enable EXTI0 interrupts
NVIC->ISER[0] = NVIC_ISER_SETENA_6;
//enable interrupts globally
__enable_irq ();
while(1)
{
}
}