Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

gpio error



  • can you please help
    i am using raspberry pi 3b with Emeria.os (embbed android os).
    my super user permission request is working fine the problem is that i can not access the gpio files.

    I art : Late-enabling -Xcheck:jni
    W System : ClassLoader referenced unknown path:
    I QtCore : Start
    W linker : /data/app/org.qtproject.example-1/lib/arm/libQt5AndroidExtras.so: unsupported flags DT_FLAGS_1=0x80
    W linker : /data/app/org.qtproject.example-1/lib/arm/libQt5Gui.so: unsupported flags DT_FLAGS_1=0x80
    W linker : /data/data/org.qtproject.example/qt-reserved-files/plugins/platforms/android/libqtforandroid.so: unsupported flags DT_FLAGS_1=0x80
    I Qt : qt started
    I art : Do partial code cache collection, code=10KB, data=26KB
    I art : After code cache collection, code=10KB, data=26KB
    I art : Increasing code cache capacity to 128KB
    E linker : normalize_path - invalid input: "C", the input path should be absolute
    W linker : Warning: unable to normalize "C"
    E linker : normalize_path - invalid input: "C", the input path should be absolute
    W linker : Warning: unable to normalize "C"
    I GRALLOC-MOD: Opening GPU0 module
    D vc4-drm-winsys: vc4 screen creation <= 38
    D vc4-drm-winsys: created new vc4 screen 40 => 0xa8fa6318
    I GRALLOC-PIPE: Created a pipe manager for vc4 and fd 38
    D vc4-drm-winsys: vc4 screen creation <= 43
    D vc4-drm-winsys: created new vc4 screen 44 => 0xa8fa6a98
    I OpenGLRenderer: Initialized EGL, version 1.4
    D OpenGLRenderer: Swap behavior 1
    W OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
    D OpenGLRenderer: Swap behavior 0
    D libconsole.so: Permission granted
    D libconsole.so: Unable to open /sys/class/gpio/export
    F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x4 in tid 5899 (RenderThread)
    "org.qtproject.example" died.

    #include <QCoreApplication>
    #include <stdio.h>
    #include <QDebug>
    #ifdef Q_OS_ANDROID
    #include <QtAndroidExtras>
    #endif
    #include <errno.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/stat.h>
    #include <sys/types.h>
    #include <unistd.h>

    int export_pin(){

    // Export the desired pin by writing to /sys/class/gpio/export

    int fd = open("/sys/class/gpio/export", O_WRONLY);
    if (fd == -1) {
        perror("Unable to open /sys/class/gpio/export");
         qDebug()<<"Unable to open /sys/class/gpio/export";
        exit(1);
    }
    
    if (write(fd, "24", 2) != 2) {
        perror("Error writing to /sys/class/gpio/export");
         qDebug()<<"Error writing to /sys/class/gpio/export";
        exit(1);
    

    }

    return fd;
    }

    int unexport_pin(){
    // Unexport the pin by writing to /sys/class/gpio/unexport

    int fd = open("/sys/class/gpio/unexport", O_WRONLY);
    

    if (fd == -1) {
    perror("Unable to open /sys/class/gpio/unexport");
    qDebug()<<"Unable to open /sys/class/gpio/unexport";
    exit(1);
    }

    if (write(fd, "24", 2) != 2) {
    perror("Error writing to /sys/class/gpio/unexport");
    qDebug()<<"Error writing to /sys/class/gpio/unexport";
    exit(1);
    }

    return fd;

    }

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    #ifdef Q_OS_ANDROID
        auto  result = QtAndroid::checkPermission(QString("android.permission.ACCESS_SUPERUSER"));
            if(result == QtAndroid::PermissionResult::Denied){
                QtAndroid::PermissionResultMap resultHash = QtAndroid::requestPermissionsSync(QStringList({"android.permission.ACCESS_SUPERUSER"}));
                if(resultHash["android.permission.ACCESS_SUPERUSER"] == QtAndroid::PermissionResult::Denied)
                    qDebug()<< "Permission rejected";
            }
            else{
    
             qDebug()<<"Permission granted";
    
            int fd;
    
                fd = export_pin();
                close(fd);
    
                // Set the pin to be an output by writing "out" to /sys/class/gpio/gpio24/direction
    
                fd = open("/sys/class/gpio/gpio24/direction", O_WRONLY);
                if (fd == -1) {
                    perror("Unable to open /sys/class/gpio/gpio24/direction");
                     qDebug()<<"Unable to open /sys/class/gpio/gpio24/direction";
                    exit(1);
                }
    
                if (write(fd, "out", 3) != 3) {
                    perror("Error writing to /sys/class/gpio/gpio24/direction");
                    qDebug()<<"Error writing to /sys/class/gpio/gpio24/direction";
                    exit(1);
                }
    
                close(fd);
    
                fd = open("/sys/class/gpio/gpio24/value", O_WRONLY);
                if (fd == -1) {
                    perror("Unable to open /sys/class/gpio/gpio24/value");
                    exit(1);
                }
    
                // Toggle LED 500 ms on and 500ms off forever
    
                  while (1) {
                    if (write(fd, "1", 1) != 1) {
                        perror("Error writing to /sys/class/gpio/gpio24/value");
                        exit(1);
                    }
                    else{
    
                      qDebug()<< "ON";
                    }
                    usleep(500000);
    
                    if (write(fd, "0", 1) != 1) {
                        perror("Error writing to /sys/class/gpio/gpio24/value");
                        exit(1);
                    }
                    else{
    
                      qDebug()<< "OFF";
                    }
    
                    usleep(500000);
                }
    
                close(fd);
    
                fd = unexport_pin();
    
                close(fd);
    
    
            }
    #endif
    
    
    return a.exec();
    

    }



  • I suggest you to use pigpio or wiringpi library in order to manage raspberry pi gpios.



  • Thanks mrdebug
    but i do not think it will work because i tried the following
    code since /dev/gpiomem does not need root permission
    int fd = open("/dev/gpiomem", O_RDONLY) ;

    if (fd == -1) {

       qDebug()<<"Unable to open /dev/gpiomem";
      exit(1);
    

    }
    else{

      qDebug()<< "gpio /dev/gpiomem";
    

    }
    and still unable to access the file


  • Lifetime Qt Champion

    Hi,

    Since you are using Emeria.os, you should ask their folks on whether these GPIO's are accessible and if so, how.



  • This post is deleted!


  • @SGaist
    thank you for the reply
    Sorry its Emteria.os
    i was told to
    the gpios are accessable
    Make sure those files are accessible from your application. Check Linux file permissions. and how do i check for these permissions?


  • Lifetime Qt Champion

    @tinashe said in gpio error:

    and how do i check for these permissions?

    ls -lh /sys/class/gpio/unexport
    

    You should read about UNIX/Linux basics.
    To get access for the group your user is in:

    chmod g+rw /sys/class/gpio/unexport
    

    For user

    chmod u+rw /sys/class/gpio/unexport
    


  • @jsulm
    thank you very much
    how do i run this in my main file or c++ file because i have seen these commands run in cmd terminal.


  • Lifetime Qt Champion

    @tinashe said in gpio error:

    how do i run this in my main file or c++ file because i have seen these commands run in cmd terminal

    Why do you want to do it in your app? Just do it once in a terminal...
    The commands I provided to set access rights need root access (via sudo for example):

    sudo chmod g+rw /sys/class/gpio/unexport
    

    So, your app will not be able to do this.
    But you should really check how access rights are handled in Emteria.os



  • @jsulm said in gpio error:

    For user
    unfortunately i can not do it on the terminal.
    or can i do this in the cmd.txt found in boot folder?



  • @jsulm
    unfortunately the ssh server is locked for demo which am using currently


  • Lifetime Qt Champion

    @tinashe See "man 2 chmod".
    But most probably your user will need to start the app as root (sudo for example).



  • @tinashe

    i was told to do permission seeting in /data/init.d



  • @jsulm
    chmod g+rw /sys/class/gpio/unexport
    cd /etc/udev/rules.d/
    touch local.rules
    ACTION=="add", KERNEL=="gpio", MODE="0666"
    and did notwork


  • Lifetime Qt Champion

    @tinashe This does not have anything to do with Qt now, please find out how to do this with Linux.



  • @jsulm
    thank you very much will do that


Log in to reply