This is the documentation for the latest (master) development branch of Zephyr. If you are looking for the documentation of previous releases, use the drop-down menu on the left and select the desired version.

GPIO

Overview

Configuration Options

Related configuration options:

API Reference

group gpio_interface

GPIO Driver APIs.

GPIO input/output configuration flags

GPIO_INPUT

Enables pin as input.

GPIO_OUTPUT

Enables pin as output, no change to the output state.

GPIO_DISCONNECTED

Disables pin for both input and output.

GPIO_OUTPUT_LOW

Configures GPIO pin as output and initializes it to a low state.

GPIO_OUTPUT_HIGH

Configures GPIO pin as output and initializes it to a high state.

GPIO_OUTPUT_INACTIVE

Configures GPIO pin as output and initializes it to a logic 0.

GPIO_OUTPUT_ACTIVE

Configures GPIO pin as output and initializes it to a logic 1.

GPIO interrupt configuration flags

The GPIO_INT_* flags are used to specify how input GPIO pins will trigger interrupts. The interrupts can be sensitive to pin physical or logical level. Interrupts sensitive to pin logical level take into account GPIO_ACTIVE_LOW flag. If a pin was configured as Active Low, physical level low will be considered as logical level 1 (an active state), physical level high will be considered as logical level 0 (an inactive state).

GPIO_INT_DISABLE

Disables GPIO pin interrupt.

GPIO_INT_EDGE_RISING

Configures GPIO interrupt to be triggered on pin rising edge and enables it.

GPIO_INT_EDGE_FALLING

Configures GPIO interrupt to be triggered on pin falling edge and enables it.

GPIO_INT_EDGE_BOTH

Configures GPIO interrupt to be triggered on pin rising or falling edge and enables it.

GPIO_INT_LEVEL_LOW

Configures GPIO interrupt to be triggered on pin physical level low and enables it.

GPIO_INT_LEVEL_HIGH

Configures GPIO interrupt to be triggered on pin physical level high and enables it.

GPIO_INT_EDGE_TO_INACTIVE

Configures GPIO interrupt to be triggered on pin state change to logical level 0 and enables it.

GPIO_INT_EDGE_TO_ACTIVE

Configures GPIO interrupt to be triggered on pin state change to logical level 1 and enables it.

GPIO_INT_LEVEL_INACTIVE

Configures GPIO interrupt to be triggered on pin logical level 0 and enables it.

GPIO_INT_LEVEL_ACTIVE

Configures GPIO interrupt to be triggered on pin logical level 1 and enables it.

GPIO drive strength flags

The GPIO_DS_* flags are used with gpio_pin_configure to specify the drive strength configuration of a GPIO pin.

The drive strength of individual pins can be configured independently for when the pin output is low and high.

The GPIO_DS_*_LOW enumerations define the drive strength of a pin when output is low.

The GPIO_DS_*_HIGH enumerations define the drive strength of a pin when output is high.

The interface supports two different drive strengths: DFLT - The lowest drive strength supported by the HW ALT - The highest drive strength supported by the HW

On hardware that supports only one standard drive strength, both DFLT and ALT have the same behavior.

GPIO_DS_DFLT_LOW

Default drive strength standard when GPIO pin output is low.

GPIO_DS_ALT_LOW

Alternative drive strength when GPIO pin output is low. For hardware that does not support configurable drive strength use the default drive strength.

GPIO_DS_DFLT_HIGH

Default drive strength when GPIO pin output is high.

GPIO_DS_ALT_HIGH

Alternative drive strength when GPIO pin output is high. For hardware that does not support configurable drive strengths use the default drive strength.

GPIO pin active level flags

GPIO_ACTIVE_LOW

GPIO pin is active (has logical value ‘1’) in low state.

GPIO_ACTIVE_HIGH

GPIO pin is active (has logical value ‘1’) in high state.

GPIO pin drive flags

GPIO_OPEN_DRAIN

Configures GPIO output in open drain mode (wired AND).

Note

’Open Drain’ mode also known as ‘Open Collector’ is an output configuration which behaves like a switch that is either connected to ground or disconnected.

GPIO_OPEN_SOURCE

Configures GPIO output in open source mode (wired OR).

Note

’Open Source’ is a term used by software engineers to describe output mode opposite to ‘Open Drain’. It behaves like a switch that is either connected to power supply or disconnected. There exist no corresponding hardware schematic and the term is generally unknown to hardware engineers.

GPIO pin bias flags

GPIO_PULL_UP

Enables GPIO pin pull-up.

GPIO_PULL_DOWN

Enable GPIO pin pull-down.

Defines

GPIO_INT_DEBOUNCE

Enable GPIO pin debounce.

Note

Drivers that do not support a debounce feature should ignore this flag rather than rejecting the configuration with -ENOTSUP.

GPIO_MAX_PINS_PER_PORT

Maximum number of pins that are supported by gpio_port_pins_t.

Typedefs

typedef uint32_t gpio_port_pins_t

Identifies a set of pins associated with a port.

The pin with index n is present in the set if and only if the bit identified by (1U << n) is set.

typedef uint32_t gpio_port_value_t

Provides values for a set of pins associated with a port.

The value for a pin with index n is high (physical mode) or active (logical mode) if and only if the bit identified by (1U << n) is set. Otherwise the value for the pin is low (physical mode) or inactive (logical mode).

Values of this type are often paired with a gpio_port_pins_t value that specifies which encoded pin values are valid for the operation.

typedef uint8_t gpio_pin_t

Provides a type to hold a GPIO pin index.

This reduced-size type is sufficient to record a pin number, e.g. from a devicetree GPIOS property.

typedef uint8_t gpio_dt_flags_t

Provides a type to hold GPIO devicetree flags.

All GPIO flags that can be expressed in devicetree fit in the low 8 bits of the full flags field, so use a reduced-size type to record that part of a GPIOS property.

typedef uint32_t gpio_flags_t

Provides a type to hold GPIO configuration flags.

This type is sufficient to hold all flags used to control GPIO configuration, whether pin or interrupt.

typedef void (*gpio_callback_handler_t)(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins)

Define the application callback handler function signature.

Note: cb pointer can be used to retrieve private data through

CONTAINER_OF() if original struct gpio_callback is stored in another private structure.
Parameters
  • port: Device struct for the GPIO device.

  • cb: Original struct gpio_callback owning this handler

  • pins: Mask of pins that triggers the callback handler

Functions

int gpio_pin_interrupt_configure(const struct device *port, gpio_pin_t pin, gpio_flags_t flags)

Configure pin interrupt.

Note

This function can also be used to configure interrupts on pins not controlled directly by the GPIO module. That is, pins which are routed to other modules such as I2C, SPI, UART.

Parameters
  • port: Pointer to device structure for the driver instance.

  • pin: Pin number.

  • flags: Interrupt configuration flags as defined by GPIO_INT_*.

Return Value
  • 0: If successful.

  • -ENOTSUP: If any of the configuration options is not supported (unless otherwise directed by flag documentation).

  • -EINVAL: Invalid argument.

  • -EBUSY: Interrupt line required to configure pin interrupt is already in use.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_pin_configure(const struct device *port, gpio_pin_t pin, gpio_flags_t flags)

Configure a single pin.

Parameters
  • port: Pointer to device structure for the driver instance.

  • pin: Pin number to configure.

  • flags: Flags for pin configuration: ‘GPIO input/output configuration flags’, ‘GPIO drive strength flags’, ‘GPIO pin drive flags’, ‘GPIO pin bias flags’, GPIO_INT_DEBOUNCE.

Return Value
  • 0: If successful.

  • -ENOTSUP: if any of the configuration options is not supported (unless otherwise directed by flag documentation).

  • -EINVAL: Invalid argument.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_get_raw(const struct device *port, gpio_port_value_t *value)

Get physical level of all input pins in a port.

A low physical level on the pin will be interpreted as value 0. A high physical level will be interpreted as value 1. This function ignores GPIO_ACTIVE_LOW flag.

Value of a pin with index n will be represented by bit n in the returned port value.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • value: Pointer to a variable where pin values will be stored.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_get(const struct device *port, gpio_port_value_t *value)

Get logical level of all input pins in a port.

Get logical level of an input pin taking into account GPIO_ACTIVE_LOW flag. If pin is configured as Active High, a low physical level will be interpreted as logical value 0. If pin is configured as Active Low, a low physical level will be interpreted as logical value 1.

Value of a pin with index n will be represented by bit n in the returned port value.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • value: Pointer to a variable where pin values will be stored.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_set_masked_raw(const struct device *port, gpio_port_pins_t mask, gpio_port_value_t value)

Set physical level of output pins in a port.

Writing value 0 to the pin will set it to a low physical level. Writing value 1 will set it to a high physical level. This function ignores GPIO_ACTIVE_LOW flag.

Pin with index n is represented by bit n in mask and value parameter.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • mask: Mask indicating which pins will be modified.

  • value: Value assigned to the output pins.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_set_masked(const struct device *port, gpio_port_pins_t mask, gpio_port_value_t value)

Set logical level of output pins in a port.

Set logical level of an output pin taking into account GPIO_ACTIVE_LOW flag. Value 0 sets the pin in logical 0 / inactive state. Value 1 sets the pin in logical 1 / active state. If pin is configured as Active High, the default, setting it in inactive state will force the pin to a low physical level. If pin is configured as Active Low, setting it in inactive state will force the pin to a high physical level.

Pin with index n is represented by bit n in mask and value parameter.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • mask: Mask indicating which pins will be modified.

  • value: Value assigned to the output pins.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_set_bits_raw(const struct device *port, gpio_port_pins_t pins)

Set physical level of selected output pins to high.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pins: Value indicating which pins will be modified.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_set_bits(const struct device *port, gpio_port_pins_t pins)

Set logical level of selected output pins to active.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pins: Value indicating which pins will be modified.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_clear_bits_raw(const struct device *port, gpio_port_pins_t pins)

Set physical level of selected output pins to low.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pins: Value indicating which pins will be modified.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_clear_bits(const struct device *port, gpio_port_pins_t pins)

Set logical level of selected output pins to inactive.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pins: Value indicating which pins will be modified.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_toggle_bits(const struct device *port, gpio_port_pins_t pins)

Toggle level of selected output pins.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pins: Value indicating which pins will be modified.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_set_clr_bits_raw(const struct device *port, gpio_port_pins_t set_pins, gpio_port_pins_t clear_pins)

Set physical level of selected output pins.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • set_pins: Value indicating which pins will be set to high.

  • clear_pins: Value indicating which pins will be set to low.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_port_set_clr_bits(const struct device *port, gpio_port_pins_t set_pins, gpio_port_pins_t clear_pins)

Set logical level of selected output pins.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • set_pins: Value indicating which pins will be set to active.

  • clear_pins: Value indicating which pins will be set to inactive.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_pin_get_raw(const struct device *port, gpio_pin_t pin)

Get physical level of an input pin.

A low physical level on the pin will be interpreted as value 0. A high physical level will be interpreted as value 1. This function ignores GPIO_ACTIVE_LOW flag.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pin: Pin number.

Return Value
  • 1: If pin physical level is high.

  • 0: If pin physical level is low.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_pin_get(const struct device *port, gpio_pin_t pin)

Get logical level of an input pin.

Get logical level of an input pin taking into account GPIO_ACTIVE_LOW flag. If pin is configured as Active High, a low physical level will be interpreted as logical value 0. If pin is configured as Active Low, a low physical level will be interpreted as logical value 1.

Note: If pin is configured as Active High, the default, gpio_pin_get() function is equivalent to gpio_pin_get_raw().

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pin: Pin number.

Return Value
  • 1: If pin logical value is 1 / active.

  • 0: If pin logical value is 0 / inactive.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_pin_set_raw(const struct device *port, gpio_pin_t pin, int value)

Set physical level of an output pin.

Writing value 0 to the pin will set it to a low physical level. Writing any value other than 0 will set it to a high physical level. This function ignores GPIO_ACTIVE_LOW flag.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pin: Pin number.

  • value: Value assigned to the pin.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_pin_set(const struct device *port, gpio_pin_t pin, int value)

Set logical level of an output pin.

Set logical level of an output pin taking into account GPIO_ACTIVE_LOW flag. Value 0 sets the pin in logical 0 / inactive state. Any value other than 0 sets the pin in logical 1 / active state. If pin is configured as Active High, the default, setting it in inactive state will force the pin to a low physical level. If pin is configured as Active Low, setting it in inactive state will force the pin to a high physical level.

Note: If pin is configured as Active High, gpio_pin_set() function is equivalent to gpio_pin_set_raw().

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pin: Pin number.

  • value: Value assigned to the pin.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

int gpio_pin_toggle(const struct device *port, gpio_pin_t pin)

Toggle pin level.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • pin: Pin number.

Return Value
  • 0: If successful.

  • -EIO: I/O error when accessing an external GPIO chip.

  • -EWOULDBLOCK: if operation would block.

void gpio_init_callback(struct gpio_callback *callback, gpio_callback_handler_t handler, gpio_port_pins_t pin_mask)

Helper to initialize a struct gpio_callback properly.

Parameters
  • callback: A valid Application’s callback structure pointer.

  • handler: A valid handler function pointer.

  • pin_mask: A bit mask of relevant pins for the handler

int gpio_add_callback(const struct device *port, struct gpio_callback *callback)

Add an application callback.

Note: enables to add as many callback as needed on the same port.

Return

0 if successful, negative errno code on failure.

Note

Callbacks may be added to the device from within a callback handler invocation, but whether they are invoked for the current GPIO event is not specified.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • callback: A valid Application’s callback structure pointer.

int gpio_remove_callback(const struct device *port, struct gpio_callback *callback)

Remove an application callback.

Note: enables to remove as many callbacks as added through

gpio_add_callback().
Return

0 if successful, negative errno code on failure.

Warning

It is explicitly permitted, within a callback handler, to remove the registration for the callback that is running, i.e. callback. Attempts to remove other registrations on the same device may result in undefined behavior, including failure to invoke callbacks that remain registered and unintended invocation of removed callbacks.

Parameters
  • port: Pointer to the device structure for the driver instance.

  • callback: A valid application’s callback structure pointer.

int gpio_get_pending_int(const struct device *dev)

Function to get pending interrupts.

The purpose of this function is to return the interrupt status register for the device. This is especially useful when waking up from low power states to check the wake up source.

Parameters
  • dev: Pointer to the device structure for the driver instance.

Return Value
  • status: != 0 if at least one gpio interrupt is pending.

  • 0: if no gpio interrupt is pending.

struct gpio_driver_config
#include <gpio.h>

This structure is common to all GPIO drivers and is expected to be the first element in the object pointed to by the config field in the device structure.

struct gpio_driver_data
#include <gpio.h>

This structure is common to all GPIO drivers and is expected to be the first element in the driver’s struct driver_data declaration.

struct gpio_callback
#include <gpio.h>

GPIO callback structure.

Used to register a callback in the driver instance callback list. As many callbacks as needed can be added as long as each of them are unique pointers of struct gpio_callback. Beware such structure should not be allocated on stack.

Note: To help setting it, see gpio_init_callback() below