GPIO

Low-level GPIO peripheral driver.

This is a basic GPIO (General-purpose input/output) interface to allow platform independent access to a MCU’s input/output pins. This interface is intentionally designed to be as simple as possible, to allow for easy implementation and maximum portability.

The interface provides capabilities to initialize a pin as output-, input- and interrupt pin. With the API you can basically set/clear/toggle the digital signal at the hardware pin when in output mode. Configured as input you can read a digital value that is being applied to the pin externally. When initializing an external interrupt pin, you can register a callback function that is executed in interrupt context once the interrupt condition applies to the pin. Usually you can react to rising or falling signal flanks (or both).

In addition the API provides to set standard input/output circuit modes such as e.g. internal push-pull configurations.

All modern micro controllers organize their GPIOs in some form of ports, often named ‘PA’, ‘PB’, ‘PC’…, or ‘P0’, ‘P1’, ‘P2’…, or similar. Each of these ports is then assigned a number of pins, often 8, 16, or 32. A hardware pin can thus be described by its port/pin tuple. To access a pin, the GPIO_PIN(port, pin) macro should be used. For example: If your platform has a pin PB22, it will be port=1 and pin=22. The GPIO_PIN macro should be overridden by a MCU, to allow for efficient encoding of the the port/pin tuple. For example, on many platforms it is possible to OR the pin number with the corresponding ports base register address. This allows for efficient decoding of pin number and base address without the need of any address lookup.

In case the driver does not define it, the below macro definition is used to simply map the port/pin tuple to the pin value. In that case, predefined GPIO definitions in RIOT/boards/ * /include/periph_conf.h will define the selected GPIO pin.

enum gpio_mode_t
GPIO_IN_ANALOG = ((uint8_t)OVERRIDE_ANALOG)
input, analog
GPIO_IN_OUT
input and output
GPIO_IN_OD
input and open-drain output
GPIO_IN_OD_PU
input and open-drain output
GPIO_IN = ((uint8_t)OVERRIDE_DISABLE)
input, no pull
GPIO_IN_PD = ((uint8_t)OVERRIDE_PULLDOWN)
input, pull-down
GPIO_IN_PU = ((uint8_t)OVERRIDE_PULLUP)
input, pull-up
GPIO_OUT = ((uint8_t)OVERRIDE_ENABLE)
output
GPIO_OD = (0xff)
not supported
GPIO_OD_PU = (0xff)
not supported
enum gpio_flank_t
GPIO_NONE = 0
GPIO_LOW
emit interrupt when pin low
GPIO_HIGH = 5
emit interrupt on low level
GPIO_FALLING
emit interrupt on falling flank
GPIO_RISING
emit interrupt on rising flank
GPIO_BOTH
emit interrupt on both flanks
unsigned int gpio_t

GPIO type identifier.

void(* gpio_cb_t()

Signature of event callback functions triggered from interrupts.

Parameters

arg:optional context for the callback

int gpio_init(gpio.h::gpio_t pin, cc2538/include/periph_cpu.h::gpio_mode_t mode)

Initialize the given pin as general purpose input or output.

When configured as output, the pin state after initialization is undefined. The output pin’s state should be untouched during the initialization. This behavior can however not be guaranteed by every platform.

Parameters

pin:pin to initialize
mode:mode of the pin, see gpio_mode_t

Return values

  • 0 on success
  • -1 on error
int gpio_init_int(gpio.h::gpio_t pin, cc2538/include/periph_cpu.h::gpio_mode_t mode, atmega_common/include/periph_cpu_common.h::gpio_flank_t flank, gpio.h::gpio_cb_t cb, void * arg)

Initialize a GPIO pin for external interrupt usage.

The registered callback function will be called in interrupt context every time the defined flank(s) are detected.

The interrupt is activated automatically after the initialization.

Note

You have to add the module periph_gpio_irq to your project to enable this function

Parameters

pin:pin to initialize
mode:mode of the pin, see gpio_mode_t
flank:define the active flank(s)
cb:callback that is called from interrupt context
arg:optional argument passed to the callback

Return values

  • 0 on success
  • -1 on error
void gpio_irq_enable(gpio.h::gpio_t pin)

Enable pin interrupt if configured as interrupt source.

Note

You have to add the module periph_gpio_irq to your project to enable this function

Parameters

pin:the pin to enable the interrupt for

void gpio_irq_disable(gpio.h::gpio_t pin)

Disable the pin interrupt if configured as interrupt source.

Note

You have to add the module periph_gpio_irq to your project to enable this function

Parameters

pin:the pin to disable the interrupt for

int gpio_read(gpio.h::gpio_t pin)

Get the current value of the given pin.

Parameters

pin:the pin to read

Return values

  • 0 when pin is LOW
  • >0 for HIGH
void gpio_set(gpio.h::gpio_t pin)

Set the given pin to HIGH.

Parameters

pin:the pin to set

void gpio_clear(gpio.h::gpio_t pin)

Set the given pin to LOW.

Parameters

pin:the pin to clear

void gpio_toggle(gpio.h::gpio_t pin)

Toggle the value of the given pin.

Parameters

pin:the pin to toggle

void gpio_write(gpio.h::gpio_t pin, int value)

Set the given pin to the given value.

Parameters

pin:the pin to set
value:value to set the pin to, 0 for LOW, HIGH otherwise

GPIO_PIN( x, y)

Convert (port, pin) tuple to gpio_t value.

1
((gpio_t)((x & 0) | y))
GPIO_UNDEF

GPIO pin not defined.

1
((gpio_t)(UINT_MAX))
struct gpio_isr_ctx_t

Default interrupt context for GPIO pins.

gpio.h::gpio_cb_t cb

interrupt callback

void * arg

optional argument