|
/*
|
|
* gpio.cpp
|
|
*
|
|
* Created on: Mar 6, 2012
|
|
* Author: joe
|
|
*/
|
|
#include <fcntl.h>
|
|
#include <termios.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
#include "gpio.h"
|
|
|
|
#define GPIO_DIR_IN 0
|
|
#define GPIO_DIR_OUT 1
|
|
|
|
int gpio_export(unsigned gpio)
|
|
{
|
|
int fd, len;
|
|
char buf[11];
|
|
|
|
fd = open("/sys/class/gpio/export", O_WRONLY);
|
|
if (fd < 0) {
|
|
perror("gpio/export");
|
|
return fd;
|
|
}
|
|
|
|
len = snprintf(buf, sizeof(buf), "%d", gpio);
|
|
write(fd, buf, len);
|
|
close(fd);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int gpio_unexport(unsigned gpio)
|
|
{
|
|
int fd, len;
|
|
char buf[11];
|
|
|
|
fd = open("/sys/class/gpio/unexport", O_WRONLY);
|
|
if (fd < 0) {
|
|
perror("gpio/export");
|
|
return fd;
|
|
}
|
|
|
|
len = snprintf(buf, sizeof(buf), "%d", gpio);
|
|
write(fd, buf, len);
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
static int gpio_dir(unsigned gpio, unsigned dir)
|
|
{
|
|
int fd, len;
|
|
char buf[60];
|
|
|
|
len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
|
|
|
|
fd = open(buf, O_WRONLY);
|
|
if (fd < 0) {
|
|
perror("gpio/direction");
|
|
return fd;
|
|
}
|
|
|
|
if (dir == GPIO_DIR_OUT)
|
|
write(fd, "out", 4);
|
|
else
|
|
write(fd, "in", 3);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int gpio_dir_out(unsigned gpio)
|
|
{
|
|
return gpio_dir(gpio, GPIO_DIR_OUT);
|
|
}
|
|
|
|
int gpio_dir_in(unsigned gpio)
|
|
{
|
|
return gpio_dir(gpio, GPIO_DIR_IN);
|
|
}
|
|
|
|
int gpio_set_value(unsigned gpio, unsigned value)
|
|
{
|
|
int fd, len;
|
|
char buf[60];
|
|
|
|
len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
|
|
|
|
fd = open(buf, O_WRONLY);
|
|
if (fd < 0) {
|
|
perror("gpio/value");
|
|
return fd;
|
|
}
|
|
|
|
if (value)
|
|
write(fd, "1", 2);
|
|
else
|
|
write(fd, "0", 2);
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|
|
|
|
int gpio_get_value(unsigned gpio, unsigned *value)
|
|
{
|
|
int fd, len;
|
|
char buf[60];
|
|
|
|
len = snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
|
|
|
|
fd = open(buf, O_RDONLY);
|
|
if (fd < 0) {
|
|
perror("gpio/value");
|
|
return fd;
|
|
}
|
|
|
|
read(fd, value, 1);
|
|
|
|
if (*value == '1')
|
|
*value = 1;
|
|
else
|
|
*value = 0;
|
|
|
|
close(fd);
|
|
return 0;
|
|
}
|