Linux驱动入门(五)阻塞方式实现按键驱动

copy from :https://blog.csdn.net/weixin_42462202/article/details/100011481

文章目录
Linux驱动入门(五)阻塞方式实现按键驱动
一、阻塞方式实现按键驱动思路
二、内核的等待队列
三、注册中断
四、源码
五、测试

本文目标:实现一个阻塞的按键驱动程序,当应用调用read函数读取时阻塞等待,直到按键按下才放回
一、阻塞方式实现按键驱动思路
上一篇文章讲解非阻塞的方式实现按键驱动,而如果以非阻塞方式实现驱动的话,应用层就需要轮询地读取,这无疑是非常耗费cpu的,本文将修改上一篇文章的驱动程序,实现一个阻塞读取的按键驱动程序,下面开始将阻塞方式的实现思路

如果应用调用read函数要阻塞,那么说明驱动程序的button_read也要阻塞,那么谁来唤醒?

答案是中断程序,通过向内核注册中断,当检测到按键按下时,就触发中断,中断程序读取按键的值,并唤醒button_read所阻塞的进程,button_read在放回结果给应用

关于如何阻塞和如何注册中断,下面将一一介绍

二、内核的等待队列
要实现阻塞需要使用内核的等待队列,等待队列很早就作为一个基本的功能单位出现在Linux内核里了,它以队列为基础数据结构,与进程调度机制紧密结合,可以用来同步对系统资源的访问

Linux内核提供对等待队列的操作如下

定义一个”等待队列头部“

wait_queue_head_t my_queue;

初始化一个”等待队列头部“

init_waitqueue_head(&my_queue);

Linux内核提供一个更加便捷的宏定义,定义并初始化一个“等待队列头部”

DECLARE_WAIT_QUEUE_HEAD (name)

定义等待队列元素

DEFINE_WAIT(name)

添加/移除等待队列

void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);

唤醒等待队列

void wake_up(wait_queue_head_t *queue);
void wake_up_interruptible(wait_queue_head_t *queue);

wake_up可以唤醒处于TASK_UNINTERRUPTIBLE和TASK_INTERRUPTIBLE状态的进程

wake_up_interruptible只能唤醒处于TASK_INTERRUPTIBLE状态的进程

下面是一个使用等待队列阻塞等待的例子

static DECLARE_WAIT_QUEUE_HEAD (wq_head); //定义并初始化一个等待队列头
static int condition; //等待队列唤醒条件

void my_wait()
{
DEFINE_WAIT(wait); //定义一个等待队列元素

while(!condition) //当条件满足时退出
{
add_wait_queue(&wq_head, &wait); //将等待队列元素加入等待队列中

set_current_state(TASK_UNINTERRUPTIBLE); //设置当前进程状态

schedule(); //调度进程,此时进程将睡眠,等待被唤醒

/* 进程被唤醒后会再次判断条件是否满足
* 如果满足,进程唤醒
* 否则进程继续加入等待队列然后调度睡眠
*/
}

set_current_state(TASK_RUNNING); //设置进程状态
}

可以使用下面方式唤醒该进程

condition = 1;
wake_up(&wq_head);

上述我们实现的wait_event函数确实有点复杂,为了方便使用,Linux内核又提供了一套等待事件的宏定义,宏定义的实现跟我们所写的my_wait函数类似

等待事件

wait_event(queue, condition)
wait_event_interruptible(queue, condition)
wait_event_timeout(queue, condition, timeout)
wait_event_interruptible_timeout(queue, condition, timeout)

其中queue表示等待队列头,condition表示等待条件,这些宏定义的实现跟我们上面实现的my_wait函数是类似的

需要注意的是

如果使用下面宏定义等待事件

wait_event(queue, condition)
wait_event_timeout(queue, condition, timeout)

需要用下面函数唤醒

void wake_up(wait_queue_head_t *queue);

如果使用下面宏定义等待事件

wait_event_interruptible(queue, condition)
wait_event_interruptible_timeout(queue, condition, timeout)

可以使用下面函数唤醒

void wake_up(wait_queue_head_t *queue);
void wake_up_interruptible(wait_queue_head_t *queue);

下面再使用这些宏定义来实现我们的my_wait函数

static DECLARE_WAIT_QUEUE_HEAD (wq_head); //定义并初始化一个等待队列头
static int condition; //等待队列唤醒条件

void my_wait()
{
wait_event_interruptible(wq_head, condition);
}

唤醒可以使用下面方法

condition = 1;
wake_up_interruptible(&wq_head);

这样子等待队列的使用就变得非常简单了

三、注册中断
首先看Linux内核如何描述中断

内核对中断的描述

对于每款芯片,都有自己对应的一个irqs.h文件

例如S5PV210的文件位于linux/arch/arm/mach-s5pv210/include/mach/irqs.h

其中使用宏定义定义了一系列的中断

/* VIC0: System, DMA, Timer */
#define IRQ_EINT0 S5P_IRQ_VIC0(0)
#define IRQ_EINT1 S5P_IRQ_VIC0(1)
#define IRQ_EINT2 S5P_IRQ_VIC0(2)
...

/* VIC1: ARM, Power, Memory, Connectivity, Storage */
...
#define IRQ_UART3 S5P_IRQ_VIC1(13)
#define IRQ_IIC S5P_IRQ_VIC1(14)
#define IRQ_SPI0 S5P_IRQ_VIC1(15)
...

/* VIC2: Multimedia, Audio, Security */
#define IRQ_LCD0 S5P_IRQ_VIC2(0)
...
#define IRQ_FIMC0 S5P_IRQ_VIC2(5)
...

/* VIC3: Etc */
...
#define IRQ_ADC1 S5P_IRQ_VIC3(9)
...

内核注册中断的接口

内核使用request_irq注册中断

/*
* irq:中断号(irqs.h)
* irq_handler_t:中断函数 typedef irqreturn_t (*irq_handler_t)(int, void *);
* flags:中断触发条件
* dev:传递给中断函数的操作,如果不是共享中断,此参数允许为NULL
*/
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,
const char *name, void *dev)

其中typedef irqreturn_t (*irq_handler_t)(int, void *)第一个参数为中断号,第二个参数为申请中断时传递的参数

内核使用free_irq注销中断

/*
* irq:中断号(irqs.h)
* dev:申请中断时传递的参数
*/
void free_irq(unsigned int irq, void *dev);

在Linux驱动入门(四)非阻塞方式实现按键驱动中,可以看到GPIO的外部中断为外部中断2,所以可以这样注册中断

static irqreturn_t button_irq(int irq, void *data)
{

return IRQ_HANDLED;
}

/* 外部中断2,下降沿触发 */
request_irq(IRQ_EINT2, button_irq, IRQF_TRIGGER_FALLING, "button", NULL);

四、源码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>

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

static dev_t dev_id;
static struct cdev *button_dev;
static struct class *button_class;

/* 定义并初始化一个等待队列头 */
static DECLARE_WAIT_QUEUE_HEAD(button_wq_head);
static int button_conditon;
static int button_val;

static irqreturn_t button_irq(int irq, void *data)
{
/* 判断等待队列中是否有等待元素 */
if(!waitqueue_active(&button_wq_head))
return IRQ_HANDLED;

/* 读取按键值 */
button_val = gpio_get_value(S5PV210_GPH0(2));

/* 唤醒等待队列 */
button_conditon = 1;
wake_up_interruptible(&button_wq_head);

return IRQ_HANDLED;
}

static int button_open(struct inode *inode, struct file *file)
{
int ret;

ret = request_irq(IRQ_EINT2, button_irq, IRQF_TRIGGER_FALLING, "button_irq", NULL);

return 0;
}

static ssize_t button_read(struct file *file, char __user *data, size_t size, loff_t *loff)
{
int ret;
int val;

/* 睡眠等待 */
button_conditon = 0;
wait_event_interruptible(button_wq_head, button_conditon);
button_conditon = 0;

val = button_val;
ret = copy_to_user(data, &val, sizeof(val));

return sizeof(val);
}

static int button_release(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT2, NULL);

return 0;
}

static struct file_operations button_fops = {
.owner = THIS_MODULE,
.open = button_open,
.read = button_read,
.release = button_release,
};

static __init int button_init(void)
{
/* 申请设备号 */
alloc_chrdev_region(&dev_id, 1, 1, "button");

/* 分配字符设备 */
button_dev = cdev_alloc();

/* 设置字符设备 */
cdev_init(button_dev, &button_fops);

/* 注册字符设备 */
cdev_add(button_dev, dev_id, 1);

/* 创建设备节点 */
button_class = class_create(THIS_MODULE, "button"); //创建类
device_create(button_class, NULL, dev_id, NULL, "button"); //创建设备节点

gpio_request(S5PV210_GPH0(2), "button");

return 0;
}

static __exit void button_exit(void)
{
/* 注销设备节点 */
device_destroy(button_class, dev_id);
class_destroy(button_class);

/* 注销字符设备 */
cdev_del(button_dev);
kfree(button_dev);

/* 注销注册的设备号 */
unregister_chrdev_region(dev_id, 1);

gpio_free(S5PV210_GPH0(2));
}

module_init(button_init);
module_exiMODULE_LICENSE("GPL");


五、测试
将上面驱动程序保存为button_drv.c,执行Makefile,生成button_drv.ko

加载模块,此时生成设备节点/dev/button

应用测试程序

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

#define BUTTON_DEV "/dev/button"

int main(int argc, char* argv[])
{
int val;

int fd = open(BUTTON_DEV, O_RDONLY);
if(fd < 0)
{
printf("failed to open %s ", BUTTON_DEV);
return -1;
}

while(1)
{
read(fd, &val, sizeof(val));
printf("read return ");

if(val == 0)
{
printf("button press ");
}
}


close(fd);

return 0;
}

编译运行

效果是只有当按键按下的时候,read函数才会返回
————————————————
版权声明:本文为CSDN博主「JT同学」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42462202/article/details/100011481

Always Believe Something Beauitful Will Be Happen
原文地址:https://www.cnblogs.com/Oude/p/12456470.html