---
name: "gpio"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/gpio"
---

# gpio(3)

library to handle GPIO pins

## NAME

**gpio\_open**, **gpio\_close** – library to handle GPIO pins

## LIBRARY

library “libgpio”

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<libgpio.h\>**

*gpio\_handle\_t*\

**gpio\_open**(*unsigned int unit*);

*gpio\_handle\_t*\

**gpio\_open\_device**(*const char \*device*);

*void*\

**gpio\_close**(*gpio\_handle\_t handle*);

*int*\

**gpio\_pin\_list**(*gpio\_handle\_t handle*, *gpio\_config\_t \*\*pcfgs*);

*int*\

**gpio\_pin\_config**(*gpio\_handle\_t handle*, *gpio\_config\_t \*cfg*);

*int*\

**gpio\_pin\_set\_name**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*, *char \*name*);

*int*\

**gpio\_pin\_set\_flags**(*gpio\_handle\_t handle*, *gpio\_config\_t \*cfg*);

*gpio\_value\_t*\

**gpio\_pin\_get**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_set**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*, *gpio\_value\_t value*);

*int*\

**gpio\_pin\_toggle**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_low**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_high**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_input**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_output**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_opendrain**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_pushpull**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_tristate**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_pullup**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_pulldown**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_invin**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_invout**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_pin\_pulsate**(*gpio\_handle\_t handle*, *gpio\_pin\_t pin*);

*int*\

**gpio\_configure\_events**(*gpio\_handle\_t handle*, *uint32\_t report\_type*, *uint32\_t fifo\_size*);

*int*\

**gpio\_fileno**(*gpio\_handle\_t handle*);

## DESCRIPTION

The **libgpio** library provides an interface to configure GPIO pins. The library operates with a *gpio\_handle\_t* opaque type which can be created with **gpio\_open**() or **gpio\_open\_device**(). When no more GPIO operations are needed, this handle can be destroyed with **gpio\_close**().

To get a list of all available pins, one can call **gpio\_pin\_list**(). This function takes a pointer to a *gpio\_config\_t* which is dynamically allocated. This pointer should be freed with [free(3)](/free/3.md) when it is no longer necessary.

The function **gpio\_pin\_config**() retrieves the current configuration of a pin. The pin number should be passed in via the *g\_pin* variable which is part of the *gpio\_config\_t* structure.

The function **gpio\_pin\_set\_name**() sets the name used to describe a pin.

The function **gpio\_pin\_set\_flags**() configures a pin with the flags passed in by the *gpio\_config\_t* structure. The pin number should also be passed in through the *g\_pin* variable. All other structure members will be ignored by this function. The list of flags can be found in ⟨sys/gpio.h⟩.

The get or set the state of a GPIO pin, the functions **gpio\_pin\_get**() and **gpio\_pin\_set**() are available, respectively. To toggle the state, use **gpio\_pin\_toggle**().

The functions **gpio\_pin\_low**() and **gpio\_pin\_high**() are wrappers around **gpio\_pin\_set**().

The functions **gpio\_pin\_input**(), **gpio\_pin\_output**(), **gpio\_pin\_opendrain**(), **gpio\_pin\_pushpull**(), **gpio\_pin\_tristate**(), **gpio\_pin\_pullup**(), **gpio\_pin\_pulldown**(), **gpio\_pin\_invin**(), **gpio\_pin\_invout**() and **gpio\_pin\_pulsate**() are wrappers around **gpio\_pin\_set\_flags**().

The function **gpio\_configure\_events**() configures the interrupt report type and FIFO size for buffered gpio interrupts. The report type is specified by one of the following values:

- GPIO\_EVENT\_REPORT\_DETAIL

  Events are reported using *struct* *gpio\_event\_detail*.

- GPIO\_EVENT\_REPORT\_SUMMARY

  Events are reported using *struct* *gpio\_event\_summary*.

By default, the report type is GPIO\_EVENT\_REPORT\_DETAIL, with a default FIFO size of 2 \* number of pins belonging to the *gpio\_device\_t* instance. The FIFO argument is only meaningful when *report\_type* is GPIO\_EVENT\_REPORT\_DETAIL. The structures associated with each report type are defined in ⟨sys/gpio.h⟩. This setting is tracked on a per device instance basis. The FIFO size cannot be reduced below the default value, nor can it be decreased after it has been increased. If any pin on the device has already been configured for interrupts, **gpio\_configure\_events**() fails and returns -1. On success 0 is returned.

The function **gpio\_fileno**() returns the file descriptor associated with the *gpio\_handle\_t* instance.

File operations have the following semantics:

- [read(2)](/read/2.md)

  Read one or more gpio interrupts that have occured since the last successful [read(2)](/read/2.md). The results are placed into the output buffer of the type previously established via **gpio\_configure\_events**(). If there are no pending interrupts, [read(2)](/read/2.md) blocks until an interrupt occurs, unless O\_NONBLOCK is set.

- [poll(2)](/poll/2.md)

  When receiving notification via [poll(2)](/poll/2.md) or similar interfaces, the file descriptor becomes readable when one or more gpio interrupts are pending.

## EXAMPLES

The following example shows how to configure pin 16 as output and then drive it high:

```
#include <sys/types.h>
#include <err.h>
#include <libgpio.h>
gpio_handle_t handle;
handle = gpio_open(0);
if (handle == GPIO_INVALID_HANDLE)
	err(1, "gpio_open failed");
gpio_pin_output(handle, 16);
gpio_pin_high(handle, 16);
gpio_close(handle);
```

The following example shows how to get a configuration of a pin:

```
gpio_config_t cfg;
cfg.g_pin = 32;
gpio_pin_config(handle, &cfg);
```

The structure will contain the name of the pin and its flags.

## SEE ALSO

[gpiobus(4)](/gpiobus/4.md), [gpioctl(8)](/gpioctl/8.md)

## HISTORY

The **libgpio** library first appeared in FreeBSD 11.0.

## AUTHORS

The **libgpio** library was implemented by Rui Paulo ⟨*rpaulo@FreeBSD.org*⟩.
