gpio.h

General Purpose Input/Output (GPIO) port and Alternate Function Input/Output (AFIO) support.

Types

struct stm32f1::gpio_reg_map

GPIO register map type.

struct stm32f2_f4::gpio_reg_map

GPIO register map type.

struct gpio_dev

GPIO device type.

enum stm32f1::gpio_pin_mode

GPIO pin modes.

These only allow for 50MHZ max output speeds; if you want slower, use direct register access.

Values:

GPIO_OUTPUT_PP = GPIO_CR_CNF_OUTPUT_PP | GPIO_CR_MODE_OUTPUT_50MHZ

Output push-pull.

GPIO_OUTPUT_OD = GPIO_CR_CNF_OUTPUT_OD | GPIO_CR_MODE_OUTPUT_50MHZ

Output open-drain.

GPIO_AF_OUTPUT_PP = GPIO_CR_CNF_AF_OUTPUT_PP | GPIO_CR_MODE_OUTPUT_50MHZ

Alternate function output push-pull.

GPIO_AF_OUTPUT_OD = GPIO_CR_CNF_AF_OUTPUT_OD | GPIO_CR_MODE_OUTPUT_50MHZ

Alternate function output open drain.

GPIO_INPUT_ANALOG = GPIO_CR_CNF_INPUT_ANALOG | GPIO_CR_MODE_INPUT

Analog input.

GPIO_INPUT_FLOATING = GPIO_CR_CNF_INPUT_FLOATING | GPIO_CR_MODE_INPUT

Input floating.

GPIO_INPUT_PD = GPIO_CR_CNF_INPUT_PU_PD | GPIO_CR_MODE_INPUT

Input pull-down.

GPIO_INPUT_PU

Input pull-up.

struct stm32f1::afio_reg_map

AFIO register map.

typedef exti_cfg stm32f1::afio_exti_port

Deprecated.

Use exti_cfg instead.

In previous versions of libmaple, exti_attach_interrupt() took an afio_exti_port argument; afio_exti_port was also a member of struct gpio_dev. This isn’t portable, so we now use exti_cfg instead. This typedef (and the macros AFIO_EXTI_PA, ..., AFIO_EXTI_PG) exist to preserve backwards compatibility.

typedef exti_num stm32f1::afio_exti_num

Deprecated.

Use exti_num instead.

In previous versions of libmaple, exti_attach_interrupt() took an afio_exti_num argument. This isn’t portable, so we use exti_num instead. This typedef (and the macros AFIO_EXTI_0, ..., AFIO_EXTI_15) exist to preserve backwards compatibility.

typedef enum stm32f1::afio_remap_peripheral stm32f1::afio_remap_peripheral

Available peripheral remaps.

See
afio_remap()

typedef enum stm32f1::afio_debug_cfg stm32f1::afio_debug_cfg

Debug port configuration.

Used to configure the behavior of JTAG and Serial Wire (SW) debug ports and their associated GPIO pins.

See
afio_cfg_debug_ports()

Devices

struct gpio_dev *stm32f1::GPIOA

GPIO port A device.

struct gpio_dev *stm32f1::GPIOB

GPIO port B device.

struct gpio_dev *stm32f1::GPIOC

GPIO port C device.

struct gpio_dev *stm32f1::GPIOD

GPIO port D device.

struct gpio_dev *stm32f1::GPIOE

GPIO port E device.

struct gpio_dev *stm32f1::GPIOF

GPIO port F device.

struct gpio_dev *stm32f1::GPIOG

GPIO port G device.

Functions

void gpio_init(gpio_dev *dev)

Initialize a GPIO device.

Enables the clock for and resets the given device.

Parameters
  • dev -

    GPIO device to initialize.

void gpio_init_all(void)

Initialize and reset all available GPIO devices.

void gpio_set_mode(gpio_dev *dev, uint8 pin, gpio_pin_mode mode)

Set the mode of a GPIO pin.

See
gpio_pin_mode
Parameters
  • dev -

    GPIO device.

  • pin -

    Pin on the device whose mode to set, 015.

  • mode -

    General purpose or alternate function mode to set the pin to.

static exti_cfg gpio_exti_port(gpio_dev *dev)

Get a GPIO port’s corresponding EXTI port configuration.

Parameters
  • dev -

    GPIO port whose exti_cfg to return.

static void gpio_write_bit(gpio_dev *dev, uint8 pin, uint8 val)

Set or reset a GPIO pin.

Pin must have previously been configured to output mode.

Parameters
  • dev -

    GPIO device whose pin to set.

  • pin -

    Pin on to set or reset

  • val -

    If true, set the pin. If false, reset the pin.

static uint32 gpio_read_bit(gpio_dev *dev, uint8 pin)

Determine whether or not a GPIO pin is set.

Pin must have previously been configured to input mode.

Return
True if the pin is set, false otherwise.
Parameters
  • dev -

    GPIO device whose pin to test.

  • pin -

    Pin on dev to test.

static void gpio_toggle_bit(gpio_dev *dev, uint8 pin)

Toggle a pin configured as output push-pull.

Parameters
  • dev -

    GPIO device.

  • pin -

    Pin on dev to toggle.

void stm32f1::afio_init(void)

Initialize the AFIO clock, and reset the AFIO registers.

afio_exti_select(exti, port)

Macro for exti_select((exti), (port)).

void stm32f1::afio_remap(afio_remap_peripheral p)
static void stm32f1::afio_cfg_debug_ports(afio_debug_cfg config)

Enable or disable the JTAG and SW debug ports.

See
afio_debug_cfg
Parameters
  • config -

    Desired debug port configuration

Register Map Base Pointers

GPIOA_BASE

GPIO port A register map base pointer.

GPIOB_BASE

GPIO port B register map base pointer.

GPIOC_BASE

GPIO port C register map base pointer.

GPIOD_BASE

GPIO port D register map base pointer.

GPIOE_BASE

GPIO port E register map base pointer.

GPIOF_BASE

GPIO port F register map base pointer.

GPIOG_BASE

GPIO port G register map base pointer.

AFIO_BASE

AFIO register map base pointer.

Register Bit Definitions

GPIO Control Registers

These values apply to both the low and high configuration registers (ST RM0008: GPIOx_CRL and GPIOx_CRH). You can shift them right by the appropriate number of bits for the GPIO port bit you’re interested in to obtain a bit mask.

For example, to mask out just the value of GPIOA_CRH_CNF12, note that GPIO port bit 12’s configuration starts at bit 18 in the corresponding CRH. Thus, an appropriate mask is GPIOA_BASE->CRH & (GPIO_CR_CNF << 18).

GPIO_CR_CNF_INPUT_ANALOG
GPIO_CR_CNF_INPUT_FLOATING
GPIO_CR_CNF_INPUT_PU_PD
GPIO_CR_CNF_OUTPUT_PP
GPIO_CR_CNF_OUTPUT_OD
GPIO_CR_CNF_AF_OUTPUT_PP
GPIO_CR_CNF_AF_OUTPUT_OD
GPIO_CR_MODE_INPUT
GPIO_CR_MODE_OUTPUT_10MHZ
GPIO_CR_MODE_OUTPUT_2MHZ
GPIO_CR_MODE_OUTPUT_50MHZ

Event Control Register

AFIO_EVCR_EVOE
AFIO_EVCR_PORT_PA
AFIO_EVCR_PORT_PB
AFIO_EVCR_PORT_PC
AFIO_EVCR_PORT_PD
AFIO_EVCR_PORT_PE
AFIO_EVCR_PIN_0
AFIO_EVCR_PIN_1
AFIO_EVCR_PIN_2
AFIO_EVCR_PIN_3
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AFIO_MAPR_USART3_REMAP_FULL
AFIO_MAPR_USART2_REMAP
AFIO_MAPR_USART1_REMAP
AFIO_MAPR_I2C1_REMAP
AFIO_MAPR_SPI1_REMAP

External Interrupt Configuration Register 1

AFIO_EXTICR1_EXTI3
AFIO_EXTICR1_EXTI3_PA
AFIO_EXTICR1_EXTI3_PB
AFIO_EXTICR1_EXTI3_PC
AFIO_EXTICR1_EXTI3_PD
AFIO_EXTICR1_EXTI3_PE
AFIO_EXTICR1_EXTI3_PF
AFIO_EXTICR1_EXTI3_PG
AFIO_EXTICR1_EXTI2
AFIO_EXTICR1_EXTI2_PA
AFIO_EXTICR1_EXTI2_PB
AFIO_EXTICR1_EXTI2_PC
AFIO_EXTICR1_EXTI2_PD
AFIO_EXTICR1_EXTI2_PE
AFIO_EXTICR1_EXTI2_PF
AFIO_EXTICR1_EXTI2_PG
AFIO_EXTICR1_EXTI1
AFIO_EXTICR1_EXTI1_PA
AFIO_EXTICR1_EXTI1_PB
AFIO_EXTICR1_EXTI1_PC
AFIO_EXTICR1_EXTI1_PD
AFIO_EXTICR1_EXTI1_PE
AFIO_EXTICR1_EXTI1_PF
AFIO_EXTICR1_EXTI1_PG
AFIO_EXTICR1_EXTI0
AFIO_EXTICR1_EXTI0_PA
AFIO_EXTICR1_EXTI0_PB
AFIO_EXTICR1_EXTI0_PC
AFIO_EXTICR1_EXTI0_PD
AFIO_EXTICR1_EXTI0_PE
AFIO_EXTICR1_EXTI0_PF
AFIO_EXTICR1_EXTI0_PG

External Interrupt Configuration Register 2

AFIO_EXTICR2_EXTI7
AFIO_EXTICR2_EXTI7_PA
AFIO_EXTICR2_EXTI7_PB
AFIO_EXTICR2_EXTI7_PC
AFIO_EXTICR2_EXTI7_PD
AFIO_EXTICR2_EXTI7_PE
AFIO_EXTICR2_EXTI7_PF
AFIO_EXTICR2_EXTI7_PG
AFIO_EXTICR2_EXTI6
AFIO_EXTICR2_EXTI6_PA
AFIO_EXTICR2_EXTI6_PB
AFIO_EXTICR2_EXTI6_PC
AFIO_EXTICR2_EXTI6_PD
AFIO_EXTICR2_EXTI6_PE
AFIO_EXTICR2_EXTI6_PF
AFIO_EXTICR2_EXTI6_PG
AFIO_EXTICR2_EXTI5
AFIO_EXTICR2_EXTI5_PA
AFIO_EXTICR2_EXTI5_PB
AFIO_EXTICR2_EXTI5_PC
AFIO_EXTICR2_EXTI5_PD
AFIO_EXTICR2_EXTI5_PE
AFIO_EXTICR2_EXTI5_PF
AFIO_EXTICR2_EXTI5_PG
AFIO_EXTICR2_EXTI4
AFIO_EXTICR2_EXTI4_PA
AFIO_EXTICR2_EXTI4_PB
AFIO_EXTICR2_EXTI4_PC
AFIO_EXTICR2_EXTI4_PD
AFIO_EXTICR2_EXTI4_PE
AFIO_EXTICR2_EXTI4_PF
AFIO_EXTICR2_EXTI4_PG

AF Remap and Debug I/O Configuration Register 2

AFIO_MAPR2_FSMC_NADV
AFIO_MAPR2_TIM14_REMAP
AFIO_MAPR2_TIM13_REMAP
AFIO_MAPR2_TIM11_REMAP
AFIO_MAPR2_TIM10_REMAP
AFIO_MAPR2_TIM9_REMAP