#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
/****************************************************************
* Constants
****************************************************************/
#define SYSFS_GPIO_DIR "/sys/class/gpio"
#define POLL_TIMEOUT (3 * 1000) /* 3 seconds */
#define MAX_BUF 64
/****************************************************************
* gpio_export
****************************************************************/
int gpio_export(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
sprintf(buf,"%s%s",SYSFS_GPIO_DIR,"/export");
fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror("gpio/export");
return fd;
}
//len = snprintf(buf, sizeof(buf), "%d", gpio);
//write(fd, buf, len);
sprintf(buf, "%d", gpio);
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
/****************************************************************
* gpio_unexport
****************************************************************/
int gpio_unexport(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
sprintf(buf,"%s%s",SYSFS_GPIO_DIR,"/unexport");
fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror("gpio/export");
return fd;
}
//len = snprintf(buf, sizeof(buf), "%d", gpio);
//write(fd, buf, len);
sprintf(buf, "%d", gpio);
write(fd, buf, strlen(buf));
close(fd);
return 0;
}
/****************************************************************
* gpio_set_dir
****************************************************************/
int gpio_set_dir(unsigned int gpio, unsigned int out_flag)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/direction", gpio);
printf("direction str: %s\n",buf);
fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror("gpio/direction");
return fd;
}
if (out_flag)
write(fd, "out", 4);
else
write(fd, "in", 3);
close(fd);
return 0;
}
/****************************************************************
* gpio_set_value
****************************************************************/
int gpio_set_value(unsigned int gpio, unsigned int value)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror("gpio/set-value");
return fd;
}
if (value)
write(fd, "1", 2);
else
write(fd, "0", 2);
close(fd);
return 0;
}
/****************************************************************
* gpio_get_value
****************************************************************/
int gpio_get_value(unsigned int gpio, unsigned int *value)
{
int fd, len;
char buf[MAX_BUF];
char ch;
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY);
if (fd < 0)
{
perror("gpio/get-value");
return fd;
}
read(fd, &ch, 1);
if (ch != '0')
{
*value = 1;
}
else
{
*value = 0;
}
close(fd);
return 0;
}
/****************************************************************
* gpio_set_edge
****************************************************************/
int gpio_set_edge(unsigned int gpio, char *edge)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/edge", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror("gpio/set-edge");
return fd;
}
write(fd, edge, strlen(edge) + 1);
close(fd);
return 0;
}
/****************************************************************
* gpio_fd_open
****************************************************************/
int gpio_fd_open_rd(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_RDONLY | O_NONBLOCK );
if (fd < 0)
{
perror("gpio/fd_open");
}
return fd;
}
int gpio_fd_open_wr(unsigned int gpio)
{
int fd, len;
char buf[MAX_BUF];
len = snprintf(buf, sizeof(buf), SYSFS_GPIO_DIR "/gpio%d/value", gpio);
fd = open(buf, O_WRONLY);
if (fd < 0)
{
perror("gpio/fd_open");
}
return fd;
}
/****************************************************************
* gpio_fd_close
****************************************************************/
int gpio_fd_close(int fd)
{
return close(fd);
}
/****************************************************************
* Main
****************************************************************/
#if 0
//测试ok。但不能正常取消导出的GPIO端口。
int main(int argc, char **argv)
{
struct pollfd fdset[2];
int nfds = 2;
int gpio_fd, timeout, rc;
char *buf[MAX_BUF];
unsigned int gpioID;
unsigned int gpio;
int len;
int i;
if (argc < 2)
{
printf("usage: test_gpio_out nPort.\n");
printf("valid port is 1 and 2. [1 for GPIO port1]; [2 for GPIO port2].\n");
exit(-1);
}
gpioID = atoi(argv[1]);
if (gpioID==1)
gpio=29;
else if (gpioID==2)
gpio=26;
else
{
printf("Invalid ID.\n");
exit(0);
}
gpio_export(gpio);
gpio_set_dir(gpio, 1); //output
gpio_fd = gpio_fd_open_wr(gpio);
for (i=0; i<10; i++)
{
printf("%d\n",i);
if (i%2)
write(gpio_fd, "1", 1);
else
write(gpio_fd, "0", 1);
sleep(1);
}
gpio_fd_close(gpio_fd);
gpio_unexport(gpio); //问题:无法取消导出gpio。用下面的代码就可以。
return 0;
}
#endif
#if 1
//测试ok。并且能正常取消导出的GPIO端口
int main (int argc,char** argv)
{
int i;
char lpPort[16];
char lpDrvStr[128];
if (argc!=2)
{
printf("usage: test_gpio_out nPort.\n");
return (-1);
}
else
sprintf(lpPort,"%s",argv[1]);
//(1)导出26号GPIO接口
int fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0)
{
printf("Open file /sys/class/gpio/export failed!\n");
return -1;
}
write(fd, lpPort, strlen(lpPort));
close(fd);
//(2)将GPIO接口方向设置为输出
sprintf(lpDrvStr,"/sys/class/gpio/gpio%s/direction",lpPort);
printf("drv: %s\n",lpDrvStr);
fd = open(lpDrvStr, O_WRONLY);
if (fd < 0)
{
printf("Open file %s failed!\n",lpDrvStr);
return -1;
}
write(fd, "out", 3);
close(fd);
//(3)输出10个周期的方波
sprintf(lpDrvStr,"/sys/class/gpio/gpio%s/value",lpPort);
printf("drv: %s\n",lpDrvStr);
fd = open(lpDrvStr, O_WRONLY);
if (fd < 0)
{
printf("Open file %s failed!\n",lpDrvStr);
return -1;
}
for (i=0; i<10; i++)
{
printf("%d\n",i);
if (i%2)
write(fd, "1", 1);
else
write(fd, "0", 1);
sleep(1);
}
close(fd);
//(4)取消26号GPIO接口的导出
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0)
{
printf("Open file /sys/class/gpio/unexport failed!\n");
return -1;
}
write(fd, lpPort, strlen(lpPort));
close(fd);
return 0;
}
#endif