linux字符驱动之poll机制轮询(select)按键驱动

https://blog.csdn.net/lwj103862095/article/details/17536069

#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/module.h>
#include <linux/device.h> //class_create
#include <mach/regs-gpio.h> //S3C2410_GPF1
//#include <asm/arch/regs-gpio.h>
#include <mach/hardware.h>
//#include <asm/hardware.h>
#include <linux/interrupt.h> //wait_event_interruptible
#include <linux/poll.h> //poll

/* 定义并初始化等待队列头 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);

static struct class *fourthdrv_class;
static struct device *fourthdrv_device;

static struct pin_desc{
unsigned int pin;
unsigned int key_val;
};

static struct pin_desc pins_desc[4] = {
{S3C2410_GPF1,0x01},
{S3C2410_GPF4,0x02},
{S3C2410_GPF2,0x03},
{S3C2410_GPF0,0x04},
};

static int ev_press = 0;

/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 /
/
键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
int major;

/* 用户中断处理函数 */
static irqreturn_t 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)
{
    /* 松开 */
    key_val = 0x80 | (pindesc->key_val);
}
else
{
    /* 按下 */
    key_val = pindesc->key_val;
}

ev_press = 1;                             /* 表示中断已经发生 */
 wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
return IRQ_HANDLED;

}
static int fourth_drv_open(struct inode * inode, struct file * filp)
{
/* K1 ---- EINT1,K2 ---- EINT4,K3 ---- EINT2,K4 ---- EINT0
* 配置GPF1、GPF4、GPF2、GPF0为相应的外部中断引脚
* IRQT_BOTHEDGE应该改为IRQ_TYPE_EDGE_BOTH
*/
request_irq(IRQ_EINT1, buttons_irq, IRQ_TYPE_EDGE_BOTH, “K1”,&pins_desc[0]);
request_irq(IRQ_EINT4, buttons_irq, IRQ_TYPE_EDGE_BOTH, “K2”,&pins_desc[1]);
request_irq(IRQ_EINT2, buttons_irq, IRQ_TYPE_EDGE_BOTH, “K3”,&pins_desc[2]);
request_irq(IRQ_EINT0, buttons_irq, IRQ_TYPE_EDGE_BOTH, “K4”,&pins_desc[3]);
return 0;
}

static ssize_t fourth_drv_read(struct file *file, char __user *user, size_t size,loff_t *ppos)
{
if (size != 1)
return -EINVAL;

/* 当没有按键按下时,休眠。
 * 即ev_press = 0;
 * 当有按键按下时,发生中断,在中断处理函数会唤醒
 * 即ev_press = 1;
 * 唤醒后,接着继续将数据通过copy_to_user函数传递给应用程序
 */
wait_event_interruptible(button_waitq, ev_press);
copy_to_user(user, &key_val, 1);

/* 将ev_press清零 */
ev_press = 0;
return 1;    

}

static int fourth_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT1,&pins_desc[0]);
free_irq(IRQ_EINT4,&pins_desc[1]);
free_irq(IRQ_EINT2,&pins_desc[2]);
free_irq(IRQ_EINT0,&pins_desc[3]);
return 0;
}

static unsigned int fourth_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;

/* 该函数,只是将进程挂在button_waitq队列上,而不是立即休眠 */
poll_wait(file, &button_waitq, wait);

/* 当没有按键按下时,即不会进入按键中断处理函数,此时ev_press = 0
 * 当按键按下时,就会进入按键中断处理函数,此时ev_press被设置为1
 */
if(ev_press)
{
    mask |= POLLIN | POLLRDNORM;  /* 表示有数据可读 */
}

/* 如果有按键按下时,mask |= POLLIN | POLLRDNORM,否则mask = 0 */
return mask;  

}

/* File operations struct for character device */
static const struct file_operations fourth_drv_fops = {
.owner = THIS_MODULE,
.open = fourth_drv_open,
.read = fourth_drv_read,
.release = fourth_drv_close,
.poll = fourth_drv_poll,
};

/* 驱动入口函数 /
static int fourth_drv_init(void)
{
/
主设备号设置为0表示由系统自动分配主设备号 */
major = register_chrdev(0, “fourth_drv”, &fourth_drv_fops);

/* 创建fourthdrv类 */
fourthdrv_class = class_create(THIS_MODULE, "fourthdrv");

/* 在fourthdrv类下创建buttons设备,供应用程序打开设备*/
fourthdrv_device = device_create(fourthdrv_class, NULL, MKDEV(major, 0), NULL, "buttons");

return 0;

}

/* 驱动出口函数 */
static void fourth_drv_exit(void)
{
unregister_chrdev(major, “fourth_drv”);
device_unregister(fourthdrv_device); //卸载类下的设备
class_destroy(fourthdrv_class); //卸载类
}

module_init(fourth_drv_init); //用于修饰入口函数
module_exit(fourth_drv_exit); //用于修饰出口函数

MODULE_AUTHOR(“LWJ”);
MODULE_DESCRIPTION(“Just for Demon”);
MODULE_LICENSE(“GPL”); //遵循GPL协议

/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>

/* fourth_test
*/
int main(int argc ,char *argv[])

{
int fd;
unsigned char key_val;
struct pollfd fds;
int ret;

fd = open("/dev/buttons",O_RDWR);
if (fd < 0)
{
    printf("open error\n");
}
fds.fd = fd;
fds.events = POLLIN;
while(1)
{
    /* A value of 0 indicates  that the call timed out and no file descriptors were ready
     * poll函数返回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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xx-xx-xxx-xxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值