Difference between revisions of "Gpio beaglebone black"

From Teknologisk videncenter
Jump to: navigation, search
m
m (config-pin command)
Line 1: Line 1:
==config-pin command==
+
=native config-pin command=
 
<source lang=bash>
 
<source lang=bash>
 
debian@beaglebone:/sys$ config-pin  P9_16 gpio_pu
 
debian@beaglebone:/sys$ config-pin  P9_16 gpio_pu
Line 6: Line 6:
 
</source>
 
</source>
 
P9_16 (gpio 51) configure to gpio mode configured as input with pull-up resistor. Current input value "0".
 
P9_16 (gpio 51) configure to gpio mode configured as input with pull-up resistor. Current input value "0".
 +
=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.
 +
 
==GPIOD version 1.4.1==
 
==GPIOD version 1.4.1==
 
<source lang=c>
 
<source lang=c>

Revision as of 11:31, 9 November 2023

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

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.

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;
}