Hi, I am new to this group. Following is a code I tried from Derek Malloy's 
videos, to read the gpio pin values. When I run this program I get 
"Segmentation fault". I can't make out how it happens. Can anybody help me 
out?

#include <stdio.h>
#include <string.h>
#define MAX 128
int readButton(int);

int main()
{
        int i = 1000;
        while(i--)
                readButton(60);
        return 0;
}


int readButton(int pin)
{
        char setValue[4], GPIOPin[4], GPIODirection[MAX], GPIOValue[MAX];
        FILE *GPIOHandler;
        int i;
        sprintf(GPIOPin, "%d", pin);
        sprintf(GPIODirection, "/sys/class/gpio/gpio%s/direction", GPIOPin);
        sprintf(GPIOValue, "/sys/class/gpio/gpio%s/value", GPIOPin);
        if ((GPIOHandler = fopen("/sys/class/gpio/export", "ab")) == NULL)
        {
                fprintf(stderr, "Failed to export the GPIO\n");
                return 1;
        }
        strcpy(setValue, GPIOPin);
        fwrite(GPIOPin, sizeof(char), 2, GPIOHandler);
        fclose(GPIOHandler);
        if ((GPIOHandler = fopen(GPIODirection, "rb+")) == NULL)
        {
                fprintf(stderr, "Failed to set the direction\n");
                return 1;
        }
        strcpy(setValue, "in");
        fwrite(&setValue, sizeof(char), 3, GPIOHandler);
        fclose(GPIOHandler);

        if ((GPIOHandler = fopen(GPIOValue, "rb+")) == NULL)
        {
                fprintf(stderr, "Failed to set read file\n");
                return 1;
        }
        fread(&setValue, sizeof(char), 1, GPIOHandler);
        fclose(GPIOHandler);
        printf("The value of the pin is %s\n", setValue[0]);
        if ((GPIOHandler = fopen("/sys/class/gpio/unexport", "ab")) == NULL)
        {
                fprintf(stderr, "Failed to unxport GPIO \n");
                return 1;
        }
        strcpy(setValue, GPIOPin);
        fwrite(&setValue, sizeof(char), 2, GPIOHandler);
        fclose(GPIOHandler);
        return 0;
}



-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to beagleboard+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to