驱动开发 —— 文件IO模型(阻塞/非阻塞)

  我们使用中断的目的,就是为了在中断发生时,才去读操作,避免像查询一样一直read,从而占据大量的CPU。

  一、阻塞: 当进程在读取外部设备的资源(数据),资源没有准备好,进程就会休眠

    linux应用中,大部分的函数接口都是阻塞 scanf(); read(); write(); accept();

  休眠读取:

程序设计目的:App去读取按键值,如果有按键中断触发(键值有改变)则打印,否则休眠.

如上框图所示:

  在main函数中,进入while(1)死循环之后,执行read操作,

  若按键值更新,则读取键值

  若未更新,则进入休眠并等待更新,更新后,唤醒进程。

如何设置休眠机制?

1,将当前进程加入到等待队列头中
        add_wait_queue(wait_queue_head_t * q, wait_queue_t * wait)
2,将当前进程状态设置成(可接受中断信号)TASK_INTERRUPTIBLE
        set_current_state(TASK_INTERRUPTIBLE)
3,让出调度--休眠
        schedule(void)

更加智能方便的接口,可以实现以上功能:
wait_event_interruptible(wq, condition);

在驱动中如何写阻塞代码:

1,等待队列头
      wait_queue_head_t
    
      init_waitqueue_head(wait_queue_head_t *q);//初始化队列头
    
2,在需要等待(没有数据)的时候,进行休眠
        wait_event_interruptible(wait_queue_head_t wq, condition) // 内部会构建一个等待队列项/节点wait_queue_t
        参数1: 等待队列头
        参数2: 条件,如果是为假,就会等待,如果为真,就不会等待
                可以用一标志位,来表示是否有数据


3,在一个合适的时候(有数据),会将进程唤醒
        wake_up_interruptible(wait_queue_head_t *q)

        用法:
            wake_up_interruptible(&key_dev->wq_head);
            //同时设置标志位
            key_dev->key_state  = 1;

代码示例:

  1 #include <linux/init.h>
  2 #include <linux/module.h>
  3 #include <linux/of.h>
  4 #include <linux/of_irq.h>
  5 #include <linux/interrupt.h>
  6 #include <linux/slab.h>
  7 #include <linux/fs.h>
  8 #include <linux/device.h>
  9 #include <linux/kdev_t.h>
 10 #include <linux/err.h>
 11 #include <linux/device.h>
 12 #include <asm/io.h>
 13 #include <asm/uaccess.h>
 14 #include <linux/wait.h>
 15 #include <linux/sched.h>
 16 
 17 
 18 
 19 #define GPXCON_REG 0X11000C20   //不可以从数据寄存器开始映射,要配置寄存器
 20 #define KEY_ENTER  28
 21 
 22 //0、设计一个描述按键的数据的对象
 23 struct key_event{
 24     int code;    //按键类型:home,esc,enter
 25     int value;   //表状态,按下,松开
 26 };
 27 
 28 //1、设计一个全局对象——— 描述key的信息
 29 struct key_desc{
 30     unsigned int dev_major;
 31     int irqno;  //中断号
 32     struct class  *cls;
 33     struct device *dev;
 34     void *reg_base;
 35     struct key_event event;
 36     wait_queue_head_t wq_head;
 37     int key_state;   //表示是否有数据
 38 };
 39 
 40 struct key_desc *key_dev;
 41 
 42 
 43 irqreturn_t key_irq_handler(int irqno, void *devid)
 44 {
 45     printk("----------%s---------",__FUNCTION__);
 46 
 47     int value;
 48     //读取按键状态
 49     value = readl(key_dev->reg_base + 4) & (0x01<<2);
 50     
 51     if(value){
 52         printk("key3 up\n");
 53         key_dev->event.code  = KEY_ENTER;
 54         key_dev->event.value = 0;
 55     }else{
 56         printk("key3 down\n");
 57         key_dev->event.code  = KEY_ENTER;
 58         key_dev->event.value = 1;
 59     }
 60 
 61     //表示有数据,唤醒等待队列中的等待项
 62     wake_up_interruptible(&key_dev->wq_head);
 63     
 64     //同时设置标志位,表示有数据
 65     key_dev->key_state = 1;
 66     
 67     return IRQ_HANDLED;
 68 }
 69 
 70 
 71 //获取中断号
 72 int get_irqno_from_node(void)
 73 {
 74     int irqno;
 75     //获取设备树中的节点
 76     struct device_node *np = of_find_node_by_path("/key_int_node");
 77     if(np){
 78         printk("find node success\n");
 79     }else{
 80         printk("find node failed\n");
 81     }
 82 
 83     //通过节点去获取中断号
 84     irqno = irq_of_parse_and_map(np, 0);
 85     printk("iqrno = %d",key_dev->irqno);
 86 
 87     return irqno;
 88 }
 89 
 90 ssize_t key_drv_read (struct file * filp, char __user * buf, size_t count, loff_t * fops)
 91 {
 92     //printk("----------%s---------",__FUNCTION__);
 93     int ret;
 94 
 95     //在没有数据时,进行休眠
 96         //key_state在zalloc初始化空间后,为0,则阻塞
 97     wait_event_interruptible(key_dev->wq_head, key_dev->key_state);
 98     
 99     ret = copy_to_user(buf, &key_dev->event, count);
100     if(ret > 0)
101     {
102         printk("copy_to_user error\n");
103         return -EFAULT;
104     }
105 
106     
107     //传递给用户数据后,将数据清除,否则APP每次读都是第一次的数据
108     memset(&key_dev->event, 0, sizeof(key_dev->event));
109     key_dev->key_state = 0;
110     
111     return count;
112 }
113 
114 ssize_t key_drv_write (struct file *filp, const char __user * buf, size_t count, loff_t * fops)
115 {
116     printk("----------%s---------",__FUNCTION__);
117     return 0;
118 }
119 
120 int key_drv_open (struct inode * inode, struct file *filp)
121 {
122     printk("----------%s---------",__FUNCTION__);
123     return 0;
124 }
125 
126 int key_drv_close (struct inode *inode, struct file *filp)
127 {
128     printk("----------%s---------",__FUNCTION__);
129     return 0;
130 }
131 
132 
133 const struct file_operations key_fops = {
134     .open    = key_drv_open,
135     .read    = key_drv_read,
136     .write   = key_drv_write,
137     .release = key_drv_close,
138 
139 };
140 
141 
142 
143 static int __init key_drv_init(void)
144 {
145     //演示如何获取到中断号
146     int ret;
147     
148     //1、设定全局设备对象并分配空间
149     key_dev = kzalloc(sizeof(struct key_desc), GFP_KERNEL);  //GFP_KERNEL表正常分配内存
150                           //kzalloc相比于kmalloc,不仅分配连续空间,还会将内存初始化清零
151 
152     //2、动态申请设备号
153     key_dev->dev_major = register_chrdev(0, "key_drv", &key_fops);
154 
155     //3、创建设备节点文件
156     key_dev->cls = class_create(THIS_MODULE, "key_cls");
157     key_dev->dev = device_create(key_dev->cls, NULL, MKDEV(key_dev->dev_major, 0), NULL, "key0");
158 
159     //4、硬件初始化 -- 地址映射或中断申请    
160     
161     key_dev->reg_base = ioremap(GPXCON_REG,8);
162 
163     key_dev->irqno = get_irqno_from_node();
164     
165     ret = request_irq(key_dev->irqno, key_irq_handler, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 
166         "key3_eint10", NULL);
167     if(ret != 0)
168     {
169         printk("request_irq error\n");
170         return ret;
171     }
172 
173     //初始化等待队列头
174     init_waitqueue_head(&key_dev->wq_head);  //wait_queue_head_t *q
175     
176     
177     
178     return 0;
179 }
180 
181 static void __exit key_drv_exit(void)
182 {
183     iounmap(GPXCON_REG);
184     free_irq(key_dev->irqno, NULL);  //free_irq与request_irq的最后一个参数一致
185     device_destroy(key_dev->cls, MKDEV(key_dev->dev_major, 0));
186     class_destroy(key_dev->cls);
187     unregister_chrdev(key_dev->dev_major, "key_drv");
188     kfree(key_dev);
189 }
190 
191 
192 
193 module_init(key_drv_init);
194 module_exit(key_drv_exit);
195 
196 MODULE_LICENSE("GPL");
key_drv.c
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <unistd.h>
 5 #include <sys/types.h>
 6 #include <sys/stat.h>
 7 #include <fcntl.h>
 8 
 9 
10 #define KEY_ENTER  28
11 
12 //0、设计一个描述按键的数据的对象
13 struct key_event{
14     int code;    //按键类型:home,esc,enter
15     int value;   //表状态,按下,松开
16 };
17 
18 
19 int main(int argc, char *argv[])
20 {
21     struct key_event event;
22     int fd;
23     fd = open("/dev/key0", O_RDWR);
24     if(fd < 0)
25     {
26         perror("open");
27         exit(1);
28     }
29 
30     while(1)
31     {
32         read(fd, &event, sizeof(struct key_event));
33 
34         if(event.code == KEY_ENTER)
35         {
36             if(event.value)
37             {
38                 printf("APP__ key enter down\n");
39             }else{
40 
41                 printf("APP__ key enter up\n");
42             }
43         }
44     }
45 
46     close(fd);
47 
48     return 0;
49 }
key_test.c
 1 ROOTFS_DIR = /home/linux/source/rootfs#根文件系统路径
 2 
 3 APP_NAME = key_test
 4 MODULE_NAME = key_drv
 5 
 6 CROSS_COMPILE = /home/linux/toolchains/gcc-4.6.4/bin/arm-none-linux-gnueabi-
 7 CC = $(CROSS_COMPILE)gcc
 8 
 9 ifeq ($(KERNELRELEASE),)
10 
11 KERNEL_DIR = /home/linux/kernel/linux-3.14-fs4412          #编译过的内核源码的路径
12 CUR_DIR = $(shell pwd)     #当前路径
13 
14 all:
15     make -C $(KERNEL_DIR) M=$(CUR_DIR) modules  #把当前路径编成modules
16     $(CC) $(APP_NAME).c -o $(APP_NAME)
17     @#make -C 进入到内核路径
18     @#M 指定当前路径(模块位置)
19 
20 clean:
21     make -C $(KERNEL_DIR) M=$(CUR_DIR) clean
22 
23 install:
24     sudo cp -raf *.ko $(APP_NAME) $(ROOTFS_DIR)/drv_module     #把当前的所有.ko文件考到根文件系统的drv_module目录
25 
26 else
27 
28 obj-m += $(MODULE_NAME).o    #指定内核要把哪个文件编译成ko
29 
30 endif
Makefile

测试:

./key_test  &  后台运行,,top查看后台进程

 在没有按键中断时,进程休眠阻塞

二、非阻塞

  在读写的时候,若没有数据,立刻返回,并且返回一个出错码

  (设计上,非阻塞方式在linux中用的比较少,因为会比较消耗资源)

 在while循环中,如果没有数据,非阻塞的方式机会一直在内核与用户空间返回出错码,消耗资源,和没有休眠的方式差不多。

用户空间:
open("/dev/key0", O_RDWR|O_NONBLOCK);
//将fd设置为非阻塞方式,后续的IO操作都是基于非阻塞的fd
------------------------------------
内核空间:
驱动中需要去区分,当前模式是阻塞还是非阻塞
//如果当前是非阻塞模式,并且没有数据,立马返回一个出错码
ssize_t key_drv_read (struct file *filep, char __user *buf, size_t count, loff_t *foops)
{
    if(filp->f_flags & O_NONBLOCK && !key_dev->key_state)
        return -EAGAIN;
}
    //如果为阻塞方式 或者 非阻塞下有数据,都不满足判断,会执行以下代码,兼容非阻塞模式
      ....
    wait_event_interruptible    
      ....
 

猜你喜欢

转载自www.cnblogs.com/y4247464/p/12387359.html