首页 > 代码库 > linux输入子系统(input subsystem)之按键输入和LED控制
linux输入子系统(input subsystem)之按键输入和LED控制
实验现象:在控制台打印按键值,并且通过按键控制相应的LED亮灭。
1.代码
input_subsys_drv.c
1 #include <linux/module.h> 2 #include <linux/version.h> 3 4 #include <linux/init.h> 5 #include <linux/fs.h> 6 #include <linux/interrupt.h> 7 #include <linux/irq.h> 8 #include <linux/sched.h> 9 #include <linux/pm.h> 10 #include <linux/sysctl.h> 11 #include <linux/proc_fs.h> 12 #include <linux/delay.h> 13 #include <linux/platform_device.h> 14 #include <linux/input.h> 15 #include <linux/irq.h> 16 17 #include <asm/gpio.h> 18 #include <asm/io.h> 19 #include <asm/arch/regs-gpio.h> 20 21 22 struct pin_desc{ 23 int irq; 24 char *name; 25 unsigned int pin; 26 unsigned int key_val; 27 }; 28 29 struct pin_desc pins_desc[4] = { 30 {IRQ_EINT0, "S2", S3C2410_GPF0, KEY_L}, 31 {IRQ_EINT2, "S3", S3C2410_GPF2, KEY_S}, 32 {IRQ_EINT11, "S4", S3C2410_GPG3, KEY_ENTER}, 33 {IRQ_EINT19, "S5", S3C2410_GPG11, KEY_LEFTSHIFT}, 34 }; 35 36 static struct input_dev *input_subsys_dev; 37 static struct pin_desc *irq_pd; 38 static struct timer_list buttons_timer; 39 40 static irqreturn_t buttons_irq(int irq, void *dev_id) 41 { 42 /* [cgw]: 按键IO发生边沿中断时重新设置定时间隔 43 * 用于按键消抖 44 */ 45 irq_pd = (struct pin_desc *)dev_id; 46 buttons_timer.data = http://www.mamicode.com/irq_pd->pin; 47 mod_timer(&buttons_timer, jiffies+HZ/100); 48 return IRQ_RETVAL(IRQ_HANDLED); 49 } 50 51 static void buttons_timer_function(unsigned long data) 52 { 53 struct pin_desc * pindesc = irq_pd; 54 unsigned int pinval; 55 56 if (!pindesc) 57 return; 58 59 /* [cgw]: 获取按键IO状态 */ 60 pinval = s3c2410_gpio_getpin((unsigned int)data); 61 62 /* [cgw]: 根据按键IO状态上报按键事件 */ 63 if (pinval) 64 { 65 /* [cgw]: 上报按键弹起 */ 66 input_report_key(input_subsys_dev, pindesc->key_val, 0); 67 //input_sync(input_subsys_dev); 68 } 69 else 70 { 71 /* [cgw]: 上报按键按下 */ 72 input_report_key(input_subsys_dev, pindesc->key_val, 1); 73 //input_sync(input_subsys_dev); 74 } 75 76 //printk("timer occur!\n"); 77 } 78 79 80 static int led_event(struct input_dev *dev, unsigned int type, unsigned int code, int value) 81 { 82 printk("led event!\n"); 83 printk("value: 0x%x\n", value); 84 85 /* [cgw]: 根据应用程序下发的LED控制事件 86 * 亮灭LED 87 */ 88 //if (code == SND_BELL) { 89 if (code == LED_MUTE) { 90 if (value =http://www.mamicode.com/= 0xAA) { 91 /* [cgw]: 点亮 */ 92 s3c2410_gpio_setpin(S3C2410_GPF4, 0); 93 } else if (value =http://www.mamicode.com/= 0xEE) { 94 /* [cgw]: 熄灭 */ 95 s3c2410_gpio_setpin(S3C2410_GPF4, 1); 96 } 97 98 return 0; 99 }100 101 return -1;102 }103 104 int input_subsys_open(struct input_dev *dev)105 { 106 int i, retval;107 108 /* [cgw]: 设置按键IO为中断输入 */109 s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_EINT0);110 s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2);111 s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_EINT11);112 s3c2410_gpio_cfgpin(S3C2410_GPG11, S3C2410_GPG11_EINT19);113 114 /* [cgw]: 设置LED IO为输出,初始为熄灭LED */115 s3c2410_gpio_cfgpin(S3C2410_GPF4, S3C2410_GPF4_OUTP);116 s3c2410_gpio_setpin(S3C2410_GPF4, 1);117 118 s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);119 s3c2410_gpio_setpin(S3C2410_GPF5, 1);120 121 s3c2410_gpio_cfgpin(S3C2410_GPF5, S3C2410_GPF5_OUTP);122 s3c2410_gpio_setpin(S3C2410_GPF5, 1);123 124 /* [cgw]: 为按键IO分配中断线 */125 for (i = 0; i < 4; i++)126 {127 retval = request_irq(pins_desc[i].irq, buttons_irq, IRQT_BOTHEDGE, pins_desc[i].name, &pins_desc[i]);128 }129 130 printk("input subsys open!\n");131 132 return 0;133 }134 135 136 137 static int input_subsys_init(void)138 {139 /* [cgw]: 分配一个输入设备 */140 input_subsys_dev = input_allocate_device();141 input_subsys_dev->name = "input_subsys_dev";142 143 /* [cgw]: 设置支持的事件类型 */144 set_bit(EV_KEY, input_subsys_dev->evbit);145 //set_bit(EV_REP, input_subsys_dev->evbit);146 147 set_bit(EV_LED, input_subsys_dev->evbit);148 //set_bit(EV_SND, input_subsys_dev->evbit);149 150 /* [cgw]: 设置支持的事件码 */151 set_bit(KEY_L, input_subsys_dev->keybit);152 set_bit(KEY_S, input_subsys_dev->keybit);153 set_bit(KEY_ENTER, input_subsys_dev->keybit);154 set_bit(KEY_LEFTSHIFT, input_subsys_dev->keybit);155 156 set_bit(LED_MUTE, input_subsys_dev->ledbit);157 //set_bit(SND_BELL, input_subsys_dev->sndbit);158 159 /* [cgw]: 分配输入设备的open方法,用户操作open(/dev/xxx, ...)时调用 */160 input_subsys_dev->open = input_subsys_open;161 /* [cgw]: 分配输入设备的event方法,用户在应用程序write()时 */162 input_subsys_dev->event = led_event;163 164 /* [cgw]: 注册输入设备 */165 input_register_device(input_subsys_dev);166 167 /* [cgw]: 初始化定时器,用于按键消抖 */168 init_timer(&buttons_timer);169 buttons_timer.function = buttons_timer_function;170 add_timer(&buttons_timer);171 172 printk("input subsys init!\n");173 174 return 0;175 }176 177 static void input_subsys_exit(void)178 {179 int i;180 181 /* [cgw]: 释放按键IO中断 */182 for (i = 0; i < 4; i++)183 {184 free_irq(pins_desc[i].irq, &pins_desc[i]);185 }186 187 /* [cgw]: 删除定时器 */188 del_timer(&buttons_timer);189 /* [cgw]: 注销输入设备 */190 input_unregister_device(input_subsys_dev);191 /* [cgw]: 释放输入设备内存空间 */192 input_free_device(input_subsys_dev); 193 }194 195 module_init(input_subsys_init);196 197 module_exit(input_subsys_exit);198 199 MODULE_LICENSE("GPL");
input_subsys_test.c
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <stdio.h> 5 #include <poll.h> 6 #include <signal.h> 7 #include <sys/types.h> 8 #include <unistd.h> 9 #include <fcntl.h>10 11 #include <linux/input.h>12 13 14 15 int fd;16 17 void my_signal_fun(int signum)18 {19 struct input_event buttons_event, leds_event;20 21 /* [cgw]: 异步通知产生时返回的数据 */22 read(fd, &buttons_event, sizeof(struct input_event));23 24 /* [cgw]: 打印事件类型,事件码,事件值 */25 printf("type: 0x%x code: 0x%x value: 0x%x\n", 26 buttons_event.type,27 buttons_event.code, 28 buttons_event.value);29 30 /* [cgw]: 返回的是KEY_L或KEY_S值 */31 if (buttons_event.code == KEY_L || buttons_event.code == KEY_S) {32 /* [cgw]: 按键弹起 */ 33 if (buttons_event.value =http://www.mamicode.com/= 0) {34 35 /* [cgw]: 构造一个EV_LED事件 */36 37 //leds_event.type = EV_SND;38 leds_event.type = EV_LED;39 //leds_event.code = SND_BELL;40 leds_event.code = LED_MUTE;41 42 /* [cgw]: KEY_L和KEY_S控制LED的亮灭 */43 if (buttons_event.code == KEY_L) {44 leds_event.value = http://www.mamicode.com/0xAA;45 } else if (buttons_event.code == KEY_S) {46 leds_event.value = http://www.mamicode.com/0xEE; 47 }48 49 /* [cgw]: 发送LED控制事件 */50 write(fd, &leds_event, sizeof(struct input_event));51 52 printf("led write!\n");53 }54 }55 }56 57 int main(int argc, char **argv)58 {59 int Oflags;60 61 /* [cgw]: 设置需要处理的信号SIGIO,即输入文件会请求一个SIGIO62 * 信号,当有新数据到来这个信号会发给filp->f_owner进程63 */64 signal(SIGIO, my_signal_fun);65 66 fd = open("/dev/event1", O_RDWR | O_NONBLOCK);67 68 //printf("fd = 0x%x\n", fd);69 70 if (fd < 0)71 {72 printf("can‘t open!\n");73 }74 75 /* [cgw]: 根据文件标识符fd,设置能够获得这个文件的进程(owner) 76 * getpid()获得当前进程ID77 */78 fcntl(fd, F_SETOWN, getpid());79 80 /* [cgw]: 获得file->f_flags标志 */81 Oflags = fcntl(fd, F_GETFL); 82 83 /* [cgw]: 置位FASYNC使能异步通知 */84 fcntl(fd, F_SETFL, Oflags | FASYNC);85 86 while (1)87 {88 /* [cgw]: 休眠 */89 sleep(1000);90 }91 92 return 0;93 }
makefile
1 KERN_DIR = /work/system/linux-2.6.22.6 2 3 all: 4 make -C $(KERN_DIR) M=`pwd` modules 5 6 clean: 7 make -C $(KERN_DIR) M=`pwd` modules clean 8 rm -rf modules.order 9 10 obj-m += input_subsys_drv.o
2. 实验
2.1
安装驱动程序:
insmod input_subsys_drv.ko
1 # insmod input_subsys_drv.ko2 input: input_subsys_dev as /class/input/input13 input subsys open!4 input subsys init!
运行应用程序
./input_subsys_test
1 # ./input_subsys_test 2 type: 0x1 code: 0x26 value: 0x1 3 type: 0x1 code: 0x26 value: 0x0 4 led event! 5 value: 0xaa 6 led write! 7 type: 0x11 code: 0x7 value: 0xaa 8 type: 0x1 code: 0x1f value: 0x1 9 type: 0x1 code: 0x1f value: 0x010 led event!11 value: 0xee12 led write!13 type: 0x11 code: 0x7 value: 0xee
3. 现象分析
按一下按键KEY_L,终端输出:
type: 0x1 code: 0x26 value: 0x1 //按键按下type: 0x1 code: 0x26 value: 0x0 //按键弹起led event! //应用程序操作write, 发送LED控制事件,调用led_event()value: 0xaa //读到的LED亮的命令led write! //返回应用程序继执行type: 0x11 code: 0x7 value: 0xaa //因为应用程序发送了事件给驱动程序,驱动把这个事件作为一个输入事件(相当于按键输入事件),同样通过异步通知反馈到应用程序
The end!
linux输入子系统(input subsystem)之按键输入和LED控制
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。