驅(qū)動(dòng)程序 本文引用地址:http://m.butianyuan.cn/article/201611/316606.htm#include #include #include #include #include #include #include #include #include #include #include #include #includeinterrupt.h> #include #include #include #define DEV_NAME "qjz_buttons"http://DEV_NAME要與應(yīng)用程序的open調(diào)用的NAME一樣 static int buttons_major,buttons_minor=0;//定義設(shè)備號(hào) static struct cdev buttons_cdev;//定義設(shè)備結(jié)構(gòu) static struct class *buttons_class; static dev_t dev;
struct button_irq_desc { int irq; int pin; int pin_setting; int number; char *name; }; static struct button_irq_desc button_irqs[ ]= { {IRQ_EINT1,S3C2410_GPF1,S3C2410_GPF1_EINT1,0,"KEY1"}, {IRQ_EINT4,S3C2410_GPF4,S3C2410_GPF4_EINT4,1,"KEY2"}, {IRQ_EINT2,S3C2410_GPF2,S3C2410_GPF2_EINT2,2,"KEY3"}, {IRQ_EINT0,S3C2410_GPF0,S3C2410_GPF0_EINT0,3,"KEY4"}, }; static volatile int key_values[]={0,0,0,0}; static DECLARE_WAIT_QUEUE_HEAD(button_waitq); static volatile int ev_press = 0; static irqreturn_t buttons_interrupt(int irq,void *dev_id) { struct button_irq_desc *button_irqs=(struct button_irq_desc *)dev_id; int up = s3c2410_gpio_getpin(button_irqs->pin); if(!up) { key_values[button_irqs->number]=(button_irqs->number+1); } ev_press = 1; wake_up_interruptible(&button_waitq); return IRQ_RETVAL(IRQ_HANDLED); } static int buttons_open(struct inode *inode,struct file *filp) { int i,err; unsigned long irq_type; irq_type=ioread32(S3C2410_EXTINT0);//設(shè)置中斷類型為上升沿 irq_type&=~(3<<1 | 3<<5 | 3<<9 | 3<<17 ); irq_type|=(3<<1 | 3<<5 | 3<<9 | 3<<17 ); iowrite32(irq_type,S3C2410_EXTINT0); for(i = 0;i < 4; i++) { s3c2410_gpio_cfgpin(button_irqs[i].pin,button_irqs[i].pin_setting); err = request_irq(button_irqs[i].irq, buttons_interrupt, NULL, button_irqs[i].name,(void*)&button_irqs[i]); if(err)//成功返回值0,失敗返回一個(gè)負(fù)的錯(cuò)誤碼 break; } if(err)//失敗就要釋放已經(jīng)注冊(cè)成功的中斷 { i--; for(;i>=0;i--) { disable_irq(button_irqs[i].irq); free_irq(button_irqs[i].irq,(void*)&button_irqs[i]); } return -EBUSY; } return 0; } static int buttons_close(struct inode *inode,struct file *file) { int i; for(i=0;i<4;i++) { disable_irq(button_irqs[i].irq); free_irq(button_irqs[i].irq,(void*)&button_irqs[i]); } return 0; } static int buttons_read(struct file *filp,char __user *buff,size_t count,loff_t *offp) { unsigned long err; if(!ev_press) { if(filp->f_flags & O_NONBLOCK) return -EAGAIN; else wait_event_interruptible(button_waitq,ev_press); } ev_press = 0; err=copy_to_user(buff,(const void*)key_values,count); memset((void*)key_values,0,sizeof(key_values)); return err?-EFAULT:4; } static unsigned int buttons_poll(struct file *filp,poll_table *wait) { unsigned int mask = 0; poll_wait(filp,&button_waitq,wait); if(ev_press) mask |=POLLIN|POLLRDNORM; return mask; } static struct file_operations buttons_fops = { .owner = THIS_MODULE, .open = buttons_open, .release = buttons_close, .read = buttons_read, .poll = buttons_poll, }; static int __init buttons_init(void) { int result; if(buttons_major) { dev = MKDEV(buttons_major,buttons_minor); result = register_chrdev_region(dev,1,DEV_NAME); } else { result = alloc_chrdev_region(&dev,buttons_minor,1,DEV_NAME); buttons_major = MAJOR(dev); } if(result < 0) { printk(KERN_WARNING "buttons:cant get major %dn",buttons_major); return result; } else { printk(KERN_WARNING"registered oknbuttons_major = %dn",buttons_major); } /*led_setup_cdev(&leds_cdev,0);*/ cdev_init(&buttons_cdev,&buttons_fops); buttons_cdev.owner = THIS_MODULE; buttons_cdev.ops = &buttons_fops; result = cdev_add(&buttons_cdev,dev,1); if(result < 0) { printk(KERN_WARNING"cdev_add failed!n"); return result; } else { printk(KERN_WARNING"cdev_add is ok!!n"); } buttons_class = class_create(THIS_MODULE,DEV_NAME); if(IS_ERR(buttons_class)) { printk(KERN_ALERT"err:fail in buttons_class!n"); return -1; } else { printk(KERN_WARNING"SUCCESS IN buttons_CLASS!n"); } class_device_create(buttons_class,NULL,MKDEV(buttons_major,0),NULL,DEV_NAME); /*class_device_create(leds_class,NULL,dev,NULL,DEV_NAME);*/ printk(KERN_WARNING"buttons Module initialed!n"); return 0; }
static void __exit buttons_exit(void) { int devno = MKDEV(buttons_major,buttons_minor); unregister_chrdev_region(devno,1); cdev_del(&buttons_cdev); class_device_destroy(buttons_class,devno); class_destroy(buttons_class); printk(KERN_WARNING"buttons module exit!n"); } module_init(buttons_init); module_exit(buttons_exit); MODULE_LICENSE("Dual BSD/GPL"); MODULE_DESCRIPTION("buttons device drver"); MODULE_AUTHOR("xxx"); 應(yīng)用程序 #include #include #include #include #include #include #include #include #include #include int main(void) { int i; int buttons_fd; int key_value[4]; buttons_fd=open("/dev/qjz_buttons",0); if(buttons_fd < 0) { perror("open buttons fail"); exit(1); } for(;;) { fd_set reads; int ret; FD_ZERO(&reads); FD_SET(buttons_fd,&reads); ret = select(buttons_fd+1,&reads,NULL,NULL,NULL); if(ret<0) { perror("select"); exit(1); } if(ret ==0) { printf("timeoutn"); } else if(FD_ISSET(buttons_fd,&reads)) { int ret=read(buttons_fd,key_value,sizeof(key_value)); printf("%dn",ret); //if(ret !=sizeof(key_value)) //{ //if(errno !=EAGAIN)//教程中這里是有的,但是不知道為什么每次讀不出來(lái)按鍵值,屏蔽后,就可以讀出來(lái)!如圖! //perror("read buttonsn"); //continue; //} //else //{ for(i=0;i<4;i++) { if(key_value[i] !=0) printf("K%d pressd,key value=0x%dn",i+1,key_value[i]);} continue; // } } } close(buttons_fd); return 0; } 奶奶的,怎么按一次有的時(shí)候會(huì)出現(xiàn)多次按下去的情況那?還是驅(qū)動(dòng)的問(wèn)題!感覺(jué)應(yīng)該是中斷的抖動(dòng)造成的!關(guān)于如何去抖,要看看! 上面的應(yīng)用程序用到了select,其實(shí)直接阻塞讀,就是直接read也可以得到按鍵值!我覺(jué)得select用在多輸入多輸出更有意義,也就是說(shuō)用在多線程編程(例如聊天服務(wù)器里面的多點(diǎn)對(duì)多點(diǎn),)。我不清楚用在這里是干什么?。菜聘M(jìn)程的效率有關(guān)系)、直接阻塞讀,就是進(jìn)程來(lái)讀,當(dāng)沒(méi)有讀到數(shù)據(jù)就一直阻塞,一直到讀取到數(shù)據(jù)才返回! 按照網(wǎng)上的資料說(shuō)的是:Select在Socket編程中還是比較重要的,可是對(duì)于初學(xué)Socket的人來(lái)說(shuō)都不太愛(ài)用Select寫(xiě)程序,他們只是習(xí)慣寫(xiě)諸如connect、accept、recv或recvfrom這樣的阻塞程序(所謂阻塞方式block,顧名思義,就是進(jìn)程或是線程執(zhí)行到這些函數(shù)時(shí)必須等待某個(gè)事件的發(fā)生,如果事件沒(méi)有發(fā)生,進(jìn)程或線程就被阻塞,函數(shù)不能立即返回)??墒鞘褂肧elect就可以完成非阻塞(所謂非阻塞方式non-block,就是進(jìn)程或線程執(zhí)行此函數(shù)時(shí)不必非要等待事件的發(fā)生,一旦執(zhí)行肯定返回,以返回值的不同來(lái)反映函數(shù)的執(zhí)行情況,如果事件發(fā)生則與阻塞方式相同,若事件沒(méi)有發(fā)生則返回一個(gè)代碼來(lái)告知事件未發(fā)生,而進(jìn)程或線程繼續(xù)執(zhí)行,所以效率較高)方式工作的程序,它能夠監(jiān)視我們需要監(jiān)視的文件描述符的變化情況——讀寫(xiě)或是異常。 我覺(jué)得吧:像本例中在前面用到for循環(huán),這樣就會(huì)照成select的效率低下!他也是一直循環(huán)的去檢查有沒(méi)有讀到數(shù)據(jù)。這豈不是跟阻塞讀差不多啦! 阻塞read應(yīng)用程序如下: #include #include #include #include #include #include #include #include #include int main(void) { int i; int buttons_fd; int key_value[4]; buttons_fd=open("/dev/qjz_buttons",0); if(buttons_fd < 0) { perror("open buttons fail"); exit(1); } for(;;) { read(buttons_fd,key_value,sizeof(key_value)); for(i=0;i<4;i++) { if(key_value[i] !=0) printf("K%d pressd,key value=0x%dn",i+1,key_value[i]);} continue; } close(buttons_fd); return 0; } |
評(píng)論