Segmentation fault error while trying to set the direction of the GPIO pin to perform read operation

Hi Everyone,

I am new to beaglebone black linux and looking for some support for the issue I am facing. I have been following Derek Molleys video tutorials on how to configure and program the GPIO’s here http://derekmolloy.ie/beaglebone/beaglebone-gpio-programming-on-arm-embedded-linux/. Everything was going fine, until I encountered this weird bug that has to do something with the memory segments I believe. Searched on net and came across someone who had faced the same issue here https://groups.google.com/forum/#!topic/beagleboard/Xe-oIuQOeI8, did the respective changes in the code as suggested, yet I keep getting segmentation fault error and the execution stops while displaying “Could not set the direction of the pin”. Here is the code I am trying to fix,

#include
#include <stdio.h>
#include <string.h>
#include <unistd.h>
using namespace std;
#define MAX 64

int flashGPIOLED(int, int);
int readbutton(int);

int main()
{
//flashGPIOLED(60, 5);
readbutton(15);
}

int readbutton(int inputpin)
{
cout << “Scanning input pin: " << inputpin << “active” << endl;
FILE *ButtonHandle = NULL;
char Inputpin_no[4],Inputpin_val[2], Inputpin_dir[MAX],setValue[MAX];
sprintf(Inputpin_no,”%d",inputpin);
sprintf(Inputpin_val,"/sys/class/gpio/gpio%d/value",inputpin);
sprintf(Inputpin_dir,“sys/class/gpio/gpio%d/direction”,inputpin);

//Export the pin
if((ButtonHandle = fopen("/sys/class/gpio/export",“ab”))==NULL)
{
printf(“Could not export the pin\n”);
return 1;
}
strcpy(setValue,Inputpin_no);
fwrite(&setValue, sizeof(char), 2, ButtonHandle);
fclose(ButtonHandle);

//Set the direction of the pin
if((ButtonHandle = fopen(Inputpin_dir, “rb+”))==NULL)
{
printf(“Could not set the direction\n”);
return 1;-------------------------------------------------------------> Control comes here and display "Could not set the direction and segmentation fault
}
strcpy(setValue,“in”);
fwrite(&setValue, sizeof(char), 3, ButtonHandle);
fclose(ButtonHandle);

//Read the input pin
if((ButtonHandle = fopen(Inputpin_val, “rb+”))==NULL)
{
printf(“Cannot open the value handle\n”);
return 1;
}
fread(&setValue, sizeof(char),1,ButtonHandle);
fclose(ButtonHandle);
cout << "The value of the input pin is: " << setValue[0] << endl;

//Unexport the pin
if((ButtonHandle = fopen(“sys/class/gpio/unexport”, “ab”))==NULL)
{
printf(“Could not unexport\n”);
return 0;
}
strcpy(setValue,Inputpin_no);
fwrite(&setValue, sizeof(char), 3, ButtonHandle);
fclose(ButtonHandle);
}

int flashGPIOLED(int GPIOPin, int times)
{
cout << “GPIO LED BLINKING DEMO OF PIN:” << GPIOPin << “STARTS” << endl;
FILE *LEDHandle = NULL;
char setValue[4], GPIOString[4], GPIOValue[MAX], GPIODirection[MAX];
sprintf(GPIOString, “%d”, GPIOPin);
sprintf(GPIOValue, “/sys/class/gpio/gpio%d/value”, GPIOPin);
sprintf(GPIODirection, “/sys/class/gpio/gpio%d/direction”, GPIOPin);

//Export the pin
if((LEDHandle = fopen("/sys/class/gpio/export",“ab”))==NULL)
{
printf(“Cannot export the GPIO pin\n”);
return 1;
}
strcpy(setValue,GPIOString);
fwrite(&setValue, sizeof(char), 2, LEDHandle);
fclose(LEDHandle);

//Set the direction of the Pin
if((LEDHandle = fopen(GPIODirection, “rb+”))==NULL)
{
printf(“Cannot open direction handle\n”);
return 1;
}
strcpy(setValue, “out”);
fwrite(&setValue, sizeof(char), 3, LEDHandle);
fclose(LEDHandle);

//Flash the LED int times
for(int i=0 ; i<2*times ; i++)
{
if((LEDHandle = fopen(GPIOValue, “rb+”))==NULL)
{
printf(“Cannot open value handle\n”);
return 1;
}
if(i%2 == 1)
strcpy(setValue,“0”);
if(i%2 == 0)
strcpy(setValue,“1”);
fwrite(&setValue, sizeof(char), 1, LEDHandle);
fclose(LEDHandle);
sleep(1);
}
//Unexport the pin
if((LEDHandle = fopen("/sys/class/gpio/unexport",“ab”))==NULL)
{
printf(“Cannot unexport the GPIOPin\n”);
return 1;
}
strcpy(setValue, GPIOString);
fwrite(&setValue, sizeof(char),2,LEDHandle);
fclose(LEDHandle);

cout << “GPIO LED Flash sequence of pin:” << GPIOPin << “ended” << endl;
return 0;
}

I have been trying for a while to fix this segmentation fault problem. I don’t understand whats wrong with the above code that is causing this problem. Any advise and help regarding this would be great
Thank you all in advance.

Regrads
~VD

The value for the sysfs node is not correct (you're missing the
initial /). I don't know where the SEGV comes from: compile with with
the -g flag and run under gdb, and it'll tell you which line is the
SEGV in.

Also the code could stand a bit more error checking / testing. sprintf() for example returns an int, negative number for failure, and I also believe it sets errno. Or on success it returns the number of bytes written, not including the terminating null.

Anyway, since you’re not testing for a negative number on the return value of sprintf(), you have no idea if it’s failing or not . . . That, and there’s probably more I missed, because I only briefly “reviewed” the code. Seems overly long winded though . . .