OK6410的6个按键驱动程序(Linux),测试通过
来源:互联网 发布:散打软件 编辑:程序博客网 时间:2024/06/06 11:41
如果你的飞凌Linux-3.0.1内核源码没有编译过,编译时可能会出现:
scripts/basic/fixdep: 1: scripts/basic/fixdep: ELF: not found
scripts/basic/fixdep: 2: scripts/basic/fixdep: Syntax error: "(" unexpected
这时要先到Linux-3.0.1目录下 执行make scripts
注意:由于OK6410的GPIO按键中断已经被飞凌自带的按键驱动注册,所以运行我们编写的按键驱动前要先去掉飞凌自带的按键驱动,方法:make menuconfig->Device Drivers->input device support->Key Boards->GPIO Buttons 去掉前面的*,即不选该项即可。
按键驱动:
#include <linux/module.h>#include <linux/kernel.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/poll.h>#include <linux/irq.h>#include <asm/irq.h>#include <linux/interrupt.h>#include <asm/uaccess.h>#include <mach/regs-gpio.h>#include <mach/hardware.h>#include <linux/platform_device.h>#include <linux/cdev.h>#include <linux/miscdevice.h>#include <linux/sched.h>#include <linux/gpio.h>#include <mach/gpio-bank-n.h>#define DEVICE_NAME "buttons"struct button_irq_desc { int irq; int pin; int pin_setting; int number; char *name;};static struct button_irq_desc button_irqs [] = { {IRQ_EINT(0), S3C64XX_GPN(0) , S3C64XX_GPN0_EINT0 , 0, "KEY0"}, {IRQ_EINT(1), S3C64XX_GPN(1) , S3C64XX_GPN1_EINT1 , 1, "KEY1"}, {IRQ_EINT(2), S3C64XX_GPN(2) , S3C64XX_GPN2_EINT2 , 2, "KEY2"}, {IRQ_EINT(3), S3C64XX_GPN(3) , S3C64XX_GPN3_EINT3 , 3, "KEY3"}, {IRQ_EINT(4), S3C64XX_GPN(4) , S3C64XX_GPN4_EINT4 , 4, "KEY4"}, {IRQ_EINT(5), S3C64XX_GPN(5), S3C64XX_GPN5_EINT5 , 5, "KEY5"},};static volatile int key_values [] = {0, 0, 0, 0, 0, 0};static DECLARE_WAIT_QUEUE_HEAD(button_waitq);static volatile int ev_press = 0;static irqreturn_t buttons_interrupt(int irq, void *dev_id){ struct button_irq_desc *button_irqs = (struct button_irq_desc *)dev_id; int down; // udelay(0); down = !gpio_get_value(button_irqs->pin);//按键发生了变化 if (down != (key_values[button_irqs->number] & 1)) { // Changedkey_values[button_irqs->number] = 0 + down; ev_press = 1; wake_up_interruptible(&button_waitq); } return IRQ_RETVAL(IRQ_HANDLED);}static int s3c24xx_buttons_open(struct inode *inode, struct file *file){ int i; int err = 0; for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {if (button_irqs[i].irq < 0) {continue;}/*注册中断,六个按钮全都由buttons_interrupt中断函数处理,上升下降沿都触发中断 这里用button_irqs[i]的地址作为dev_id,保证dev_id的唯一性,发生中断的时候, 会把这个指针传递给中断处理函数*/ err = request_irq(button_irqs[i].irq, buttons_interrupt, IRQ_TYPE_EDGE_BOTH, button_irqs[i].name, (void *)&button_irqs[i]); if (err) break; }//如果注册中断过程中出错,则注销注册成功的中断 if (err) { i--; for (; i >= 0; i--) { if (button_irqs[i].irq < 0) {continue; } disable_irq(button_irqs[i].irq); free_irq(button_irqs[i].irq, (void *)&button_irqs[i]); } return -EBUSY; } ev_press = 1; return 0;}static int s3c24xx_buttons_close(struct inode *inode, struct file *file){ int i; //注销中断 for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {if (button_irqs[i].irq < 0) { continue;}free_irq(button_irqs[i].irq, (void *)&button_irqs[i]); } return 0;}static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){ unsigned long err; if (!ev_press) {//没有新按键事件处理,如果用户要求不阻塞就直接返回,否则阻塞在button_waitq队列上。if (filp->f_flags & O_NONBLOCK) return -EAGAIN;else wait_event_interruptible(button_waitq, ev_press); } ev_press = 0; err = copy_to_user(buff, (const void *)key_values, min(sizeof(key_values), count)); return err ? -EFAULT : min(sizeof(key_values), count);}static unsigned int s3c24xx_buttons_poll( struct file *file, struct poll_table_struct *wait){ unsigned int mask = 0;//使用poll_wait将等待队列添加到poll_table中 poll_wait(file, &button_waitq, wait);//返回掩码:设备可读通常返回(POLLIN|POLLRDNORM)。设备可写通常返回(POLLOUT|POLLWRNORM) if (ev_press) mask |= POLLIN | POLLRDNORM; return mask;}static 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 miscdevice misc = {.minor = MISC_DYNAMIC_MINOR,.name = DEVICE_NAME,.fops = &dev_fops,};static int __init dev_init(void){int ret;ret = misc_register(&misc);printk (DEVICE_NAME"\tinitialized\n");return ret;}static void __exit dev_exit(void){misc_deregister(&misc);}module_init(dev_init);module_exit(dev_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("wuwuchuan");
Makefile
ifneq ($(KERNELRELEASE),) obj-m := buttons.o else KDIR := /home/wu55uw/forlinx/linux-3.0.1 all: make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux- clean: rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul* endif
测试程序:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ioctl.h> int main(int argc, char **argv) { int fd; int val; int i; int key_value[6]; fd = open("/dev/buttons",0); if(fd<0) { printf("open devie error\n"); return -1; } while(1) { val = read(fd,key_value, sizeof(key_value)); if(val<0) { printf("read error\n"); continue; } for(i=0;i<6;i++) { if(key_value[i]) printf("KEY%d pressed\n",(i+1),key_value[i]); else printf("KEY%d released\n",(i+1),key_value[i]); } printf("----------------new key event----------------\n"); } close(fd); return 0; }
测试结果:
0 0
- OK6410的6个按键驱动程序(Linux),测试通过
- OK6410的单个按键驱动程序(Linux),测试通过
- OK6410按键驱动程序(Linux),测试通过后分享给大家
- OK6410按键驱动程序(Linux),测试通过后分享给大家
- ok6410按键中断驱动程序
- ok6410增加工作队列的platform按键驱动程序
- Linux下的led驱动程序,ok6410
- ok6410按键驱动与测试
- OK6410的按键驱动
- OK6410 Linux下LED驱动程序
- linux按键驱动程序心得
- ok6410 按键测试c语言版程序
- OK6410按键驱动程序(改)亲测可以运行
- OK6410按键驱动程序(改)亲测可以运行
- mini2440的按键驱动程序和测试程序详解
- 嵌入式linux之按键驱动程序
- LINUX下ADC按键驱动程序
- linux设备驱动程序的编写--将usb鼠标当做按键
- 冒险岛
- Gtest在vs 2010上的配置
- setResult和onActivityResult的用法小记
- 广播 (Broadcast)
- VVDocumenter安装不成功的解决办法
- OK6410的6个按键驱动程序(Linux),测试通过
- three.js学习笔记 为模型增添物理效果
- MySQL:最简单,也是最容易犯错的sql问题
- 热学
- android studio 单元测试
- JavaWeb会话Session知识大纲
- 关于emoji表情数据库处理方案 iOS 新手笔记
- iOS 使用纯代码或xib创建圆角视图
- Android 实现应用下载并在通知栏展示进度通知 打开APK文件