linux内核版本:linux-2.6.30.4

目的:我们在中断方式的按键应用程序中,如果没有按键按下,read就会永远在那等待,所以如果在这个程序里还想做其他事就不可能了。因此我们这次改进它,让它在等待5秒钟,如果5秒钟内没有按键按下就返回,可以在read后面做其他事,只需要在驱动程序里加入poll机制就可以完成目的。

 #include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/device.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <mach/regs-gpio.h>
#include <linux/poll.h> static struct class *fourth_drv_class;
static struct class_device *fourth_drv_class_dev; static volatile unsigned long *gpfcon;
static volatile unsigned long *gpfdat;
static int major; struct pin_desc
{
unsigned int pin;
unsigned int key_val;
}; static unsigned char key_val; static struct pin_desc pins_desc[] =
{
{S3C2410_GPF1, 0x01},
{S3C2410_GPF4, 0x02},
{S3C2410_GPF2, 0x03},
{S3C2410_GPF0, 0x04},
}; static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = ; static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pin = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pin->pin); if (pinval)
{
//松开
key_val = 0x80 | pin->key_val;
}
else
{
//按下
key_val = pin->key_val;
} wake_up_interruptible(&button_waitq);
ev_press = ; return IRQ_HANDLED;
} static int fourth_drv_open(struct inode *inode, struct file *file)
{
request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S1", &pins_desc[]);
request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S2", &pins_desc[]);
request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S3", &pins_desc[]);
request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, "S4", &pins_desc[]);
return ;
} static ssize_t fourth_drv_read(struct file *file, char __user *buf,
size_t count, loff_t *ppos)
{
if (count != )
return -EINVAL;
/*如果没有按键动作,休眠*/
wait_event_interruptible(button_waitq, ev_press); /*如果有按键动作发生,返回键值*/
copy_to_user(buf, &key_val, );
ev_press = ;
return ;
} static int fourth_drv_release(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT1, &pins_desc[]);
free_irq(IRQ_EINT4, &pins_desc[]);
free_irq(IRQ_EINT2, &pins_desc[]);
free_irq(IRQ_EINT0, &pins_desc[]);
return ;
} static unsigned int fourth_drv_poll(struct file *file, poll_table *wait) {
unsigned int mask = ; /*这并不会休眠,只是把当前进程挂到队列里*/
poll_wait(file, &button_waitq, wait); if (ev_press)
{
mask |= (POLLIN | POLLRDNORM);
}
return mask;
} static struct file_operations fourth_fops = {
.owner = THIS_MODULE,
.open = fourth_drv_open,
.read = fourth_drv_read,
.poll = fourth_drv_poll,
.release = fourth_drv_release,
}; int fourth_init()
{
int ret;
major = register_chrdev(, "fourth_drv", &fourth_fops);
fourth_drv_class = class_create(THIS_MODULE, "fourth_drv");
device_create(fourth_drv_class, NULL, MKDEV(major, ), NULL, "buttons"); gpfcon = (volatile unsigned long *)ioremap(0x56000050, );
gpfdat = gpfcon + ; return ;
}
static void fourth_exit()
{
unregister_chrdev(major, "fourth_drv");
device_destroy(fourth_drv_class, MKDEV(major, ));
class_destroy(fourth_drv_class);
iounmap(gpfcon);
} module_init(fourth_init);
module_exit(fourth_exit); MODULE_LICENSE("GPL");

测试程序:

 #include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h> /* fourthdrvtest
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
int ret;
struct pollfd fds[]; fd = open("/dev/buttons", O_RDWR);
if (fd < )
{
printf("can't open!\n");
} fds[].fd = fd;
fds[].events = POLLIN;
while ()
{
ret = poll(fds, , );
if (ret == )
{
printf("time out!\n");
}
else
{
read(fd, &key_val, );
printf("key_val === 0x%x\n", key_val);
}
} return ;
}

运行效果:

韦东山驱动视频笔记——3.字符设备驱动程序之poll机制的更多相关文章

  1. 字符设备驱动程序之poll机制(韦大仙)

    明确为什么要引用poll机制? while(1) { read(fd,&key_val,1);//如果没有按键按下,它会一直在等待.现在想做这么一件事情:如果5s后,没有按键按下,它就会返回. ...

  2. 《linux设备驱动开发详解》笔记——6字符设备驱动

    6.1 字符设备驱动结构 先看看字符设备驱动的架构: 6.1.1 cdev cdev结构体是字符设备的核心数据结构,用于描述一个字符设备,cdev定义如下: #include <linux/cd ...

  3. 嵌入式Linux驱动学习之路(二十一)字符设备驱动程序总结和块设备驱动程序的引入

    字符设备驱动程序 应用程序是调用C库中的open read write等函数.而为了操作硬件,所以引入了驱动模块. 构建一个简单的驱动,有一下步骤. 1. 创建file_operations 2. 申 ...

  4. arm-linux字符设备驱动开发之---简单字符设备驱动

    一.linux系统将设备分为3类:字符设备.块设备.网络设备.使用驱动程序: 1.字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面 ...

  5. Linux驱动实践:你知道【字符设备驱动程序】的两种写法吗?

    作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...

  6. LINUX设备驱动程序笔记(三)字符设备驱动程序

          <一>.主设备号和次设备号        对字符设备的訪问时通过文件系统内的设备名称进行的.那些设备名称简单称之为文件系统树的节点,它们通常位于/dev文件夹. 字符设备驱动程 ...

  7. Linux 简单字符设备驱动程序 (自顶向下)

    第零章:扯扯淡 特此总结一下写的一个简单字符设备驱动程序的过程,我要强调一下“自顶向下”这个介绍方法,因为我觉得这样更容易让没有接触过设备驱动程序的童鞋更容易理解,“自顶向下”最初从<计算机网络 ...

  8. ARM Linux字符设备驱动程序

    1.主设备号和次设备号(二者一起为设备号): 一个字符设备或块设备都有一个主设备号和一个次设备号.主设备号用来标识与设备文件相连的驱动程序,用来反  映设备类型.次设备号被驱动程序用来辨别操作的是哪个 ...

  9. 浅析Linux字符设备驱动程序内核机制

    前段时间在学习linux设备驱动的时候,看了陈学松著的<深入Linux设备驱动程序内核机制>一书. 说实话.这是一本非常好的书,作者不但给出了在设备驱动程序开发过程中的所须要的知识点(如对 ...

随机推荐

  1. HDU 1041 Computer Transformation (简单大数)

    Computer Transformation http://acm.hdu.edu.cn/showproblem.php?pid=1041 Problem Description A sequenc ...

  2. mysql 存储过程事务

    DECLARE t_error INTEGER DEFAULT ; DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=; START TRAN ...

  3. 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数

    http://poj.org/problem?id=1284 题意:给定一个奇素数p,求p的原根个数. 原根: { (xi mod p) | 1 <= i <= p-1 } is equa ...

  4. ubuntu系统使用minicom终端操作说明

    http://blog.chinaunix.net/uid-22030783-id-3350834.html 在linux下,使用minicom作为串口终端工具,默认的串口设备是/dev/ttyS0, ...

  5. iOS开发——技术精华Swift篇&Swift 2.0和Objective-C2.0混编之第三方框架的使用

    swift 语言是苹果公司在2014年的WWDC大会上发布的全新的编程语言.Swift语言继承了C语言以及Objective-C的特性,且克服了C语言的兼容性问题.Swift语言采用安全编程模式,且引 ...

  6. MongoDB驱动特性检查列表

    http://docs.mongodb.org/meta-driver/latest/legacy/feature-checklist-for-mongodb-drivers/ 1. 基本(Essen ...

  7. qt中如何启动其他应用程序(如果不成功,还有许多原因即QProcess::ProcessError可供分析)

    类 QDesktopServices 提供的方法 访问 常用的桌面 服务 , 如 浏览 器 . 播放器. 电子邮件客户端 . 我们 使用 QDesktopServices :: openUrl(url ...

  8. CentOS中的chkconfig命令

    chkconfig:    chkconfig命令主要用来更新(启动或停止)和查询系统服务的运行级信息.谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接.语法:    ...

  9. NET在SQL Server中的图片存取技术

    本文总结如何在.Net WinForm和.Net WebForm(asp.net)中将图片存入SQL Server中并读取显示的方法 .1.使用asp.net将图片上传并存入SQL Server中,然 ...

  10. 第一个Struts2程序

    Struts2.3.16, Tomcat6.0.37,Java8 /web.xml <?xml version="1.0" encoding="UTF-8" ...