Gpio beaglebone black

From Teknologisk videncenter
Jump to: navigation, search

native config-pin command

debian@beaglebone:/sys$ config-pin  P9_16 gpio_pu
debian@beaglebone:/sys$ config-pin -q P9_16
P9_16 Mode: gpio_pu Direction: in Value: 0

P9_16 (gpio 51) configure to gpio mode configured as input with pull-up resistor. Current input value "0".

libgpiod Library

See: exploring libgpiod library

install the libgpiod library if not installed.

gpiodetect
List all GPIO chips present on the system, their names, labels and number of GPIO lines.
gpioinfo
List all lines of specified GPIO chips, their names, consumers, direction, active state and additional flags.
gpioget
Read values of specified GPIO lines.
gpioset
Set values of specified GPIO lines, and potentially keep the lines exported and wait until timeout, user input or signal.
gpiofind
Find the GPIO chip name and line offset given the line name.
gpiomon
Wait for events on GPIO lines, specifying which events to watch, how many events to process before exiting or if the events should be reported to the console.

gpioset example

The following sets line 18 on chip 2 to 1 in three seconds.

gpioset --mode=time -s 3 2 18=1

gpioget

The following reads state of line 19 in chip 2

gpioget 2 19
1

GPIOD version 1.4.1

/*
Install libgpiod-dev and libgpiod-doc for development
compile with -lgpiod

See: https://www.ics.com/blog/gpio-programming-exploring-libgpiod-library

Hardware: Beaglbone Black with Seeed studio Grove cape and Blue LED
          button in GPIO_50 Digital I/O.
*/
#include <gpiod.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
  const char *chipname = "gpiochip1";
  struct gpiod_chip *chip;
  struct gpiod_line *lineBlue;    // Red LED
  struct gpiod_line *lineButton; // Pushbutton
  int i, val;

  printf("GPIOD version: %s\n", gpiod_version_string());
  // Open GPIO chip
  chip = gpiod_chip_open_by_name(chipname);
  if (chip == 0) {
          fprintf(stderr," ERROR gpiod_chip_open_by_name() failed\n");
          return(1);
  }
  printf("kernel name for gpiochip1: %s\n",gpiod_chip_name(chip));

  // Open GPIO lines
  lineBlue = gpiod_chip_get_line(chip, 18);   // Grove cape it's called 50
  if (lineBlue == 0) {
          fprintf(stderr," ERROR gpiod_get_line() failed: lineBlue\n");
          return(1);
  }
  lineButton = gpiod_chip_get_line(chip, 19); // Grove cape it's called 51
  if (lineButton == 0) {
          fprintf(stderr," ERROR gpiod_get_line() failed: lineButton\n");
          return(1);
        }
  // Open LED lines for output
  gpiod_line_request_output(lineBlue, "lineBlue", 1);
  // Open switch line for input
  gpiod_line_request_input(lineButton, "LineButton");

  // Blink LED
  i = 0;
  while (true) {
    gpiod_line_set_value(lineBlue, (i & 1) != 0);

    // Read button status and exit if pressed
    val = gpiod_line_get_value(lineButton);
    if (val == 0) {
      printf("FINISHED with count  %d\n",i);
      break;
    }

    usleep(100000);
    i++;
  }

  // Release lines and chip
  gpiod_line_release(lineBlue);
  gpiod_line_release(lineButton);
  gpiod_chip_close(chip);
  return 0;
}

GPIO Interrupts