zoukankan      html  css  js  c++  java
  • Linux学习 :中断处理机制 & poll机制

      中断是指在CPU正常运行期间,由于内外部事件或由程序预先安排的事件引起的CPU暂时停止正在运行的程序,转而为该内部或外部事件或预先安排的事件服务 的程序中去,服务完毕后再返回去继续运行被暂时中断的程序。Linux中通常分为外部中断(又叫硬件中断)和内部中断(又叫异常)。

    单片机中断处理

    ①分辨中断类型
    ②调用处理函数
    ③清中断

    Linux系统 : asm_do_IRQ

    1.申请中断:request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,const char *name, void *dev);
    ①分配irqaction结构。
    ②setup_irq(irq, action);    a.加入中断链表:irq_desc[irq]->action.  b.设置中断引脚和中断类型:desc->chip->settype()  c.使能中断:desc->chip->startup/enable
    2.释放中断:free_irq(irq, dev_id);
    ①从链表取出中断
    ②禁止中断,free

    中断线共享的数据结构:
       struct irqaction {
        irq_handler_t handler; /* 具体的中断处理程序 */
        unsigned long flags;/*中断处理属性*/
        const char *name; /* 名称,会显示在/proc/interreupts 中 */
        void *dev_id; /* 设备ID,用于区分共享一条中断线的多个处理程序 */
        struct irqaction *next; /* 指向下一个irq_action 结构 */
        int irq;  /* 中断通道号 */
        struct proc_dir_entry *dir; /* 指向proc/irq/NN/name 的入口*/
        irq_handler_t thread_fn;/*线程中断处理函数*/
        struct task_struct *thread;/*线程中断指针*/
        unsigned long thread_flags;/*与线程有关的中断标记属性*/
    };

    thread_flags参见枚举型:
    enum {
        IRQTF_RUNTHREAD,/*线程中断处理*/
        IRQTF_DIED,/*线程中断死亡*/
        IRQTF_WARNED,/*警告信息*/
        IRQTF_AFFINITY,/*调整线程中断的关系*/
    };

    多个中断处理程序可以共享同一条 中断线(引脚),irqaction 结构中的 next 成员用来把共享同一条中断线的所有中断处理程序组成一个单向链表,dev_id 成员用于区分各个中断处理程序。

    3.poll机制
     app: poll
     kernel: sys_poll -> do_sys_poll(..., timeout_jiffies) -> poll_initwait(&table); -> init_poll_funcptr(&pwq->pt, __pollwait); -> pt->qproc = qproc -> do_poll(nfds, head, &table, timeout) :
          for(;;)
          {
            if (do_pollfd(pfd, pt)){   ->   mask = file->f_op->poll(file, pwait);  return mask;
                           //驱动poll:
                            __pollwait(flip, &button_waitq, p); //把当前进程挂到button_waitq队列里去
              count++;
              pt = NULL;
            }
            //break的条件:count非0, 超时,有信号在等待处理
            if (count || !*timeout || signal_pending(current))
              break;
            //休眠__timeout 5s
            __timeout = schedule_timeout(__timeout);
          }
       

    等待队列:

              在 Linux 驱动程序设计中,可以使用等待队列可以看作保存进程的容器,在阻塞进程时,将进程放入等待队列;

              当唤醒进程时,从等待队列中取出进程.

    等待队列的 定义 和 初始化 wait_queue_head_t    DECLARE_WAIT_QUEUE_HEAD  :

              Linux 2.6 内核提供了如下关于等待队列的操作:

              1,定义等待队列.

                        wait_queue_head_t   my_queue

              2,初始化等待队列.

                        init_waitqueue_head ( &my_queue )

              3,定义并初始化等待队列.

                       DECLARE_WAIT_QUEUE_HEAD  ( my_queue )


    等待队列的 睡眠  wait_event_interruptible :

    有条件睡眠:

                  1,  wait_event ( queue , condition )

                        当 condition ( 一个布尔表达式 ) 为真,立即返回;否则让进程进入 TASK_UNINTERRUPTIBLE 模式

                        睡眠,并挂在 queue 参数所指定的等待队列上.


                 2,  wait_event_interruptible ( queue , condition )

                        当 condition ( 一个布尔表达式 ) 为真,立即返回;否则让进程进入 TASK_INTERRUPTIBLE 模式

                        睡眠,并挂在 queue 参数所指定的等待队列上.


                  3, int  wait_event_killable ( wait_queue_t  queue , condition )

                        当 condition ( 一个布尔表达式 ) 为真,立即返回;否则让进程进入 TASK_KILLABLE 模式

                        睡眠,并挂在 queue 参数所指定的等待队列上.


    无条件睡眠:

                        ( 老版本,不建议使用 )

                        sleep_on  ( wait_queue_head_t  *q )

                        让进程进入 不可中断 的睡眠,并把它放入等待队列 q.


                        interruptible_sleep_on  ( wait_queue_head_t  *q )

                        让进程进入 可中断 的睡眠,并把它放入等待队列 q.


    等待队列中唤醒进程 wake_up :

                        wake_up ( wait_queue_t  *q )

                        从等待队列 q 中唤醒状态为 TASKUNINTERRUPTIBLE ,TASK_INTERRUPTIBLE ,TASK_KILLABLE

                        的所有进程.

                        wake_up_interruptible ( wait_queue_t  *q )

                        从等待队列 q 中唤醒状态为 TASK_INTERRUPTIBLE 的进程.

     
          

    按键中断驱动示例代码
    1.驱动代码:button_drv.c

    #include <linux/module.h>
    #include <linux/kernel.h>
    #include <linux/fs.h>
    #include <linux/init.h>
    #include <linux/delay.h>
    #include <linux/irq.h>
    #include <asm/uaccess.h>
    #include <asm/irq.h>
    #include <asm/io.h>
    #include <asm/arch/regs-gpio.h>
    #include <asm/hardware.h>
    #include <linux/poll.h>
     
     
    static struct class *buttondrv_class;
    static struct class_device    *buttondrv_class_dev;
     
    volatile unsigned long *gpfcon;
    volatile unsigned long *gpfdat;
     
    volatile unsigned long *gpgcon;
    volatile unsigned long *gpgdat;
     
    /*该宏初始化一个button_waitq队列*/
    static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
     
    /* 中断事件标志, 中断服务程序将它置1,button_drv_read将它清0 */
    static volatile int ev_press = 0;
     
     
    struct pin_desc{
        unsigned int pin;
        unsigned int key_val;
    };
     
     
    /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
    /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
    static unsigned char key_val;
     
    struct pin_desc pins_desc[4] = {
        {S3C2410_GPF0, 0x01},
        {S3C2410_GPF2, 0x02},
        {S3C2410_GPG3, 0x03},
        {S3C2410_GPG11, 0x04},
    };
     
     
    /*
      * 确定按键值
      */
    static irqreturn_t buttons_irq(int irq, void *dev_id)
    {
        struct pin_desc * pindesc = (struct pin_desc *)dev_id;
        unsigned int pinval;
        
        pinval = s3c2410_gpio_getpin(pindesc->pin);
     
        if (pinval)
        {
            /* 松开 */
            key_val = 0x80 | pindesc->key_val;
        }
        else
        {
            /* 按下 */
            key_val = pindesc->key_val;
        }
     
        ev_press = 1;                  /* 表示中断发生了 */
        wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
     
        
        return IRQ_RETVAL(IRQ_HANDLED);
    }
     
    static int button_drv_open(struct inode *inode, struct file *file)
    {
        /* 配置GPF0,2为输入引脚 */
        /* 配置GPG3,11为输入引脚 */
        request_irq(IRQ_EINT0,  buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
        request_irq(IRQ_EINT2,  buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
        request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
        request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);    
     
        return 0;
    }
     
    ssize_t button_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
    {
        if (size != 1)
            return -EINVAL;
     
        /* 如果没有按键动作, 休眠 */
        wait_event_interruptible(button_waitq, ev_press); //ev_press : 0-休眠,1-返回
     
        /* 如果有按键动作, 返回键值 */
        copy_to_user(buf, &key_val, 1);
        ev_press = 0;
        
        return 1;
    }
     
    int button_drv_close(struct inode *inode, struct file *file)
    {
        free_irq(IRQ_EINT0, &pins_desc[0]);
        free_irq(IRQ_EINT2, &pins_desc[1]);
        free_irq(IRQ_EINT11, &pins_desc[2]);
        free_irq(IRQ_EINT19, &pins_desc[3]);
        return 0;
    }
     
    static unsigned button_drv_poll(struct file *file, poll_table *wait)
    {
        unsigned int mask = 0;
        poll_wait(file, &button_waitq, wait); // 不会立即休眠
     
        if (ev_press)
            mask |= POLLIN | POLLRDNORM;
     
        return mask;
    }
     
    static struct file_operations button_drv_fops = {
        .owner   =  THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
        .open    =  button_drv_open,      
        .read    =    button_drv_read,        
        .release =  button_drv_close,
        .poll    =  button_drv_poll,
    };
     
     
    int major;
    static int button_drv_init(void)
    {
        major = register_chrdev(0, "button_drv", &button_drv_fops);
     
        buttondrv_class = class_create(THIS_MODULE, "button_drv");
     
        buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
     
        gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
        gpfdat = gpfcon + 1;
     
        gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
        gpgdat = gpgcon + 1;
     
        return 0;
    }
     
    static void button_drv_exit(void)
    {
        unregister_chrdev(major, "button_drv");
        class_device_unregister(buttondrv_class_dev);
        class_destroy(buttondrv_class);
        iounmap(gpfcon);
        iounmap(gpgcon);
        return 0;
    }
     
     
    module_init(button_drv_init);
     
    module_exit(button_drv_exit);
     
    MODULE_LICENSE("GPL");

    2.测试代码:buttondrvtest.c

    #include <sys/types.h> 
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <poll.h>
     
     
    /* buttondrvtest */
    int main(int argc, char **argv)
    {
        int fd;
        unsigned char key_val;
        int ret;
     
        struct pollfd fds[1]; //同时可以查询多个,当前只定义一个
        
        fd = open("/dev/buttons", O_RDWR);
        if (fd < 0)
        {
            printf("can't open! ");
        }
     
        fds[0].fd     = fd; //要查询的文件
        fds[0].events = POLLIN; //期待获得的值,POLLIN:有数据等待读取
        while (1)
        {
         //read(fd, &key_val, 1); //wait_event_interruptible(button_waitq, ev_press); 会休眠阻塞到中断发生返回,使用poll机制可以避免此问题
         //printf("key_val = 0x%x ", key_val);
    //sleep(5);

            //int poll(struct pollfd *fds, nfds_t nfds, int timeout);
         ret = poll(fds, 1, 5000);
            if (ret == 0)
            {
                printf("time out ");
            }
            else
            {
                read(fd, &key_val, 1);
                printf("key_val = 0x%x ", key_val);
            }
        }
        return 0;
    }

    3.Makefile:

    KERN_DIR = /work/system/linux-2.6.22.6

    all:
        make -C $(KERN_DIR) M=`pwd` modules

    clean:
        make -C $(KERN_DIR) M=`pwd` modules clean
        rm -rf modules.order

    obj-m    += button_drv.o

     

  • 相关阅读:
    js 构造函数 constructor
    js foreach和map区别
    js 静态方法和实例方法
    学习知识点总结(es6篇)
    java1.5新特性(转)
    21 Managing the Activity Lifecycle
    Java进阶Collection集合框架概要·16
    Java进阶核心之集合框架Map下集·18
    Java进阶核心之集合框架Set·19
    Java进阶核心之集合框架List·17
  • 原文地址:https://www.cnblogs.com/blogs-of-lxl/p/5872758.html
Copyright © 2011-2022 走看看