|
|
|
@ -15,6 +15,7 @@ int gpio_export(const char *gpio);
|
|
|
|
int gpio_direction(const char *gpio, const char *direction); // this function
|
|
|
|
int gpio_direction(const char *gpio, const char *direction); // this function
|
|
|
|
int gpio_write(const char *gpio, const char *value); // write function
|
|
|
|
int gpio_write(const char *gpio, const char *value); // write function
|
|
|
|
int gpio_read(const char *gpio); //read function
|
|
|
|
int gpio_read(const char *gpio); //read function
|
|
|
|
|
|
|
|
int gpio_unexport(const char *gpio);
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
int main(int argc, char *argv[]){
|
|
|
|
int fd;
|
|
|
|
int fd;
|
|
|
|
@ -47,16 +48,8 @@ int main(int argc, char *argv[]){
|
|
|
|
gpio_export(pin);
|
|
|
|
gpio_export(pin);
|
|
|
|
gpio_direction(pin, dir);
|
|
|
|
gpio_direction(pin, dir);
|
|
|
|
gpio_write(pin, val);
|
|
|
|
gpio_write(pin, val);
|
|
|
|
|
|
|
|
gpio_unexport(pin);
|
|
|
|
|
|
|
|
|
|
|
|
// unexport
|
|
|
|
|
|
|
|
fd = open("/sys/class/gpio/chip0/unexport", O_WRONLY);
|
|
|
|
|
|
|
|
if(fd<0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
perror("Error opening the GPIO path");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
write(fd,"17",2);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// comple functions
|
|
|
|
// comple functions
|
|
|
|
@ -66,7 +59,7 @@ int gpio_export(const char *gpio){
|
|
|
|
int fd;
|
|
|
|
int fd;
|
|
|
|
fd = open("/sys/class/gpio/chip0/export", O_WRONLY);
|
|
|
|
fd = open("/sys/class/gpio/chip0/export", O_WRONLY);
|
|
|
|
if(fd<0){
|
|
|
|
if(fd<0){
|
|
|
|
perror("Error opening the GPIO path");
|
|
|
|
perror("Error opening the GPIO path during export");
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
write(fd,gpio,strlen(gpio));
|
|
|
|
write(fd,gpio,strlen(gpio));
|
|
|
|
@ -101,7 +94,7 @@ int gpio_write(const char *gpio, const char *value){
|
|
|
|
"/sys/class/gpio/chip0/gpio%s/value", gpio);
|
|
|
|
"/sys/class/gpio/chip0/gpio%s/value", gpio);
|
|
|
|
fd = open(path, O_WRONLY);
|
|
|
|
fd = open(path, O_WRONLY);
|
|
|
|
if(fd<0){
|
|
|
|
if(fd<0){
|
|
|
|
perror("Error opening the GPIO path");
|
|
|
|
perror("Error opening the GPIO path during write");
|
|
|
|
return -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(write(fd,value, strlen(value)) < 0) {
|
|
|
|
if(write(fd,value, strlen(value)) < 0) {
|
|
|
|
@ -113,3 +106,27 @@ int gpio_write(const char *gpio, const char *value){
|
|
|
|
close(fd);
|
|
|
|
close(fd);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// gpio read
|
|
|
|
|
|
|
|
int gpio_read(const char *gpio); //read function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// unexport
|
|
|
|
|
|
|
|
int gpio_unexport(const char *gpio){
|
|
|
|
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = open("/sys/class/gpio/chip0/unexport", O_WRONLY);
|
|
|
|
|
|
|
|
if(fd<0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
perror("Error opening the GPIO path during unexport");
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if(write(fd,gpio, strlen(gpio)) < 0) {
|
|
|
|
|
|
|
|
perror("unexport");
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|