john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Problem using libgpiod

Tue Nov 26, 2019 11:15 am

I installed the library
sudo apt-get install gpiod
sudo apt-get install libgpiod-dev
And I see It
ls -l /usr/bin/gpio*

apt search libgpiod
Now I use it in QT

Code: Select all

#include <gpio.h>

struct gpiod_chip *chip;

int GPIO_Init()
{
    chip = gpiod_chip_open("/dev/gpiochip0");
  
    return 0;
}
But I get the error
undefined reference to 'gpiod_chip_open' 
How to fix it?

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Problem using libgpiod

Tue Nov 26, 2019 1:26 pm

john77 wrote:
Tue Nov 26, 2019 11:15 am
I installed the library
sudo apt-get install gpiod
sudo apt-get install libgpiod-dev
And I see It
ls -l /usr/bin/gpio*

apt search libgpiod
Now I use it in QT

Code: Select all

#include <gpio.h>

struct gpiod_chip *chip;

int GPIO_Init()
{
    chip = gpiod_chip_open("/dev/gpiochip0");
  
    return 0;
}
But I get the error
undefined reference to 'gpiod_chip_open' 
How to fix it?


How did you try to build (compile and link) your example? (A reference to the required library will probably be required.)

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Tue Nov 26, 2019 1:57 pm

I see. Should add it in the QT project
LIBS += -lgpiod
Now it compiles OK.

Another question - how do I now the actual pin number?
line = gpiod_chip_get_line(chip, 3);
3 - is it GPIO3 - pin 5 on P1 connector?

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Problem using libgpiod

Tue Nov 26, 2019 2:08 pm

john77 wrote:
Tue Nov 26, 2019 1:57 pm
I see. Should add it in the QT project
LIBS += -lgpiod
Now it compiles OK.

Another question - how do I now the actual pin number?
line = gpiod_chip_get_line(chip, 3);
3 - is it GPIO3 - pin 5 on P1 connector?


Many reference documents available, including -
https://pinout.xyz/


But refer to the documentation for the library you are using to determine what numbering scheme is being used.

User avatar
joan
Posts: 15100
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem using libgpiod

Tue Nov 26, 2019 2:22 pm

It's a Linux library. It will be using Broadcom GPIO numbers (not pin numbers).

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Tue Nov 26, 2019 2:37 pm

joan wrote:
Tue Nov 26, 2019 2:22 pm
It's a Linux library. It will be using Broadcom GPIO numbers (not pin numbers).
But how do I now how to map the second argument (X) in gpiod_chip_get_line(chip, X); to the real pin ?
If I use a linux image for RasppberyPi it should be connected on the kernel level, as I understand.

Do you mean gpiod_chip_get_line(chip, X); = GPIOX ?

trejan
Posts: 2980
Joined: Tue Jul 02, 2019 2:28 pm

Re: Problem using libgpiod

Tue Nov 26, 2019 3:04 pm

john77 wrote:
Tue Nov 26, 2019 2:37 pm
Do you mean gpiod_chip_get_line(chip, X); = GPIOX ?
Yes. They're labelled as BCM X on https://pinout.xyz

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Tue Nov 26, 2019 3:15 pm

trejan wrote:
Tue Nov 26, 2019 3:04 pm
john77 wrote:
Tue Nov 26, 2019 2:37 pm
Do you mean gpiod_chip_get_line(chip, X); = GPIOX ?
Yes. They're labelled as BCM X on https://pinout.xyz
I see. Thank you very much.
One more question - to set pin as output - gpiod_line_request_output(line, ''out3'', 0); is it?

hippy
Posts: 8555
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Problem using libgpiod

Wed Nov 27, 2019 12:42 pm

trejan wrote:
Tue Nov 26, 2019 3:04 pm
john77 wrote:
Tue Nov 26, 2019 2:37 pm
Do you mean gpiod_chip_get_line(chip, X); = GPIOX ?
Yes. They're labelled as BCM X on https://pinout.xyz
That's how it currently is on Raspbian but "gpiod" is I believe intended to be a virtualisation and abstraction of the hardware so the mapping could be anything.

The intention AIUI is to provide a means that a user program can run on any platform or board without change. For example, Set("AlarmBell") should work on any board regardless of what that board is or which pin an alarm bell might be connected to, Set("ActivityLed") should light that up on any Pi no matter whether it is controlled by a GPIO pin directly or through a separate I/O expander chip, whether active high or active low.

It's similar to how "/dev/serial0" on a Pi is mapped to "/dev/ttyAMA0" or "/dev/ttyS0". The user doesn't need to care which it is providing they use "/dev/serial0".

But, whether "gpiod" is actually usable or fit for purpose is however questionable IMO.

Notionally one should not be using a number or anything board specific to access a particular pin but, in practical terms, that's exactly how things are currently done, how we expect to do it.

The current Pi implementation doesn't even name most of the "gpiod" pins and giving them "GPIO0"-"GPIO53" names defeats the abstraction intended. The "gpiod" system isn't even 'live'; doesn't recognise that some program has taken control of a pin if it does that directly, through RPi.GPIO or WiringPi etc.

Thus "gpiod" may be the recommended way, may be the future, but I looked at it and decided it wasn't yet fit for 'prime time', have ignored it since.

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Wed Nov 27, 2019 12:54 pm

I see. Thank you. So what method to use to work with pins? What library better?

hippy
Posts: 8555
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Problem using libgpiod

Wed Nov 27, 2019 12:58 pm

john77 wrote:
Wed Nov 27, 2019 12:54 pm
I see. Thank you. So what method to use to work with pins? What library better?
I use RPi.GPIO in my Python code, have used direct memory access via "/dev/gpiomem" to the SoC peripheral memory in C and assembler.

User avatar
B.Goode
Posts: 10725
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Problem using libgpiod

Wed Nov 27, 2019 1:03 pm

john77 wrote:
Wed Nov 27, 2019 12:54 pm
I see. Thank you. So what method to use to work with pins? What library better?

One of the people who has tried to help in the thread, @joan, has their own solution that might be worth investigating - http://abyz.me.uk/rpi/pigpio/

(I am not a C user so do not speak from experience and make no claim of 'better'. However, the python interface and standalone utilities have always worked reliably for me.)

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Wed Nov 27, 2019 1:15 pm

I see. The pigpio library is working only on RaspberryPi or it runs on any Linux platform?

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Wed Nov 27, 2019 1:22 pm

hippy wrote:
Wed Nov 27, 2019 12:58 pm
I use RPi.GPIO in my Python code, have used direct memory access via "/dev/gpiomem" to the SoC peripheral memory in C and assembler.
It's very interesting. I tend to use generic methods, so it'll be less pain to port it in case of emergency.
can you show direct memory access via "/dev/gpiomem" example?

User avatar
joan
Posts: 15100
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem using libgpiod

Wed Nov 27, 2019 1:24 pm

john77 wrote:
Wed Nov 27, 2019 1:15 pm
I see. The pigpio library is working only on RaspberryPi or it runs on any Linux platform?
The pigpio library ONLY runs on the Raspberry Pi.

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Wed Nov 27, 2019 1:39 pm

joan wrote:
Wed Nov 27, 2019 1:24 pm
john77 wrote:
Wed Nov 27, 2019 1:15 pm
I see. The pigpio library is working only on RaspberryPi or it runs on any Linux platform?
The pigpio library ONLY runs on the Raspberry Pi.
I see. Thank you.

User avatar
HermannSW
Posts: 3180
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany
Contact: Website Twitter YouTube

Re: Problem using libgpiod

Wed Nov 27, 2019 2:41 pm

joan wrote:
Wed Nov 27, 2019 1:24 pm
The pigpio library ONLY runs on the Raspberry Pi.
What about "Debian with Raspberry Pi Desktop"?
https://www.raspberrypi.org/downloads/r ... i-desktop/
https://stamm-wilbrandt.de/en/Raspberry_camera.html
https://stamm-wilbrandt.de/en#raspcatbot
https://github.com/Hermann-SW/raspiraw
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/working_with_FPGAs

User avatar
joan
Posts: 15100
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem using libgpiod

Wed Nov 27, 2019 2:48 pm

The core pigpio library (C linked with -lpigpio) can only run on a Raspberry Pi. pigpio uses hardware features of the Broadcom SoC. The only SBCs manufactured with the Broadcom SoC are Raspberry Pis.

hippy
Posts: 8555
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Problem using libgpiod

Wed Nov 27, 2019 3:21 pm

john77 wrote:
Wed Nov 27, 2019 1:22 pm
hippy wrote:
Wed Nov 27, 2019 12:58 pm
I use RPi.GPIO in my Python code, have used direct memory access via "/dev/gpiomem" to the SoC peripheral memory in C and assembler.
It's very interesting. I tend to use generic methods, so it'll be less pain to port it in case of emergency.
can you show direct memory access via "/dev/gpiomem" example?
This is a little dated now but still a good reference -

https://elinux.org/RPi_GPIO_Code_Samples

When programming I/O on a Pi one typically uses the GPIO pin one wants or needs to use. Making the code portable would be done by either setting a named constant or variable to be the pin number one wishes to use, using that named entity in the main body of code.

Using a variable means that can be set from command line arguments if specified or a default if not. For example, incomplete -

Code: Select all

DEFAULT_INPUT_PIN = 23
inputPin = DEFAULT_INPUT_PIN
ff sys.argv[1] == "IN":
  inputPin = int(sys.argv[2])
GPIO.setup(inputPin, GPIO.IN)
One could also create a mapping routine for a string specifier to map to a specific pin. That seems to me to be one intent of "gpiod" though seemingly not possible on a Pi without writing one's own mapping routines anyway.

RPi.GPIO, WiringPi and other wrappers and libraries support mappings of various 'logical pin numbers' or 'header pin numbers' to actual GPIO pin numbers. Mostly for historical reason and that's fallen out of favour as being confusing. Only using BCM / GPIOn numbers is recommended.

User avatar
joan
Posts: 15100
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Problem using libgpiod

Wed Nov 27, 2019 3:29 pm

If you want a Linux portable method you will have to use either sysfs (deprecated) or the gpiochip interface (libgpiod). Everything else is Pi specific.

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Wed Nov 27, 2019 3:50 pm

I see. Thank you.

trejan
Posts: 2980
Joined: Tue Jul 02, 2019 2:28 pm

Re: Problem using libgpiod

Wed Nov 27, 2019 4:20 pm

The generic method whilst portable is missing features like pullup configuration. If you need that then you'll need to use a Pi specific method like Joan's pigpio.

LdB
Posts: 1648
Joined: Wed Dec 07, 2016 2:29 pm

Re: Problem using libgpiod

Wed Nov 27, 2019 4:40 pm

You can easily add the extra pullup functionality to libgpiod its trivial or even alternatively you can punch it directly (although not portable).
I think the only current issue is Raspbian is a bit behind mainline.
Can we not make mountains out of molehills.

john77
Posts: 49
Joined: Tue Nov 26, 2019 11:04 am

Re: Problem using libgpiod

Mon Dec 30, 2019 1:42 pm

First I initialize it

Code: Select all

struct gpiod_chip *chip;
struct gpiod_line *line;

int rv, value;

void GPIO_Init(const char *filename)
{
    chip = gpiod_chip_open(filename);
    if (!chip)
    {
        printf("GPIO Init Failed\n");
    }
    else
    {
       printf("GPIO Init Success\n");
    }

    line = gpiod_chip_get_line(chip, 23);
    if (!line)
    {
        printf("Line 23 failed\n");
    }
    rv = gpiod_line_request_input(line, "can_int");
    if (rv)
    {
       printf("Failed to configure line 23\n");
    }

    line = gpiod_chip_get_line(chip, 17);
    if (!line)
    {
        printf("Line 17 failed\n");
    }
    rv = gpiod_line_request_output(line, "out17", 0);
    if (rv)
    {
       printf("Failed to configure line 17\n");
    }
  }
Then read / write functions

Code: Select all

int GPIO_GetPin(int pin_num)
{
    int pin_val;

    line = gpiod_chip_get_line(chip, pin_num);

    pin_val = gpiod_line_get_value(line);

    return pin_val;
}

void GPIO_SetPin(int pin_num, int pin_val)
{
    line = gpiod_chip_get_line(chip, pin_num);
    gpiod_line_set_value(line, pin_val);
}
in main.cpp (pins GPIO17 and GPIO23 connected together)

Code: Select all

 GPIO_Init("/dev/gpiochip0");
 
 GPIO_SetPin(17, 1);
 pin_val = GPIO_GetPin(23);
But I don't see GPIO17 going high and pin_val returns -1.

What could be a problem?

User avatar
DougieLawson
Posts: 40212
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Problem using libgpiod

Mon Dec 30, 2019 11:36 pm

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All fake doctors are on my foes list.

Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Return to “C/C++”