4.1.6.3. GPIO

4.1.6.3.1. General-Purpose I/O (GPIO) Port Registers

Each General-Purpose Input/Output (GPIO) port in the STM32 microcontrollers has several registers that are used for configuring and controlling the pins. These registers allow the user to set pin modes, configure output types and speeds, enable pull-up/pull-down resistors, read input values, and control pin states. Here’s a breakdown of the key registers for a GPIO port:

4.1.6.3.2. Configuration Registers

4.1.6.3.2.1. GPIOx_MODER (Mode Register)

This register is used to configure the mode of each GPIO pin. The mode can be:

  • 00: Input mode

  • 01: Output mode

  • 10: Alternate function mode

  • 11: Analog mode

4.1.6.3.2.2. GPIOx_OTYPER (Output Type Register)

This register controls the output type for each pin. It can be:

  • 0: Push-pull output

  • 1: Open-drain output

4.1.6.3.2.3. GPIOx_OSPEEDR (Output Speed Register)

This register configures the output speed for each pin. The options include:

  • 00: Low speed

  • 01: Medium speed

  • 10: High speed

  • 11: Very high speed

4.1.6.3.2.4. GPIOx_PUPDR (Pull-up/Pull-down Register)

This register configures the internal pull-up or pull-down resistors for each pin:

  • 00: No pull-up, no pull-down

  • 01: Pull-up

  • 10: Pull-down

  • 11: Reserved

4.1.6.3.3. Data Registers

4.1.6.3.3.1. GPIOx_IDR (Input Data Register)

This register contains the value of the input pins. Each bit corresponds to the input state of a specific GPIO pin:

  • 0: Pin is low

  • 1: Pin is high

4.1.6.3.3.2. GPIOx_ODR (Output Data Register)

This register holds the output values for the GPIO pins. Each bit corresponds to the output state of a specific pin:

  • 0: Pin is low

  • 1: Pin is high

4.1.6.3.4. Set/Reset Register

4.1.6.3.4.1. GPIOx_BSRR (Bit Set/Reset Register)

This register allows setting or resetting individual GPIO pins. The first 16 bits are used to set the corresponding pin, and the last 16 bits are used to reset the corresponding pin:

  • Writing a 1 to a bit in the first 16 bits sets the corresponding pin.

  • Writing a 1 to a bit in the last 16 bits resets the corresponding pin.

4.1.6.3.5. Lock Register

4.1.6.3.5.1. GPIOx_LCKR (Lock Register)

This register is used to lock the configuration of GPIO pins. Once a pin is locked, its configuration cannot be changed except through a reset. The lock sequence is:

  1. Write 1 to the LCKK bit (bit 16).

  2. Write the desired pin configuration to the LCK[15:0] bits.

  3. Write 1 to the LCKK bit again.

  4. The LCK bit for a pin is locked when it becomes 1 after the second write to LCKK.

4.1.6.3.6. Alternate Function Selection Registers

4.1.6.3.6.1. GPIOx_AFRL (Alternate Function Low Register)

This register is used for selecting alternate functions for pins 0 to 7. Each pair of bits (AF[1:0]) determines the alternate function for each pin:

  • Example: If pin 0 is configured for an alternate function, its corresponding bits in AFRL define which alternate function is used.

4.1.6.3.6.2. GPIOx_AFRH (Alternate Function High Register)

This register is used for selecting alternate functions for pins 8 to 15. Each pair of bits (AF[1:0]) corresponds to the alternate function selection for these pins.