linux驱动学习笔记 中断方式的按键驱动

前言

为何写这个文章

最近在根据韦东山老师的视频来学习linux驱动;
看视频经常想睡觉(韦东山老师课程讲的非常好,想睡觉是个人原因。。。);
于是就干脆直接拿源码来分析,一边分析一边写博客,这样学习不易犯困,还能留下记录方便日后归纳复习。

这个驱动涉及linux驱动的知识点归纳
  • 如何注册、解除注册中断:使用request_irq 与free_irq函数
  • 如何等待中断发生 : 使用等待队列机制
  • 如何从内核空间向用户空间传递数据 : 使用copy_to_user函数

首先根据硬件确定驱动所需功能

在这里插入图片描述
如图所示,我们需要实现以上四个按键的中断方式驱动。

复制所需要用到的包含

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

定义主设备号、以及设备名称

#define DEVICE_NAME     "buttons"   /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define BUTTON_MAJOR    232         /* 主设备号 */

然后写初始化函数和卸载函数

//定义一个file_operations 结构体
static struct file_operations s3c24xx_buttons_fops;

//写一个初始化函数
static int __init s3c24xx_buttons_init(void)
{
    int ret;

    /* 注册字符设备驱动程序
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数
     * BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号
     */
    ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }
    
    printk(DEVICE_NAME " initialized\n");
    return 0;
}


//写一个卸载函数
static void __exit s3c24xx_buttons_exit(void)
{
    /* 卸载驱动程序 */
    unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
}

//指定驱动的初始化和卸载函数
module_init(s3c24xx_buttons_init);
module_exit(s3c24xx_buttons_exit);


完善s3c24xx_buttons_fops结构体

static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{

}
static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
}
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
}
static struct file_operations s3c24xx_buttons_fops = {
    .owner   =   THIS_MODULE,    /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
    .open    =   s3c24xx_buttons_open,
    .release =   s3c24xx_buttons_close, 
    .read    =   s3c24xx_buttons_read,
};

完善s3c24xx_buttons_open函数

在s3c24xx_buttons_open函数内,需要注册按键中断。

1、首先了解一下中断注册函数与注销函数

参考资料:https://www.cnblogs.com/ranson7zop/p/7567161.html

/*
在linux内核中用于申请中断的函数是request_irq(),函数原型在Kernel/irq/manage.c中定义:
*/
int request_irq(unsigned int irq, irq_handler_t handler,unsigned long irqflags, const char*devname,void *dev_id)
void free_irq(unsigned int irq, void *dev_id)
  • unsigned int irq 是要申请的硬件中断号 , 比如 IRQ_EINT0。
  • irq_handler_t handler 是回调函数,是中断发生时调用的函数
  • unsigned long irqflags 中断处理的属性,比如IRQF_TRIGGER_FALLING下降沿中断
  • const char*devname 中断的名称,cat /proc/interrupts中可以看到此名称。
  • void *dev_id 多个中断公用一个回调函数时可以用这个来返回一些参数,确定是什么中断发生了
  • request_irq()返回0表示成功,返回-INVAL表示中断号无效或处理函数指针为NULL,返回-EBUSY表示中断已经被占用且不能共享
2、根据硬件确定按键中断的注册信息
struct button_irq_desc {
    int irq;
    unsigned long flags;
    char *name;
};

/* 用来指定按键所用的外部中断引脚及中断触发方式, 名字 */
static struct button_irq_desc button_irqs [] = {
    {IRQ_EINT19, IRQF_TRIGGER_FALLING, "KEY1"}, /* K1 */
    {IRQ_EINT11, IRQF_TRIGGER_FALLING, "KEY2"}, /* K2 */
    {IRQ_EINT2,  IRQF_TRIGGER_FALLING, "KEY3"}, /* K3 */
    {IRQ_EINT0,  IRQF_TRIGGER_FALLING, "KEY4"}, /* K4 */
};
3、定义一个四个按键中断共享的回调函数,以及回调函数要返回的参数
//可以作为判断是哪个按键按下了
static volatile int press_cnt [] = {0, 0, 0, 0};
//将press_cnt作为dev_id注册进中断,我们可以在buttons_interrupt对press_cnt进行更改,然后返回
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
4、在s3c24xx_buttons_open函数内注册中断
/* 应用程序对设备文件/dev/buttons执行open(...)时,
 * 就会调用s3c24xx_buttons_open函数
 */
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
    int i;
    int err;
    
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
        // 注册中断处理函数
        err = request_irq(button_irqs[i].irq,buttons_interrupt,button_irqs[i].flags, button_irqs[i].name, (void *)&press_cnt[i]);
        if (err)
            break;
    }

    if (err) {
        // 释放已经注册的中断
        i--;
        for (; i >= 0; i--)
            free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
        return -EBUSY;
    }
    
    return 0;
}

完善s3c24xx_buttons_close函数

s3c24xx_buttons_close应该对按键中断进行解除注册

/* 应用程序对设备文件/dev/buttons执行close(...)时,
 * 就会调用s3c24xx_buttons_close函数
 */
static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
    int i;
    
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
        // 释放已经注册的中断
        free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
    }

    return 0;
}

完善s3c24xx_buttons_read函数

1、梳理一下read函数应该做些什么,用到什么功能
  • 应用程序调用这个函数的时候,不应该进入循环查询的流程,而是应该等待中断发生,这就需要用到linux的等待队列,以及唤醒机制
  • 按键按下后,read函数应该怎么样返回一个按键值给应用程序?
2、了解LINUX的等待队列以及唤醒
//声明一个等待队列头
DECLARE_WAIT_QUEUE_HEAD(name);
//唤醒处于TASK_INTERRUPTIBLE的进程
wake_up_interruptible(name);
//等待以第一个参数作为等待队列头的等待队列被唤醒,第二个参数condition必须被满足,否则继续阻塞。
wait_event_interruptible(name, condition)
  • 可以知道我们还需要一个condition作为唤醒的条件。
  • 我们可以定义一个全局变量,作为condition
首先实现等待中断发生的功能
  • 定义一个等待队列头
DECLARE_WAIT_QUEUE_HEAD(button_waitq);
  • 定义一个condition,用来唤醒队列
/* 中断事件标志, 中断服务程序将它置1,s3c24xx_buttons_read将它清0 */
static volatile int ev_press = 0;
  • 在中断回调函数内加入ev_press 满足condition, 并唤醒队列
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{

	
    ev_press = 1;                /* 表示中断发生了 */
	
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    
    return IRQ_RETVAL(IRQ_HANDLED);
}
  • 在read函数内等待中断唤醒,并在唤醒后清ev_press 这个条件
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
    /* 如果ev_press等于0,休眠 */
    wait_event_interruptible(button_waitq, ev_press);
    ev_press = 0;
}
实现按键键值传递给read
  • 首先修改中断回调函数,根据注册时的定义,不同按键中断的dev_id对应不同的press_cnt[],根据这个在回调函数内可以对dev_id进行更改来传递按键值
  • read函数只要根据press_cnt[]数组就能知道是什么按键按下了
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
    volatile int *press_cnt = (volatile int *)dev_id;
    
    *press_cnt = *press_cnt + 1; /* 按键计数加1 */
	
    ev_press = 1;                /* 表示中断发生了 */
	
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    
    return IRQ_RETVAL(IRQ_HANDLED);
}
实现按键键值传递给用户程序
unsigned long copy_to_user(void *to, const void *from, unsigned long n)
//to:目标地址(用户空间)
//from:源地址(内核空间)
//n:将要拷贝数据的字节数
//返回:成功返回0,失败返回没有拷贝成功的数据字节数
  • 修改read函数如下,将内核空间的press_cnt传递到用户空间,并清空press_cnt数组
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, 
                                         size_t count, loff_t *offp)
{
    unsigned long err;
    
    /* 如果ev_press等于0,休眠 */
    wait_event_interruptible(button_waitq, ev_press);

    /* 执行到这里时,ev_press等于1,将它清0 */
    ev_press = 0;

    /* 将按键状态复制给用户,并清0 */
    err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
    memset((void *)press_cnt, 0, sizeof(press_cnt));

    return err ? -EFAULT : 0;
}

这样,我们的read函数也完成了

末尾添加一些驱动信息

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("http://www.100ask.net");             // 驱动程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");   // 一些描述信息
MODULE_LICENSE("GPL");                              // 遵循的协议

最后贴上完整的驱动代码

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>

#define DEVICE_NAME     "buttons"   /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */
#define BUTTON_MAJOR    232         /* 主设备号 */

struct button_irq_desc {
    int irq;
    unsigned long flags;
    char *name;
};

/* 用来指定按键所用的外部中断引脚及中断触发方式, 名字 */
static struct button_irq_desc button_irqs [] = {
    {IRQ_EINT19, IRQF_TRIGGER_FALLING, "KEY1"}, /* K1 */
    {IRQ_EINT11, IRQF_TRIGGER_FALLING, "KEY2"}, /* K2 */
    {IRQ_EINT2,  IRQF_TRIGGER_FALLING, "KEY3"}, /* K3 */
    {IRQ_EINT0,  IRQF_TRIGGER_FALLING, "KEY4"}, /* K4 */
};

/* 按键被按下的次数(准确地说,是发生中断的次数) */
static volatile int press_cnt [] = {0, 0, 0, 0};

/* 等待队列: 
 * 当没有按键被按下时,如果有进程调用s3c24xx_buttons_read函数,
 * 它将休眠
 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

/* 中断事件标志, 中断服务程序将它置1,s3c24xx_buttons_read将它清0 */
static volatile int ev_press = 0;


static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
    volatile int *press_cnt = (volatile int *)dev_id;
    
    *press_cnt = *press_cnt + 1; /* 按键计数加1 */
	
    ev_press = 1;                /* 表示中断发生了 */
	
    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    
    return IRQ_RETVAL(IRQ_HANDLED);
}


/* 应用程序对设备文件/dev/buttons执行open(...)时,
 * 就会调用s3c24xx_buttons_open函数
 */
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
    int i;
    int err;
    
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
        // 注册中断处理函数
        err = request_irq(button_irqs[i].irq, buttons_interrupt, button_irqs[i].flags, 
                          button_irqs[i].name, (void *)&press_cnt[i]);
        if (err)
            break;
    }

    if (err) {
        // 释放已经注册的中断
        i--;
        for (; i >= 0; i--)
            free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
        return -EBUSY;
    }
    
    return 0;
}


/* 应用程序对设备文件/dev/buttons执行close(...)时,
 * 就会调用s3c24xx_buttons_close函数
 */
static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
    int i;
    
    for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
        // 释放已经注册的中断
        free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
    }

    return 0;
}


/* 应用程序对设备文件/dev/buttons执行read(...)时,
 * 就会调用s3c24xx_buttons_read函数
 */
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, 
                                         size_t count, loff_t *offp)
{
    unsigned long err;
    
    /* 如果ev_press等于0,休眠 */
    wait_event_interruptible(button_waitq, ev_press);

    /* 执行到这里时,ev_press等于1,将它清0 */
    ev_press = 0;

    /* 将按键状态复制给用户,并清0 */
    err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
    memset((void *)press_cnt, 0, sizeof(press_cnt));

    return err ? -EFAULT : 0;
}

/* 这个结构是字符设备驱动程序的核心
 * 当应用程序操作设备文件时所调用的open、read、write等函数,
 * 最终会调用这个结构中的对应函数
 */
static struct file_operations s3c24xx_buttons_fops = {
    .owner   =   THIS_MODULE,    /* 这是一个宏,指向编译模块时自动创建的__this_module变量 */
    .open    =   s3c24xx_buttons_open,
    .release =   s3c24xx_buttons_close, 
    .read    =   s3c24xx_buttons_read,
};

/*
 * 执行“insmod s3c24xx_buttons.ko”命令时就会调用这个函数
 */
static int __init s3c24xx_buttons_init(void)
{
    int ret;

    /* 注册字符设备驱动程序
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数
     * BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号
     */
    ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }
    
    printk(DEVICE_NAME " initialized\n");
    return 0;
}

/*
 * 执行”rmmod s3c24xx_buttons.ko”命令时就会调用这个函数 
 */
static void __exit s3c24xx_buttons_exit(void)
{
    /* 卸载驱动程序 */
    unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
}

/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_buttons_init);
module_exit(s3c24xx_buttons_exit);

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_AUTHOR("http://www.100ask.net");             // 驱动程序的作者
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");   // 一些描述信息
MODULE_LICENSE("GPL");                              // 遵循的协议

猜你喜欢

转载自blog.csdn.net/tiantangmoke/article/details/103060997