#include
#include
#include
#include
#include
#include
#define msg(args...) printf(args)
//函式宣告
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)
msg("failed to open export for writing!\n");
return(-1);
len = snprintf(buffer, sizeof(buffer), "%d", pin);
printf("%s,%d,%d\n",buffer,sizeof(buffer),len);
if (write(fd, buffer, len) < 0)
msg("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)
msg("failed to open unexport for writing!\n");
return -1;
len = snprintf(buffer, sizeof(buffer), "%d", pin);
if (write(fd, buffer, len) < 0)
msg("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)
msg("failed to open gpio direction for writing!\n");
return -1;
if (write(fd, &dir_str[dir == 0 ? 0 : 3], dir == 0 ? 2 : 3) < 0)
msg("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)
msg("failed to open gpio value for writing!\n");
return -1;
if (write(fd, &values_str[value == 0 ? 0 : 1], 1) < 0)
msg("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)
msg("failed to open gpio value for reading!\n");
return -1;
if (read(fd, value_str, 3) < 0)
msg("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 0:
ptr = 0;
break;
case 1:
ptr = 5;
break;
case 2:
ptr = 12;
break;
case 3:
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)
msg("failed to open gpio edge for writing!\n");
return -1;
if (write(fd, &dir_str[ptr], strlen(&dir_str[ptr])) < 0)
msg("failed to set edge!\n");
return -1;
close(fd);
return 0;
//gpio1_17
int main()
int gpio_fd, ret;
struct pollfd fds[1];
char buff[10];
unsigned char cnt = 0;
gpio_unexport(44);
gpio_unexport(45);
//p8_12 init
gpio_export(44);
gpio_direction(44, 1);//output out
gpio_write(44, 1);
//p8_11 init
gpio_export(45);
gpio_direction(45, 0);//input in
gpio_edge(45,2);
gpio_fd = open("/sys/class/gpio/gpio45/value",o_rdonly);
if(gpio_fd < 0)
msg("failed to open value!\n");
return -1;
fds[0].fd = gpio_fd;
fds[0].events = pollpri;
while(1)
ret = read(gpio_fd,buff,10);
if( ret == -1 )
msg("read\n");
ret = poll(fds,1,0);
if( ret == -1 )
msg("poll\n");
if( fds[0].revents & pollpri)
ret = lseek(gpio_fd,0,seek_set);
if( ret == -1 )
msg("lseek\n");
//gpio_write(44, cnt++%2);
printf("**********************************\n");
usleep(5);
return 0;
中斷的使用
對於飛思卡爾codewarrior的中斷使用,一般有3種方法 1.把 pragma trap proc放在中斷程式前面,並把中斷向量表放到 prm。例如 pragma trap proc void pit1 void 例如 interrupt void intpit1 void 向量表的首位址放入 ...
外部中斷的使用
stm32學習筆記 外部中斷的使用 中斷對於開發嵌入式系統來講的地位絕對是毋庸置疑的,在c51 微控制器時代,一共只有 5個中斷,其中 2個外部中斷,2個定時 計數器中斷和乙個串列埠中斷,但是在 stm32 中,中斷數量大大增加,而且中斷的設定也更加複雜。今天就將來 一下關於 stm32 中的中斷系...
ZYNQ Linux 使用UIO中斷
zynq中的uio驅動和中斷程式學習 xilinx petalinux學習 testing uio with interrupt on zynq ultrascale 何曄 當zynq遇到linux userspace i o uio 在kernel drivers uio uio pdrv gen...