linux系统gpio中断的实现(提供例程)

该接口基于linux提供的通用中断API接口实现。

1.流程:
gpio外部引脚发生电平变化(上升沿/下降沿)—>触发驱动层(driver.c)的中断服务函数—>在中断服务函数中通过异步消息机制通知到应用层(app.c)

2.中断响应时间测试过程:
外部引脚发生变化(绿线)时间T1 -->触发驱动层中断服务函数—>中断服务函数里通过异步通知到应用层(在应用层翻转另一个GPIO引脚(黄线)电平)时间T2 。
在这里插入图片描述
使用示波器测试响应时间:T2-T1 = 33ms左右
测试环境:ti-AM5728芯片 + linux-4.14.79-rt47-ga72bf1418

3.下面是驱动以及应用层的代码:
driver.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/cdev.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <linux/device.h>
#include <asm/io.h>
#include <asm/switch_to.h>
#include <asm/uaccess.h>

#include <asm/gpio.h>    
#include <linux/interrupt.h>
#include <linux/irq.h>

#define simple_MAJOR 200
 
static struct class *my_class;
int major;
static struct fasync_struct *fasync_queue; //异步通知队列

/*关键点!!!*/
#define GPIO_NUM GPIO_TO_PIN(3,5)  //假设中断引脚为:GPIO3_5
static unsigned int irq_num;
										  


/* 打开 */
int simple_open(struct inode *inode,struct file *filp){
	return 0;

}

/* 关闭 */
int simple_release(struct inode *inode,struct file *filp){
	return 0;
}

/*
简介:系统调用,应用层的read()将会调用该函数 
参数:buf: 数据传输目的地址
      count:传输数据长度
      f_pos:偏移量
*/
ssize_t simple_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos){

	return count;
}
 
/*
简介:系统调用,应用层的write()将会调用该函数 
参数:buf: 数据传输源地址
      count:传输数据长度
      f_pos:偏移量
*/ 
ssize_t simple_write(struct file *file,const char __user *buf,size_t count,loff_t *f_pos){

	return count;
}

/*fasync方法的实现*/
static int my_fasync(int fd, struct file * filp, int on)
{
    int retval;
    retval=fasync_helper(fd,filp,on,&fasync_queue);
    /*将该设备登记到fasync_queue队列中去*/
    if(retval<0)
      return retval;
    return 0;
}

/* 设备驱动操作结构体,该结构体的每一个成员的名字都对应着一个系统调用 */  
static const struct file_operations simple_fops={
	.owner=THIS_MODULE,
	.open=simple_open,
	.release=simple_release,
    .read=simple_read,
	.write=simple_write,
    .fasync=my_fasync,
};

/* 在中断服务函数中向应用层发送消息-异步通知! */ 
void irq_callback (unsigned   long  date){
	printk("driver irq work !!!\n");
	if (fasync_queue) {
        kill_fasync(&fasync_queue, SIGIO, POLL_IN);
    }
}

/* 加载 */
int simple_init_module(void){
	int rtn;
	
	/* 注册相应的设备驱动 */ 
	major = register_chrdev(0,"my_device",&simple_fops);
	if(major<0){
		printk("Unable to register character device %d!/n",major);
		return major;
	}

	/* 自动创建设备节点 */ 
	my_class = class_create(THIS_MODULE, "my_class");
	device_create(my_class, NULL, MKDEV(major, 0), NULL,"my_device");
	
	/*gpio申请*/
	rtn = gpio_request(GPIO_NUM, "my_irq");
	if(rtn!=0){
		printk("my_irq irq pin request io failed.\n");
	}

	/*获取gpio中断号*/
	irq_num = gpio_to_irq(GPIO_NUM);
	
	/*GPIO中断服务函数注册,*/                    /*上升沿触发*/               
	rtn = request_irq(irq_num, irq_callback,IRQF_TRIGGER_FALLING,"my_irq", NULL);
	if (rtn<0) {
		printk("my_irq request irq false\n");
	} else {
		printk("my_irq request irq success: %d\n",irq_num);
	}
	
    printk("module_init sucessful!!!\n");
	return 0;
}

/* 卸载 */ 
void simple_cleanup_module(void){
	/* 卸载相应的设备驱动 */ 
	unregister_chrdev(major,"my_device");     
	device_destroy(my_class,MKDEV(major, 0));
	class_destroy(my_class);
	
	/*释放GPIO*/
	gpio_free(GPIO_NUM);
	

    printk("module_exit sucessful!!!\n");
}

/* 宏实现 */
module_init(simple_init_module);
module_exit(simple_cleanup_module);

/* 开源许可声明 */  
MODULE_LICENSE("Dual BSD/GPL");
 




app.c

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>

static int fd;

/* 内核产生异步通知,调用该应用层函数处理 */
void sigterm_handler(int signo)
{
	printk("app irq work !!!\n");
}

int main(void)
 {
    int oflags;

    fd=open("/dev/my_device",O_RDWR);  //打开设备文件
	
    /* 启动异步通知信号驱动机制 */
    signal(SIGIO, sigterm_handler);
    fcntl(fd, F_SETOWN, getpid());
    oflags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, oflags | FASYNC);
	
    /*建立一个死循环,防止程序结束 */
    while(1)
    {
        printf("sleep\n");
        usleep(200000);  //2ms
    }
	
    close(fd);
    return 0;
 }



  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值