基于tiny210v2的linux-3.9.6内核驱动移植2:按键驱动
来源:互联网 发布:透明显示屏软件下载 编辑:程序博客网 时间:2024/05/21 07:57
一、主要代码
/* * linux/drivers/char/smdkv210_buttons.c * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/sched.h>#include <linux/poll.h>#include <linux/irq.h>#include <asm/irq.h>#include <asm/io.h>#include <linux/interrupt.h>#include <asm/uaccess.h>#include <mach/hardware.h>#include <linux/platform_device.h>#include <linux/cdev.h>#include <linux/miscdevice.h>#include <mach/map.h>#include <mach/gpio.h>#include <mach/regs-clock.h>#include <mach/regs-gpio.h>#define DEVICE_NAME"buttons"struct button_desc {int gpio;int number;char *name;struct timer_list timer;};static struct button_desc buttons[] = {{ S5PV210_GPH2(0), 0, "KEY0" },{ S5PV210_GPH2(1), 1, "KEY1" },{ S5PV210_GPH2(2), 2, "KEY2" },{ S5PV210_GPH2(3), 3, "KEY3" },{ S5PV210_GPH3(0), 4, "KEY4" },{ S5PV210_GPH3(1), 5, "KEY5" },{ S5PV210_GPH3(2), 6, "KEY6" },{ S5PV210_GPH3(3), 7, "KEY7" },};static volatile char key_values[] = {'0', '0', '0', '0', '0', '0', '0', '0'};static DECLARE_WAIT_QUEUE_HEAD(button_waitq);static volatile int ev_press = 0;static void smdkv210_buttons_timer(unsigned long _data){struct button_desc *bdata = (struct button_desc *)_data;int down;int number;unsigned tmp;tmp = gpio_get_value(bdata->gpio);/* active low */down = !tmp;printk("KEY %d: %08x\n", bdata->number, down);number = bdata->number;if (down != (key_values[number] & 1)) {key_values[number] = '0' + down;ev_press = 1;wake_up_interruptible(&button_waitq);}}static irqreturn_t button_interrupt(int irq, void *dev_id){struct button_desc *bdata = (struct button_desc *)dev_id;mod_timer(&bdata->timer, jiffies + msecs_to_jiffies(40));return IRQ_HANDLED;}static int smdkv210_buttons_open(struct inode *inode, struct file *file){int irq;int i;int err = 0;for (i = 0; i < ARRAY_SIZE(buttons); i++) {if (!buttons[i].gpio)continue;setup_timer(&buttons[i].timer, smdkv210_buttons_timer,(unsigned long)&buttons[i]);irq = gpio_to_irq(buttons[i].gpio);err = request_irq(irq, button_interrupt, IRQ_TYPE_EDGE_BOTH, buttons[i].name, (void *)&buttons[i]);if (err)break;}if (err) {i--;for (; i >= 0; i--) {if (!buttons[i].gpio)continue;irq = gpio_to_irq(buttons[i].gpio);disable_irq(irq);free_irq(irq, (void *)&buttons[i]);del_timer_sync(&buttons[i].timer);}return -EBUSY;}ev_press = 1;return 0;}static int smdkv210_buttons_close(struct inode *inode, struct file *file){int irq, i;for (i = 0; i < ARRAY_SIZE(buttons); i++) {if (!buttons[i].gpio)continue;irq = gpio_to_irq(buttons[i].gpio);free_irq(irq, (void *)&buttons[i]);del_timer_sync(&buttons[i].timer);}return 0;}static int smdkv210_buttons_read(struct file *filp, char __user *buff,size_t count, loff_t *offp){unsigned long err;if (!ev_press) {if (filp->f_flags & O_NONBLOCK)return -EAGAIN;elsewait_event_interruptible(button_waitq, ev_press);}ev_press = 0;err = copy_to_user((void *)buff, (const void *)(&key_values),min(sizeof(key_values), count));return err ? -EFAULT : min(sizeof(key_values), count);}static unsigned int smdkv210_buttons_poll( struct file *file,struct poll_table_struct *wait){unsigned int mask = 0;poll_wait(file, &button_waitq, wait);if (ev_press)mask |= POLLIN | POLLRDNORM;return mask;}static struct file_operations dev_fops = {.owner= THIS_MODULE,.open= smdkv210_buttons_open,.release= smdkv210_buttons_close, .read= smdkv210_buttons_read,.poll= smdkv210_buttons_poll,};static struct miscdevice misc = {.minor= MISC_DYNAMIC_MINOR,.name= DEVICE_NAME,.fops= &dev_fops,};static int __init button_dev_init(void){int ret;ret = misc_register(&misc);printk(DEVICE_NAME"\tinitialized\n");return ret;}static void __exit button_dev_exit(void){misc_deregister(&misc);}module_init(button_dev_init);module_exit(button_dev_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("FriendlyARM Inc.");
修改该目录下的Kconfig
添加:
Makefile添加:
测试程序:
#include<stdio.h> #include<stdlib.h> #include<unistd.h> #include<sys/ioctl.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h> #include<sys/select.h> #include<sys/time.h> #include<errno.h> int main(void) { int buttons_fd; char buttons[6]= {'0','0', '0', '0','0', '0'}; buttons_fd= open("/dev/buttons",0); if(buttons_fd< 0) { perror("opendevice buttons"); exit(1); } for (;;){ charcurrent_buttons[6]; intcount_of_changed_key; inti; if(read(buttons_fd,current_buttons,sizeofcurrent_buttons)!=sizeof current_buttons){ perror("readbuttons:"); exit(1); } for(i= 0,count_of_changed_key = 0; i< sizeofbuttons / sizeofbuttons[0];i++) { if (buttons[i]!= current_buttons[i]){ buttons[i]=current_buttons[i]; printf("%skey%d is %s",count_of_changed_key?", ": "", i+1,buttons[i] =='0'? "up" : "down"); count_of_changed_key++; } } if(count_of_changed_key){ printf("\n"); } } close(buttons_fd); return0; }
- 基于tiny210v2的linux-3.9.6内核驱动移植2:按键驱动
- 基于tiny210v2的linux-3.9.6内核驱动移植1:led驱动
- 基于tiny210v2的linux-3.9.6内核驱动移植3:LCD移植
- 基于tiny210v2的linux-3.9.6内核驱动移植4:PWM移植
- Linux-2.6.32.2内核在mini2440上的移植(十六)---基于中断的按键驱动移植
- Linux-2.6.32.2内核在mini2440上的移植(十六)---基于中断的按键驱动移植
- Linux-2.6.32.2内核在mini2440上的移植(十六)---基于中断的按键驱动移植
- 基于fl2440内核linux-3.0移植----添加按键驱动
- Linux-4.9.2内核在mini2440上的移植(十三)——按键驱动移植
- S3c6410 linux内核移植(6)---添加按键驱动
- 【引用】Linux-2.6.32.2内核在mini2440上的移植(十六)---基于中断的按键驱动移植
- 移植linux-3.9内核到tiny210v2
- 基于S3C2440的Linux-3.6.6移植——按键驱动
- S3C2410 LCD驱动的移植(基于linux-2.6.15.4内核)
- 基于Linux-2.6.35内核的wifi驱动移植
- linux-3.6.6按键驱动移植
- linux内核input驱动按键
- 【Linux内核驱动】按键中断
- 基于tiny210v2编译openwrt
- uva10891 - Game of Sum(动归)
- 移植linux-3.9内核到tiny210v2
- 基于tiny210v2的linux-3.9.6内核驱动移植1:led驱动
- uboot从SD卡烧写内核和文件系统
- 基于tiny210v2的linux-3.9.6内核驱动移植2:按键驱动
- JavaScript 概述
- 基于tiny210v2的linux-3.9.6内核驱动移植3:LCD移植
- 开源个安卓程序:蓝牙遥控智能车程序-单片机控制安卓上位机
- 移植opencv-2.4.6到tiny210v2
- Teradata 之top n与sample n
- JavaScript内置对象Date
- J2EE学习中一些值得研究的开源项目
- 分享下载CentOS的网址