0

How i can access gpio wandboard in c?

I have a wandboard with ubuntu 14.04 and a wanted access our gpio in my c program. I can access in shell script, and i can put my script in my c code, but i wanted a mode of access the gpio directly my c code, without using shell command.

this is my shell command

echo 91 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio91/direction
echo 1 > /sys/class/gpio/gpio91/value
echo 0 > /sys/class/gpio/gpio91/value
Community
  • 1
  • 1
user3651443
  • 29
  • 1
  • 11
  • 2
    Those are files. Just open them and write. What kind of magic do you think the `echo` command is doing on them? – indiv Sep 25 '14 at 19:20
  • with these command I can read or write signals from GPIO pins, and can, for example, using a protoboard – user3651443 Sep 25 '14 at 19:29

1 Answers1

1

Just use basic C file IO.

echo 91 > /sys/class/gpio/export

would be

FILE *fp = fopen("/sys/class/gpio/export", "w");
if (fp) {
    if (fprintf(fp, "91") < 0) {
        perror("fprintf to /sys/class/gpio/export");
    }
    if (fclose(fp) == EOF) {
        // error can very well happen when fclose flushes, must check
        perror("fclose of /sys/class/gpio/export");
    }
} else {
    perror("fopen of /sys/class/gpio/export");
}

Expanding that to the other cases should be easy enough.

hyde
  • 60,639
  • 21
  • 115
  • 176