Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
GPIO(3)			    Library Functions Manual		       GPIO(3)

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) 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  in-
       stance.	 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 de-
       vice 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  re-
       turned.

       The  function gpio_fileno() returns the file descriptor associated with
       the gpio_handle_t instance.

       File operations have the	following semantics:

       read(2)	 Read one or more gpio interrupts that have occured since  the
		 last  successful  read(2).   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)  blocks  until	an interrupt occurs, unless O_NONBLOCK
		 is set.

       poll(2)	 When receiving	notification via  poll(2)  or  similar	inter-
		 faces,	 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), gpioctl(8)

HISTORY
       The libgpio library first appeared in FreeBSD 11.0.

AUTHORS
       The libgpio library was implemented by Rui Paulo	<rpaulo@FreeBSD.org>.

FreeBSD	15.0		       September 3, 2025		       GPIO(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=gpio_open&manpath=FreeBSD+15.0-RELEASE+and+Ports>

home | help