欢迎来到天天文库
浏览记录
ID:9338428
大小:227.50 KB
页数:11页
时间:2018-04-28
《实验 8-2 键盘中断驱动实验》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、实验8-2键盘中断驱动实验【实验目的】Ø掌握键盘原理。Ø熟悉驱动的中断机制。【实验步骤】第一步:利用vi编辑器,编写一个Keypad.c驱动代码;1、增加驱动所需的头文件和变量#include#include#include#include#include#include#include#include2、m/arch-pxa/pxa-regs.h>MODULE_LICENSE("GPL");#defineDEVICE_NAME"emdoor_kbd"#defineKEYVALUE_HAVE1#defineKEYVALUE_NO0#defineKPC_DIR1<<5#defineKPC_MAT1<<22staticintEmdoor_kbd_fasync(int,structfile*,int);typedefunsignedcharKBD_RET;structfasync_struct*fasync;//键盘结构体定义ty3、pedefstruct{KBD_RETkbd_buff;/*protectagainstoverrun*/unsignedintkbd_status;wait_queue_head_twq;spinlock_tlock;}KBD_DEV;实现键盘驱动读操作函数staticssize_tEmdoor_kbd_read(structfile*filp,char*buffer,size_tcount,loff_t*ppos){KBD_DEV*kbd=filp->private_data;KBD_RETkbd_ret;while(4、1){if(kbd->kbd_status==KEYVALUE_HAVE){kbd_ret=kbd->kbd_buff;copy_to_user(buffer,&kbd_ret,sizeof(KBD_RET));kbd->kbd_status=KEYVALUE_NO;returnsizeof(KBD_RET);}else{if(filp->f_flags&O_NONBLOCK)return-EAGAIN;interruptible_sleep_on(&(kbd->wq));if(signal_pending(current5、))return-ERESTARTSYS;}}returnsizeof(KBD_RET);}1、实现键盘驱动中断服务例程staticvoidEmdoor_isr_kbd(intirq,void*dev_id,structpt_regs*reg){printk(KERN_EMERG"Emdoor_isr_kbd,Interrupt");intkpc_value;KBD_DEV*kbd=(KBD_DEV*)dev_id;spin_lock_irq(&(kbd->lock));kpc_value=KPC;if(kpc_val6、ue&KPC_MAT){kbd->kbd_buff=KPAS&0xFF;if(kbd->kbd_buff!=0xFF){switch(kbd->kbd_buff){case0x0:kbd->kbd_buff=5;break;case0x1:kbd->kbd_buff=6;break;case0x2:kbd->kbd_buff=7;break;case0x5:kbd->kbd_buff=8;break;case0x10:kbd->kbd_buff=9;break;case0x11:kbd->kbd_buff=10;break7、;case0x12:kbd->kbd_buff=11;break;case0x15:kbd->kbd_buff=12;break;case0x20:kbd->kbd_buff=13;break;case0x21:kbd->kbd_buff=14;break;case0x22:kbd->kbd_buff=15;break;case0x25:kbd->kbd_buff=16;break;default:break;}kbd->kbd_status=KEYVALUE_HAVE;}}elseif(kpc_value&KPC_DIR8、){kbd->kbd_buff=KPDK&0xFF;if(kbd->kbd_buff!=0x0){switch(kbd->kbd_buff){case0x40:kbd->kbd_buff=1;break;case0x2:kbd->kbd_buff=2;break;case0x4:kbd->kbd_buf
2、m/arch-pxa/pxa-regs.h>MODULE_LICENSE("GPL");#defineDEVICE_NAME"emdoor_kbd"#defineKEYVALUE_HAVE1#defineKEYVALUE_NO0#defineKPC_DIR1<<5#defineKPC_MAT1<<22staticintEmdoor_kbd_fasync(int,structfile*,int);typedefunsignedcharKBD_RET;structfasync_struct*fasync;//键盘结构体定义ty
3、pedefstruct{KBD_RETkbd_buff;/*protectagainstoverrun*/unsignedintkbd_status;wait_queue_head_twq;spinlock_tlock;}KBD_DEV;实现键盘驱动读操作函数staticssize_tEmdoor_kbd_read(structfile*filp,char*buffer,size_tcount,loff_t*ppos){KBD_DEV*kbd=filp->private_data;KBD_RETkbd_ret;while(
4、1){if(kbd->kbd_status==KEYVALUE_HAVE){kbd_ret=kbd->kbd_buff;copy_to_user(buffer,&kbd_ret,sizeof(KBD_RET));kbd->kbd_status=KEYVALUE_NO;returnsizeof(KBD_RET);}else{if(filp->f_flags&O_NONBLOCK)return-EAGAIN;interruptible_sleep_on(&(kbd->wq));if(signal_pending(current
5、))return-ERESTARTSYS;}}returnsizeof(KBD_RET);}1、实现键盘驱动中断服务例程staticvoidEmdoor_isr_kbd(intirq,void*dev_id,structpt_regs*reg){printk(KERN_EMERG"Emdoor_isr_kbd,Interrupt");intkpc_value;KBD_DEV*kbd=(KBD_DEV*)dev_id;spin_lock_irq(&(kbd->lock));kpc_value=KPC;if(kpc_val
6、ue&KPC_MAT){kbd->kbd_buff=KPAS&0xFF;if(kbd->kbd_buff!=0xFF){switch(kbd->kbd_buff){case0x0:kbd->kbd_buff=5;break;case0x1:kbd->kbd_buff=6;break;case0x2:kbd->kbd_buff=7;break;case0x5:kbd->kbd_buff=8;break;case0x10:kbd->kbd_buff=9;break;case0x11:kbd->kbd_buff=10;break
7、;case0x12:kbd->kbd_buff=11;break;case0x15:kbd->kbd_buff=12;break;case0x20:kbd->kbd_buff=13;break;case0x21:kbd->kbd_buff=14;break;case0x22:kbd->kbd_buff=15;break;case0x25:kbd->kbd_buff=16;break;default:break;}kbd->kbd_status=KEYVALUE_HAVE;}}elseif(kpc_value&KPC_DIR
8、){kbd->kbd_buff=KPDK&0xFF;if(kbd->kbd_buff!=0x0){switch(kbd->kbd_buff){case0x40:kbd->kbd_buff=1;break;case0x2:kbd->kbd_buff=2;break;case0x4:kbd->kbd_buf
此文档下载收益归作者所有