Subsection 9.2.5 Part 4b: The Edge, the Mask, and the NVIC
Three steps remain, and each of them is much easier to program than choosing the port. The edge and the mask are a single bit each, and the bit number corresponds to the line number. The NVIC is the one function call we already used for the timer.
Which edge. The button pin idles HIGH and goes LOW when the button is pressed, so the button press creates a falling edge and the release a rising one. We need to notify the EXTI that we are looking for a falling edge on EXTI4, and we do that in the edge-detection stage of the signal path, which is controlled by
EXTI_FTSR1, the falling trigger selection register (RM0490 §12.5.2). That register decides for each line whether a falling edge counts as an event. Each bit FT0 through FT15 corresponds to one EXTI line, and setting the bit enables falling-edge detection, so we set bit 4 (Figure 9.2.12). There is a matching EXTI_RTSR1 for rising edges; setting the same bit in both would interrupt on press and release, which some designs want and this one does not.
EXTI_FTSR1, the falling trigger selection register (RM0490 §12.5.2, offset 0x004). One bit per line, FT0 through FT15, each read-write: 1 enables falling-edge detection on that line, 0 disables it. Setting FT4 is what makes a press on PB4 an event at all.
Whether it may interrupt. Detecting an edge and requesting an interrupt are deliberately separate in this hardware, and the interrupt mask register is the third of the four steps (Figure 9.2.13). If an edge is detected on a line that is masked in
EXTI_IMR1, the event does not generate an interrupt request to the NVIC. For a masked line, the falling edge does not set the corresponding bit in EXTI_FPR1, the falling edge pending register (RM0490 §12.4), which is the fourth and last stage of the path. The register’s own page, §12.5.5, says only that each bit is set on a falling edge event and does not mention the mask at all, so §12.4 is the one to read for this. For an unmasked line, the pending bit is set and the EXTI logic then generates the interrupt request that is sent to the NVIC. That separation has several uses. The same detected edge can be routed to the EXTI’s event output instead of its interrupt output, through EXTI_EMR1. A source can be switched off and back on with one bit without disturbing its trigger configuration, and a low-power design can choose which pins are allowed to wake the chip. We want the interrupt, so we set bit 4.
EXTI_IMR1, the CPU wakeup with interrupt mask register (RM0490 §12.5.7, offset 0x080). One bit per line: 1 lets a detected event on that line request an interrupt, 0 masks it. Every GPIO line, IM0 through IM15, is masked at reset, so an unset bit here is a very quiet bug: the edge is detected, nothing is recorded in EXTI_FPR1, and nothing ever happens. (The reset value is 0xFFF8 0000: the direct peripheral lines 19, 23, 25 and 31 are unmasked at reset, which is why the top of the register is not all zeros.)
Whether the NVIC will listen. This is the timer’s second enable, unchanged. The CMSIS (Cortex Microcontroller Software Interface Standard) function takes the constant built from Table 40’s acronym, and for us that is the row covering lines 4 through 15:
EXTI->FTSR1 |= EXTI_FTSR1_FT4; // falling edge on line EXTI4
EXTI->IMR1 |= EXTI_IMR1_IM4; // unmask line EXTI4
NVIC_EnableIRQ(EXTI4_15_IRQn); // let the request reach the CPU
That is the whole configuration. Four things have to be true so that a press on PB4 reaches the handler: port B is selected on line EXTI4 (
EXTI_EXTICR2), a falling edge counts as an event (EXTI_FTSR1), the line is unmasked so the event may become a request (EXTI_IMR1), and the NVIC is willing to deliver it (NVIC_EnableIRQ()). Forget any one of them and there is no error, no warning, and no interrupt.
Assembled, with the pin configuration you already have in front of it, this is the whole initialization. The top part configures the pin as an input pin; the bottom part sets it up to be able to interrupt on a falling edge.
void pb4_exti_init(void) {
__disable_irq(); // disable global interrupts
RCC->IOPENR |= RCC_IOPENR_GPIOBEN; // Enable clock access to GPIOB
GPIOB->MODER = (GPIOB->MODER & ~GPIO_MODER_MODE4_Msk)
| (GPIO_INPUT << GPIO_MODER_MODE4_Pos); // Configure PB4 as input pin
GPIOB->PUPDR = (GPIOB->PUPDR & ~GPIO_PUPDR_PUPD4_Msk)
| (GPIO_PULLUP << GPIO_PUPDR_PUPD4_Pos); // enable pull-up for PB4
EXTI->EXTICR[1] &= ~EXTI_EXTICR2_EXTI4_Msk; // Configure EXTICR2,
EXTI->EXTICR[1] |= (EXTI_PB << EXTI_EXTICR2_EXTI4_Pos); // accessed as EXTICR[1]
EXTI->FTSR1 |= EXTI_FTSR1_FT4; // Falling edge trigger for line EXTI4
EXTI->IMR1 |= EXTI_IMR1_IM4; // Unmask line EXTI4
NVIC_EnableIRQ(EXTI4_15_IRQn); // Enable EXTI4 line in NVIC
__enable_irq(); // Enable global interrupts
}
