中断处理------文件IO模型(一)按键驱动的实现基于tiny4412开发板

12 篇文章 0 订阅
10 篇文章 0 订阅

按键驱动其实和简单的字符设备驱动一样,只是用特定函数实现特定的功能,下面就来写一个简单的按键驱动。

我们将一个普通的字符的设备驱动给拿过来

#include <linux/module.h>
#include <linux/init.h>
ssize_t  my_read (struct file *filp, char __user *buffer, size_t count, loff_t *fpos)
{
	printk("my_read\n");
	return 0;
	
}
ssize_t my_write (struct file *filp, char __user *buffer, size_t count, loff_t *fpos)
{
	printk("my_write\n");
	return 0;
	
}
int my_open (struct inode *node, struct file *filp)
{
	printk("my_open\n");
	return 0;
	
}
int my_close (struct inode *node, struct file *filp)
{
	printk("my_close\n");
	return 0;

}


static const struct file_operations my_operations = {
	.owner 		=THIS_MODULE,
	.open 		= my_open,
	.release 	=my_close ,
	.read		=my_read, 
	.write		=my_write
};

static int __init hello_drv_init(void)
{ 
	return 0;
}


static int __exit hello_drv_exit(void)
{
	return 0;
}



module_init(hello_drv_init);
module_exit(hello_drv_exit);
MODULE_LICENSE("GPL");

在此基础上,把它改成按键驱动
首先我们需要一个结构体来描述按键的各个属性


struct key_event{
        int code;//按键类型
        int value;//按键状态,按下或者抬起(    0  /  1 )
};

struct key_dsc{
         int major;//主设备号
        struct  class *key_class;
        struct  device *key_dev;
        int irq;//中断号
         unsigned long flag;//触发按键的方式,高低电平或者上升,下降沿触发
        struct key_event my_event;
};

在入口函数init中

my_key.flag=IRQF_DISABLED |
                    IRQF_TRIGGER_FALLING |
                    IRQF_TRIGGER_RISING;
                    //设置IRQF_DISABLED非共享中断,IRQF_TRIGGER_FALLING下降沿,IRQF_TRIGGER_RISING上升沿触发

然后获取中断号,中断号有很多方法获得,主流方法是通过设备树添加节点然后可以调用相应的方法获取,这里我们用gpio_to_irq来获取
这里用到的按键是key1,通过查看原理图
在这里插入图片描述
然后一路追踪,发现在芯片接出的引脚为
在这里插入图片描述
所以中断号获取
my_key.irq=gpio_to_irq(EXYNOS4_GPX3(2))

中断号已经获取到了,接下来就是将这个中断号注册内核,这样才能识别

request_irq(my_key.irq,key_handle_t,my_key.flag,"key1",NULL);

很显然我们发现了一个中断服务函数,当我们按下按键的时候触发了中断,就会做出一个反应,这个反应用一个中断服务函数key_handle_t实现

irqreturn_t key_handle_t(int irq,void *dev_id){
        int dn=0;
        dn=gpio_get_value(EXYNOS4_GPX3(2));//根据这个函数获取GPX3_2的按键状态
        if(!dn)
                {
                        printk("\n key   down !! \n");
                                                my_key.my_event.code=KEY_ENTER;
                                                my_key.my_event.value=1;
                }
        else
                {
                        printk("\n key   up !! \n");
                                                my_key.my_event.code=KEY_ENTER;
                                                my_key.my_event.value=0;
                }

        return IRQ_HANDLED;

}

当按键按下的时候就会触发这个函数,value的值 按下为0,抬起为1

驱动完整代码如下:

#include<linux/kernel.h>
#include<linux/init.h>
#include<linux/module.h>
#include<linux/device.h>
#include<linux/fs.h>
#include <asm/uaccess.h>
#include<linux/slab.h>
#include<linux/interrupt.h> //中断注册注销头文件
#include<linux/gpio.h>   //gpio相关的头文件
#include<linux/cdev.h>
#include <linux/string.h>


struct key_event{
        int code;//按键类型
        int value;//按键状态,按下或者抬起(    0  /  1 )
};

struct key_dsc{
         int major;
        struct  class *key_class;
        struct  device *key_dev;
        int irq;//中断号
         unsigned long flag;
        struct key_event my_event;
};
#define device_name "qin_key"
#define class_name "qin_class"
#define KEY_ENTER 28

struct key_dsc my_key;

ssize_t key_read(struct file *file, const char __user *buffer, size_t count, loff_t *fpos){
        //  ****************read函数目的是将案按键的数组传到应用层,kbuf[4]   ************
    
                if(count>4)//防止数据溢出
                {
                        count=4;
                }
    
    
        if(copy_to_user(buffer,&my_key.my_event,sizeof(struct key_event)))//copy_to_user 返回零代表copy失败,1代表成功
                {
                        printk("\n   copy  fail!!   \n");
                }
    
                memset(&my_key.my_event,0,sizeof(struct key_event));
        return count;
}

ssize_t key_write (struct file *file, const char __user *buf, size_t count, loff_t *fpos){
        printk("\n              key_ is write!!         \n");

}


int  key_open (struct inode *inode, struct file *file){
                printk("\n              key_ is open!!          \n");
                                return 0;
}

int key_close(struct inode *inode, struct file *file){
                printk("\n              key_ is close!!         \n");
}

irqreturn_t key_handle_t(int irq,void *dev_id){
        int dn=0;
        dn=gpio_get_value(EXYNOS4_GPX3(2));//根据这个函数获取GPX3_2的按键状态
        if(!dn)
                {
                        printk("\n key   down !! \n");
                                                my_key.my_event.code=KEY_ENTER;
                                                my_key.my_event.value=1;
                }
        else
                {
                        printk("\n key   up !! \n");
                                                my_key.my_event.code=KEY_ENTER;
                                                my_key.my_event.value=0;
                }

        return IRQ_HANDLED;

}

static struct file_operations myfops={
        .owner=THIS_MODULE,
        .open=key_open,
        .release=key_close,
        .write=key_write,
        .read=key_read,
};

static int __init key_init(void)
{
                my_key.irq=0;
        my_key.major=250;

        my_key.flag=IRQF_DISABLED |
                    IRQF_TRIGGER_FALLING |
                    IRQF_TRIGGER_RISING;//设置属性,上升,下降沿触发

        my_key.irq=gpio_to_irq(EXYNOS4_GPX3(2));//获取中断号
        request_irq(my_key.irq,key_handle_t,my_key.flag,"key1",NULL);//将这个按键注册到内核,这样才能识别
     



                if(register_chrdev(my_key.major,"qin",&myfops))//返回值为1则失败
                        {
                                        printk("\nregister is faile !\n");
                        }
                else
                        {
                                                printk("\nregister is ok !\n");
                        }

        my_key.key_class=class_create(THIS_MODULE,"class");
        my_key.key_dev=device_create(my_key.key_class,NULL,MKDEV(my_key.major,0),NULL,"qin");
        printk("\nI am key_dev\n");
        return 0;
}
static int __exit key_exit(void)
{
        free_irq(my_key.irq,NULL); //注销key1中断
                device_destroy(my_key.key_class,MKDEV(my_key.major,0));//注销dev
                class_destroy(my_key.key_class);//注销class
        unregister_chrdev(my_key.major,"qin");

        printk("\n   bye      bye~~~~~~~\n");
        return 0;
}
module_init(key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");


app完整代码如下:

#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
#include<string.h>
#define KEY_ENTER 28

struct key_event{
        int code;//按键类型
        int value;//按键状态,按下或者抬起(    0  /  1 )
};

int main()
{
        struct key_event key;
        int fd=0;
        fd=open("/dev/qin",O_RDWR);
        if(fd<0)
                {       printf("open file fail!");
    
                }
        else
                {
                        printf("\n   open file success !!\n");
                }
        while(1)
                {
                read(fd,&key,sizeof(struct key_event));
                 if(key.code==KEY_ENTER)
                        {
                        if(key.value==0)
                                {
                                        printf("\n              key value   is %d       \n",key.code);  
                                        printf("\n              key value   is %d       \n",key.value); 
                                        printf("\n              key up!!!!!!!!!!        \n");   
                                }
                        if(key.value==1)
                                {
                                        printf("\n              key value   is %d       \n",key.code);
                                        printf("\n              key value   is %d       \n",key.value); 
                                        printf("\n              key down!!!!!!!!!!      \n");    
                                }
                        }
                }
        return 0;
}

由于app代码过于简单,就不解释了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值