ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

rk3288 poll机制学习使用

2021-04-01 21:34:02  阅读:223  来源: 互联网

标签:struct int button key rk3288 gpio 机制 poll


适用场景

使用休眠-唤醒的方式等待某个事件发生时,有一个缺点:等待的时间可能很久。我们可以加上一个超时时间,这时就可以使用 poll 机制。

  1. APP 不知道驱动程序中是否有数据,可以先调用 poll 函数查询一下,poll 函数可以传入***超时时间***;
  2. APP 进入内核态,调用到驱动程序的 poll 函数,如果有数据的话立刻返回;
  3. 如果发现没有数据时就***休眠一段时间***;
  4. 当有数据时,比如当按下按键时,驱动程序的中断服务程序被调用,它会记录数据、唤醒 APP;
  5. 当超时时间到了之后,内核也会唤醒 APP;
  6. APP 根据 poll 函数的返回值就可以知道是否有数据,如果有数据就调用 read 得到数据

使用流程

  1. app里调用open函数

  2. 驱动层调用drv_open函数

  3. app调用poll函数

  4. 驱动层调用drv_poll函数,
    drv_poll把自己的线程挂入等待队列wq中,drv_poll同时判断是否有数据,返回一个状态。

  5. 假设没有数据,则会休眠一段时间。

  6. 如果在休眠期间,发生了按键中断。则在中断服务程序中记录按键值,并且从wq中把线程唤醒。

  7. 线程唤醒后,继续执行for循环,再次调用poll,drv_poll返回数据状态。

  8. 如果有数据,内核态返回到用户态。

  9. App调用read函数读数据。

  10. 当调用app调用poll后,进入内核态。

  11. 导致驱动程序的drv_poll被调用

  12. 假设当前没有数据,则休眠一会

  13. 休眠后,一直没有按下按键,超时时间到,内核会唤醒线程。

  14. 线程从休眠中被唤醒,继续执行for循环,再次调用drv_poll,drv_poll返回数据状态。

  15. 没有数据,但是超时时间到了,那从内核态返回到用户态。

  16. App不能调用read函数读数据。

注意:这里有个for循环,并不是指的App里循环调用poll的for循环,而是内核中的for循环。

驱动编写思路

编写驱动给app提供poll函数,它需要做两件事情:

  1. 把当前线程挂入队列wq:使用poll_wait(),app调用一次poll,可能导致驱动调用两次drv_poll。但是poll_wait()会避免重复挂入wq。
  2. 返回设备状态:App调用poll函数时,drv_poll要返回当前是否有数据。有数据(POLLIN|POLLRDNORM)或(POLLOUT|POLLWRNORM)。POLLRDNORM和POLLWRNORM是为了兼容某些App。

驱动poll代码,调用poll_wait,进入gpio_key_wait等待队列休眠:

static unsigned int button_poll(struct file *fp, poll_table * wait)
{
	printk("%s,button poll\n", __FUNCTION__);
	poll_wait(fp, &gpio_key_wait, wait);
	return IsEmpty(irqBuff) ? 0 : POLLIN | POLLRDNORM;
}

中断函数任然需要唤醒它:

static irqreturn_t myBtn_irq_request(int irq, void *dev_id)
{
	struct gpio_key *gpio_key = dev_id;
	int val;
	val = gpiod_get_value(gpio_key->gpiod);

	printk(KERN_WARNING"key %d %d\n", gpio_key->gpio, val);
	val = (myBtn_key->gpio << 8)|val;
	AddQ(irqBuff, val);
	wake_up_interruptible(&gpio_key_wait);

	return IRQ_HANDLED;
}

同时需要read函数去读取对应数据:

static ssize_t button_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	int err;
	int val;

	wait_event_interruptible(gpio_key_wait, !IsEmpty(irqBuff));
	val = DeleteQ(irqBuff);
	err = copy_to_user(buf, &val, 4);
	if(err != 4) {
		return -1;
	}
	return 4;
}

应用函数编程

App中可以调用poll/select函数,并且支持多种事件,具体看man函数。

  • POLLIN 有数据可读
  • POLLRDNORM 等同于 POLLIN
  • POLLRDBAND Priority band data can be read,有优先级较较高的“band data”可读,Linux 系统中很少使用这个事件
  • POLLPRI 高优先级数据可读
  • POLLOUT 可以写数据
#include <poll.h>
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
//fds是pollfd结构,nfds是fds的个数,timeout是超时时间
//			struct pollfd {
//               int   fd;         /* file descriptor */		//文件描述符,检测的文件
//               short events;     /* requested events */		//检测哪个事件
//               short revents;    /* returned events */		//返回的事件
//           };
//

源码

//button_poll.c
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/uaccess.h>
#include <linux/irqreturn.h>
#include <linux/gpio/consumer.h>
#include <linux/of_gpio.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/wait.h>
#include <linux/poll.h>

struct gpio_key {
	int gpio;
	struct gpio_desc *gpiod;
	int flag;
	int irq;
};
static struct gpio_key *myBtn_key;
static int button_major = 0;
static struct class *button_class;

static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);

#define MaxSize 128
struct QNode {
	int Data[MaxSize];
	int rear;
	int front;
};

typedef struct QNode *Queue;

int IsEmpty(Queue Q);
void AddQ(Queue PtrQ, int item);
int DeleteQ(Queue PtrQ);

int IsEmpty(Queue Q)
{
	return (Q->rear == Q->front);   //1:empty 0:not empty
}


void AddQ(Queue PtrQ, int item)
{
    if((PtrQ->rear+1)%MaxSize == PtrQ->front) {
		printk("%s,Queue full\n", __FUNCTION__);
        return;
    }
    PtrQ->rear = (PtrQ->rear+1)%MaxSize;
    PtrQ->Data[PtrQ->rear] = item;
} 


int DeleteQ(Queue PtrQ)
{
    if(PtrQ->front == PtrQ->rear) {
		printk("%s,Queue empty\n", __FUNCTION__);
        return -1;
    } else {
        PtrQ->front = (PtrQ->front+1)%MaxSize;
        return PtrQ->Data[PtrQ->front];
    }
}

static Queue irqBuff;
static ssize_t button_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	int err;
	int val;

	wait_event_interruptible(gpio_key_wait, !IsEmpty(irqBuff));
	val = DeleteQ(irqBuff);
	err = copy_to_user(buf, &val, 4);
	if(err != 4) {
		return -1;
	}
	return 4;
}

static unsigned int button_poll(struct file *fp, poll_table * wait)
{
	printk("%s,button poll\n", __FUNCTION__);
	poll_wait(fp, &gpio_key_wait, wait);
	return IsEmpty(irqBuff) ? 0 : POLLIN | POLLRDNORM;
}


static struct file_operations button_ops = {
	.owner = THIS_MODULE,
	.read  = button_read, 
	.poll  = button_poll,
};

static irqreturn_t myBtn_irq_request(int irq, void *dev_id)
{
	struct gpio_key *gpio_key = dev_id;
	int val;
	val = gpiod_get_value(gpio_key->gpiod);

	printk(KERN_WARNING"key %d %d\n", gpio_key->gpio, val);
	val = (myBtn_key->gpio << 8)|val;
	AddQ(irqBuff, val);
	wake_up_interruptible(&gpio_key_wait);

	return IRQ_HANDLED;
}

static int my_button_probe(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	int count;
	enum of_gpio_flags flag;
	int i, err;

	count = of_gpio_count(node);
	if(!count) {
		printk("%s,there isn't any gpio availiable\n", __FUNCTION__);
		return -1;
	}
	
	myBtn_key = (struct gpio_key*)kzalloc(sizeof(struct gpio_key)*count, GFP_KERNEL);
	if(!myBtn_key) {
		printk("%s,kzalloc malloc failed\n", __FUNCTION__);
		return -1;
	}


	for(i=0;i<count;i++) {
		myBtn_key[i].gpio = of_get_gpio_flags(node, i, &flag);
		if(myBtn_key[i].gpio < 0) {
			printk("%s, of_get_gpio_flags failed\n", __FUNCTION__);
			return -1;
		}
		myBtn_key[i].gpiod = gpio_to_desc(myBtn_key[i].gpio);
		myBtn_key[i].flag  = flag & OF_GPIO_ACTIVE_LOW;
		myBtn_key[i].irq   = gpio_to_irq(myBtn_key[i].gpio);
		err = request_irq(myBtn_key[i].irq, myBtn_irq_request, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
				"myBtn_key", &myBtn_key[i]);
	}

    button_major = register_chrdev(0, "mybutton", &button_ops);
    if (button_major < 0) {
        printk(KERN_ERR "button : couldn't get a major number.\n");
        return -1;
    }

    button_class = class_create(THIS_MODULE, "button_class");
    if(IS_ERR(button_class)) {
        printk(KERN_ERR "button class: create failed\n");
		unregister_chrdev(button_major, "mybutton");
        return -1;
    }


	device_create(button_class, NULL, MKDEV(button_major, 0), NULL, "mybutton%d", 0);

	return 0;
}

static int my_button_remove(struct platform_device *pdev)
{
	struct device_node *node= pdev->dev.of_node;
	int count;
	int i;

	device_destroy(button_class, MKDEV(button_major, 0));
	class_destroy(button_class);
	unregister_chrdev(button_major, "mybutton");

	count = of_gpio_count(node);
	for(i=0;i<count;i++) {
		free_irq(myBtn_key[i].irq, &myBtn_key[i]);
	}

	kfree(myBtn_key);
	return 0;
}

static struct of_device_id mybuttons[] = {
	{ .compatible = "mybtn,btn_drv" },
	{ },
};

static struct platform_driver my_button_driver = {
	.probe  = my_button_probe,
	.remove = my_button_remove,
	.driver = {
		.name = "button_dirver",
		.of_match_table = mybuttons,
	},
};

static int gpio_button_init(void)
{
	int err;
	irqBuff = (Queue)kzalloc(sizeof(struct QNode), GFP_KERNEL);
	err = platform_driver_register(&my_button_driver);
	printk(KERN_WARNING"my button dirver init\n");
	return 0;
}

static void gpio_button_exit(void)
{
	platform_driver_unregister(&my_button_driver);
	kfree(irqBuff);
	printk(KERN_WARNING"my button dirver exit\n");
}

module_init(gpio_button_init);
module_exit(gpio_button_exit);
MODULE_LICENSE("GPL");
//button_test.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <signal.h>


int main(int argc, char *argv[])
{
	int fd;
	int val;
	struct pollfd fds[1];
	int timeout_ms = 1000;

	if(argc != 2) {
		printf("Usage: %d <dev>\n", argv[0]);
		return -1;
	}

	fd = open(argv[1], O_RDWR);
	if(fd<0) {
		printf("can not open file %s\n", argv[1]);
		return -1;
	}

	fds[0].fd = fd;
	fds[0].events = POLLIN;

	while(1) {
		if((poll(fds, 1, timeout_ms) == 1) && (fds[0].revents & POLLIN)) {
			read(fd, &val, 4);
			printf("get button : %x \n", val);
		} else {
			printf("timeout\n");
		}
	}

	close(fd);
	return 0;
}

标签:struct,int,button,key,rk3288,gpio,机制,poll
来源: https://blog.csdn.net/ch122633/article/details/115380573

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有