- 论坛徽章:
- 0
|
本帖最后由 heyangya 于 2010-08-05 14:54 编辑
哦,我现在也迷惑在按键驱动中,可否看一下你的代码?
linuxman-phil 发表于 2010-08-05 07:17 ![]()
- /*
- S2、S3、S4、S5 四个按键用到以下四个I/O口:EINT0/GPF0 EINT2/GPF2 EINT3/GPF3 EINT4/GPF4 当键被按下去时,会产生相应的中断
- */
- #include <linux/kernel.h>
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/types.h>
- #include <linux/fs.h>
- #include <linux/kdev_t.h>
- #include <linux/cdev.h>
- #include <mach/irqs.h>
- #include <linux/interrupt.h>
- #include <linux/timer.h>
- #include <asm/atomic.h>
- #define DEV_NAME "my_button_driver"
- #define DEV_COUNT 4 //按键数目
- unsigned int dev_major=230;
- //设备结构体
- struct my_dev_struct
- {
- unsigned int id;//设备ID
- int irq_num;//中断号
- char name[10];//设备名
- };
- struct my_dev_struct mydevs[]=
- {
- {0,IRQ_EINT0,"KEY_0"},
- {1,IRQ_EINT2,"KEY_1"},
- {2,IRQ_EINT3,"KEY_2"},
- {3,IRQ_EINT4,"KEY_3"},
- };
- struct timer_list key_timers[DEV_COUNT];//定时器数组
- atomic_t a_key_press=ATOMIC_INIT(0);//是否被按下
- void timer_handler(unsigned long arg)
- {
- struct my_dev_struct * devp=(struct my_dev_struct *)arg;
- printk("%s is pressed!\n",devp->name);
- atomic_dec(&a_key_press);
- }
- irqreturn_t interrupt_hander(int irq, void *dev_id)
- {
- atomic_inc(&a_key_press);
- if(atomic_dec_and_test(&a_key_press))
- {
- struct my_dev_struct * devp=(struct my_dev_struct *)dev_id;
- int num=devp->id;
- atomic_inc(&a_key_press);
- init_timer(&key_timers[num]);
- key_timers[num].function=timer_handler;
- key_timers[num].expires=jiffies+HZ/10;
- key_timers[num].data=(unsigned long)devp;
- add_timer(&key_timers[num]);
- }
- return IRQ_RETVAL(IRQ_HANDLED);
- }
- int dev_open(struct inode *inode,struct file *filp)
- {
- int i,retval;
- for(i=0;i<DEV_COUNT;i++)
- {
- //请求中断
- retval=request_irq(mydevs[i].irq_num,interrupt_hander,IRQF_TRIGGER_LOW|IRQF_DISABLED,mydevs[i].name,(void*)&mydevs[i]);
- if(retval)
- goto error;
- }
- return 0;
- error:
- i--;
- while(i>=0)
- free_irq(mydevs[i].irq_num,(void*)&mydevs[i--]);
- return retval;
- }
- //设备关闭后,释放中断
- int dev_release(struct inode *inode,struct file *filp)
- {
- int i;
- for(i=0;i<DEV_COUNT;i++)
- {
- free_irq(mydevs[i].irq_num,(void*)&mydevs[i]);
- }
- return 0;
- }
- //文件操作结构体
- static struct file_operations fops=
- {
- .owner = THIS_MODULE,
- .open = dev_open,
- .release = dev_release,
- };
- //注册设备
- static int __init my_button_init(void)
- {
- int err;
- err=register_chrdev(dev_major,DEV_NAME,&fops);
- if(err)
- {
- printk("register_chrdev error!\n");
- goto error;
- }
- return 0;
- error:
- return -EBUSY;
- }
- //注销字符设备
- static void __exit my_button_exit(void)
- {
- unregister_chrdev(dev_major,DEV_NAME);
- }
- module_init(my_button_init);
- module_exit(my_button_exit);
- MODULE_LICENSE("GPL");
复制代码 |
|