dimitry-ishenko-cpp / gpio

C++ GPIO Library for Linux
GNU General Public License v3.0
8 stars 3 forks source link

'Program received signal SIGABRT : Cannot set pin mode - No such device' after setting pin to gpio::mode::in. #7

Open Petross404 opened 2 years ago

Petross404 commented 2 years ago

Hi,

I am using this code with my Adafruit FT232H:

#include <gpio++.hpp>

#include <asio.hpp>
#include <iostream>
#include <thread>

int main( int argc, char** argv )
{
    asio::io_service io;
    auto chip = gpio::get_chip(io, "0");

    auto pin = chip->pin(2);
    if( pin->supports(gpio::in) )
    {
        pin->as(gpio::mode::in); // <- Throws
        std::cout << "Success" << std::endl;
    }

    std::cout << "Monitoring pin:" << std::endl;
    pin->on_state_changed([](gpio::state s)
    {
        std::cout << "State=" << s << std::endl;
    });

    io.run();
    return 0;
}

I am getting this:

*** Program received signal SIGABRT (Aborted) ***
terminate called after throwing an instance of 'std::runtime_error'
  what():  chip:0#2: Cannot set pin mode - No such device

Some info:

$ gpioinfo 
gpiochip0 - 4 lines:
        line   0:      unnamed       unused   input  active-high 
        line   1:      unnamed       unused   input  active-high 
        line   2:      unnamed       unused   input  active-high 
        line   3:      unnamed       kernel   input  active-high [used]

I am trying to read an input event with a physical button connected to pin 2 (C8 for my board). Needless to say that using the pins as output (flashing a led) is working ok. Also, the other pins don't work for input - they crash too.

Any ideas why the library is throwing?

dimitry-ishenko commented 2 years ago

@Petross404 are you using a modified version of the library?

If I search the repo for "Cannot set pin mode" I only see errors like "Cannot set pin mode - Invalid mode:" and "Cannot set pin mode - Invalid flag:". Nothing that says "Cannot set pin mode - No such device". Unless it is thrown by the kernel...

Can you post the backtrace?

Petross404 commented 2 years ago

No, I am not. It's installed directly from this repository.

I searched as well for these strings and I couldn't figure it out. I even considered that running as normal user is to blame, but flashing the led is working, when ran as normal user.

Do you mean "stacktrace"?

dimitry-ishenko commented 2 years ago

Yes stack trace. Can you post that?

Petross404 commented 2 years ago
#0 __pthread_kill_implementation() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/nptl/pthread_kill.c:43
#1 __pthread_kill_internal() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/nptl/pthread_kill.c:78
#2 __GI_raise() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/sysdeps/posix/raise.c:26
#3 __GI_abort() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/stdlib/abort.c:79
#4 ??() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#5 ??() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#6 std::terminate()() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#7 __cxa_throw() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#8 ??() at /usr/lib64/libgpio++.so.4
#9 gpio::generic::pin::mode(gpio::literals::mode, gpio::literals::flag, gpio::literals::state)() at /usr/lib64/libgpio++.so.4
#10 gpio::pin::as<gpio::literals::mode>() at /usr/include/gpio++/pin.hpp:82
#11 main() at /mnt/Volume_3/Gentoo/projects/Embedded/gpioPlus1/main.cpp:79

And this is line 79:

    if ( pin->supports( in ) )
    {
        pin->as( gpio::mode::in ); // <- Line 79
        std::cout << "Success" << std::endl;
    }

Many symbols are missing and I understand this makes the debugging harder. Anyway, does setting a gpio pin as input work for you? Again, I am using the program as it's shown on my first post here.

Thank you for your time.

dimitry-ishenko commented 2 years ago

Did you build the library from source or installed one of the binary packages?

If you built it from source, you can compile with debug info. Just add -DCMAKE_BUILD_TYPE=Debug to the cmake command.

Petross404 commented 2 years ago

With debug info for this library

#0 __pthread_kill_implementation() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/nptl/pthread_kill.c:43
#1 __pthread_kill_internal() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/nptl/pthread_kill.c:78
#2 __GI_raise() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/sysdeps/posix/raise.c:26
#3 __GI_abort() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/stdlib/abort.c:79
#4 ??() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#5 ??() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#6 std::terminate()() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#7 __cxa_throw() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#8 gpio::generic::pin::mode_in() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/include/g++-v11/ext/new_allocator.h:89
#9 gpio::generic::pin::mode() at /mnt/Volume_3/Gentoo/temp/portage/dev-libs/gpio++-4.2/work/gpio++-4.2-Source/generic/pin.cpp:68
#10 gpio::pin::as<gpio::literals::mode>() at /usr/include/gpio++/pin.hpp:82
#11 main() at /mnt/Volume_3/Gentoo/projects/Embedded/gpioPlus1/main.cpp:79

I think this error message

terminate called after throwing an instance of 'std::runtime_error'
  what():  chip:0#2: Cannot set pin mode - **No such device**

comes from here and a few lines later here. But why asio can't find the chip and/or the pin?

EDIT: I am using the 4.2 version and pointing to code from master branch. I will use the latest commit and report back.

EDIT No2: I installed the latest commit and it's failing again.

I think it's the same, but anyway here it is:

#0 __pthread_kill_implementation() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/nptl/pthread_kill.c:43
#1 __pthread_kill_internal() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/nptl/pthread_kill.c:78
#2 __GI_raise() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/sysdeps/posix/raise.c:26
#3 __GI_abort() at /mnt/Volume_3/Gentoo/temp/portage/sys-libs/glibc-2.35-r4/work/glibc-2.35/stdlib/abort.c:79
#4 ??() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#5 ??() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#6 std::terminate()() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#7 __cxa_throw() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/libstdc++.so.6
#8 gpio::generic::pin::mode_in() at /usr/lib/gcc/x86_64-pc-linux-gnu/11.3.0/include/g++-v11/ext/new_allocator.h:89
#9 gpio::generic::pin::mode() at /mnt/Volume_3/Gentoo/temp/portage/dev-libs/gpio++-9999/work/gpio++-9999/generic/pin.cpp:68
#10 gpio::pin::as<gpio::literals::mode>() at /usr/include/gpio++/pin.hpp:82
#11 main() at /mnt/Volume_3/Gentoo/projects/Embedded/gpioPlus1/main.cpp:79
dimitry-ishenko commented 2 years ago

Hmm, that's still not much info, but at least we know it's failing inside gpio::generic::pin::mode_in().

Have you tried running your program as root? Maybe you don't have permissions to change pin mode?

Petross404 commented 2 years ago

Unfortunately, running as root doesn't help.

# ./gpioplus1 
Initiliazed ftdi-cbus with 4 pins.
terminate called after throwing an instance of 'std::runtime_error'
  what():  chip:0#2: Cannot set pin mode - No such device
Aborted

If you notice, my pins are by default reported as input from gpioinfo tool. Maybe trying to set a pin as input again is the reason behind this crash:

        asio::io_service io;
    auto chip = gpio::get_chip( io, "0" );

    std::cout << "Initiliazed " << chip->name() << " with "
              << chip->pin_count() << " pins." << std::endl;

    gpio::pin* pin = chip->pin( line );

    if ( pin->supports( in ) && pin->mode() != gpio::mode::in ) // <- Notice the second condition
    {
        pin->as( gpio::mode::in );

        std::cout << "Success" << std::endl;
    }

    std::cout << "Monitoring pin:" << std::endl;
    pin->on_state_changed( []( gpio::state s )
    {
        std::cout << "State=" << s << std::endl;
    } );

    io.run();
    return 0;

Now I am getting:

/mnt/Volume_3/Gentoo/projects/Embedded/gpioPlus1/build> /mnt/Volume_3/Gentoo/projects/Embedded/gpioPlus1/build/gpioplus1
Initiliazed ftdi-cbus with 4 pins.
Monitoring pin:
*** Finished ***

Do you think that's to blame? If yes, an internal check from the library or a relevant message would help a lot.

Irrelevant question: Isn't this program supposed to poll for events and report as soon as state changed? Because it completed as soon as I pressed "Execute".

dimitry-ishenko commented 2 years ago

I will try to get RPi box up and running in a few days and test this.