• 嵌入式快速入门学习笔记-input输入子系统


    input输入子系统介绍

    Linux通过输入子系统统一管理键盘、鼠标、触摸屏等输入设备,同时也是为了将自己编写的驱动融入到QT中。

    input输入子系统架构

    在这里插入图片描述
    最上层:输入事件驱动层((Event) Handlers)举例:evdev.c和mousedev.c和joydev.c
    中间层:输入核心层(Input Core)举例:input.c
    最下层:设备驱动层(Drivers)举例:drivers/input/xxx.c文件

    input输入子系统开发方法

    1)输入事件驱动层和输入核心层不需要动,只需更改设备驱动层
    2)核心工作是对具体输入设备硬件的操作和性能调整

    通过按键中断输入键盘字符

    在进行代码编写前需注意:
    1)将kernel/arch/arm/mach-s5pv210/button-x210.c中的代码注释掉(可全部注释),防止对自己写的代码产生误解。

    button-x210.c是BSP包内核自带的按键代码,他是通过时间轮询扫描的方式进行按键识别。

    1)在s3c_button_probe()函数中初始化定时器
    init_timer(&timer);
    timer.function = s3cbutton_timer_handler;
    
    timer.expires = jiffies + (HZ / 100);
    add_timer(&timer);
    2)在s3cbutton_timer_handler()函数中restart定时器
    mod_timer(&timer, jiffies + (HZ / 100));
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    本次实验中是使用中断的方式判断按键并上传输入子系统,如果不注释掉button-x210.c文件,虽然申请的按键中断可以覆盖button-x210.c文件对该按键的定义,但没有申请的按键还是会通过时间轮询扫描识别。

    2)

     UP     -> KP_COL0 -> GPH2_0
     RIGHT  -> KP_COL1 -> GPH2_1
     MENU   -> KP_COL3 -> GPH2_3 (KEY_A)
     BACK   -> KP_COL2 -> GPH2_2 (KEY_B)
     上面几个按键属于keypad列扫描,不是EINT类型
    
    • 1
    • 2
    • 3
    • 4
    • 5

    添加定时器消抖进行按键识别驱动代码

    #include  
    #include  
    #include 
    #include  
    #include 
    
    #include 			// arch/arm/mach-s5pv210/include/mach/irqs.h
    #include 
    #include 
    
    /*
     * X210:
     *
     * POWER  -> EINT1   -> GPH0_1
     * LEFT   -> EINT2   -> GPH0_2
     * DOWN   -> EINT3   -> GPH0_3
     * UP     -> KP_COL0 -> GPH2_0
     * RIGHT  -> KP_COL1 -> GPH2_1
     * MENU   -> KP_COL3 -> GPH2_3 (KEY_A)
     * BACK   -> KP_COL2 -> GPH2_2 (KEY_B)
     */
    
    #define BUTTON_IRQ	IRQ_EINT2
    
    static struct input_dev *button_dev;//定义全局input_dev变量
    static struct timer_list button_timer;//定义全局时钟变量
    
    //在按键中断函数中启动定时器进行消抖
    static irqreturn_t button_interrupt(int irq, void *dev_id)
    {
    	//10ms后启动定时器处理函数
    	mod_timer(&button_timer, jiffies + HZ / 100);
    	return IRQ_RETVAL(IRQ_HANDLED);
    }
     
    static void button_timer_function(unsigned long data) 
    { 
    	unsigned int pinval;
    
    	s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0));		// input模式
    	pinval = gpio_get_value(S5PV210_GPH0(2));					//获取GPIO值
    	s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));		// eint2模式
    
    	input_report_key(button_dev, KEY_Z, !pinval);//上报定义的按键值
    	input_sync(button_dev);
    }
    
    static int __init button_init(void) 
    { 
    	int error;
    	
    	//1.分配一个input_dev结构体
    	button_dev = input_allocate_device();
    	if (!button_dev) 
    	{ 
    		printk(KERN_ERR "key-s5pv210.c: Not enough memory\n");
    		error = -ENOMEM;
    		goto err_free_irq; 
    	}
    		
    	//2.设置
    	//产生哪类事件 EV_REP:可长按
    	button_dev->evbit[0] = BIT_MASK(EV_KEY);
    	//产生哪些操作
    	button_dev->keybit[BIT_WORD(KEY_Z)] = BIT_MASK(KEY_Z);
    
    	//3.注册
    	error = input_register_device(button_dev);
    	if (error) 
    	{ 
    		printk(KERN_ERR "key-s5pv210.c: Failed to register device\n");
    		goto err_free_dev; 
    	}
    	
    	//4.硬件相关操作
    	//定时器
    	init_timer(&button_timer);
    	button_timer.function = button_timer_function;
    	add_timer(&button_timer);
    	//GPIO申请
    	error = gpio_request(S5PV210_GPH0(2), "GPH0_2");
    	if(error)
    		printk("key-s5pv210: request gpio GPH0(2) fail");
    	s3c_gpio_cfgpin(S5PV210_GPH0(2), S3C_GPIO_SFN(0x0f));		// eint2模式
    	//中断申请
    	if (request_irq(BUTTON_IRQ, button_interrupt, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, "button-x210", NULL)) 
    	{ 
    		printk(KERN_ERR "key-s5pv210.c: Can't allocate irq %d\n", BUTTON_IRQ);
    		return -EBUSY; 
    	}
    	
    	return 0;
    	
    err_free_dev:
    	input_free_device(button_dev);
    err_free_irq:
    	free_irq(BUTTON_IRQ, button_interrupt);
    	return error; 
    }
    
    static void __exit button_exit(void) 
    {
    	del_timer(&button_timer);
    	input_unregister_device(button_dev); 
    	free_irq(BUTTON_IRQ, button_interrupt); 
    }
    
    module_init(button_init); 
    module_exit(button_exit); 
    
    MODULE_LICENSE("GPL");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111

    验证

    1)

    ls /dev/event* -l
    
    • 1

    通过该指令查看insmod前后是否有新的event生成
    2)

    exec 0</dev/tty1
    
    • 1

    通过该指令查看按键上报的键值是否正确

  • 相关阅读:
    深度学习基础之GFLOPS(2)
    01字典树+dp
    用尾指针标识的单循环链表实现队列r
    人工智能-4计算机视觉和图像处理01
    一波三折的react + less
    Milvus 2.1 版本更新 - 简单可信赖、性能持续提升
    力扣198. 打家劫舍
    plc简单问题求看看对不对
    【Flutter小记3】Android打开前置或广角摄像头的同时打开闪光灯方案
    docker管理之consul注册中心
  • 原文地址:https://blog.csdn.net/wuli_Thames/article/details/126428306