我是靠谱客的博主 无语龙猫,最近开发中收集的这篇文章主要介绍BBB板子中的GPIO引脚中断输入,下降沿触发,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<poll.h>

#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,%dn",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[] = "inout";  
    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[] = "nonerisingfallingboth"; 
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("readn");
	
		ret = poll(fds,1,0);
		if( ret == -1 )
		MSG("polln");
		if( fds[0].revents & POLLPRI)
		{
			ret = lseek(gpio_fd,0,SEEK_SET);
			if( ret == -1 )
			MSG("lseekn");
		
			//gpio_write(44, cnt++%2);
			printf("**********************************n");
		}
		usleep(5);
	}
	return 0;
}

最后

以上就是无语龙猫为你收集整理的BBB板子中的GPIO引脚中断输入,下降沿触发的全部内容,希望文章能够帮你解决BBB板子中的GPIO引脚中断输入,下降沿触发所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(68)

评论列表共有 0 条评论

立即
投稿
返回
顶部