• 0
Saad Zia

GPIO Input value won't change from Userspace

Question

Dear Experts

I am using Petalinux 2015.4 and Zedboard. I am reading a GPIO which is supposed to go from 0 to 1 and in commandline, I can see that it happens. But when I execute it in the code, the value always remain 0 as initial value is 0. My code is as follows:

int number=-1;
    unsigned char error = 0;
    int fd_done;
    //Export GPIO
    fd_done = open("/sys/class/gpio/export", O_WRONLY);
    if(fd_done < 0)
    {
#if DEBUG_GPIOBit
        printf("ERROR:\tGPIO Export Failed\n\r");
#endif
        error=1;
    }
    write(fd_done,"903",3);
    close(fd_done);
    //Set GPIO as input
    fd_done = open("/sys/class/gpio/gpio903/direction", O_WRONLY);
    if(fd_done < 0)
    {
#if DEBUG_GPIOBit
        printf("ERROR:\tGPIO Direction Failed\n\r");
#endif
        error=1;
    }
    write(fd_done,"in",2);
    close(fd_done);
    
    char value;
    fd_done = open("/sys/class/gpio/gpio903/value", O_WRONLY);
    lseek(fd_done, 0, SEEK_SET);
    read(fd_done, &value, 8);
    printf("Done Value: \t %d \n",value);
    close(fd_done);
    fd_done = open("/sys/class/gpio/unexport", O_WRONLY);
    write( fd_done,"903",3);
    close(fd_done);
    return number;

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now