Linux驱动开发基础__阻塞与非阻塞
创始人
2024-05-23 00:30:13
0

目录

1 引入

2 应用编程

3 驱动编程

4 代码

4.1 gpio_key_drv.c

4.2 button_test.c


1 引入

所谓阻塞,就是等待某件事情发生。比如调用 read 读取按键时,如果没有按键数据则 read 函数不会返回,它会让线程休眠等待。 
使用 poll 时,如果传入的超时时间不为 0,这种访问方法也是阻塞的。 
使用 poll 时,可以设置超时时间为 0,这样即使没有数据它也会立刻返回,这就是非阻塞方式。能不能让 read 函数既能工作于阻塞方式,也可以工作于非阻塞方式?可以! 
APP 调用 open 函数时,传入 O_NONBLOCK,就表示要使用非阻塞方式;默认是阻塞方式。 
注意:对于普通文件、块设备文件,O_NONBLOCK 不起作用。 
注 意 : 对 于 字 符 设 备 文 件 , O_NONBLOCK 起 作 用 的 前 提 是 驱 动 程 序 针 O_NONBLOCK 做了处理。 
只能在 open 时表明 O_NONBLOCK 吗?在 open 之后,也可以通过 fcntl 修改为阻塞或非阻塞。 

2 应用编程

open 时设置: 

int  fd = open(“/dev/xxx”, O_RDWR | O_NONBLOCK);  /* 非阻塞方式 */ 
int  fd = open(“/dev/xxx”, O_RDWR );  /* 阻塞方式 */ 

open 之后设置: 

int flags = fcntl(fd, F_GETFL); 
fcntl(fd, F_SETFL, flags | O_NONBLOCK);  /* 非阻塞方式 */ 
fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);  /* 阻塞方式 */ 

 3 驱动编程

以 drv_read 为例: 

static ssize_t drv_read(struct file *fp, char __user *buf, size_t count, loff_t *ppo
s) 
{   
if (queue_empty(&as->queue) && fp->f_flags & O_NONBLOCK) return -EAGAIN; wait_event_interruptible(apm_waitqueue, !queue_empty(&as->queue)); …… 
} 

从驱动代码也可以看出来,当 APP 打开某个驱动时,在内核中会有一个struct  file 结构体对应这个驱动,这个结构体中有 f_flags,就是打开文件时的标记位;可以设置 f_flasgs 的 O_NONBLOCK 位,表示非阻塞;也可以清除这个位表示阻塞。 
驱动程序要根据这个标记位决定事件未就绪时是休眠和还是立刻返回。 

4 代码

4.1 gpio_key_drv.c

#include 
#include #include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include struct 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;struct fasync_struct *button_fasync;#define NEXT_POS(x) ((x+1) % 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;if (is_key_buf_empty() && (file->f_flags & O_NONBLOCK))return -EAGAIN;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;
}static int gpio_key_drv_fasync(int fd, struct file *file, int on)
{if (fasync_helper(fd, file, on, &button_fasync) >= 0)return 0;elsereturn -EIO;
}/* 定义自己的file_operations结构体                                              */
static struct file_operations gpio_key_drv = {.owner	 = THIS_MODULE,.read    = gpio_key_drv_read,.poll    = gpio_key_drv_poll,.fasync  = gpio_key_drv_fasync,
};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);kill_fasync(&button_fasync, SIGIO, POLL_IN);return IRQ_HANDLED;
}/* 1. 从platform_device获得GPIO* 2. gpio=>irq* 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 isn't 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");

4.2 button_test.c


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include static int fd;/** ./button_test /dev/100ask_button0**/
int main(int argc, char **argv)
{int val;struct pollfd fds[1];int timeout_ms = 5000;int ret;int	flags;int i;/* 1. 判断参数 */if (argc != 2) {printf("Usage: %s \n", argv[0]);return -1;}/* 2. 打开文件 */fd = open(argv[1], O_RDWR | O_NONBLOCK);if (fd == -1){printf("can not open file %s\n", argv[1]);return -1;}for (i = 0; i < 10; i++) {if (read(fd, &val, 4) == 4)printf("get button: 0x%x\n", val);elseprintf("get button: -1\n");}flags = fcntl(fd, F_GETFL);fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);while (1){if (read(fd, &val, 4) == 4)printf("get button: 0x%x\n", val);elseprintf("while get button: -1\n");}close(fd);return 0;
}

相关内容

热门资讯

可悲乎,可喜乎?【优质5篇】 可悲乎,可喜乎? 篇一近年来,社会上普遍存在着一种现象:人们对于生活中的不公平感到愤怒和失望,但又常...
争做当代活雷锋初中作文(推荐... 篇一:争做当代活雷锋雷锋是中国近代历史上一位备受敬仰的人物,他无私奉献、乐于助人的精神感动了无数人。...
初一学生作文【优选6篇】 初一学生作文 篇一:我的暑假生活暑假终于来了,我迫不及待地计划着如何度过这个美好的假期。在这个特殊的...
我最喜欢的动物初一英语作文【... 我最喜欢的动物初一英语作文 篇一My Favorite Animal - CatsCats have...
事实的力量初中抒情作文【通用... 事实的力量初中抒情作文 篇一事实的力量每个人都有属于自己的故事,故事中蕴含着真实的事实。这些事实或让...
最新辞旧迎新作文【精选6篇】 最新辞旧迎新作文 篇一辞旧迎新,迎接新的一年,是一个让人充满期待和激动的时刻。回首过去的一年,我们或...
绿水青山就是金山银山作文60... 绿水青山就是金山银山作文600字 篇一绿水青山就是金山银山绿水青山就是金山银山,这句话深深地激励着我...
开一朵花作文700字_初一作... 开一朵花作文700字_初一作文 篇一开一朵花春天是一个美丽的季节,大地上的一切都在复苏,花朵开始盛开...
初中入团申请书(精彩6篇) 初中入团申请书 篇一尊敬的团组织:您好!我是一名热爱学习、积极向上的初中生,我怀着激动的心情向您递交...
初一·新学期作文【实用6篇】 初一·新学期作文 篇一新学期,新气象新学期又开始了,我怀着激动的心情迎接着这个全新的挑战。这个学期,...
初一的开学第一天作文400字... 初一的开学第一天作文400字 篇一初一的开学第一天,我迫不及待地来到了学校。整个校园洋溢着喜庆的气氛...
一件小事500作文初一共41... 一件小事500作文初一 第一篇中国的文字博大精深,一不小心就会闹出”说别人就是说自己“的笑话。我就曾...
写给初一的自己作文500字【... 写给初一的自己作文500字 篇一亲爱的初一的我:你好!你现在已经进入初中生活,迈入了一个新的阶段。回...
我的爸爸作文(精选6篇) 我的爸爸作文 篇一爸爸,你是我心中最伟大的人我的爸爸是一个平凡而又伟大的人。他有着坚韧的意志和无私的...
背影初中作文(优选6篇) 背影初中作文 篇一背影初中作文在生活中,我们都会遇到许多人,其中有些人会给我们留下深刻的印象。而在我...
墙的故事中考作文【经典3篇】 墙的故事中考作文 篇一墙的故事中考作文墙,是我们生活中常见的存在,无论是家庭住宅的分隔墙,还是城市的...
我最讨厌的人500字初一作文... 我最讨厌的人500字初一作文 第一篇从小到大,不知不觉间,近5000天的日子,悄悄从我们指间溜走。在...
那些人那些事初一作文(精彩3... 篇一:那些人那些事初一作文初中生活就像一部精彩的电影,里面有各种各样的人物和故事。在这个阶段,我遇到...
你是我最敬佩的人中考作文(精... 你是我最敬佩的人中考作文 篇一我最敬佩的人是我的父亲。他是一个平凡的农民,但他的坚韧、勤劳和乐观的精...
中考高考英语作文范文模板【推... 中考高考英语作文范文模板 篇一标题:The Importance of Time Managemen...