Skip to main content

Subsection 9.2.4 Part 4a: Choosing the Port (EXTI_EXTICR2)

PB4 has to win the competition for line EXTI4, and the register that decides it is EXTI_EXTICR2. There are four of these registers between them covering all sixteen lines, four lines to a register (EXTI0 to EXTI3 in the first, EXTI4 to EXTI7 in the second, and so on); the one that holds EXTI4 is the second, and how the four are laid out and addressed is in SubsectionΒ 9.3.1 for when you need it. What you need now is on one page of the Reference Manual. Which byte of EXTI_EXTICR2 corresponds to line EXTI4, and what code do you need to write into it to select a port?

Activity 9.2.2. Which Register, Which Byte, Which Code.

Open the reference manual, RM0490 Β§12.5.6, EXTI external interrupt selection register.

(b)

The registers themselves are named, in the reference manual, EXTICR1, EXTICR2, EXTICR3 and EXTICR4. If each is stored in an element of the four-element C array EXTI->EXTICR[ ], how do we refer to EXTI_EXTICR2 in C?
Working it through: the four fields of EXTI_EXTICR2 are EXTI4, EXTI5, EXTI6 and EXTI7, lowest byte first, so line EXTI4 is bits [7:0]. The port codes on that page are 0x00 for PA, 0x01 for PB, 0x02 for PC, 0x03 for PD and 0x05 for PF; 0x04 is reserved, because this package has no port E. And the array index is one, not two: the reference manual numbers its registers from 1 and C numbers array elements from 0, so EXTI_EXTICR1 through EXTI_EXTICR4 are EXTI->EXTICR[0] through EXTI->EXTICR[3]. This is a genuinely easy slip to make and it produces a program that builds cleanly and never interrupts.
Figure 9.2.10. EXTI_EXTICR2 with the four lines it controls named (EXTI4 in bits [7:0], then EXTI5, EXTI6 and EXTI7) and port B’s code, 0x01, written into the EXTI4 field. The eight bits shown as 0 0 0 0 0 0 0 1 are that byte, read from bit 7 down to bit 0: bit 0 set, the rest clear.
Selecting port B for EXTI4 therefore looks like this in code, using the mask and position macros from the device header and the port constant from ES28.h:
EXTI->EXTICR[1] &= ~EXTI_EXTICR2_EXTI4_Msk;             // clear the EXTI4 field
EXTI->EXTICR[1] |= (EXTI_PB << EXTI_EXTICR2_EXTI4_Pos); // select port B