国外手机网站设计,可信赖的网站建设案例,海南网站建设fwlit,网站原创文章来源文章目录 一、POLL机制1、应用场景2、执行流程 二、程序1、驱动程序2、测试应用程序 三、总结 一、POLL机制
1、应用场景
使用休眠-唤醒的方式等待某个事件发生时#xff0c;有一个缺点#xff1a;等待的时间可能很久。我们可以加上一个超时时间#xff0c;这时就可以使用… 文章目录 一、POLL机制1、应用场景2、执行流程 二、程序1、驱动程序2、测试应用程序 三、总结 一、POLL机制
1、应用场景
使用休眠-唤醒的方式等待某个事件发生时有一个缺点等待的时间可能很久。我们可以加上一个超时时间这时就可以使用POLL机制。 简单理解就是 我在等待一个外设信号但 POLL机制 比 休眠-唤醒的死等 多了一个功能就是我可以设置超时时间假如超时后我的应用程序又应该去执行什么。相比之下在应用程序中会多了一些灵活性。
2、执行流程 首先我们熟悉当应用程序执行open()函数时内核会调用驱动里对应的drv_open()函数。poll()函数也差不多当应用程序执行poll()函数内核最终会调用到驱动里对应的drv_poll()函数。 但是应用程序中的poll() 和 驱动程序的drv_poll()中间的sys_poll()还会做些事情。
如上图我们从第三步开始分析基于读取按键值的情景 ①、… ②、… ③、应用程序poll()执行后进入到sys_poll()里面的程序是内核开发者完成的 ④、sys_poll()里有个for循环此时会调用驱动层里我们写的drv_poll()drv_poll()只做两件事 第一、drv_poll()要把自己这个线程挂入等待队列 wq 中但未休眠 第二、返回状态 ⑤、此时drv_poll()返回没有数据则sys_poll()进入到else分支开始休眠若休眠时间超过指定时间则回到for循环开头再次调用drv_poll()drv_poll()依旧返回没数据但目前已经超时sys_poll()开始返回到应用层应用层收到的结果是超时 ⑥、假如在休眠时按键按下且数据被记录按键中断程序会唤醒线程 ⑦、此时sys_poll()重新回到for循环开头再次调用drv_poll()drv_poll()知道有按键按下了则返回有数据⑧、sys_poll()随后经过if判断后返回到应用层应用层收到的结果是有数据 最后应用程序中收到poll()返回的有数据才会进一步调用read()函数读取按键值。
二、程序
1、驱动程序
#include linux/module.h
#include linux/poll.h#include linux/fs.h
#include linux/errno.h
#include linux/miscdevice.h
#include linux/kernel.h
#include linux/major.h
#include linux/mutex.h
#include linux/proc_fs.h
#include linux/seq_file.h
#include linux/stat.h
#include linux/init.h
#include linux/device.h
#include linux/tty.h
#include linux/kmod.h
#include linux/gfp.h
#include linux/gpio/consumer.h
#include linux/platform_device.h
#include linux/of_gpio.h
#include linux/of_irq.h
#include linux/interrupt.h
#include linux/irq.h
#include linux/slab.hstruct gpio_key{int gpio;struct gpio_desc *gpiod;int flag;int irq;
} ;static struct gpio_key *gpio_keys_100ask;/* 主设备号 */
static int major 0;
static struct class *gpio_key_class;/* 环形缓冲区 */
#define BUF_LEN 128
static int g_keys[BUF_LEN];
static int r, w;#define NEXT_POS(x) ((x1) % BUF_LEN)static int is_key_buf_empty(void)
{return (r w);
}static int is_key_buf_full(void)
{return (r NEXT_POS(w));
}static void put_key(int key)
{if (!is_key_buf_full()){g_keys[w] key;w NEXT_POS(w);}
}static int get_key(void)
{int key 0;if (!is_key_buf_empty()){key g_keys[r];r NEXT_POS(r);}return key;
}static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);/* 实现对应的open/read/write等函数填入file_operations结构体 */
static ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{//printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);int err;int key;wait_event_interruptible(gpio_key_wait, !is_key_buf_empty());key get_key();err copy_to_user(buf, key, 4);return 4;
}static unsigned int gpio_key_drv_poll(struct file *fp, poll_table * wait)
{printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);poll_wait(fp, gpio_key_wait, wait);return is_key_buf_empty() ? 0 : POLLIN | POLLRDNORM;
}/* 定义自己的file_operations结构体 */
static struct file_operations gpio_key_drv {.owner THIS_MODULE,.read gpio_key_drv_read,.poll gpio_key_drv_poll,
};static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{struct gpio_key *gpio_key dev_id;int val;int key;val gpiod_get_value(gpio_key-gpiod);printk(key %d %d\n, gpio_key-gpio, val);key (gpio_key-gpio 8) | val;put_key(key);wake_up_interruptible(gpio_key_wait);return IRQ_HANDLED;
}/* 1. 从platform_device获得GPIO* 2. gpioirq* 3. request_irq*/
static int gpio_key_probe(struct platform_device *pdev)
{int err;struct device_node *node pdev-dev.of_node;int count;int i;enum of_gpio_flags flag;printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);count of_gpio_count(node);if (!count){printk(%s %s line %d, there isnt any gpio available\n, __FILE__, __FUNCTION__, __LINE__);return -1;}gpio_keys_100ask kzalloc(sizeof(struct gpio_key) * count, GFP_KERNEL);for (i 0; i count; i){gpio_keys_100ask[i].gpio of_get_gpio_flags(node, i, flag);if (gpio_keys_100ask[i].gpio 0){printk(%s %s line %d, of_get_gpio_flags fail\n, __FILE__, __FUNCTION__, __LINE__);return -1;}gpio_keys_100ask[i].gpiod gpio_to_desc(gpio_keys_100ask[i].gpio);gpio_keys_100ask[i].flag flag OF_GPIO_ACTIVE_LOW;gpio_keys_100ask[i].irq gpio_to_irq(gpio_keys_100ask[i].gpio);}for (i 0; i count; i){err request_irq(gpio_keys_100ask[i].irq, gpio_key_isr, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, 100ask_gpio_key, gpio_keys_100ask[i]);}/* 注册file_operations */major register_chrdev(0, 100ask_gpio_key, gpio_key_drv); /* /dev/gpio_key */gpio_key_class class_create(THIS_MODULE, 100ask_gpio_key_class);if (IS_ERR(gpio_key_class)) {printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);unregister_chrdev(major, 100ask_gpio_key);return PTR_ERR(gpio_key_class);}device_create(gpio_key_class, NULL, MKDEV(major, 0), NULL, 100ask_gpio_key); /* /dev/100ask_gpio_key */return 0;
}static int gpio_key_remove(struct platform_device *pdev)
{//int err;struct device_node *node pdev-dev.of_node;int count;int i;device_destroy(gpio_key_class, MKDEV(major, 0));class_destroy(gpio_key_class);unregister_chrdev(major, 100ask_gpio_key);count of_gpio_count(node);for (i 0; i count; i){free_irq(gpio_keys_100ask[i].irq, gpio_keys_100ask[i]);}kfree(gpio_keys_100ask);return 0;
}static const struct of_device_id ask100_keys[] {{ .compatible 100ask,gpio_key },{ },
};/* 1. 定义platform_driver */
static struct platform_driver gpio_keys_driver {.probe gpio_key_probe,.remove gpio_key_remove,.driver {.name 100ask_gpio_key,.of_match_table ask100_keys,},
};/* 2. 在入口函数注册platform_driver */
static int __init gpio_key_init(void)
{int err;printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);err platform_driver_register(gpio_keys_driver); return err;
}/* 3. 有入口函数就应该有出口函数卸载驱动程序时就会去调用这个出口函数* 卸载platform_driver*/
static void __exit gpio_key_exit(void)
{printk(%s %s line %d\n, __FILE__, __FUNCTION__, __LINE__);platform_driver_unregister(gpio_keys_driver);
}/* 7. 其他完善提供设备信息自动创建设备节点 */
module_init(gpio_key_init);
module_exit(gpio_key_exit);MODULE_LICENSE(GPL);2、测试应用程序
#include sys/types.h
#include sys/stat.h
#include fcntl.h
#include unistd.h
#include stdio.h
#include string.h
#include poll.h/** ./button_test /dev/100ask_button0**/
int main(int argc, char **argv)
{int fd;int val;struct pollfd fds[1];int timeout_ms 5000;int ret;/* 1. 判断参数 */if (argc ! 2) {printf(Usage: %s dev\n, argv[0]);return -1;}/* 2. 打开文件 */fd open(argv[1], O_RDWR);if (fd -1){printf(can not open file %s\n, argv[1]);return -1;}fds[0].fd fd;fds[0].events POLLIN;while (1){/* 3. 读文件 */ret poll(fds, 1, timeout_ms);if ((ret 1) (fds[0].revents POLLIN)){read(fd, val, 4);printf(get button : 0x%x\n, val);}else{printf(timeout\n);}}close(fd);return 0;
}三、总结
1、以上专业术语或名词解释有个人理解感谢指点纠错