S5PV210(TQ210)學(xué)習(xí)筆記——輸入子系統(tǒng)驅(qū)動(dòng)
輸入子系統(tǒng)的原理分析強(qiáng)烈推薦觀看韋東山老師的視頻講座,講的非常清楚,我這里是按照輸入子系統(tǒng)的方式實(shí)現(xiàn)的按鍵驅(qū)動(dòng),下面是源碼,放在這里做個(gè)備份:
本文引用地址:http://m.butianyuan.cn/article/201611/322808.htm- #include
- #include
- #include
- #include
- #include
interrupt.h> - #include
- staticstructinput_dev*buttons_dev;
- staticstructtimer_listtimer;
- structbutton_desc*button_desc=NULL;
- structbutton_desc{
- char*name;
- unsignedintpin;
- unsignedintirq;
- unsignedintval;
- };
- staticstructbutton_descbuttons_desc[8]={
- [0]={
- .name="S1",
- .pin=S5PV210_GPH0(0),
- .irq=IRQ_EINT(0),
- .val=KEY_L,
- },
- [1]={
- .name="S2",
- .pin=S5PV210_GPH0(1),
- .irq=IRQ_EINT(1),
- .val=KEY_S,
- },
- [2]={
- .name="S3",
- .pin=S5PV210_GPH0(2),
- .irq=IRQ_EINT(2),
- .val=KEY_C,
- },
- [3]={
- .name="S4",
- .pin=S5PV210_GPH0(3),
- .irq=IRQ_EINT(3),
- .val=KEY_ENTER,
- },
- [4]={
- .name="S5",
- .pin=S5PV210_GPH0(4),
- .irq=IRQ_EINT(4),
- .val=KEY_LEFTCTRL,
- },
- [5]={
- .name="S6",
- .pin=S5PV210_GPH0(5),
- .irq=IRQ_EINT(5),
- .val=KEY_MINUS,
- },
- [6]={
- .name="S7",
- .pin=S5PV210_GPH2(6),
- .irq=IRQ_EINT(22),
- .val=KEY_CAPSLOCK,
- },
- [7]={
- .name="S8",
- .pin=S5PV210_GPH2(7),
- .irq=IRQ_EINT(23),
- .val=KEY_SPACE,
- },
- };
- staticvoidtimer_function(unsignedlongdata){
- if(button_desc==NULL)
- return;
- if(gpio_get_value(button_desc->pin)){
- input_event(buttons_dev,EV_KEY,button_desc->val,0);
- }
- else{
- input_event(buttons_dev,EV_KEY,button_desc->val,1);
- }
- input_sync(buttons_dev);
- }
- staticirqreturn_tirq_handler(intirq,void*devid){
- button_desc=(structbutton_desc*)devid;
- mod_timer(&timer,jiffies+HZ/100);
- returnIRQ_RETVAL(IRQ_HANDLED);
- }
- staticintbuttons_init(void){
- inti;
- buttons_dev=input_allocate_device();
- if(buttons_dev==NULL){
- printk(KERN_ERR"Error:allocateinputdevicefailed!");
- return-ENOMEM;
- }
- __set_bit(EV_KEY,buttons_dev->evbit);
- __set_bit(EV_REP,buttons_dev->evbit);
- __set_bit(KEY_L,buttons_dev->keybit);
- __set_bit(KEY_S,buttons_dev->keybit);
- __set_bit(KEY_C,buttons_dev->keybit);
- __set_bit(KEY_SPACE,buttons_dev->keybit);
- __set_bit(KEY_MINUS,buttons_dev->keybit);
- __set_bit(KEY_ENTER,buttons_dev->keybit);
- __set_bit(KEY_LEFTCTRL,buttons_dev->keybit);
- __set_bit(KEY_CAPSLOCK,buttons_dev->keybit);
- printk("1");
- if(input_register_device(buttons_dev)){
- gotoerror_1;
- }
- printk("2");
- init_timer(&timer);
- timer.function=timer_function;
- add_timer(&timer);
- printk("3");
- for(i=0;i!=8;++i){
- if(request_irq(buttons_desc[i].irq,irq_handler,
- IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,buttons_desc[i].name,&buttons_desc[i])){
- gotoerror_2;
- }
- }
- printk("4");
- return0;
- error_2:
- for(--i;i>=0;--i){
- free_irq(buttons_desc[i].irq,&buttons_desc[i]);
- }
- input_unregister_device(buttons_dev);
- error_1:
- input_free_device(buttons_dev);
- return-EBUSY;
- }
- staticvoidbuttons_exit(void){
- inti;
- for(i=0;i!=8;++i){
- free_irq(buttons_desc[i].irq,&buttons_desc[i]);
- }
- input_unregister_device(buttons_dev);
- input_free_device(buttons_dev);
- }
- module_init(buttons_init);
- module_exit(buttons_exit);
- MODULE_LICENSE("GPL");
很顯然,基于輸入子系統(tǒng)的按鍵驅(qū)動(dòng)比起直接編寫的字符驅(qū)動(dòng)設(shè)備要簡單的多,不過按鍵消抖還是要加的,上面的代碼加上了按鍵消抖。如果在開發(fā)過程中遇到了什么問題,還是歡迎留言討論。
評論