三星210--input中断输入驱动实例

本文深入解析了驱动程序实现中按键输入与中断处理的关键技术,包括使用 Linux 内核 API 进行中断配置、GPIO 操作及事件报告等。详细介绍了驱动程序初始化、中断处理函数实现以及设备注册流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

三 、驱动程序例子

[html]view plaincopyprint? 
  1. #include <linux/input.h>      
  2. #include <linux/module.h>      
  3. #include <linux/init.h>      
  4. #include <linux/kernel.h>      
  5. #include <linux/irq.h>      
  6. #include <linux/interrupt.h>      
  7. #include <mach/gpio.h>      
  8. #include <mach/regs-gpio.h>      
  9. #include <asm/irq.h>      
  10. #include <asm/io.h>      
  11.           
  12. #define DEV_NAME         "KEY1"   
  13. #define DEV_NAME1     "KEY2"      
  14. #define BUTTON_IRQ        IRQ_EINT0      
  15. #define BUTTON_IRQ1        IRQ_EINT0      
  16.    
  17. static struct input_dev *button_dev;     
  18. void delay()   
  19. {   
  20.     unsigned int i,j;   
  21.     for(i=5000;i>0;i--)   
  22.     {   
  23.         for(j=5000;j>0;j--);   
  24.     }   
  25. }   
  26.    
  27. static irqreturn_t button_interrupt(int irq, void *p)     
  28. {     
  29.     /*get pin value <down 0, up 1> */     
  30.     int val = gpio_get_value (S5PV210_GPH0(0));    // s3c2410_gpio_getpin(S3C2410_GPG(0));     
  31.     /* val must a variable */     
  32.     printk("Enter EIENT0 ! \n");   
  33.     input_report_key(button_dev,KEY_5, val);     
  34.     input_sync(button_dev);   
  35.        
  36.     input_report_key(button_dev,KEY_5, !val);     
  37.     input_sync(button_dev);   
  38.    
  39. delay();   
  40.     input_report_key(button_dev,KEY_LEFT, val);     
  41.     input_sync(button_dev);   
  42.        
  43.     input_report_key(button_dev,KEY_LEFT, !val);     
  44.     input_sync(button_dev);   
  45.    
  46.     return IRQ_RETVAL(IRQ_HANDLED);     
  47. }   
  48.    
  49. //--------------------------------------------------------------------------------------------   
  50. static int __init button_init(void)     
  51. {     
  52.     int err;     
  53.        
  54.     gpio_direction_input(S5PV210_GPH0(0));   
  55.     s3c_gpio_cfgpin(S5PV210_GPH0(0), S3C_GPIO_SFN(0x0000000f));    //设置为外部中断 eint0 ,参考GP0CON【】   
  56.     set_irq_type(IRQ_EINT0, IRQ_TYPE_EDGE_FALLING);       
  57.     gpio_direction_input(S5PV210_GPH0(1));   
  58.     s3c_gpio_cfgpin(S5PV210_GPH0(1), S3C_GPIO_SFN(0x000000f0));    //设置为外部中断 eint1 ,参考GP0CON【】   
  59.     set_irq_type(IRQ_EINT1, IRQ_TYPE_EDGE_FALLING);           
  60.               
  61.     if(request_irq(BUTTON_IRQ, button_interrupt,IRQ_TYPE_EDGE_FALLING,DEV_NAME, button_dev))   
  62.     {   
  63.         input_free_device(button_dev);   
  64.         printk(KERN_ERR"cannot allocate irq0");     
  65.         return- EBUSY;     
  66.     }     
  67.    
  68.    
  69. //--------------------------------------------------------------------------------------                 
  70.     button_dev= input_allocate_device();     
  71.     if(button_dev == NULL)   
  72.     {     
  73.         printk(KERN_ERR"not enough memory\n");     
  74.         err= - ENOMEM;     
  75.         goto err_free_irq;     
  76.     }     
  77. //-----------------------------------------------------------------------------------------   
  78.     button_dev->name = "Mini Button Optical Finger Navigation Module ";   
  79. printk("1");   
  80.     button_dev->phys = "OFN/OFN0";   
  81. printk("2");   
  82.     button_dev->id.bustype = BUS_HOST;   
  83. printk("3");   
  84.     button_dev->id.vendor = 0x0001;   
  85. printk("4");   
  86.     button_dev->id.product = 0x0001;   
  87. printk("5");   
  88.     button_dev->id.version = 0x0100;   
  89. printk("6");   
  90.    
  91. //-----------------------------------------------------------------------------------------     
  92. //    __set_bit(EV_REP, button_dev->evbit);   
  93.     set_bit(EV_KEY,button_dev->evbit);     
  94.     set_bit(KEY_5,button_dev->keybit);   
  95.     set_bit(KEY_LEFT,button_dev->keybit);   
  96. //------------------------------------------------------------------------------------------   
  97.    
  98.     err= input_register_device(button_dev);     
  99.     if(err)   
  100.     {     
  101.         printk(KERN_ERR"failed to register device\n");     
  102.         goto err_free_dev;     
  103.     }     
  104.     printk("initialized\n");     
  105.     return 0;     
  106.           
  107.     err_free_dev:     
  108.         input_free_device(button_dev);     
  109.         return err;   
  110.     err_free_irq:     
  111.         free_irq(BUTTON_IRQ,NULL);     
  112.         return err;     
  113. }   
  114.    
  115. static void __exit button_exit(void)     
  116. {     
  117.     input_unregister_device(button_dev);     
  118.     free_irq(BUTTON_IRQ,NULL);     
  119. }     
  120.    
  121. module_init(button_init);     
  122. module_exit(button_exit);     
  123.    
  124. MODULE_LICENSE("GPL");     
  125. MODULE_AUTHOR("light>");  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值