S5PV210按键POLL实现机制

本文详细解析了S5PV210平台上的按键驱动程序设计,包括驱动初始化、中断处理、定时器使用及与应用程序的交互流程。阐述了如何通过定时器和中断唤醒应用程序,并实现按键状态的读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

驱动程序:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/ioport.h>

#include <linux/interrupt.h>
#include <linux/wait.h>
#include <linux/poll.h>

#include <asm/uaccess.h>
#include <asm/io.h>

#include <mach/irqs.h>
#include <mach/gpio.h>

static int major_button = -1;
static struct class * class_button = NULL;
static struct device * device_button = NULL;
static int press_mark = 0;
static int key_val = 0;
static DECLARE_WAIT_QUEUE_HEAD(button_press_mark);
static struct timer_list buttons_timer; /* 定义一个定时器结构体 */


static void buttons_timer_function(unsigned long data)
{
	key_val = gpio_get_value(S5PV210_GPH0(2));
	printk(KERN_INFO "buttons_timer_function %d\n", key_val);
	if (key_val)
	{
		printk(KERN_INFO "gpio == 1 up\n");
	}
	else
	{
		printk(KERN_INFO "gpio == 0 press\n");
	}

	press_mark = 1;
	//为什么加了poll之后,还要去唤醒进程呢?
	wake_up_interruptible(&button_press_mark);
}

static irqreturn_t button_interrupt_mark(int irq, void * data)
{
	mod_timer(&buttons_timer, jiffies + (HZ /50));
	return IRQ_HANDLED;
}


static int button_mark_open (struct inode * inode, struct file * file)
{
	int err = -1;
	if (gpio_request(S5PV210_GPH0(2), "gph0_2"))
	{
		printk(KERN_ERR "gpio_request S5PV210_GPH0(2) error\n");
		goto err_gpio_request;
	}
	s3c_gpio_cfgpin(S5PV210_GPH0(2), 0xf);

	err = request_irq(IRQ_EINT2, button_interrupt_mark, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 
		"irq-eint2", NULL);
	if (err)
	{
		printk(KERN_ERR "request_irq IRQ_EINT2 error\n");
		goto err_request_irq;
	}

	return 0;
err_request_irq:
	gpio_free(S5PV210_GPH0(2));
err_gpio_request:
	return -EBUSY;
}

static int button_mark_close (struct inode * inode, struct file * file)
{
	disable_irq(IRQ_EINT2);
	free_irq(IRQ_EINT2, NULL);	
	gpio_free(S5PV210_GPH0(2));
	return 0;
}

static ssize_t button_mark_read (struct file * file, 
	char __user * out, size_t size, loff_t * offset)
{
	int ret = 0;
	if (1 != size)
	{
		return -EINVAL;
	}
	//当press_mark = 0的时候, button读的时候应用进程将被阻塞,
	wait_event_interruptible(button_press_mark, press_mark);

	ret = copy_to_user(out, &key_val, size);
	if (ret)
	{
		printk(KERN_ERR "copy_to_user error\n");
		return -EINVAL;
	}


	press_mark = 0;
	
	return 0;
}

static unsigned int button_mark_poll (struct file * file, 
	struct poll_table_struct * wait)
{
	unsigned int mask = 0;
	poll_wait(file, &button_press_mark, wait);

	if (press_mark)
		mask |= POLLIN | POLLRDNORM;

	return mask;
}

static struct file_operations button_operations_mark = 
{
	.owner = THIS_MODULE,
	.open = button_mark_open,
	.release = button_mark_close,	
	.read = button_mark_read,
	.poll = button_mark_poll,
};




static int __init s5pv210_button_init(void)
{

	init_timer(&buttons_timer);
	buttons_timer.function = buttons_timer_function;
	add_timer(&buttons_timer);
	
	major_button = register_chrdev(0, "button", &button_operations_mark);
	if (major_button < 0)
	{
		printk(KERN_ERR "register_chrdev error\n");
		goto err_register_chrdev;
	}
	class_button = class_create(THIS_MODULE, "BUTTON_MARK");
	if (!class_button)
	{
		printk(KERN_ERR "class_create error\n");
		goto err_class_create;
	}
	device_button = device_create(class_button, NULL, MKDEV(major_button, 0), 
		NULL, "button-mark");
	if (!device_button)
	{
		printk(KERN_ERR "device_create error\n");
		goto err_device_create;
	}
	return 0;
	
err_device_create:
	class_destroy(class_button);
err_class_create:
	unregister_chrdev(major_button, "button");
err_register_chrdev:
	del_timer(&buttons_timer);
	return -EBUSY;
}

static void __exit s5pv210_button_exit(void)
{
	device_destroy(class_button, MKDEV(major_button, 0));
	class_destroy(class_button);
	unregister_chrdev(major_button, "button");
	del_timer(&buttons_timer);
}

module_init(s5pv210_button_init);
module_exit(s5pv210_button_exit);

// MODULE_xxx这种宏作用是用来添加模块描述信息
MODULE_LICENSE("GPL"); // 描述模块的许可证
MODULE_AUTHOR("Mark 867439374@qq.com"); // 描述模块的作者
MODULE_DESCRIPTION("s5pv210 button driver"); // 描述模块的介绍信息
MODULE_ALIAS("s5pv210_button"); // 描述模块的别名信息

应用程序:

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


#define PATHNAME	"/dev/button-mark"
static int val = 0;
int main(void)
{
	int fd = -1;
	int ret = -1;
	struct pollfd fds; 
	fd = open(PATHNAME, O_RDWR);
	if (fd < 0)
	{
		perror("");
		return -1;
	}
	fds.fd = fd;
	fds.events = POLLIN; //普通或优先级带数据可读
	
	while(1)
	{
		//返回:就绪描述字的个数,0-超时,-1-出错
		ret = poll(&fds, 1,10000); //1代表监视一个fd, 10000 = 10000ms
		if (0 == ret)
		{
			printf("time out!\n");
		}
		else
		{
			read(fd, &val, 1);
			printf("read: %d\n", val);
		}
	}
	close(fd);
	return 0;
}

程序相关解释:
1.当应用程序使用ret = poll(&fds, 1,10000); //1代表监视一个fd, 10000 = 10000ms,这个会让进程睡眠
2.唤醒条件:第一种方式是定时时间到,第二种方式是驱动程序里面唤醒进程
3.应用程序调用poll函数,实际上是调用到驱动的:poll_wait(file, &button_press_mark, wait);,然后在这里睡眠了。
4.第一种唤醒方式:定时时间到,驱动里面返回0给应用,然后应用这边打印timeout。
5.第二种唤醒方式:有按键按下/弹起,驱动程序里面会唤醒刚才调用poll睡眠的进程,wake_up_interruptible(&button_press_mark);,
6.唤醒了之后,唤醒了之后,mask |= POLLIN | POLLRDNORM;,poll函数等待的事件发生了
7.然后就会去执行read函数,read函数里面加(wait_event_interruptible(button_press_mark, press_mark)?
或者不加这个函数都可以,因为此时press_mark=1,而且进程已经被唤醒。
8.wait_event_interruptible(button_press_mark, press_mark);这个函数,只有被wake_up_interruptible唤醒,而且press_mark==1的条件下,才会往下去执行。
9.按下之后,就会再次调用poll函数,驱动button_mark_poll,然后就又睡眠了。
10.松开的时候,又会触发中断,和按下的时候,分析是一样的。
11.所以即使按下不松,也会timeout的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值