poll机制按键驱动.doc_第1页
poll机制按键驱动.doc_第2页
poll机制按键驱动.doc_第3页
poll机制按键驱动.doc_第4页
poll机制按键驱动.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Poll机制按键驱动实验器材: ok6410开发板RedHat5.0 实验目的: 利用poll机制来测试按键驱动实验原理: 从上图可以得出: 当按键没有按下时,为高电平; 当按下时,为低电平 S2-7按键分别对应的是外部中断XEINT0-5驱动程序:#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define GPNCON 0x7F008830#define GPNDAT 0x7F008834volatile unsigned long *s3c6410_GPNCON;volatile unsigned long *s3c6410_GPNDAT;static struct class *mythird_key_class;static struct device *mythird_key_class_dev;static DECLARE_WAIT_QUEUE_HEAD(key_waitq); /定义一个等待队列头/* 中断事件标志,中断服务程序将它置,函数将它置 */static volatile int ev_press = 0; /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 */* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 ,0x85, 0x86 */static unsigned int keyvals;struct mythird_keyunsigned int irq_no;unsigned int key_val;unsigned int num;char *name;static struct mythird_key third_keys6 = IRQ_EINT(0), 0x01, 0, KEY1 ,IRQ_EINT(1), 0x02, 1, KEY2 ,IRQ_EINT(2), 0x03, 2, KEY3 ,IRQ_EINT(3), 0x04, 3, KEY4 ,IRQ_EINT(4), 0x05, 4, KEY5 ,IRQ_EINT(5), 0x06, 5, KEY6 ,;static irqreturn_t mykey_handle(int irq, void *dev_id)int num;unsigned int tmp;unsigned int pinval;struct mythird_key *pindesc = (struct mythird_key *)dev_id;num = pindesc-num;tmp = readl(S3C64XX_GPNDAT);pinval = (tmp & (1key_val | 0x80;else /按下keyvals = pindesc-key_val;ev_press = 1;wake_up_interruptible(&key_waitq);return IRQ_RETVAL(IRQ_HANDLED);static int mythird_key_open(struct inode *inode, struct file *file)/* 配置 GPN0-5 为中断引脚*/int i = 0;int err;for(i = 0; i sizeof(third_keys)/sizeof(third_keys0); i+)err = request_irq(third_keysi.irq_no, mykey_handle, IRQ_TYPE_EDGE_BOTH, third_, (void*)&third_keysi);return 0;static int mythird_key_close(struct inode *inode, struct file *file)int i= 0;for(i = 0; i sizeof(third_keys)/sizeof(third_keys0); i+ )free_irq(third_keysi.irq_no, (void*)&third_keysi);return 0;static int mythird_key_read(struct file *file, char _user *buf, size_t size, loff_t *offp)if(size != 1)return -EINVAL;/*如果没有按键按下, 就休眠*/wait_event_interruptible(key_waitq, ev_press);/*如果有按键按下, 就唤醒*/ev_press = 0;copy_to_user(buf, &keyvals, 1);return 1;static unsigned mythird_key_poll(struct file *file, poll_table *wait)unsigned int mask = 0;poll_wait(file, &key_waitq, wait); /只是将进程挂到key_waitq队列, 并不会休眠if(ev_press)mask |= POLLIN | POLLRDNORM;return mask;static struct file_operations mythird_key = .owner = THIS_MODULE, .open = mythird_key_open,.read = mythird_key_read,.release = mythird_key_close,.poll = mythird_key_poll,;static int major;static int _init mythird_key_init(void)major = register_chrdev(0, mythird_key, &mythird_key);mythird_key_class = class_create(THIS_MODULE, mythird_key);mythird_key_class_dev = device_create(mythird_key_class, NULL, MKDEV(major, 0), NULL, key);s3c6410_GPNCON = (volatile unsigned long *)ioremap(GPNCON, 16);s3c6410_GPNDAT = s3c6410_GPNCON + 1;return 0;static void _exit mythird_key_exit(void)unregister_chrdev(major,mythird_key);device_destroy(mythird_key_class, MKDEV(major, 0);class_destroy(mythird_key_class);iounmap(s3c6410_GPNCON);module_init(mythird_key_init);module_exit(mythird_key_exit);MODULE_LICENSE(GPL);测试程序:#include #include #include #include int main()int fd;int ret;unsigned int keyvals;struct pollfd fds1;/我们只需要监控一个fd = open(/dev/key, O_RDWR);if(fd 0)printf(open key error!n);return -1;fds0.fd = fd; /需要监控是那个fds0.events = POLLIN; / 等待数据去读while(1)ret = poll(fds, 1, 5000);if(ret = 0)/超时printf(time outn);elseread(fd, &keyvals, 1 );printf(keyvals = 0x%xn,keyvals);return 0;Makefile:KERN_DIR = /root/linux2.6.28(前提是linux2.6.28要编译好)all:make -C $(KERN_DIR) M=pwd modu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论