Gpio beaglebone black

From Teknologisk videncenter
Revision as of 07:52, 11 June 2023 by Heth (talk | contribs)
Jump to: navigation, search

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".

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);
  if (lineBlue == 0) {
          fprintf(stderr," ERROR gpiod_get_line() failed: lineBlue\n");
          return(1);
  }
  lineButton = gpiod_chip_get_line(chip, 19);
  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;
}