Skip to main content

Subsection 9.2.3 Part 3: Why the EXTI Sits Between the Pin and the NVIC

The reading gave the reason: too many pins, not enough NVIC lines. Figure 9.2.3 is what that looks like in the hardware. Pin 0 of every port feeds one multiplexer whose output is called EXTI0; pin 1 of every port feeds another, EXTI1; and so on to EXTI15. Sixteen lines, one per pin number, and each of them can carry the signal from any one port, which means PA13, PB13 and PC13 all compete for EXTI13, and only one of them can have it at a time.
Figure 9.2.3. The EXTI’s GPIO multiplexer (RM0490 §12.3.3). Pin \(n\) of every port is an input to the multiplexer whose output is line EXTI\(n\text{,}\) so there are sixteen lines rather than one per pin. Because each multiplexer has one output, only one port can be selected on a given line at a time: PA4 and PB4 cannot both interrupt through EXTI4.
Sixteen lines is a large reduction from forty-five pins, and it is still more room than the NVIC has to give. Its vector table is shared with every other peripheral on the chip (the timers, the ADC, the USARTs, I2C), so the EXTI is allocated three positions in that table rather than sixteen. So there is an additional condensing step. The sixteen lines are grouped onto three interrupt requests: EXTI0 and EXTI1 raise IRQ5, EXTI2 and EXTI3 raise IRQ6, and EXTI4 through EXTI15, twelve lines, all raise IRQ7.
Those three requests are the three vector-table entries. Look them up the same way we looked up TIM14 for the timer, RM0490 §11.3, Table 40 (Figure 9.2.4), and you find EXTI0_1 at position 5 with priority 12, EXTI2_3 at position 6 with priority 13, and EXTI4_15 at position 7 with priority 14. (The lower the priority number, the higher the priority.) Our button is on PB4, so it arrives on EXTI4, so it arrives at EXTI4_15_IRQHandler, the same function that would run for a pin on EXTI11. We’ll deal with the consequence when we write the handler: it has to find out which line actually fired.
Figure 9.2.4. The three EXTI rows as part of RM0490’s Table 40, the vector table. EXTI0_1 is position 5, EXTI2_3 is position 6, and EXTI4_15, the one PB4 uses, is position 7, with priority 14 and vector address 0x0000_005C. Sixteen EXTI lines are served by these three entries. The naming rule is the one the timer used: take the acronym from this table and append _IRQn for the NVIC constant. Handler names are sometimes non-standard, though this one is not, so the safest source for a handler name is the project’s startup file, startup_stm32c031c6tx.s.
Inside the EXTI block itself, the path from a pin to an interrupt request runs through four register-controlled stages (Figure 9.2.7). The multiplexer picks the port. An edge-detection circuit decides whether this is an edge worth reacting to. A mask decides whether a detected edge is allowed to become an interrupt request at all. And a pending register records that it happened, so that the EXTI logic then generates the interrupt request that is sent to the NVIC. Each stage has its own register. We’ll configure the first three next. The fourth, the pending register, is also the one your handler reads and clears, and we’ll come back to it when we write the handler.
Figure 9.2.6. The path from a button press to an interrupt request, simplified: four stages, each controlled by one register. Figure 9.2.7 shows the same path inside the reference manual’s own diagram.
Figure 9.2.7. The EXTI’s configurable-event trigger logic (RM0490 §12.3.1, Figure 24), annotated: the parts this course does not use are grayed out, and the path a button press takes is drawn in blue. The press enters the multiplexer, which EXTI_EXTICRx has pointed at port B; the asynchronous edge-detection circuit reacts to it according to EXTI_FTSR1; the AND gate passes it only if EXTI_IMR1 has unmasked the line; it is recorded in the pending request register EXTI_FPR1; and it leaves the EXTI as an IRQ, which is what reaches the NVIC inside the Cortex-M0+ at the bottom. Note the second, identical-looking AND gate and mask register above the blue path: the event path, EXTI_EMR1, which this course does not use. The rest of the grayed-out machinery handles rising edges, software-triggered events, and waking the chip from a low-power state.
Four registers, one per stage, control that path.
  • EXTI_EXTICRx, the EXTI external interrupt selection register, is the multiplexer: which port is on this line. For example, EXTI_EXTICR2 says that line EXTI4 listens to port B.
  • EXTI_FTSR1, the falling trigger selection register, is the edge detector: which edge counts as an event. For example, with bit 4 set a falling edge (1 to 0) on line 4 counts, and a rising edge is ignored.
  • EXTI_IMR1, the interrupt mask register, is the mask: whether a detected event may become an interrupt request at all. For example, with bit 4 set line 4 gets through to the NVIC, and with bit 4 clear the event is dropped.
  • EXTI_FPR1, the falling edge pending register, records that it happened, and the bit stays set until your handler clears it. For example, after a press bit 4 reads 1 until the handler writes a 1 to it.