GPIO(3)
π¦ GPIO(3) β Library Functions Manual π¦
okay bestie, welcome to the Library Functions Manual glow-up for libgpio β we're making GPIO pins make sense, no cap. π
β¨
πΈ NAME πΈ
gpio_open, gpio_close β library to handle GPIO pins
π LIBRARY π
library βlibgpioβ π«Ά
β¨ SYNOPSIS β¨
Here's the whole cast list, literally:
#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. It 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().
gpio_pin_list()β to get a list of all available pins, call this. It takes a pointer to agpio_config_twhich is dynamically allocated. This pointer should be freed withfree(3)when it is no longer necessary. Don't ghost your allocations, bestie πgpio_pin_config()β retrieves the current configuration of a pin. The pin number should be passed in via theg_pinvariable which is part of thegpio_config_tstructure.gpio_pin_set_name()β sets the name used to describe a pin. Cute, iconic, naming rights. πgpio_pin_set_flags()β configures a pin with the flags passed in by thegpio_config_tstructure. The pin number should also be passed in through theg_pinvariable. All other structure members will be ignored by this function. The list of flags can be found in<sys/gpio.h>.gpio_pin_get()andgpio_pin_set()β the get or set the state of a GPIO pin functions, respectively. To toggle the state, usegpio_pin_toggle().gpio_pin_low()andgpio_pin_high()β wrappers aroundgpio_pin_set(). Lowkey and highkey, literally.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()andgpio_pin_pulsate()β wrappers aroundgpio_pin_set_flags(). That's the whole squad, periodt.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 usingstruct gpio_event_detail.GPIO_EVENT_REPORT_SUMMARYβ Events are reported usingstruct 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.
gpio_fileno()β returns the file descriptor associated with thegpio_handle_tinstance.
File operations have the following semantics:
read(2)β Read one or more gpio interrupts that have occured since the last successfulread(2). The results are placed into the output buffer of the type previously established viagpio_configure_events(). If there are no pending interrupts,read(2)blocks until an interrupt occurs, unlessO_NONBLOCKis set.poll(2)β When receiving notification viapoll(2)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 π
β HISTORY β
The libgpio library first appeared in FreeBSD 11.0.
π AUTHORS π
The libgpio library was implemented by Rui Paulo <rpaulo@FreeBSD.org>.
π signed, sealed, delivered: this page is from Debian, dated September 3, 2025 β GPIO(3) out! π
β¨
π¦ where this page comes from
- freebsd 15.1: base 15.1
- Copyright (c) 2014 Rui Paulo
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms