7. General-purpose I/Os (GPIO)

7.1 GPIO introduction

Each general-purpose I/O port has four 32-bit configuration registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR and GPIOx_PUPDR), two 32-bit data registers (GPIOx_IDR and GPIOx_ODR), a 32-bit set/reset register (GPIOx_BSRR), a 32-bit locking register (GPIOx_LCKR) and two 32-bit alternate function selection register (GPIOx_AFRH and GPIOx_AFRL).

7.2 GPIO main features

7.3 GPIO functional description

Subject to the specific hardware characteristics of each I/O port listed in the datasheet, each port bit of the general-purpose I/O (GPIO) ports can be individually configured by software in several modes:

Each I/O port bit is freely programmable, however the I/O port registers have to be accessed as 32-bit words, half-words or bytes. The purpose of the GPIOx_BSRR register is to allow atomic read/modify accesses to any of the GPIO registers. In this way, there is no risk of an IRQ occurring between the read and the modify access.

Figure 18 shows the basic structure of a 5 V tolerant I/O port bit, Table 22 gives the possible port bit configurations.

Figure 18. Basic structure of a 5 V tolerant I/O port bit

Figure 18: Basic structure of a 5 V tolerant I/O port bit. This block diagram illustrates the internal architecture of a GPIO pin. On the left, connections include 'To on-chip peripheral' (Analog and Alternate function input), 'Read' and 'Write' signals for 'Bit set/reset registers', and 'From on-chip peripheral' (Alternate function output). The central part consists of an 'Input data register', an 'Output data register', and an 'Output control' block. The input path includes an 'Input driver' with a 'TTL Schmitt trigger'. The output path includes an 'Output driver' with 'P-MOS' and 'N-MOS' transistors, labeled 'Push-pull, open-drain or disabled'. The pin itself is connected to 'V_DD', 'V_DD_FT' (1), 'V_SS', and 'V_SS' through 'Protection diode' and 'Pull up/down' resistors. The entire structure is enclosed in a dashed box labeled 'ai15939b'.
Figure 18: Basic structure of a 5 V tolerant I/O port bit. This block diagram illustrates the internal architecture of a GPIO pin. On the left, connections include 'To on-chip peripheral' (Analog and Alternate function input), 'Read' and 'Write' signals for 'Bit set/reset registers', and 'From on-chip peripheral' (Alternate function output). The central part consists of an 'Input data register', an 'Output data register', and an 'Output control' block. The input path includes an 'Input driver' with a 'TTL Schmitt trigger'. The output path includes an 'Output driver' with 'P-MOS' and 'N-MOS' transistors, labeled 'Push-pull, open-drain or disabled'. The pin itself is connected to 'V_DD', 'V_DD_FT' (1), 'V_SS', and 'V_SS' through 'Protection diode' and 'Pull up/down' resistors. The entire structure is enclosed in a dashed box labeled 'ai15939b'.

1. \( V_{DD\_FT} \) is a potential specific to 5 V tolerant I/Os and different from \( V_{DD} \) .

Table 22. Port bit configuration table (1)

MODER(i)[1:0]OTYPER(i)OSPEEDR(i)[B:A]PUPDR(i)[1:0]I/O configuration
010SPEED[B:A]00GP outputPP
001GP outputPP + PU
010GP outputPP + PD
011Reserved
100GP outputOD
101GP outputOD + PU
110GP outputOD + PD
111Reserved (GP output OD)
100SPEED[B:A]00AFPP
001AFPP + PU
010AFPP + PD
011Reserved
100AFOD
101AFOD + PU
110AFOD + PD
111Reserved
Table 22. Port bit configuration table (1) (continued)
MODER(i)[1:0]OTYPER(i)OSPEEDR(i)[B:A]PUPDR(i)[1:0]I/O configuration
BA10
00xxx00InputFloating
xxx01InputPU
xxx10InputPD
xxx11Reserved (input floating)
11xxx00Input / outputAnalog
xxx01Reserved
xxx10
xxx11

1. GP = general-purpose, PP = push-pull, PU = pull-up, PD = pull-down, OD = open-drain, AF = alternate function.

7.3.1 General-purpose I/O (GPIO)

During and just after reset, the alternate functions are not active and the I/O ports are configured in input floating mode.

The debug pins are in AF pull-up/pull-down after reset:

When the pin is configured as output, the value written to the output data register (GPIOx_ODR) is output on the I/O pin. It is possible to use the output driver in push-pull mode or open-drain mode (only the N-MOS is activated when 0 is output).

The input data register (GPIOx_IDR) captures the data present on the I/O pin at every AHB1 clock cycle.

All GPIO pins have weak internal pull-up and pull-down resistors, which can be activated or not depending on the value in the GPIOx_PUPDR register.

7.3.2 I/O pin multiplexer and mapping

The microcontroller I/O pins are connected to onboard peripherals/modules through a multiplexer that allows only one peripheral's alternate function (AF) connected to an I/O pin at a time. In this way, there can be no conflict between peripherals sharing the same I/O pin.

Each I/O pin has a multiplexer with sixteen alternate function inputs (AF0 to AF15) that can be configured through the GPIOx_AFRL (for pin 0 to 7) and GPIOx_AFRH (for pin 8 to 15) registers:

This structure is shown in Figure 19 .

In addition to this flexible I/O multiplexing architecture, each peripheral has alternate functions mapped onto different I/O pins to optimize the number of peripherals available in smaller packages.

To use an I/O in a given configuration, proceed as follows:

Note: You can disable some or all of the JTAG/SWD pins and so release the associated pins for GPIO usage.

For more details refer to Section 6.2.10: Clock-out capability .

Table 23. Flexible SWJ-DP pin assignment

Available debug portsSWJ I/O pin assigned
PA13 / JTMS/ SWDIOPA14 / JTCK/ SWCLKPA15 / JTDIPB3 / JTDOPB4/ NJTRST
Full SWJ (JTAG-DP + SW-DP) - Reset stateXXXXX
Full SWJ (JTAG-DP + SW-DP) but without NJTRSTXXXX
JTAG-DP Disabled and SW-DP EnabledXX
JTAG-DP Disabled and SW-DP DisabledReleased

Configure the desired I/O as output or input in the GPIOx_MODER register.

For the ADC and DAC, configure the desired I/O as analog in the GPIOx_MODER register.

For other peripherals:

The EVENTOUT signal can be output as alternate function on several I/Os. An event can be signaled through the configured pin after executing the SEV instruction.

Configure the I/O pin used to output the Cortex®-M4 with FPU EVENTOUT signal by connecting it to AF15

Note: EVENTOUT is not mapped onto the following I/O pins: PC13, PC14, PC15, PH0 and PH1.

Refer to the “Alternate function mapping” table in the datasheets for the detailed mapping of the system and peripherals’ alternate function I/O pins.

Figure 19. Selecting an alternate function on STM32F446xx

Diagram showing the selection of alternate functions for pins 0 to 7. A vertical bar represents the pins. On the left, a list of alternate functions (AF0 to AF15) is shown with lines pointing to the bar. On the right, an arrow labeled 'Pin x (x = 0..7)' points to the bar, with a slash and the number '1' indicating the selected function. Below the bar, an arrow labeled 'AFRL[31:0]' points up to the bar. Diagram showing the selection of alternate functions for pins 8 to 15. Similar to the first diagram, but the right arrow is labeled 'Pin x (x = 8..15)' and the bottom arrow is labeled 'AFRH[31:0]'.

For pins 0 to 7, the GPIOx_AFR[31:0] register selects the dedicated alternate function

AF0 (system)
AF1 (TIM1/TIM2)
AF2 (TIM3..5)
AF3 (TIM8..11, CEC)
AF4 (I2C1..4, CEC)
AF5 (SPI1/2/3/4)
AF6 (SPI2/3/4, SAI1)
AF7 (SPI2/3, USART1..3, UART5, SPDIF-IN)
AF8 (SPI2/3, USART1..3, UART5, SPDIF-IN)
AF9 (CAN1/2, TIM12..14, QUADSPI)
AF10 (SAI2, QUADSPI, OTG_HS, OTG_FS)
AF11
AF12 (FMC, SDIO, OTG_HS (1) )
AF13 (DCMI)
AF14
AF15 (EVENTOUT)

Pin x (x = 0..7)
1

AFRL[31:0]

For pins 8 to 15, the GPIOx_AFRH[31:0] register selects the dedicated alternate function

AF0 (system)
AF1 (TIM1/TIM2)
AF2 (TIM3..5)
AF3 (TIM8..11, CEC)
AF4 (I2C1..4, CEC)
AF5 (SPI1/2/3/4)
AF6 (SPI2/3/4, SAI1)
AF7 (SPI2/3, USART1..3, UART5, SPDIF-IN)
AF8 (SPI2/3, USART1..3, UART5, SPDIF-IN)
AF9 (CAN1/2, TIM12..14, QUADSPI)
AF10 (SAI2, QUADSPI, OTG_HS, OTG_FS)
AF11
AF12 (FMC, SDIO, OTG_HS (1) )
AF13 (DCMI)
AF14
AF15 (EVENTOUT)

Pin x (x = 8..15)
1

AFRH[31:0]

MS35850V1

Diagram showing the selection of alternate functions for pins 0 to 7. A vertical bar represents the pins. On the left, a list of alternate functions (AF0 to AF15) is shown with lines pointing to the bar. On the right, an arrow labeled 'Pin x (x = 0..7)' points to the bar, with a slash and the number '1' indicating the selected function. Below the bar, an arrow labeled 'AFRL[31:0]' points up to the bar. Diagram showing the selection of alternate functions for pins 8 to 15. Similar to the first diagram, but the right arrow is labeled 'Pin x (x = 8..15)' and the bottom arrow is labeled 'AFRH[31:0]'.

1. Configured in FS.

7.3.3 I/O port control registers

Each of the GPIOs has four 32-bit memory-mapped control registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR) to configure up to 16 I/Os. The

GPIOx_MODER register is used to select the I/O direction (input, output, AF, analog). The GPIOx_OTYPER and GPIOx_OSPEEDR registers are used to select the output type (push-pull or open-drain) and speed (the I/O speed pins are directly connected to the corresponding GPIOx_OSPEEDR register bits whatever the I/O direction). The GPIOx_PUPDR register is used to select the pull-up/pull-down whatever the I/O direction.

7.3.4 I/O port data registers

Each GPIO has two 16-bit memory-mapped data registers: input and output data registers (GPIOx_IDR and GPIOx_ODR). GPIOx_ODR stores the data to be output, it is read/write accessible. The data input through the I/O are stored into the input data register (GPIOx_IDR), a read-only register.

See Section 7.4.5 and Section 7.4.6 for the register descriptions.

7.3.5 I/O data bitwise handling

The bit set reset register (GPIOx_BSRR) is a 32-bit register which allows the application to set and reset each individual bit in the output data register (GPIOx_ODR). The bit set reset register has twice the size of GPIOx_ODR.

To each bit in GPIOx_ODR, correspond two control bits in GPIOx_BSRR: BSRR(i) and BSRR(i+SIZE). When written to 1, bit BSRR(i) sets the corresponding ODR(i) bit. When written to 1, bit BSRR(i+SIZE) resets the ODR(i) corresponding bit.

Writing any bit to 0 in GPIOx_BSRR does not have any effect on the corresponding bit in GPIOx_ODR. If there is an attempt to both set and reset a bit in GPIOx_BSRR, the set action takes priority.

Using the GPIOx_BSRR register to change the values of individual bits in GPIOx_ODR is a “one-shot” effect that does not lock the GPIOx_ODR bits. The GPIOx_ODR bits can always be accessed directly. The GPIOx_BSRR register provides a way of performing atomic bitwise handling.

There is no need for the software to disable interrupts when programming the GPIOx_ODR at bit level: it is possible to modify one or more bits in a single atomic AHB1 write access.

7.3.6 GPIO locking mechanism

It is possible to freeze the GPIO control registers by applying a specific write sequence to the GPIOx_LCKR register. The frozen registers are GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH.

To write the GPIOx_LCKR register, a specific write / read sequence has to be applied. When the right LOCK sequence is applied to bit 16 in this register, the value of LCKR[15:0] is used to lock the configuration of the I/Os (during the write sequence the LCKR[15:0] value must be the same). When the LOCK sequence has been applied to a port bit, the value of the port bit can no longer be modified until the next reset. Each GPIOx_LCKR bit freezes the corresponding bit in the control registers (GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR, GPIOx_PUPDR, GPIOx_AFRL and GPIOx_AFRH).

The LOCK sequence (refer to Section 7.4.8 ) can only be performed using a word (32-bit long) access to the GPIOx_LCKR register due to the fact that GPIOx_LCKR bit 16 has to be set at the same time as the [15:0] bits.

For more details refer to LCKR register description in Section 7.4.8 .

7.3.7 I/O alternate function input/output

Two registers are provided to select one out of the sixteen alternate function inputs/outputs available for each I/O. With these registers, you can connect an alternate function to some other pin as required by your application.

This means that a number of possible peripheral functions are multiplexed on each GPIO using the GPIOx_AFRL and GPIOx_AFRH alternate function registers. The application can thus select any one of the possible functions for each I/O. The AF selection signal being common to the alternate function input and alternate function output, a single channel is selected for the alternate function input/output of one I/O.

To know which functions are multiplexed on each GPIO pin, refer to the datasheets.

Note: The application is allowed to select one of the possible peripheral functions for each I/O at a time.

7.3.8 External interrupt/wakeup lines

All ports have external interrupt capability. To use external interrupt lines, the port must be configured in input mode, refer to Section 10.2: External interrupt/event controller (EXTI) and Section 10.2.3: Wakeup event management .

7.3.9 Input configuration

When the I/O port is programmed as Input:

Figure 20 shows the input configuration of the I/O port bit.

Figure 20. Input floating/pull up/pull down configurations

Figure 20: Input floating/pull up/pull down configurations. This block diagram illustrates the internal circuitry of a GPIO pin configured as an input. On the left, external signals 'Read', 'Write', and 'Read/write' are shown. 'Read' connects to the 'Input data register'. 'Write' connects to 'Bit set/reset registers', which in turn connect to the 'Output data register'. 'Read/write' connects to both the 'Bit set/reset registers' and the 'Output data register'. The 'Output data register' connects to an 'output driver' (represented by a switch). The 'Input data register' connects to an 'input driver' (represented by a TTL Schmitt trigger). Both the 'input driver' and 'output driver' are enclosed in a dashed box. The output of the 'input driver' and the 'output driver' are connected to the 'I/O pin'. The 'I/O pin' is connected to 'VDD' through a 'pull up' resistor (labeled 'on/off') and a 'protection diode'. The 'I/O pin' is also connected to 'VSS' through a 'pull down' resistor (labeled 'on/off') and a 'protection diode'. The diagram is labeled 'ai15940b' in the bottom right corner.
Figure 20: Input floating/pull up/pull down configurations. This block diagram illustrates the internal circuitry of a GPIO pin configured as an input. On the left, external signals 'Read', 'Write', and 'Read/write' are shown. 'Read' connects to the 'Input data register'. 'Write' connects to 'Bit set/reset registers', which in turn connect to the 'Output data register'. 'Read/write' connects to both the 'Bit set/reset registers' and the 'Output data register'. The 'Output data register' connects to an 'output driver' (represented by a switch). The 'Input data register' connects to an 'input driver' (represented by a TTL Schmitt trigger). Both the 'input driver' and 'output driver' are enclosed in a dashed box. The output of the 'input driver' and the 'output driver' are connected to the 'I/O pin'. The 'I/O pin' is connected to 'VDD' through a 'pull up' resistor (labeled 'on/off') and a 'protection diode'. The 'I/O pin' is also connected to 'VSS' through a 'pull down' resistor (labeled 'on/off') and a 'protection diode'. The diagram is labeled 'ai15940b' in the bottom right corner.

7.3.10 Output configuration

When the I/O port is programmed as output:

Figure 21 shows the output configuration of the I/O port bit.

Figure 21: Output configuration diagram. This block diagram illustrates the internal architecture of a GPIO pin in output mode. On the left, external signals 'Read', 'Write', and 'Read/write' connect to 'Bit set/reset registers' and an 'Output data register'. These registers are connected to an 'Output control' block. The 'Output control' block drives a pair of P-MOS and N-MOS transistors. The P-MOS is connected to VDD and the N-MOS to VSS. The gates of these transistors are controlled by the 'Output control' block. The drains of the transistors are connected to the 'I/O pin'. Above the transistors, an 'Input driver' containing a 'TTL Schmitt trigger' is shown, with its output connected to an 'Input data register'. The 'Input data register' is connected to the 'Read' signal. To the right of the I/O pin, there are two protection diodes connected to VDD and VSS respectively. Below the pin, there are two weak pull-up and pull-down resistors, labeled 'pull up' and 'pull down', with 'on/off' control inputs. The entire internal circuitry is enclosed in a dashed box. The diagram is labeled 'ai15941b' in the bottom right corner.

Figure 21. Output configuration

Figure 21: Output configuration diagram. This block diagram illustrates the internal architecture of a GPIO pin in output mode. On the left, external signals 'Read', 'Write', and 'Read/write' connect to 'Bit set/reset registers' and an 'Output data register'. These registers are connected to an 'Output control' block. The 'Output control' block drives a pair of P-MOS and N-MOS transistors. The P-MOS is connected to VDD and the N-MOS to VSS. The gates of these transistors are controlled by the 'Output control' block. The drains of the transistors are connected to the 'I/O pin'. Above the transistors, an 'Input driver' containing a 'TTL Schmitt trigger' is shown, with its output connected to an 'Input data register'. The 'Input data register' is connected to the 'Read' signal. To the right of the I/O pin, there are two protection diodes connected to VDD and VSS respectively. Below the pin, there are two weak pull-up and pull-down resistors, labeled 'pull up' and 'pull down', with 'on/off' control inputs. The entire internal circuitry is enclosed in a dashed box. The diagram is labeled 'ai15941b' in the bottom right corner.

7.3.11 Alternate function configuration

When the I/O port is programmed as alternate function:

Figure 22 shows the Alternate function configuration of the I/O port bit.

Figure 22. Alternate function configuration

Figure 22: Alternate function configuration diagram showing internal circuitry for an I/O pin in alternate function mode. The diagram includes an input data register, bit self/reset registers, an output data register, a TTL Schmitt trigger, an output driver with P-MOS and N-MOS transistors, and protection diodes connected to the I/O pin. The Schmitt trigger is active ('on'), and the output driver is disabled. The I/O pin is connected to an on-chip peripheral via alternate function input and output paths.

Figure 22 illustrates the internal circuitry of an I/O pin in alternate function configuration. The diagram shows the following components and connections:

Figure 22: Alternate function configuration diagram showing internal circuitry for an I/O pin in alternate function mode. The diagram includes an input data register, bit self/reset registers, an output data register, a TTL Schmitt trigger, an output driver with P-MOS and N-MOS transistors, and protection diodes connected to the I/O pin. The Schmitt trigger is active ('on'), and the output driver is disabled. The I/O pin is connected to an on-chip peripheral via alternate function input and output paths.

7.3.12 Analog configuration

When the I/O port is programmed as analog configuration:

Note: In the analog configuration, the I/O pins cannot be 5 Volt tolerant.

Figure 23 shows the high-impedance, analog-input configuration of the I/O port bit.

Figure 23. High impedance-analog configuration

Figure 23: High impedance-analog configuration diagram showing internal circuitry for an I/O pin in analog mode. The diagram includes an input data register, bit self/reset registers, an output data register, a TTL Schmitt trigger, and protection diodes connected to the I/O pin. The Schmitt trigger is inactive ('off'), and the output driver is disabled. The I/O pin is connected to an on-chip peripheral via analog input and output paths.

Figure 23 illustrates the internal circuitry of an I/O pin in high-impedance analog configuration. The diagram shows the following components and connections:

Figure 23: High impedance-analog configuration diagram showing internal circuitry for an I/O pin in analog mode. The diagram includes an input data register, bit self/reset registers, an output data register, a TTL Schmitt trigger, and protection diodes connected to the I/O pin. The Schmitt trigger is inactive ('off'), and the output driver is disabled. The I/O pin is connected to an on-chip peripheral via analog input and output paths.

7.3.13 Using the OSC32_IN/OSC32_OUT pins as GPIO PC14/PC15 port pins

The LSE oscillator pins OSC32_IN and OSC32_OUT can be used as general-purpose PC14 and PC15 I/Os, respectively, when the LSE oscillator is off. The PC14 and PC15 I/Os are only configured as LSE oscillator pins OSC32_IN and OSC32_OUT when the LSE oscillator is ON. This is done by setting the LSEON bit in the RCC_BDCR register. The LSE has priority over the GPIO function.

Note: The PC14/PC15 GPIO functionality is lost when the 1.2 V domain is powered off (by the device entering the standby mode) or when the backup domain is supplied by V BAT (V DD no more supplied). In this case the I/Os are set in analog input mode.

7.3.14 Using the OSC_IN/OSC_OUT pins as GPIO PH0/PH1 port pins

The HSE oscillator pins OSC_IN/OSC_OUT can be used as general-purpose PH0/PH1 I/Os, respectively, when the HSE oscillator is OFF. (after reset, the HSE oscillator is off). The PH0/PH1 I/Os are only configured as OSC_IN/OSC_OUT HSE oscillator pins when the HSE oscillator is ON. This is done by setting the HSEON bit in the RCC_CR register. The HSE has priority over the GPIO function.

7.3.15 Selection of RTC additional_AF1 and RTC_AF2 alternate functions

The STM32F446xx feature two GPIO pins RTC_AF1 and RTC_AF2 that can be used for the detection of a tamper or time stamp event, or RTC_ALARM, or RTC_CALIB RTC outputs.

RTC_ALARM output: this output can be RTC Alarm A, RTC Alarm B or RTC Wakeup depending on the OSEL[1:0] bits in the RTC_CR register

The RTC_AF2 (PA0) can be used for the following purposes:

The selection of the corresponding pin is performed through the RTC_TAFCR register as follows:

The output mechanism follows the priority order listed in Table 24 and Table 25 .

Table 24. RTC_AF1 pin (1)
Pin configuration and functionRTC_ALARM enabledRTC_CALIB enabledTamper enabledTime stamp enabledTAMP1INSEL TAMPER1 pin selectionTSINSEL TIMESTAMP pin selectionALARMOUTTYPE RTC_ALARM configuration
Alarm out output OD1Don't careDon't careDon't careDon't careDon't care0
Alarm out output PP1Don't careDon't careDon't careDon't careDon't care1
Calibration out output PP01Don't careDon't careDon't careDon't careDon't care
TAMPER1 input floating00100Don't careDon't care
TIMESTAMP and TAMPER1 input floating001100Don't care
TIMESTAMP input floating0001Don't care0Don't care
Standard GPIO0000Don't careDon't careDon't care

1. OD: open drain; PP: push-pull.

Table 25. RTC_AF2 pin

Pin configuration and functionTamper enabledTime stamp enabledTAMP1INSEL TAMPER1 pin selectionTSINSEL TIMESTAMP pin selectionALARMOUTTYPE RTC_ALARM configuration
TAMPER1 input floating101Don't careDon't care
TIMESTAMP and TAMPER1 input floating1111Don't care
TIMESTAMP input floating01Don't care1Don't care
Standard GPIO00Don't careDon't careDon't care

7.4 GPIO registers

This section gives a detailed description of the GPIO registers. For a summary of register bits, register address offsets and reset values, refer to Table 26 .

The GPIO registers can be accessed by byte (8 bits), half-words (16 bits) or words (32 bits).

7.4.1 GPIO port mode register (GPIOx_MODER) (x = A..H)

Address offset: 0x00

Reset values:

31302928272625242322212019181716
MODER15[1:0]MODER14[1:0]MODER13[1:0]MODER12[1:0]MODER11[1:0]MODER10[1:0]MODER9[1:0]MODER8[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
MODER7[1:0]MODER6[1:0]MODER5[1:0]MODER4[1:0]MODER3[1:0]MODER2[1:0]MODER1[1:0]MODER0[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 2y:2y+1 MODERy[1:0] : Port x configuration bits (y = 0..15)

These bits are written by software to configure the I/O direction mode.

00: Input (reset state)

01: General purpose output mode

10: Alternate function mode

11: Analog mode

7.4.2 GPIO port output type register (GPIOx_OTYPER) (x = A..H)

Address offset: 0x04

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
OT15OT14OT13OT12OT11OT10OT9OT8OT7OT6OT5OT4OT3OT2OT1OT0
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 OTy : Port x configuration bits (y = 0..15)

These bits are written by software to configure the output type of the I/O port.

0: Output push-pull (reset state)

1: Output open-drain

7.4.3 GPIO port output speed register (GPIOx_OSPEEDR) (x = A..H)

Address offset: 0x08

Reset values:

31302928272625242322212019181716
OSPEEDR15 [1:0]OSPEEDR14 [1:0]OSPEEDR13 [1:0]OSPEEDR12 [1:0]OSPEEDR11 [1:0]OSPEEDR10 [1:0]OSPEEDR9 [1:0]OSPEEDR8 [1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
OSPEEDR7 [1:0]OSPEEDR6 [1:0]OSPEEDR5 [1:0]OSPEEDR4 [1:0]OSPEEDR3 [1:0]OSPEEDR2 [1:0]OSPEEDR1 [1:0]OSPEEDR0 [1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 2y:2y+1 OSPEEDRy[1:0] : Port x configuration bits (y = 0..15)

These bits are written by software to configure the I/O output speed.

00: Low speed

01: Medium speed

10: Fast speed

11: High speed

Note: Refer to the product datasheets for the values of OSPEEDRy bits versus V DD range and external load.

7.4.4 GPIO port pull-up/pull-down register (GPIOx_PUPDR) (x = A..H)

Address offset: 0x0C

Reset values:

31302928272625242322212019181716
PUPDR15[1:0]PUPDR14[1:0]PUPDR13[1:0]PUPDR12[1:0]PUPDR11[1:0]PUPDR10[1:0]PUPDR9[1:0]PUPDR8[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
PUPDR7[1:0]PUPDR6[1:0]PUPDR5[1:0]PUPDR4[1:0]PUPDR3[1:0]PUPDR2[1:0]PUPDR1[1:0]PUPDR0[1:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 2y:2y+1 PUPDRy[1:0] : Port x configuration bits (y = 0..15)

These bits are written by software to configure the I/O pull-up or pull-down

00: No pull-up, pull-down

01: Pull-up

10: Pull-down

11: Reserved

7.4.5 GPIO port input data register (GPIOx_IDR) (x = A..H)

Address offset: 0x10

Reset value: 0x0000 XXXX (where X means undefined)

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
IDR15IDR14IDR13IDR12IDR11IDR10IDR9IDR8IDR7IDR6IDR5IDR4IDR3IDR2IDR1IDR0
rrrrrrrrrrrrrrrr

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 IDRy : Port input data (y = 0..15)

These bits are read-only and can be accessed in word mode only. They contain the input value of the corresponding I/O port.

7.4.6 GPIO port output data register (GPIOx_ODR) (x = A..H)

Address offset: 0x14

Reset value: 0x0000 0000

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.
1514131211109876543210
ODR15ODR14ODR13ODR12ODR11ODR10ODR9ODR8ODR7ODR6ODR5ODR4ODR3ODR2ODR1ODR0
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:16 Reserved, must be kept at reset value.

Bits 15:0 ODRy : Port output data (y = 0..15)

These bits can be read and written by software.

Note: For atomic bit set/reset, the ODR bits can be individually set and reset by writing to the GPIOx_BSRR register (x = A..H).

7.4.7 GPIO port bit set/reset register (GPIOx_BSRR) (x = A..H)

Address offset: 0x18

Reset value: 0x0000 0000

31302928272625242322212019181716
BR15BR14BR13BR12BR11BR10BR9BR8BR7BR6BR5BR4BR3BR2BR1BR0
wwwwwwwwwwwwwwww
1514131211109876543210
BS15BS14BS13BS12BS11BS10BS9BS8BS7BS6BS5BS4BS3BS2BS1BS0
wwwwwwwwwwwwwwww

Bits 31:16 BRy : Port x reset bit y (y = 0..15)

These bits are write-only and can be accessed in word, half-word or byte mode. A read to these bits returns the value 0x0000.

0: No action on the corresponding ODRx bit

1: Resets the corresponding ODRx bit

Note: If both BSx and BRx are set, BSx has priority.

Bits 15:0 BSy : Port x set bit y (y= 0..15)

These bits are write-only and can be accessed in word, half-word or byte mode. A read to these bits returns the value 0x0000.

0: No action on the corresponding ODRx bit

1: Sets the corresponding ODRx bit

7.4.8 GPIO port configuration lock register (GPIOx_LCKR) (x = A..H)

This register is used to lock the configuration of the port bits when a correct write sequence is applied to bit 16 (LCKK). The value of bits [15:0] is used to lock the configuration of the GPIO. During the write sequence, the value of LCKR[15:0] must not change. When the LOCK sequence has been applied on a port bit, the value of this port bit can no longer be modified until the next reset.

Note: A specific write sequence is used to write to the GPIOx_LCKR register. Only word access (32-bit long) is allowed during this write sequence.

Each lock bit freezes a specific configuration register (control and alternate function registers).

Address offset: 0x1C

Reset value: 0x0000 0000

Access: 32-bit word only, read/write register

31302928272625242322212019181716
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.LCK K16
rw
1514131211109876543210
LCK K15LCK K14LCK K13LCK K12LCK K11LCK K10LCK K9LCK K8LCK K7LCK K6LCK K5LCK K4LCK K3LCK K2LCK K1LCK K0
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:17 Reserved, must be kept at reset value.

Bit 16 LCKK[16] : Lock key

This bit can be read any time. It can only be modified using the lock key write sequence.

0: Port configuration lock key not active

1: Port configuration lock key active. The GPIOx_LCKR register is locked until an MCU reset occurs.

LOCK key write sequence:

WR LCKR[16] = '1' + LCKR[15:0]

WR LCKR[16] = '0' + LCKR[15:0]

WR LCKR[16] = '1' + LCKR[15:0]

RD LCKR

RD LCKR[16] = '1' (this read operation is optional but it confirms that the lock is active)

Note: During the LOCK key write sequence, the value of LCK[15:0] must not change.

Any error in the lock sequence aborts the lock.

After the first lock sequence on any bit of the port, any read access on the LCKK bit will return '1' until the next CPU reset.

Bits 15:0 LCKy : Port x lock bit y (y= 0..15)

These bits are read/write but can only be written when the LCKK bit is '0'.

0: Port configuration not locked

1: Port configuration locked

7.4.9 GPIO alternate function low register (GPIOx_AFRL) (x = A..H)

Address offset: 0x20

Reset value: 0x0000 0000

31302928272625242322212019181716
AFRL7[3:0]AFRL6[3:0]AFRL5[3:0]AFRL4[3:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
AFRL3[3:0]AFRL2[3:0]AFRL1[3:0]AFRL0[3:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 AFRly : Alternate function selection for port x bit y (y = 0..7)

These bits are written by software to configure alternate function I/Os

AFRly selection:

0000: AF0

1000: AF8

0001: AF1

1001: AF9

0010: AF2

1010: AF10

0011: AF3

1011: AF11

0100: AF4

1100: AF12

0101: AF5

1101: AF13

0110: AF6

1110: AF14

0111: AF7

1111: AF15

7.4.10 GPIO alternate function high register (GPIOx_AFRH) (x = A..H)

Address offset: 0x24

Reset value: 0x0000 0000

31302928272625242322212019181716
AFRH15[3:0]AFRH14[3:0]AFRH13[3:0]AFRH12[3:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
1514131211109876543210
AFRH11[3:0]AFRH10[3:0]AFRH9[3:0]AFRH8[3:0]
rwrwrwrwrwrwrwrwrwrwrwrwrwrwrwrw

Bits 31:0 AFRHy : Alternate function selection for port x bit y (y = 8..15)

These bits are written by software to configure alternate function I/Os

AFRHy selection:

0000: AF01000: AF8
0001: AF11001: AF9
0010: AF21010: AF10
0011: AF31011: AF11
0100: AF41100: AF12
0101: AF51101: AF13
0110: AF61110: AF14
0111: AF71111: AF15

7.4.11 GPIO register map

The following table gives the GPIO register map and the reset values.

Table 26. GPIO register map and reset values

OffsetRegister313029282726252423222120191817161514131211109876543210
0x00GPIOA_MODERMODER15[1:0]MODER14[1:0]MODER13[1:0]MODER12[1:0]MODER11[1:0]MODER10[1:0]MODER9[1:0]MODER8[1:0]MODER7[1:0]MODER6[1:0]MODER5[1:0]MODER4[1:0]MODER3[1:0]MODER2[1:0]MODER1[1:0]MODER0[1:0]
Reset value10101000000000000000000000000000
0x00GPIOB_MODERMODER15[1:0]MODER14[1:0]MODER13[1:0]MODER12[1:0]MODER11[1:0]MODER10[1:0]MODER9[1:0]MODER8[1:0]MODER7[1:0]MODER6[1:0]MODER5[1:0]MODER4[1:0]MODER3[1:0]MODER2[1:0]MODER1[1:0]MODER0[1:0]
Reset value00000000000000000000001010000000
0x00GPIOx_MODER
(where x = C..H)
MODER15[1:0]MODER14[1:0]MODER13[1:0]MODER12[1:0]MODER11[1:0]MODER10[1:0]MODER9[1:0]MODER8[1:0]MODER7[1:0]MODER6[1:0]MODER5[1:0]MODER4[1:0]MODER3[1:0]MODER2[1:0]MODER1[1:0]MODER0[1:0]
Reset value00000000000000000000000000000000
0x04GPIOx_OTYPER
(where x = A..H)
ResResResResResResResResResResResResResResResResOT15OT14OT13OT12OT11OT10OT9OT8OT7OT6OT5OT4OT3OT2OT1OT0
Reset value0000000000000000
0x08GPIOx_OSPEEDER
(where x = A..H)
OSPEEDER15[1:0]OSPEEDER14[1:0]OSPEEDER13[1:0]OSPEEDER12[1:0]OSPEEDER11[1:0]OSPEEDER10[1:0]OSPEEDER9[1:0]OSPEEDER8[1:0]OSPEEDER7[1:0]OSPEEDER6[1:0]OSPEEDER5[1:0]OSPEEDER4[1:0]OSPEEDER3[1:0]OSPEEDER2[1:0]OSPEEDER1[1:0]OSPEEDER0[1:0]
Reset value x = A00001100000000000000000000000000
Reset value x = B00000000000000000000000011000000
Reset value x = C..H00000000000000000000000000000000
0x0CGPIOA_PUPDRPUPDR15[1:0]PUPDR14[1:0]PUPDR13[1:0]PUPDR12[1:0]PUPDR11[1:0]PUPDR10[1:0]PUPDR9[1:0]PUPDR8[1:0]PUPDR7[1:0]PUPDR6[1:0]PUPDR5[1:0]PUPDR4[1:0]PUPDR3[1:0]PUPDR2[1:0]PUPDR1[1:0]PUPDR0[1:0]
Reset value01100100000000000000000000000000
0x0CGPIOB_PUPDRPUPDR15[1:0]PUPDR14[1:0]PUPDR13[1:0]PUPDR12[1:0]PUPDR11[1:0]PUPDR10[1:0]PUPDR9[1:0]PUPDR8[1:0]PUPDR7[1:0]PUPDR6[1:0]PUPDR5[1:0]PUPDR4[1:0]PUPDR3[1:0]PUPDR2[1:0]PUPDR1[1:0]PUPDR0[1:0]
Reset value00000000000000000000000100000000

Table 26. GPIO register map and reset values (continued)

OffsetRegister313029282726252423222120191817161514131211109876543210
0x0CGPIOx_PUPDR
(where x = C..H)
PUPDR15[1:0]PUPDR14[1:0]PUPDR13[1:0]PUPDR12[1:0]PUPDR11[1:0]PUPDR10[1:0]PUPDR9[1:0]PUPDR8[1:0]PUPDR7[1:0]PUPDR6[1:0]PUPDR5[1:0]PUPDR4[1:0]PUPDR3[1:0]PUPDR2[1:0]PUPDR1[1:0]PUPDR0[1:0]
Reset value000000000000000000000000000000000
0x10GPIOx_IDR
(where x = A..H)
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.IDR15IDR14IDR13IDR12IDR11IDR10IDR9IDR8IDR7IDR6IDR5IDR4IDR3IDR2IDR1IDR0
Reset valuexxxxxxxxxxxxxxxx
0x14GPIOx_ODR
(where x = A..H)
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.ODR15ODR14ODR13ODR12ODR11ODR10ODR9ODR8ODR7ODR6ODR5ODR4ODR3ODR2ODR1ODR0
Reset value0000000000000000
0x18GPIOx_BSRR
(where x = A..H)
BR15BR14BR13BR12BR11BR10BR9BR8BR7BR6BR5BR4BR3BR2BR1BR0BS15BS14BS13BS12BS11BS10BS9BS8BS7BS6BS5BS4BS3BS2BS1BS0
Reset value00000000000000000000000000000000
0x1CGPIOx_LCKR
(where x = A..H)
Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.Res.LCK15LCK14LCK13LCK12LCK11LCK10LCK9LCK8LCK7LCK6LCK5LCK4LCK3LCK2LCK1LCK0
Reset value0000000000000000
0x20GPIOx_AFR
(where x = A..H)
AFRL7[3:0]AFRL6[3:0]AFRL5[3:0]AFRL4[3:0]AFRL3[3:0]AFRL2[3:0]AFRL1[3:0]AFRL0[3:0]
Reset value000000000000000000000000000000000
0x24GPIOx_AFRH
(where x = A..H)
AFRH15[3:0]AFRH14[3:0]AFRH13[3:0]AFRH12[3:0]AFRH11[3:0]AFRH10[3:0]AFRH9[3:0]AFRH8[3:0]
Reset value000000000000000000000000000000000
Refer to Section 2.2 on page 56 for the register boundary addresses.