中断字符驱动程序

来源:互联网 发布:三星一体机扫描软件 编辑:程序博客网 时间:2024/05/29 23:25

经典字符程序,对一些自己不太懂的地方稍微分析了一下

驱动程序

#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);
}
//根据《LINUX设备驱动程序的说法,volatile int *press_cnt = (volatile int *)dev_id;我们会为dev_id传递一个纸箱自己设备的数据结构指针,这样,一个管理若干同样设备的驱动程序在中断处理例程中不需要做额外的代码,就可以找出那个设备产生了中断时间。
详细可以参考
http://linux.chinaunix.net/techdoc/net/2008/12/16/1053047.shtml

/* 应用程序对设备文件/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;
}
/*request_irq是最核心的函数,函数原型int request_irq(unsigned int irq,irqreturn_t(*handler)(int, void *,sturct pt_reg *),const char *dev_name,void *dev_id);
Unsigned int irq
这是申请的中断号。
irqreturn_t(*handler)(int, void *,sturct pt_reg *)
这是要安装中断处理函数指针。
Void *dev_id
这个指针用于共享的中断信号线。它是唯一的标识符,在中断信号线空闲时可以使用它,驱动程序也可以使用它指向驱动程序自己的私数据区。
Flags
标志,详细可以参考《LINUX设备驱动程序》

在这条程序里面irqreturn_t(*handler)(int, void *,sturct pt_reg *)
/* 应用程序对设备文件/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;
}

//read函数的核心当然是copy_to_user,将驱动的内容传递到用户空间,为内核、驱动跟系统搭建了一个交流平台buf,通过这个位数不多的功用空间进行交流

/* 这个结构是字符设备驱动程序的核心
* 当应用程序操作设备文件时所调用的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;
    }
    devfs_mk_cdev(MKDEV(BUTTON_MAJOR, 0), S_IFCHR | S_IRUSR | S_IWUSR | S_IRGRP, DEVICE_NAME);
    printk(DEVICE_NAME " initialized/n");
    return 0;
}

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

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


应用程序
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    int i;
    int ret;
    int fd;
    int press_cnt[4];
   
    fd = open("/dev/buttons", 0); // 打开设备
    if (fd < 0) {
        printf("Can't open /dev/buttons/n");
        return -1;
    }

    // 这是个无限循环,进程有可能在read函数中休眠,当有按键被按下时,它才返回
    while (1) {
        // 读出按键被按下的次数
        ret = read(fd, press_cnt, sizeof(press_cnt));
        if (ret < 0) {
            printf("read err!/n");
            continue;
        }

        for (i = 0; i < sizeof(press_cnt)/sizeof(press_cnt[0]); i++) {
            // 如果被按下的次数不为0,打印出来
            if (press_cnt[i])
                printf("K%d has been pressed %d times!/n", i+1, press_cnt[i]);
        }
    }
   
    close(fd);
    return 0;

 

原创粉丝点击