美文网首页
按键驱动程序

按键驱动程序

作者: 嵌入式工作 | 来源:发表于2018-08-09 15:05 被阅读0次

    按键驱动程序

    request_irq
    free_irq
    IRQ_EINT

    1.添加request_irq、free_irq以及中断处理函数

    2记得去掉GPIO_BUTTON以及添加平台文件

    3疑问,如何把datasheet中中断9 和IRQ_EINT(9)对应的

    4疑问,app应用程序如何知道按键被按下了

    通过命令: cat /proc/interrupts查看中断

    
    #include <linux/init.h>
    #include <linux/module.h>
    
    #include <linux/cdev.h>
    #include <linux/fs.h>
    #include <linux/device.h>
    
    /*Linux中申请GPIO的头文件*/
    #include <linux/gpio.h>
    /*三星平台的GPIO配置函数头文件*/
    /*三星平台EXYNOS系列平台,GPIO配置参数宏定义头文件*/
    #include <plat/gpio-cfg.h>
    /*三星平台4412平台,GPIO宏定义头文件*/
    #include <mach/gpio-exynos4.h>
    #include <linux/platform_device.h>
    
    #include <linux/miscdevice.h>
    #include <linux/irq.h>
    #include <linux/interrupt.h>
    
    MODULE_LICENSE("Dual BSD/GPL");
    MODULE_AUTHOR("kerwin");
    
    
    #define DRIVER_NAME   "irq_key"
    #define DEVICE_NAME  "key_irq_device"
    
    
    #define LED EXYNOS4_GPL2(0)
    unsigned int maj=0,min=0;
    dev_t dev_num;
    
    
    int leds_open(struct inode *inode,struct file *filp)
    {
        
        return 0;
    }
    
    int leds_release(struct inode *inode,struct file *filp)
    {
    
        return 0;
    }
    
    
    
    long leds_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
    {
    
    
    
        return 0;
    }
    
    static struct file_operations led_ops = {
        .owner  = THIS_MODULE,
        .open   = leds_open,
        .release= leds_release,
        .unlocked_ioctl     = leds_ioctl,
    };
    
    
    struct miscdevice  m_misc={
        .minor=MISC_DYNAMIC_MINOR,
        .name =DRIVER_NAME,
        .fops=&led_ops,
    
    };
    
    irqreturn_t key_irq_handler_t (int irq, void *dev_id)
    {
        
        printk(KERN_EMERG"press:%d\n",irq); 
            return IRQ_HANDLED;
    }
    
    int key_probe(struct platform_device *pd)
    {
        misc_register(&m_misc);
        if(request_irq(IRQ_EINT(9),key_irq_handler_t,IRQ_TYPE_EDGE_FALLING,"key_irq",pd)<0)
        {
            printk(KERN_EMERG"request_irq err1\n");
        }else
        {
            printk(KERN_EMERG"request_irq success1\n");
        }
        return 0;
    }
    int key_remove(struct platform_device *pd)
    {
        misc_deregister(&m_misc);
        free_irq(IRQ_EINT(9),pd);
        return 0;
    }
    void key_shutdown(struct platform_device *pd)
    {
        
    
    }
    int key_suspend(struct platform_device *pd, pm_message_t state)
    {
        
        return 0;
    }
    int key_resume(struct platform_device *pd)
    {
        
        return 0;
    }
    static struct platform_driver key_driver = {
        .driver = {
            .name = DRIVER_NAME,
            .owner = THIS_MODULE,
        },
        .probe=key_probe,
        .remove=key_remove,
        .shutdown=key_shutdown,
        .suspend=key_suspend,
        .resume=key_resume,
    
        
    };
    int irq_key_init(void)
    {
        
        int ret;
        printk(KERN_EMERG"module_init call \n");
    
        platform_driver_register(&key_driver);
        return 0;
    }
    
    
    void irq_key_exit(void)
    {
         printk(KERN_EMERG"module_exit call \n");
           platform_driver_unregister(&key_driver);
         
        
    }
    
    module_init(irq_key_init);
    module_exit(irq_key_exit);
    
    
    

    相关文章

      网友评论

          本文标题:按键驱动程序

          本文链接:https://www.haomeiwen.com/subject/ldxcbftx.html