跳转至

09 设备IO访问技术

笔记说明

这一节,将介绍休眠和唤醒、POLL机制、异步通知、阻塞和非阻塞。这一部分的内容在树莓派上实现的方式,和韦东山讲的在IMX6ULL上实现的方式就没什么不同了。我也只是把韦东山的我觉得重要的部分提取了出来,方便自己查询。

主要参考的文章

韦东山《01_嵌入式Linux应用开发完全手册V5.1_IMX6ULL_Pro开发板》

本节源码路径02_Firmware/09_key_drv_irq_sleep_wakeup02_Firmware/10_key_drv_irq_poll02_Firmware/11_key_drv_irq_fasync02_Firmware/12_key_drv_irq_block

休眠和唤醒

休眠感觉就是一种阻塞IO的用法。

首先注册一个唤醒等待队列

static DECLARE_WAIT_QUEUE_HEAD(gpio_key_wait);

在read函数中,调用read的时候就等待唤醒,当g_key为真的时候才会执行read相关任务

/* 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    int err;
    wait_event_interruptible(gpio_key_wait, g_key);
    err = copy_to_user(buf, &g_key, 4);
    g_key = 0;

    return 4;
}

在中断服务的处理中,触发中断,就执行唤醒API,这样,就实现了休眠和唤醒

static irqreturn_t gpio_key_isr(int irq, void *dev_id)
{
    key_gpio_t *gpio_key = dev_id;
    mod_timer(&gpio_key->key_timer, jiffies + HZ/10);    
    return IRQ_RETVAL(IRQ_HANDLED);
}

static void key_timer_expire(struct timer_list *t)
{
    /* data ==> gpio */
    key_gpio_t *gpio_key = from_timer(gpio_key, t, key_timer);
    int val;

    val = gpiod_get_value(gpio_key->key_gpiod);
    printk("key %d %d\n", gpio_key->key_num, val);  
    g_key = (gpio_key->key_num  << 8) | val;
    wake_up_interruptible(&gpio_key_wait);
}

实验测试文件如下

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

/*
 * sudo ./key_drv_irq_app
 *
 */
int main(int argc, char **argv)
{
    int fd;
    int val;

    /* 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;
    }

    while (1)
    {
        /* 3. 读文件 */
        read(fd, &val, 4);
        printf("get button : 0x%x\n", val);     

    }

    close(fd);

    return 0;
}

POLL机制

他的原理就是隔一段时间去看一下有没有数据。超时就返回一个超时标志。

添加一个POLL函数就可以了。重要的是把当前线程放入队列。

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 g_key ? (POLLIN | POLLRDNORM) : 0;
}

/* 定义自己的file_operations结构体                                              */
static struct file_operations gpio_key_drv = {
    .owner   = THIS_MODULE,
    .read    = gpio_key_drv_read,
    .poll    = gpio_key_drv_poll,
};

然后实验文件修改如下

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

/*
 * sudo ./key_drv_irq_app /dev/key_drv
 *
 */
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;
}

异步通知

感觉异步通知就像主动触发一个函数执行。

使用异步通知的时候,需要提供相应的.fasync的函数,来实现获得异步通知在应用层设置的相关参数,异步通知相关代码如下。

struct fasync_struct *button_fasync;

static int gpio_key_drv_fasync(int fd, struct file *file, int on)
{
    if (fasync_helper(fd, file, on, &button_fasync) >= 0)
        return 0;
    else
        return -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,
};

这个里面的fasync_helper函数的作用,会分配构造一个 fasync_struct 结构体 button_async。该结构体,

//在驱动文件的 flag 被设置为 FAYNC 时
button_async->fa_file = filp; // filp 表示驱动程序文件,里面含有之前设置的 PID
//驱动文件被设置为非 FASYNC 时
button_async->fa_file = NULL;

以后想发送信号时,使用 button_async 作为参数就可以,它里面“可能”含有 PID。

然后我们就可以在例如中断服务函数中发送信号。

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

    printk("key %d %d\n", gpio_key->gpio, val); 
    g_key = (gpio_key->gpio << 8) | val;
    wake_up_interruptible(&gpio_key_wait);
    kill_fasync(&button_fasync, SIGIO, POLL_IN);
    return IRQ_HANDLED;
}

应用编程一般有以下几步

1.编写信号处理函数

2.注册信号处理函数

3.打开驱动

4.把进程 PID 告诉驱动

5.使能驱动的 FASYNC 功能

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

static int fd;
//1.编写信号处理函数
static void sig_func(int sig)
{
    int val;
    read(fd, &val, 4);
    printf("get button : 0x%x\n", val);
}

/*
 * sudo ./key_drv_irq_app /dev/key_drv
 *
 */
int main(int argc, char **argv)
{
    int val;
    struct pollfd fds[1];
    int timeout_ms = 5000;
    int ret;
    int flags;

    if (argc != 2) 
    {
        printf("Usage: %s <dev>\n", argv[0]);
        return -1;
    }
    //2.注册信号处理函数
    signal(SIGIO, sig_func);
    //3.打开驱动
    fd = open(argv[1], O_RDWR);
    if (fd == -1)
    {
        printf("can not open file %s\n", argv[1]);
        return -1;
    }
    //4.把进程 PID 告诉驱动
    fcntl(fd, F_SETOWN, getpid());
    //5.使能驱动的 FASYNC 功能
    flags = fcntl(fd, F_GETFL);
    fcntl(fd, F_SETFL, flags | FASYNC);

    while (1)
    {
        printf("www.100ask.net \n");
        sleep(2);
    }

    close(fd);

    return 0;
}

阻塞和非阻塞

在read函数中添加阻塞标志位判断即可

/* 实现对应的open/read/write等函数,填入file_operations结构体 */
static ssize_t gpio_key_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    int err;

    if ((g_key == 0) && (file->f_flags & O_NONBLOCK))
        return -EAGAIN;

    wait_event_interruptible(gpio_key_wait, g_key);
    err = copy_to_user(buf, &g_key, 4);
    g_key = 0;

    return 4;
}

应用代码

通过设置文件的阻塞或者非阻塞方式来达到检测

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

static int fd;

/*
 * sudo ./key_drv_irq_app /dev/key_drv
 *
 */
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 <dev>\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);
        else
            printf("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);
        else
            printf("while get button: -1\n");
    }

    close(fd);

    return 0;
}