编译环境
ubunt 16.04
arm-linux-gcc 4.3.2
linux-3.4.2
zj2440
poll的作用是把当前的文件指针挂到等待队列。
对于一个按键事件:
1、查询方法:在while(1)中不断循环调用read(fd, &key_val, 1)查询,不断去查询是否有事件发生,整个过程都是占用CPU资源,消耗CPU资源非常大。
2、中断方式:当有事件发生时,就去跳转到相应事件去处理,CPU占用时间少。若没有按键按下,则要一直处于休眠状态
3、poll方式:当有事件发生时,才去执行读read函数,按键事件没有按下时,返回超时错误信息,进程能够继续得到执行。
poll实现步骤:
1、在驱动函数file_operation结构体上添加一个.poll函数,然后在函数里执行poll_wait,这个函数用来判断硬件事件是否发生
2、测试程序需要调用ret = poll(fds, 1, 5000) 【超时时间为5s】,函数来获取事件发生信息。
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/device.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
static struct class *newdrv_class;
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgdat = NULL;
volatile unsigned long *gpgcon = NULL;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,s3c24xx_buttons_read将它清0 */
static volatile int ev_press = 0;
static unsigned char key;
struct pin_desc{
unsigned int pin;//引脚
unsigned int key_val;//键值,按下01 02 03 04;松开81 82 83 84
};
struct pin_desc pins_desc[4]={
{S3C2410_GPF(0), 0x01},
{S3C2410_GPF(2), 0x02},
{S3C2410_GPG(3), 0x03},
{S3C2410_GPG(11), 0x04},
};
static int buttons_irq (int irq, void *dev_id)
{
struct pin_desc *pindesc=(struct pin_desc *)dev_id;
unsigned int pinval;
pinval=s3c2410_gpio_getpin(pindesc->pin);//读引脚
if(pinval)//1 松开
{
key=0x80|pindesc->key_val;
}
else//按下
key=pindesc->key_val;
ev_press = 1; /* 表示中断发生了 */
wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */
return IRQ_RETVAL(IRQ_HANDLED);//接收到了中断信号,并处理,返回1
}
static int new_drv_open(struct inode *inode, struct file *file)
{
/* 中断引脚 中断函数 触发方式(双边沿) 名称 传入dev―id */
request_irq(IRQ_EINT0, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S5", &pins_desc[3]);
return 0;
}
static ssize_t new_drv_read(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
if(count!=1)
return -EINVAL;
/*没有按键动作,ev_press=0,在此休眠,唤醒后往后执行*/
wait_event_interruptible(button_waitq,ev_press);
copy_to_user(buf, &key, 1);
ev_press=0;
return 1;
}
static int new_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
static int new_drv_poll(struct file *file,poll_table *wait)
{
unsigned int mask=0;
poll_wait(file,&button_waitq, wait);//进程挂入button_waitq队列,不会立即休眠
if(ev_press) //为1时退出休眠
mask|=POLLIN|POLLRDNORM;
/*mask=0时休眠,非0时唤醒*/
return mask;
}
static struct file_operations new_drv_fops = {
.owner =THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open =new_drv_open,
.read =new_drv_read,
.release=new_drv_close,
.poll =new_drv_poll,
};
int major;
static int new_drv_init(void)
{
major = register_chrdev(0, "newdrv", &new_drv_fops); // 注册, 自动分配并返回主设备号
*/*在/sys/class/下创建类目录*/*
newdrv_class = class_create(THIS_MODULE, "newdrv");
*/*根据类目录,在驱动模块初始化函数中实现设备节点的自动创建/dev/newdrv*/*
device_create(newdrv_class, NULL, MKDEV(major, 0), NULL, "newdrv");
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void new_drv_exit(void)
{
unregister_chrdev(major, "newdrv"); // 卸载,删除
device_destroy(newdrv_class,MKDEV(major, 0));
class_destroy(newdrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
}
module_init(new_drv_init);//入口函数
module_exit(new_drv_exit);//出口函数
MODULE_LICENSE("GPL");
应用程序
#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;
unsigned char key_val;
int ret;
struct pollfd fds[1];
fd = open("/dev/newdrv", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
fds[0].fd = fd;//文件描述符
fds[0].events = POLLIN;//需要监测的事件
while (1)
{
/*ret即mask,mask=0时,休眠等待5s*/
ret = poll(fds, 1, 5000);
if (ret == 0)
{
printf("time out\n");
}
else
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
}
return 0;
}
通用驱动makefile
KERN_DIR = /work/system/linux-3.4.2
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += led.o