Linux 驱动——Button驱动3(poll机制)
button_drv.c驱动文件:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/device.h>
#include <asm/arch/regs-gpio.h>
#include <linux/irq.h>
#include <asm-arm/irq.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <asm/hardware.h>
#include <linux/poll.h>
#define DRIVER_NAME "button_drv"
#define DEVICE_NAME "button_dev"
int major;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
struct class *buttondrv_class;
struct class_device *buttondrv_class_device;
unsigned int ev_press;
DECLARE_WAIT_QUEUE_HEAD(button_waitq);
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
unsigned int key_val;
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
irqreturn_t buttons_irq(int irq, void *dev_id)
{
unsigned int pin_val;
struct pin_desc *pin_desc = (struct pin_desc *)dev_id;
pin_val = s3c2410_gpio_getpin(pin_desc->pin);
if(pin_val)
{
key_val = 0x80 | pin_desc->key_val;
}
else
{
key_val = pin_desc->key_val;
}
wake_up_interruptible(&button_waitq);
ev_press = 1;
return IRQ_HANDLED;
}
int button_drv_open(struct inode *inode, struct file *file)
{
int ret;
ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S1", (void*)&pins_desc[0]);
if(ret)
{
printk("open failed 1 \n");
return -1;
}
ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S2", (void*)&pins_desc[1]);
if(ret)
{
printk("open failed 2 \n");
return -1;
}
ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S3", (void*)&pins_desc[2]);
if(ret)
{
printk("open fail 3 \n");
return -1;
}
ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S4", (void*)&pins_desc[3]);
if(ret)
{
printk("open fail 4 \n");
return -1;
}
return 0;
}
ssize_t button_drv_read(struct file *file, char __user *userbuf, size_t count, loff_t *off)
{
int ret;
ret = copy_to_user(userbuf, &key_val, 1);
if(ret)
{
printk("copy eror \n");
return -1;
}
ev_press = 0;
return 1;
}
int button_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, (void*)&pins_desc[0]);
free_irq(IRQ_EINT2, (void*)&pins_desc[1]);
free_irq(IRQ_EINT11, (void*)&pins_desc[2]);
free_irq(IRQ_EINT19, (void*)&pins_desc[3]);
return 0;
}
unsigned int button_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if(ev_press)
{
mask |= POLLIN | POLLRDNORM;
}
return mask;
}
struct file_operations button_drv_fops = {
.owner = THIS_MODULE,
.open = button_drv_open,
.read = button_drv_read,
.release = button_drv_close,
.poll = button_drv_poll,
};
int __init button_drv_init(void)
{
major = register_chrdev(0, DRIVER_NAME, &button_drv_fops);
if(major<0)
{
printk("fail 1 button_drv \n");
}
buttondrv_class = class_create(THIS_MODULE, DEVICE_NAME);
if(buttondrv_class<0)
{
printk("fail 2 button_dev \n");
}
buttondrv_class_device = class_device_create(buttondrv_class, NULL, MKDEV(major,0), NULL, DEVICE_NAME);
if(buttondrv_class_device<0)
{
printk("fail 3 button_dev \n");
}
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
printk("register button_drv \n");
return 0;
}
void __exit button_drv_exit(void)
{
unregister_chrdev(major, DEVICE_NAME);
class_device_unregister(buttondrv_class_device);
class_destroy(buttondrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
printk("unregister button_drv \n");
}
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
Makefile文件:
obj-m += button_drv.o
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
rm -rf *.o *.ko *.order *.symvers *.mod.c
button_app.c文件:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
int main(int argc, char **argv)
{
int fd;
int ret;
char *filename;
struct pollfd fds;
unsigned char keyVal;
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd<0)
{
printf("can not open \n");
}
fds.fd = fd;
fds.events = POLLIN;
while(1)
{
ret = poll(&fds, 1, 5000); //linux系统再调用poll函数时候,如果没有发生需要的事件,那么进程进入休眠。
//如果在限定的时间内得到需要的事件,那么成功返回,如果没有则返回超时错误信息。
if(ret<0)
{
printf("time out \n");
}
else
{
read(fd, &keyVal, 1);
printf("keyVal: %x \n",keyVal);
}
}
close(fd);
return 0;
}
编译生成button_drv.ko和button_app文件,运行./button_app /dev/button_dev
Linux 驱动——Button驱动3(poll机制)的更多相关文章
- Linux 驱动——Button驱动7(Timer)消抖
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动5(atomic)原子量
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动4(fasync)异步通知
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动6(mutex、NBLOCK、O_NONBLOCK)互斥信号量、阻塞、非阻塞
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动2
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- Linux 驱动——Button驱动1
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/i ...
- Linux驱动之poll机制的理解与简单使用
之前在Linux驱动之按键驱动编写(中断方式)中编写的驱动程序,如果没有按键按下.read函数是永远没有返回值的,现在想要做到即使没有按键按下,在一定时间之后也会有返回值.要做到这种功能,可以使用po ...
- poll机制实例参考
poll机制:为了减少CPU资源的占用率,在编写驱动函数中添加poll机制 select,poll,epoll都是IO多路复用的机制.I/O多路复用就通过一种机制,可以监视多个描述符,一旦某个描述符就 ...
- 嵌入式Linux驱动学习之路(十二)按键驱动-poll机制
实现的功能是在读取按键信息的时候,如果没有产生按键,则程序休眠在read函数中,利用poll机制,可以在没有退出的情况下让程序自动退出. 下面的程序就是在读取按键信息的时候,如果5000ms内没有按键 ...
随机推荐
- python学习之可视化tkinter基础
- nigix反向代理
参考: https://www.cnblogs.com/yycc/p/8185748.html
- Linux 安装 Rabbit MQ
安装socat yum -y install socat 下载erlang软件包,本文使用erlang-19.0.4版本,下面给出下载链接 wget http://www.rabbitmq.com/r ...
- NPM 报错--fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module
fs: re-evaluating native module sources is not supported. If you are using the graceful-fs module 解决 ...
- CentOS7系统系统检查脚本
#!/bin/bash ############################################################## # File Name: .sh # Versio ...
- RTX腾讯通字体全变成横着的了
呵呵,简单,RTX字体选择里边的字体列表中同一种字体有些是带@符号的,有些没有带,记着选不带@号的就是头朝上的了.
- 【1】HTTP协议和Socket接口区别
内容提要: 1.网络七层模型 2.什么是HTTP协议 3.什么是Socket接口 1.网络七层模型 第一层:物理层 为设备之间的信息提供传输提供可靠环境,那么这个环境是什么呢? 如:同轴电缆,插头,接 ...
- CSAPP之阅读笔记-计算机系统漫游(1)
最近在看CSAPP(深入理解计算机系统第二版),其实最新版是第三版.但是,我看了一下价格100多大洋,于是去老夫子旧书网上买了本第二版的,花了30多块钱.哈哈. 网上看了一些关于此书的书评,都说是本好 ...
- 梳理:python—同一个类中的方法调用
为什么突然在此提到这个梳理问题呢? 因为在自己实践综合练习学过的知识时,突然觉得有些知识点的运用总是不成功,于是翻过课本进行回顾,总是觉得是对的,可是当再进一步思考“既然是对的,为什么在程序中总是不成 ...
- Linux c获取任意路径的硬盘使用情况
没有什么好说的,其实就是获取硬盘的statfs信息结构 代码如下: #include <stdio.h> #include <stdlib.h> #include <sy ...