libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
GPIO peripheral API
Collaboration diagram for GPIO peripheral API:

Functions

void gpio_set (uint32_t gpioport, uint16_t gpios)
 Set a Group of Pins Atomic. More...
 
void gpio_clear (uint32_t gpioport, uint16_t gpios)
 Clear a Group of Pins Atomic. More...
 
uint16_t gpio_get (uint32_t gpioport, uint16_t gpios)
 Read a Group of Pins. More...
 
void gpio_toggle (uint32_t gpioport, uint16_t gpios)
 Toggle a Group of Pins. More...
 
uint16_t gpio_port_read (uint32_t gpioport)
 Read from a Port. More...
 
void gpio_port_write (uint32_t gpioport, uint16_t data)
 Write to a Port. More...
 
void gpio_port_config_lock (uint32_t gpioport, uint16_t gpios)
 Lock the Configuration of a Group of Pins. More...
 
void gpio_mode_setup (uint32_t gpioport, uint8_t mode, uint8_t pull_up_down, uint16_t gpios)
 Set GPIO Pin Mode. More...
 
void gpio_set_output_options (uint32_t gpioport, uint8_t otype, uint8_t speed, uint16_t gpios)
 Set GPIO Output Options. More...
 
void gpio_set_af (uint32_t gpioport, uint8_t alt_func_num, uint16_t gpios)
 Set GPIO Alternate Function Selection. More...
 

Detailed Description

Author
© 2009 Uwe Hermann uwe@h.nosp@m.erma.nosp@m.nn-uw.nosp@m.e.de
© 2009 Uwe Hermann uwe@h.nosp@m.erma.nosp@m.nn-uw.nosp@m.e.de
© 2012 Ken Sarkies ksark.nosp@m.ies@.nosp@m.inter.nosp@m.node.nosp@m..on.n.nosp@m.et

Each I/O port has 16 individually configurable bits. Many I/O pins share GPIO functionality with a number of alternate functions and must be configured to the alternate function mode if these are to be accessed. A feature is available to remap alternative functions to a limited set of alternative pins in the event of a clash of requirements.

The data registers associated with each port for input and output are 32 bit with the upper 16 bits unused. The output buffer must be written as a 32 bit word, but individual bits may be set or reset separately in atomic operations to avoid race conditions during interrupts. Bits may also be individually locked to prevent accidental configuration changes. Once locked the configuration cannot be changed until after the next reset.

Each port bit can be configured as analog or digital input, the latter can be floating or pulled up or down. As outputs they can be configured as either push-pull or open drain, digital I/O or alternate function, and with maximum output speeds of 2MHz, 10MHz, or 50MHz.

On reset all ports are configured as digital floating input.

Basic GPIO Handling API.

Example 1: Push-pull digital output actions with pullup on ports C2 and C9

void gpio_toggle(uint32_t gpioport, uint16_t gpios)
Toggle a Group of Pins.
void gpio_mode_setup(uint32_t gpioport, uint8_t mode, uint8_t pull_up_down, uint16_t gpios)
Set GPIO Pin Mode.
void gpio_set(uint32_t gpioport, uint16_t gpios)
Set a Group of Pins Atomic.
void gpio_clear(uint32_t gpioport, uint16_t gpios)
Clear a Group of Pins Atomic.
void gpio_set_output_options(uint32_t gpioport, uint8_t otype, uint8_t speed, uint16_t gpios)
Set GPIO Output Options.
void gpio_port_write(uint32_t gpioport, uint16_t data)
Write to a Port.
#define GPIO_MODE_OUTPUT
#define GPIO_OTYPE_PP
Push Pull.
#define GPIO2
#define GPIO9
#define GPIOC
#define GPIO_PUPD_PULLUP
#define GPIO_OSPEED_25MHZ

Example 2: Digital input on port C12 with pullup

uint16_t gpio_port_read(uint32_t gpioport)
Read from a Port.
#define GPIO_MODE_INPUT
#define GPIO12

Function Documentation

◆ gpio_clear()

void gpio_clear ( uint32_t  gpioport,
uint16_t  gpios 
)

Clear a Group of Pins Atomic.

Clear one or more pins of the given GPIO port to 0 in an atomic operation.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be changed, use bitwise OR '|' to separate them.

Definition at line 56 of file gpio_common_all.c.

References GPIO_BSRR.

◆ gpio_get()

uint16_t gpio_get ( uint32_t  gpioport,
uint16_t  gpios 
)

Read a Group of Pins.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be read, use bitwise OR '|' to separate them.
Returns
Unsigned int16 value of the pin values. The bit position of the pin value returned corresponds to the pin number.

Definition at line 71 of file gpio_common_all.c.

References gpio_port_read().

Here is the call graph for this function:

◆ gpio_mode_setup()

void gpio_mode_setup ( uint32_t  gpioport,
uint8_t  mode,
uint8_t  pull_up_down,
uint16_t  gpios 
)

Set GPIO Pin Mode.

Sets the Pin Direction and Analog/Digital Mode, and Output Pin Pullup, for a set of GPIO pins on a given GPIO port.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]modeUnsigned int8. Pin mode GPIO Pin Direction and Analog/Digital Mode
[in]pull_up_downUnsigned int8. Pin pullup/pulldown configuration GPIO Output Pin Pullup
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be set, use bitwise OR '|' to separate them.

Definition at line 89 of file gpio_common_f0234.c.

References GPIO_MODE, GPIO_MODE_MASK, GPIO_MODER, GPIO_PUPD, GPIO_PUPD_MASK, and GPIO_PUPDR.

◆ gpio_port_config_lock()

void gpio_port_config_lock ( uint32_t  gpioport,
uint16_t  gpios 
)

Lock the Configuration of a Group of Pins.

The configuration of one or more pins of the given GPIO port is locked. There is no mechanism to unlock these via software. Unlocking occurs at the next reset.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be locked, use bitwise OR '|' to separate them.

Definition at line 132 of file gpio_common_all.c.

References GPIO_LCKK, and GPIO_LCKR.

◆ gpio_port_read()

uint16_t gpio_port_read ( uint32_t  gpioport)

Read from a Port.

Read the current value of the given GPIO port. Only the lower 16 bits contain valid pin data.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
Returns
Unsigned int16. The value held in the specified GPIO port.

Definition at line 102 of file gpio_common_all.c.

References GPIO_IDR.

Referenced by gpio_get().

Here is the caller graph for this function:

◆ gpio_port_write()

void gpio_port_write ( uint32_t  gpioport,
uint16_t  data 
)

Write to a Port.

Write a value to the given GPIO port.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]dataUnsigned int16. The value to be written to the GPIO port.

Definition at line 115 of file gpio_common_all.c.

References GPIO_ODR.

◆ gpio_set()

void gpio_set ( uint32_t  gpioport,
uint16_t  gpios 
)

Set a Group of Pins Atomic.

Set one or more pins of the given GPIO port to 1 in an atomic operation.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be changed, use bitwise OR '|' to separate them.

Definition at line 41 of file gpio_common_all.c.

References GPIO_BSRR.

◆ gpio_set_af()

void gpio_set_af ( uint32_t  gpioport,
uint8_t  alt_func_num,
uint16_t  gpios 
)

Set GPIO Alternate Function Selection.

Set the alternate function mapping number for each pin. Most pins have alternate functions associated with them. When set to AF mode, a pin may be used for one of its allocated alternate functions selected by the number given here. To determine the number to be used for the desired function refer to the individual datasheet for the particular device. A table is given under the Pin Selection chapter.

Note that a number of pins may be set but only with a single AF number. In practice this would rarely be useful as each pin is likely to require a different number.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]alt_func_numUnsigned int8. Pin alternate function number Alternate Function Pin Selection
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be set, use bitwise OR '|' to separate them.

Definition at line 178 of file gpio_common_f0234.c.

References GPIO_AFR, GPIO_AFR_MASK, GPIO_AFRH, and GPIO_AFRL.

◆ gpio_set_output_options()

void gpio_set_output_options ( uint32_t  gpioport,
uint8_t  otype,
uint8_t  speed,
uint16_t  gpios 
)

Set GPIO Output Options.

When the pin is set to output mode, this sets the configuration (analog/digital and open drain/push pull) and speed, for a set of GPIO pins on a given GPIO port.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]otypeUnsigned int8. Pin output type GPIO Output Pin Driver Type
[in]speedUnsigned int8. Pin speed GPIO Output Pin Speed
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be set, use bitwise OR '|' to separate them.

Definition at line 132 of file gpio_common_f0234.c.

References GPIO_OSPEED, GPIO_OSPEED_MASK, GPIO_OSPEEDR, and GPIO_OTYPER.

◆ gpio_toggle()

void gpio_toggle ( uint32_t  gpioport,
uint16_t  gpios 
)

Toggle a Group of Pins.

Toggle one or more pins of the given GPIO port. The toggling is not atomic, but the non-toggled pins are not affected.

Parameters
[in]gpioportUnsigned int32. Port identifier GPIO Port IDs
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be changed, use bitwise OR '|' to separate them.

Definition at line 87 of file gpio_common_all.c.

References GPIO_BSRR, and GPIO_ODR.