Segmentation fault

pablocarrio
 
Posts: 2
Joined: Mon May 08, 2017 1:38 pm

Segmentation fault

Mon May 15, 2017 4:37 pm

I've made a c++ program toggle a gpis pin, for this I've made a small class for controlling the gpis ports, I've achieved compile the code in Ubuntu 14.04 as explained in the documentation but when I upload and run it I get "Segmentation fault" in the console. First I got this error "./gpio: can't load library 'libstdc++.so.6'" but I installed libstdcpp package and was solved. If anyone can help me please

Here is the code:
gpio.cpp
Code: Select all
#include <stdio.h>
#include "gpio.h"

/******** GPIO ***********/
PinOut GPIO::setOutput(int pinId){
    PinOut pin(pinId);
    return pin;
}
PinIn GPIO::setInput(int pinId){
    PinIn pin(pinId);
    return pin;
}
/*********** Pin **********/
Pin::Pin(int pinId){
    this->pinId = pinId;
    this->exportPin();
}
Pin::~Pin(void){
    this->unexportPin();
}
bool Pin::state(void){
    int state = 0;
    char dir[50];
    sprintf(dir,"/sys/class/gpio/gpio%d/value", pinId);
    FILE *file;
    file = fopen(dir, "r");
    fscanf (file, "%d", &state);
    fclose (file);

    return (bool)state;
}
void Pin::exportPin(void){
    FILE *file;
    file = fopen("/sys/class/gpio/export", "w");
    fprintf(file,"%d",this->pinId);
    fclose(file);
}
void Pin::unexportPin(void){
    FILE *file;
    file = fopen("/sys/class/gpio/unexport", "w");
    fprintf(file,"%d",this->pinId);
    fclose(file);
}
/******** PinOut ************/
PinOut::PinOut(int pinid):Pin(pinid){
    Pin pin(pinId);
    char dir[50];
    sprintf(dir, "/sys/class/gpio/gpio%d/direction", pinId);
    FILE *file;
    file = fopen(dir, "w");
    fprintf(file,"out");
    fclose(file);
}
void PinOut::setPin(void){
    char dir[50];
    sprintf(dir, "/sys/class/gpio/gpio%d/value", this->pinId);
    FILE *file;
    file = fopen(dir, "w");
    fprintf(file,"1");
    fclose(file);
}
void PinOut::clearPin(void){
    char dir[50];
    sprintf(dir, "/sys/class/gpio/gpio%d/value", this->pinId);
    FILE *file;
    file = fopen(dir, "w");
    fprintf(file,"0");
    fclose(file);
}
void PinOut::writePin(bool val){
    char dir[50];
    sprintf(dir, "/sys/class/gpio/gpio%d/value", this->pinId);
    FILE *file;
    file = fopen(dir, "w");
    fprintf(file,"%d",val);
    fclose(file);
}
void PinOut::togglePin(void){
    //int newstate = !
    this->writePin((int)~this->state());
}
/********* PinIn ***********/
PinIn::PinIn(int pinid):Pin(pinid){
    Pin pin(pinId);
    char dir[50];
    sprintf(dir, "/sys/class/gpio/gpio%d/direction", pinId);
    FILE *file;
    file = fopen(dir, "w");
    fprintf(file,"in");
    fclose(file);
}


gpio.h
Code: Select all
#ifndef gpio
#define gpio

typedef enum {GPIO0, GPIO1, GPIO2, GPIO3, GPIO4, GPIO5, GPIO6, GPIO7, GPIO8, GPIO9, GPIO10, GPIO11, GPIO12, GPIO13, GPIO14, GPIO15, GPIO16, GPIO17, GPIO18, GPIO19, GPIO20, GPIO21, GPIO22, GPIO23, GPIO24, GPIO25, GPIO26, GPIO27, GPIO28, GPIO29, GPIO30, GPIO31, GPIO32, GPIO33, GPIO34, GPIO35, GPIO36, GPIO37, GPIO38, GPIO39, GPIO41, GPIO42, GPIO43, GPIO44 } PinId;
class Pin{
    public:
        bool state(void);
        Pin(int pinId);
        ~Pin(void);
    protected:
        void exportPin(void);
        void unexportPin(void);
        int pinId;
};
class PinOut: public Pin{
    public:
        void setPin(void);
        void writePin(bool val);
        void clearPin(void);
        void togglePin(void);
        PinOut(int pinid);
};
class PinIn: public Pin{
    public:
        void setPin(void);
        void clearPin(void);
        void togglePin(void);
        PinIn(int pinId);
};
class GPIO{
    public:
        static PinOut setOutput(int pinId);
        static PinIn setInput(int pinId);
};
#endif


main.c
Code: Select all
#include <stdio.h>
#include "gpio.h"

int main(void){
    PinOut led = GPIO::setOutput(GPIO41);
    led.togglePin();
    return 0;
}

Vonger
 
Posts: 896
Joined: Sun Oct 19, 2014 6:00 am

Re: Segmentation fault

Wed May 17, 2017 7:07 am

Segmentation fault issue normally is your missed some library. default vocore2 do not install c++ library. You need to ocmpile new openwrt with the cpp library or install cpp library to vocore2 by ipk.

avigeilpro
 
Posts: 25
Joined: Wed Mar 22, 2017 5:29 pm

Re: Segmentation fault

Thu May 18, 2017 11:50 am

Hi, personally I use this library :

gpio.h
Code: Select all
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifndef _GPIO_H
#define _GPIO_H

#define IN  0
#define OUT 1

#define LOW  0
#define HIGH 1

static int
GPIOExport(int pin)
{
#define BUFFER_MAX 3
   char buffer[BUFFER_MAX];
   ssize_t bytes_written;
   int fd;

   fd = open("/sys/class/gpio/export", O_WRONLY);
   if (-1 == fd) {
      return(-1);
   }

   bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
   write(fd, buffer, bytes_written);
   close(fd);
   return(0);
}

static int
GPIOUnexport(int pin)
{
   char buffer[BUFFER_MAX];
   ssize_t bytes_written;
   int fd;

   fd = open("/sys/class/gpio/unexport", O_WRONLY);
   if (-1 == fd) {
      return(-1);
   }

   bytes_written = snprintf(buffer, BUFFER_MAX, "%d", pin);
   write(fd, buffer, bytes_written);
   close(fd);
   return(0);
}

static int
pinMode(int pin, int dir)
{
   static const char s_directions_str[]  = "in\0out";

#define DIRECTION_MAX 35
   char path[DIRECTION_MAX];
   int fd;

   snprintf(path, DIRECTION_MAX, "/sys/class/gpio/gpio%d/direction", pin);
   fd = open(path, O_WRONLY);
   if (-1 == fd) {
      return(-1);
   }

   if (-1 == write(fd, &s_directions_str[IN == dir ? 0 : 3], IN == dir ? 2 : 3)) {
      return(-1);
   }

   close(fd);
   return(0);
}

static int
digitalRead(int pin)
{
#define VALUE_MAX 30
   char path[VALUE_MAX];
   char value_str[3];
   int fd;

   snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
   fd = open(path, O_RDONLY);
   if (-1 == fd) {
      return(-1);
   }

   if (-1 == read(fd, value_str, 3)) {
      return(-1);
   }

   close(fd);

   return(atoi(value_str));
}

static int
digitalWrite(int pin, int value)
{
   static const char s_values_str[] = "01";

   char path[VALUE_MAX];
   int fd;

   snprintf(path, VALUE_MAX, "/sys/class/gpio/gpio%d/value", pin);
   fd = open(path, O_WRONLY);
   if (-1 == fd) {
      return(-1);
   }

   if (1 != write(fd, &s_values_str[LOW == value ? 0 : 1], 1)) {
      return(-1);
   }

   close(fd);
   return(0);
}

static int
digitalToggle(int pin){
   digitalWrite(pin, 1 - digitalRead(pin));
   return(0);
}

#endif /* !_GPIO_H */


exemple of usage :

main.cpp
Code: Select all
#include <stdio.h>
#include <gpio.h>

int main(void){
    int led = 38;
    GPIOExport(led);  //export Pin

    pinMode(led,OUT);

    digitalWrite(led,HIGH); //turn on pin

    usleep(1000000);  // sleep 1s

    digitalWrite(led,LOW);  //turn off pin

    usleep(1000000);  //sleep 1s

    digitalToggle(led); //toggle pin


    GPIOUnexport(led);
    return 0;
}


I have put the gpio.h file in the folder "openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/include" of my VM machine.

Return to VoCore2/Lite/Ultimate

Who is online

Users browsing this forum: Google [Bot] and 54 guests