#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <poll.h>
#include <time.h>
#define GPIO0_2_DEV_NUM 430
enum GPIO_EDGE_MODE{
GPIO_EDGE_MODE_NONE=0,
GPIO_EDGE_MODE_RISING,
GPIO_EDGE_MODE_FALLING,
GPIO_EDGE_MODE_BOTH,
};
enum GPIO_DIRECTION_MODE{
GPIO_DIRECTION_MODE_IN=0,
GPIO_DIRECTION_MODE_OUT,
};
//函数声明
static int gpio_export(int pin);
static int gpio_unexport(int pin);
static int gpio_direction(int pin, int dir);
static int gpio_write(int pin, int value);
static int gpio_read(int pin);
static int gpio_edge(int pin, int edge);
static int gpio_export(int pin)
{
char buffer[64];
int len;
int fd;
fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0)
{
printf("Failed to open export for writing!\n");
return(-1);
}
len = snprintf(buffer, sizeof(buffer), "%d", pin);
printf("%s,%d,%d\n",buffer,(int)sizeof(buffer),len);
if (write(fd, buffer, len) < 0)
{
printf("Failed to export gpio!");
return -1;
}
close(fd);
return 0;
}
static int gpio_unexport(int pin)
{
char buffer[64];
int len;
int fd;
fd = open("/sys/class/gpio/unexport", O_WRONLY);
if (fd < 0)
{
printf("Failed to open unexport for writing!\n");
return -1;
}
len = snprintf(buffer, sizeof(buffer), "%d", pin);
if (write(fd, buffer, len) < 0)
{
printf("Failed to unexport gpio!");
return -1;
}
close(fd);
return 0;
}
//dir: 0-->IN, 1-->OUT
static int gpio_direction(int pin, int dir)
{
static const char dir_str[] = "in\0out";
char path[64];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY);
if (fd < 0)
{
printf("Failed to open gpio direction for writing!\n");
return -1;
}
if (write(fd, &dir_str[dir == GPIO_DIRECTION_MODE_IN ? 0 : 3], dir == GPIO_DIRECTION_MODE_IN ? 2 : 3) < 0)
{
printf("Failed to set direction!\n");
return -1;
}
close(fd);
return 0;
}
//value: 0-->LOW, 1-->HIGH
static int gpio_write(int pin, int value)
{
static const char values_str[] = "01";
char path[64];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_WRONLY);
if (fd < 0)
{
printf("Failed to open gpio value for writing!\n");
return -1;
}
if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0)
{
printf("Failed to write value!\n");
return -1;
}
close(fd);
return 0;
}
static int gpio_read(int pin)
{
char path[64];
char value_str[3];
int fd;
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
fd = open(path, O_RDONLY);
if (fd < 0)
{
printf("Failed to open gpio value for reading!\n");
return -1;
}
if (read(fd, value_str, 3) < 0)
{
printf("Failed to read value!\n");
return -1;
}
close(fd);
return (atoi(value_str));
}
// none表示引脚为输入,不是中断引脚
// rising表示引脚为中断输入,上升沿触发
// falling表示引脚为中断输入,下降沿触发
// both表示引脚为中断输入,边沿触发
// 0-->none, 1-->rising, 2-->falling, 3-->both
static int gpio_edge(int pin, int edge)
{
const char dir_str[] = "none\0rising\0falling\0both";
char ptr;
char path[64];
int fd;
switch(edge)
{
case GPIO_EDGE_MODE_NONE:
ptr = 0;
break;
case GPIO_EDGE_MODE_RISING:
ptr = 5;
break;
case GPIO_EDGE_MODE_FALLING:
ptr = 12;
break;
case GPIO_EDGE_MODE_BOTH:
ptr = 20;
break;
default:
ptr = 0;
}
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", pin);
fd = open(path, O_WRONLY);
if (fd < 0)
{
printf("Failed to open gpio edge for writing!\n");
return -1;
}
if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0)
{
printf("Failed to set edge!\n");
return -1;
}
close(fd);
return 0;
}
//GPIO0_2
int main(int argc, char ** argv )
{
int gpio_fd, ret;
struct pollfd fds[1];
char buff[10];
int gpio_state_flag = 0;
int interval_unit = 1;
float time_use = 0;
float interval = 0;
struct timeval last = {0};
struct timeval start;
struct timeval end;
char path[64];
if (argc == 2){
interval_unit = atoi(argv[1]);
}
printf("interval_unit = %d\n", interval_unit);
//p0_2 init
gpio_unexport(GPIO0_2_DEV_NUM);
gpio_export(GPIO0_2_DEV_NUM);
gpio_direction(GPIO0_2_DEV_NUM, GPIO_DIRECTION_MODE_OUT);//input out
gpio_edge(GPIO0_2_DEV_NUM, GPIO_EDGE_MODE_BOTH);
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", GPIO0_2_DEV_NUM);
gpio_fd = open(path,O_RDONLY);
if (gpio_fd < 0)
{
printf("Failed to open value!\n");
return -1;
}
fds[0].fd = gpio_fd;
fds[0].events = POLLPRI;
gettimeofday(&last,NULL);
while(1)
{
usleep(10000);
gettimeofday(&start,NULL); //gettimeofday(&start,&tz);结果一样
//当前和上一次操作时间大于1s
interval = (start.tv_sec-last.tv_sec)*1000000+(start.tv_usec-last.tv_usec);
if (interval > interval_unit * 1000000)
{
printf("与上次间隔时间:%f\n", interval);
printf("标准间隔时间:%d\n", interval_unit);
gpio_state_flag = !gpio_state_flag;
last.tv_sec = last.tv_sec + interval_unit;
printf("start.tv_sec:%d\n",start.tv_sec);
printf("start.tv_usec:%d\n",start.tv_usec);
ret = gpio_write(GPIO0_2_DEV_NUM, gpio_state_flag);
gettimeofday(&end,NULL);
printf("end.tv_sec:%d\n",end.tv_sec);
printf("end.tv_usec:%d\n",end.tv_usec);
time_use=(end.tv_sec-start.tv_sec)*1000000+(end.tv_usec-start.tv_usec);//微秒
printf("time_use is %.10f\n",time_use);
}
}
return 0;
}
Linux GPIO设备定时翻转
最新推荐文章于 2024-04-29 21:58:18 发布