How to Select a GPIO Pin to Be the Source Input of an External Interrupt with an STM32F407G Discovery Board in C

STM32F407G discovery board



In this article, we go over how to select a GPIO (general purpose I/O) pin to be the source input of an external interrupt with an STM32F407G board in C.

This way, we can use a GPIO pin to trigger an external interrupt in our microcontroller circuit.

We can select a GPIO pin to function as a source of an external interrupt through the syscfg external interrupt configuration registers, of which there are 4.

Before we delve into this register, let us first look at the External interrupt mapping for the STM32F407G discovery board for GPIO pins.

Below is the diagram of the External Interrupt mapping for GPIO pins.



External interrupt mapping for GPIO pins STM32F407G



So you can see the general mapping for GPIO pins serving as sources of external interrupts.

For each pin 0 of each port, 1 pin can be selected as a source of an external interrupt, and this pin then issues its interrupt over the EXTI0 line. A multiplexer selects the pin by feeding it 4 bits to select this pin through the syscf external interrupt configuration register.

For each pin 1 of each port, 1 pin can be selected as a source of an external interrupt, and this p‌in then issues its interrupt over the EXTI1 line.

This continues all the way to pin 15 of each port.

This is because there are 16 GPIO pins on each port.

So now that we understand this general mapping for external interrupts for GPIO pins, let's now go over the way to configure them.

So how you decide to select the GPIO pin which will serve as a source for an external interrupt is through the syscf external configuration register.

There are 4 such registers: SYSCFG external interrupt configuration register 1, SYSCFG external interrupt configuration register 2, SYSCFG external interrupt configuration register 3, and SYSCFG external interrupt configuration register 4.

SYSCFG external interrupt configuration register 1 deals with and can select pins 0, 1, 2, or 3 from any of the GPIO ports.

SYSCFG external interrupt configuration register 2 deals with and can select pins 4, 5, 6, or 7 from any of the GPIO ports.

SYSCFG external interrupt configuration register 3 deals with and can select pins 8, 9, 10, or 11 from any of the GPIO ports.

SYSCFG external interrupt configuration register 4 deals with and can select pins 12, 13, 14, or 15 from any of the GPIO ports.

Let's now look at the diagram of these registers as they appear on the datasheet.

Below is the SYSCFG external configuration register 1.



SYSCFG external interrupt configuration register 1 STM32407G



With all registers in the STM32 boards, this is a 32-bit register. However, only 16 bits (0 to 15) are used, while the other 16 bits are reserved. The first 4 bits are for pin 0 of each of the GPIO ports.

The second 4 bits are for pin 1 of each of the GPIO ports.

The third 4 bits are for pin 2 of each of the GPIO ports.

The fourth 4 bits are for pin 3 of each of the GPIO ports.

Below is the SYSCFG external configuration register 2.



SYSCFG external interrupt configuration register 2 STM32407G



The first 4 bits are for pin 4 of each of the GPIO ports.

The second 4 bits are for pin 5 of each of the GPIO ports.

The third 4 bits are for pin 6 of each of the GPIO ports.

The fourth 4 bits are for pin 7 of each of the GPIO ports.

Below is the SYSCFG external configuration register 3.



SYSCFG external interrupt configuration register 3 STM32407G



The first 4 bits are for pin 8 of each of the GPIO ports.

The second 4 bits are for pin 9 of each of the GPIO ports.

The third 4 bits are for pin 10 of each of the GPIO ports.

The fourth 4 bits are for pin 11 of each of the GPIO ports.

Below is the SYSCFG external configuration register 4.



SYSCFG external interrupt configuration register 4 STM32407G



The first 4 bits are for pin 12 of each of the GPIO ports.

The second 4 bits are for pin 13 of each of the GPIO ports.

The third 4 bits are for pin 14 of each of the GPIO ports.

The fourth 4 bits are for pin 15 of each of the GPIO ports.

So if you look at each register, there is an offset.

For register 1, there is an offset of 0x08

For register 2, there is an offset of 0x0C

For register 3, there is an offset of 0x10

For register 4, there is an offset of 0x14

This is shown below in the SYSCFG register maps.



SYSCFG register maps STM32F407G



So what register is this offset from? Or in other words, what is the base register?

The base register is the SYSCFG register, which is shown in the memory map of the microcontroller board.

The SYSCFG register has a base address of 0x4001 3800

Now we should have all the information to know how to select a GPIO pin.

Below we have the code to allow pin 0 of GPIO port A to be the source of an external interrupt.



So the first thing we have to do is define the base address of the SYSCFG register, which we define in a macro at the top of our code.

Then we have to define the register offset. Since pin 0 falls within SYSCFG external interrupt configuration register 1, we define this offset as a macro.

We then define the address of the SYSCFG external interrupt configuration register 1, which is the base address of the SYSCFG register and the offset for SYSCFG register 1.

Within the main function, we then define the SYSCFG register 1.

In order to select GPIO port A pin 0, we must set the first 4 bits to 0000 or clear the first 4 bits. We do this with the line, *pSyscfgReg1 &= ~(0xF << 0);

As a second example, let's select pin 13 of GPIO port D.

The code below selects this pin as the source input of an external interrupt.



So now we define the base address of the SYSCFG register as before but now the offset we use is for SYSCFG external interrupt configuration register 4, since this register contains pin 13.

To select GPIO port D, we set pin 13 to 0011. In hexadecimal, this is, 0x3

And this is how to select a GPIO pin to be the source input of an external interrupt with an STM32F407G discovery board in C.

Related Resources

How to Set Bits of a Number in C

How to Clear Bits of a Number in C



HTML Comment Box is loading comments...