《(16.3)--12.8KeyboardDriver.ppt》由会员分享,可在线阅读,更多相关《(16.3)--12.8KeyboardDriver.ppt(27页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、Keyboard Keyboard D DriverriverAn instance of a keyboard driver running on the S3C2440/Linux 2.6 kernel.Keyboard driverThe hardware circuit is shown in the figure:The column line of the keyboard is the output line,which connect the pins GPG7,GPF5,GPF4 and GPF3 of the processor from left to right.The
2、 row line of the keyboard is the input line.The left side is connected to the power supply through the pull-up resistor,and the right side is connected to the pin GPF2,GPF1,GPG5 and GPG6 of the embedded processor from top to bottom.The four pins work in interrupt mode and the falling edge is trigger
3、ed.There are many methods for key recognition of matrix keyboard.In this case,the method of scan by scan is adopted.The process is as follows.First,set the pin that connects the column line to output low level,and set the pin that connects the row line to work in interrupt mode.When the button is no
4、t pressed,the interrupt pin is connected to the power supply through a resistor and remains at a high level,so that the interrupt will not be triggered.Once a key is pressed,the level of corresponding line will be pulled down to a low level,and the corresponding interrupt will be triggered to comple
5、te line recognition.Then,the program executes the interrupt service program of the corresponding row,and sets each column line to high level one by one.Meanwhile,it continues to detect whether the key row is still low level.If it is low,it means that the key pressed is not in this column;if it is hi
6、gh,it means that the key pressed is subordinate to this column,so as to complete the column recognition.#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include#include button.c#define DEVICE_NAME dial_keystruct button_i
7、rq_desc int irq;int pin;int pin_setting;int number;char*name;struct col_descint pin;int pin_setting;static struct col_desc col_table =S3C2410_GPG(7),S3C2410_GPIO_OUTPUT,S3C2410_GPF(5),S3C2410_GPIO_OUTPUT,S3C2410_GPF(4),S3C2410_GPIO_OUTPUT,S3C2410_GPF(3),S3C2410_GPIO_OUTPUT,;button.cstatic struct but
8、ton_irq_desc button_irqs =IRQ_EINT2,S3C2410_GPF(2),S3C2410_GPF2_EINT2,0,KEY1,IRQ_EINT1,S3C2410_GPF(1),S3C2410_GPF1_EINT1,1,KEY2,IRQ_EINT13,S3C2410_GPG(5),S3C2410_GPG5_EINT13,2,KEY3,IRQ_EINT14,S3C2410_GPG(6),S3C2410_GPG6_EINT14,3,KEY4,;static volatile char key_values =0,0,0,0;static volatile char use
9、r_values =0,0;static DECLARE_WAIT_QUEUE_HEAD(button_waitq);static volatile int ev_press=0;static volatile int row=-1;static volatile int col=-1;static irqreturn_t buttons_interrupt(int irq,void*dev_id)struct button_irq_desc*button_irqs=(struct button_irq_desc*)dev_id;int down;int i,t=10;down=!s3c241
10、0_gpio_getpin(button_irqs-pin);button.c if(down=1)udelay(100);down=!s3c2410_gpio_getpin(button_irqs-pin);if(down=1)&(ev_press=0)key_valuesbutton_irqs-number=0+down;ev_press=1;for(i=0;i pin);switch(t)case 2:case 4:case 32:case 64:button.crow=button_irqs-number;col=i;break;default:row=-1;col=-1;break;
11、s3c2410_gpio_setpin(col_tablei.pin,0);if(row=0)&(col=0)break;wake_up_interruptible(&button_waitq);return IRQ_RETVAL(IRQ_HANDLED);button.cstatic int s3c24xx_buttons_open(struct inode*inode,struct file*file)int i,j;int err=0;for(i=0;i sizeof(button_irqs)/sizeof(button_irqs0);i+)if(button_irqsi.irq 0)c
12、ontinue;err=request_irq(button_irqsi.irq,buttons_interrupt,IRQ_TYPE_LEVEL_LOW,button_irqsi.name,(void*)&button_irqsi);if(err)break;button.c for(j=0;j=0;i-)if(button_irqsi.irq 0)continue;disable_irq(button_irqsi.irq);free_irq(button_irqsi.irq,(void*)&button_irqsi);return-EBUSY;button.c ev_press=1;for
13、(i=0;i 4;i+)s3c2410_gpio_setpin(col_tablei.pin,0);return 0;static int s3c24xx_buttons_close(struct inode*inode,struct file*file)int i;for(i=0;i sizeof(button_irqs)/sizeof(button_irqs0);i+)if(button_irqsi.irq f_flags&O_NONBLOCK)return-EAGAIN;else wait_event_interruptible_timeout(button_waitq,ev_press
14、,500);user_values0=row+48;user_values1=col+48;err=copy_to_user(buff,(const void*)user_values,2);user_values0=0;user_values1=0;button.ccol=-1;row=-1;ev_press=0;return err?-EFAULT:min(sizeof(key_values),count);static unsigned int s3c24xx_buttons_poll(struct file*file,struct poll_table_struct*wait)unsi
15、gned int mask=0;poll_wait(file,&button_waitq,wait);if(ev_press)mask|=POLLIN|POLLRDNORM;return mask;button.cstatic struct file_operations dev_fops=.owner =THIS_MODULE,.open =s3c24xx_buttons_open,.release=s3c24xx_buttons_close,.read =s3c24xx_buttons_read,.poll =s3c24xx_buttons_poll,;static struct misc
16、device misc=.minor=MISC_DYNAMIC_MINOR,.name=DEVICE_NAME,.fops=&dev_fops,;button.cstatic int _init dev_init(void)int ret;ret=misc_register(&misc);printk(DEVICE_NAMEtinitializedn);return ret;static void _exit dev_exit(void)misc_deregister(&misc);printk(DEVICE_NAMEexitn);module_init(dev_init);module_ex
17、it(dev_exit);MODULE_LICENSE(GPL);MODULE_AUTHOR(FriendlyARM Inc.);button.cCross-compile the driver by using the following MakefileMakefile of keyboard driver#Description:#Makefile for 2.6 device drivers#Comment/uncomment the following line to disable/enable debugging#DEBUG=y#Add your debugging flag(o
18、r not)to CFLAGSifeq($(DEBUG),y)DEBFLAGS=-O-g-DCAN_DEBUG#-O is needed to expand inlineselseDEBFLAGS=-O2endifmakefileifneq($(KERNELRELEASE),)#call from kernel build systemobj-m:=dial_keyboard_drv_task.o elseKERNELDIR?=/usr/src/linux-2.6.32.2PWD:=$(shell pwd)CC:=arm-linux-gccHOSTCC:=gccexport CC HOSTCC
19、default:$(MAKE)-C$(KERNELDIR)M=$(PWD)modulesendifclean:rm-rf*.o*.komakefileThe following program is used to test whether the keyboard device is working properly.Keyboard test program#include#include#include#include#include#include#include#include#include#include#include int main(void)int buttons_fd;
20、char current_state;char buttons2=0,0;buttons_fd=open(/dev/dial_key,0);/Open key devicebutton_test.cif(buttons_fd 0)perror(open device buttons failed);/Open key device failedexit(1);while(1)/Open successfully and execute the loop char realchar=x;char current_buttons2;if(read(buttons_fd,current_button
21、s,sizeof current_buttons)!=sizeof current_buttons)perror(read buttons:);/fail to read the keyexit(1);buttons0=current_buttons0;buttons1=current_buttons1;button_test.cswitch(buttons0)case 51:switch(buttons1)case 51:realchar=1;/key 1 break;case 50:realchar=2;/key 2break;case 49:realchar=3;/key 3break;
22、case 48:realchar=A;/key Abreak;break;button_test.ccase 50:switch(buttons1)case 51:realchar=4;/key 4break;case 50:realchar=5;/key 5break;case 49:realchar=6;/key 6break;case 48:realchar=B;/key Bbreak;break;button_test.ccase 49:switch(buttons1)case 51:realchar=7;/key 7break;case 50:realchar=8;/key 8bre
23、ak;case 49:realchar=9;/key 9break;case 48:realchar=C;/key Cbreak;break;button_test.ccase 48:switch(buttons1)case 51:realchar=*;/key*break;case 50:realchar=0;/key 0break;case 49:realchar=#;/key#break;case 48:realchar=D;/key Dbreak;break;button_test.cif(realchar!=x)/Output the value of pressed keyprintf(the put key is%cn,realchar);close(buttons_fd);return 0;button_test.cEmbedded Software DesignEmbedded Software Design