完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
驱动代码如下:
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int major=0; struct class *buttons_class; struct device *buttons_dev; unsigned int key_val=0; wait_queue_head_t button_wait; static volatile int flag_press=0; static irqreturn_t buttons_ISR(int irq, void *dev_id) { switch(irq) { case IRQ_EINT0: if( s3c2410_gpio_getpin( S3C2410_GPF(0) ) ) { key_val = 0x01; printk("key_val = 0x%xn", key_val); } else { key_val = 0x81; printk("key_val = 0x%xn", key_val); } break; case IRQ_EINT2: if( s3c2410_gpio_getpin( S3C2410_GPF(2) ) ) key_val = 0x02; else key_val = 0x82; break; case IRQ_EINT11: if( s3c2410_gpio_getpin(S3C2410_GPG(3) ) ) key_val = 0x03; else key_val = 0x83; break; case IRQ_EINT19: if( s3c2410_gpio_getpin( S3C2410_GPG(11) ) ) key_val = 0x04; else key_val = 0x84; break; default: break; } flag_press = 1; wake_up_interruptible(&button_wait); return IRQ_HANDLED; } static int buttons_open(struct inode *inode,struct file *file) { request_irq(IRQ_EINT0, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button0", 1); request_irq(IRQ_EINT2, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button2", 1); request_irq(IRQ_EINT11, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button11", 1); request_irq(IRQ_EINT19, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button19", 1); init_waitqueue_head(&button_wait); return 0; } static int buttons_close(struct inode *inode,struct file *file) { free_irq(IRQ_EINT0,1); free_irq(IRQ_EINT2,1); free_irq(IRQ_EINT11,1); free_irq(IRQ_EINT19,1); return 0; } ssize_t buttons_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) { if(size != sizeof(key_val)) { return -EINVAL; } wait_event_interruptible(button_wait, flag_press); copy_to_user(buf, &key_val, 1); flag_press = 0; return sizeof(key_val); } const struct file_operations buttons_fops = { .owner = THIS_MODULE, .open = buttons_open, .read = buttons_read, .release = buttons_close, }; static int buttons_init(void) { major = register_chrdev(0,"buttons",&buttons_fops); buttons_class = class_create(THIS_MODULE, "my_buttons"); buttons_dev = device_create(buttons_class, NULL, MKDEV(major,0), NULL, "buttons"); /* gpfcon = (volatile unsigned long *)ioremap(0x56000050 ,8); gpfdat = gpfcon + 1; gpgcon = (volatile unsigned long *)ioremap(0x56000060 ,8); gpgdat = gpgcon + 1; */ printk("buttons driver initing...n"); return 0; } static void buttons_exit(void) { unregister_chrdev(0,"buttons"); device_unregister(buttons_dev); class_destroy(buttons_class); // iounmap(gpfcon); // iounmap(gpgcon); printk("Bye,buttons.n"); } module_init(buttons_init); module_exit(buttons_exit); MODULE_LICENSE("GPL"); 测试程序代码如下: #include #include #include #include int main(int argc, char **argv) { int fd; int key_val=0; fd = open("/dev/buttons",O_RDWR); if(fd < 0) { printf("open error!n"); } while(1) { read(fd, &key_val, 1); printf("key_val = 0x%x n",key_val); } return 0; } 请高手给我看看啊,终端一直向外打印“key_val = 0x0”,看了半天没看出问题啊? |
|
相关推荐
2个回答
|
|
static int buttons_open(struct inode *inode,struct file *file)
{ request_irq(IRQ_EINT0, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button0", 1); request_irq(IRQ_EINT2, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button2", 1); request_irq(IRQ_EINT11, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button11", 1); request_irq(IRQ_EINT19, buttons_ISR, IRQ_TYPE_EDGE_BOTH, "button19", 1); init_waitqueue_head(&button_wait); return 0; } 在这里初始化了呀 |
|
|
|
百问网论坛好冷清 都没有人理我。。
|
|
|
|
只有小组成员才能发言,加入小组>>
197个成员聚集在这个小组
加入小组为什么点亮LED的例子放在NORFlash上跑会出现奇怪的现象?
2177 浏览 6 评论
1982 浏览 5 评论
韦东山老师推出的《玩转ARM裸机实战》课程将帮你以上问题一扫而光!
4540 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-20 19:46 , Processed in 0.564340 second(s), Total 48, Slave 38 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号