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 <iostream>
#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

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to