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

libopencm3 STM32F1xx General Purpose I/O More...

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_set_mode (uint32_t gpioport, uint8_t mode, uint8_t cnf, uint16_t gpios)
 Set GPIO Pin Mode. More...
 
void gpio_set_eventout (uint8_t evoutport, uint8_t evoutpin)
 Map the EVENTOUT signal. More...
 
void gpio_primary_remap (uint32_t swjdisable, uint32_t maps)
 Map Alternate Function Port Bits (Main Set) More...
 
void gpio_secondary_remap (uint32_t maps)
 Map Alternate Function Port Bits (Secondary Set) More...
 

Detailed Description

libopencm3 STM32F1xx General Purpose I/O

Author
© 2009 Uwe Hermann uwe@h.nosp@m.erma.nosp@m.nn-uw.nosp@m.e.de
Version
1.0.0
Author
© 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
Date
18 August 2012

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 on ports C2 and C9

#define GPIO_CNF_OUTPUT_PUSHPULL
Digital Output Pushpull.
Definition: f1/gpio.h:619
void gpio_toggle(uint32_t gpioport, uint16_t gpios)
Toggle a Group of Pins.
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_mode(uint32_t gpioport, uint8_t mode, uint8_t cnf, uint16_t gpios)
Set GPIO Pin Mode.
Definition: gpio.c:93
void gpio_port_write(uint32_t gpioport, uint16_t data)
Write to a Port.
#define GPIO_MODE_OUTPUT_2_MHZ
Definition: f1/gpio.h:638
#define GPIO2
#define GPIO9
#define GPIOC
Definition: f1/gpio.h:51

Example 1: Digital input on port C12

#define GPIO_CNF_INPUT_FLOAT
Digital Input Floating.
Definition: f1/gpio.h:614
uint16_t gpio_port_read(uint32_t gpioport)
Read from a Port.
#define GPIO_MODE_INPUT
Definition: f1/gpio.h:636
#define GPIO12

LGPL License Terms libopencm3 License

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_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_primary_remap()

void gpio_primary_remap ( uint32_t  swjdisable,
uint32_t  maps 
)

Map Alternate Function Port Bits (Main Set)

A number of alternate function ports can be remapped to defined alternative port bits to avoid clashes in cases where multiple alternate functions are present. Refer to the datasheets for the particular mapping desired. This provides the main set of remap functionality. See gpio_secondary_remap for a number of lesser used remaps.

The AFIO remapping feature is used only with the STM32F10x series.

Note
The Serial Wire JTAG disable controls allow certain GPIO ports to become available in place of some of the SWJ signals. Full SWJ capability is obtained by setting this to zero. The value of this must be specified for every call to this function as its current value cannot be ascertained from the hardware.
Parameters
[in]swjdisableDisable parts of the SWJ capability Serial Wire JTAG disables.
[in]mapsBitwise OR of map enable controls you wish to enable from Alternate Function Remap Controls, Alternate Function Remap Controls for CAN 1, Alternate Function Remap Controls for Timer 3, Alternate Function Remap Controls for Timer 2, Alternate Function Remap Controls for Timer 1, Alternate Function Remap Controls for USART 3. For connectivity line devices only Alternate Function Remap Controls for Connectivity are also available.

Definition at line 167 of file gpio.c.

References AFIO_MAPR, and AFIO_MAPR_SWJ_MASK.

◆ gpio_secondary_remap()

void gpio_secondary_remap ( uint32_t  maps)

Map Alternate Function Port Bits (Secondary Set)

A number of alternate function ports can be remapped to defined alternative port bits to avoid clashes in cases where multiple alternate functions are present. Refer to the datasheets for the particular mapping desired. This provides the second smaller and less used set of remap functionality. See gpio_primary_remap for the main set of remaps.

The AFIO remapping feature is used only with the STM32F10x series.

Parameters
[in]mapsUnsigned int32. Bitwise OR of map enable controls from Alternate Function Remap Controls Secondary Set

Definition at line 192 of file gpio.c.

References AFIO_MAPR2.

◆ 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_eventout()

void gpio_set_eventout ( uint8_t  evoutport,
uint8_t  evoutpin 
)

Map the EVENTOUT signal.

Enable the EVENTOUT signal and select the port and pin to be used.

Parameters
[in]evoutportUnsigned int8. Port for EVENTOUT signal EVENTOUT Port selection
[in]evoutpinUnsigned int8. Pin for EVENTOUT signal EVENTOUT Pin selection

Definition at line 139 of file gpio.c.

References AFIO_EVCR, and AFIO_EVCR_EVOE.

◆ gpio_set_mode()

void gpio_set_mode ( uint32_t  gpioport,
uint8_t  mode,
uint8_t  cnf,
uint16_t  gpios 
)

Set GPIO Pin Mode.

Sets the mode (input/output) and configuration (analog/digitial and open drain/push pull), 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 Mode
[in]cnfUnsigned int8. Pin configuration GPIO Pin Configuration
[in]gpiosUnsigned int16. Pin identifiers GPIO Pin Identifiers If multiple pins are to be set, use bitwise OR '|' to separate them.

Definition at line 93 of file gpio.c.

References GPIO_CRH, and GPIO_CRL.

◆ 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.