HDF驱动框架探路:Linux总线机制imx6ull驱动sr501红外传感器

系统 Linux
总线是处理器与一个或者多个设备之间的通道。在设备模型中,所有的设备都通过总线相连。甚至是那些内部的虚拟“平台”总线。总线可以相互插入,比如一个USB控制器通常是一个PCI设备。

[[441647]]

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

老规矩还是将最终希望跑出来的效果放出来。如下:

HDF驱动框架探路(六):linux总线机制imx6ull驱动sr501红外传感器-鸿蒙HarmonyOS技术社区

HDF驱动框架探路6:

前言

上一篇文章中最后在操作led灯的硬件时候,我们是直接读的原理图,去操作的寄存器,这种情况是我们绝大多数情况下会这样子进行操作,而本章我们的核心重点是使用总线机制,也就是通过修改设备树的方法来操作硬件。

本章框架图

HDF驱动框架探路(六):linux总线机制imx6ull驱动sr501红外传感器-鸿蒙HarmonyOS技术社区

总线框架图

HDF驱动框架探路(六):linux总线机制imx6ull驱动sr501红外传感器-鸿蒙HarmonyOS技术社区

涉及到的概念介绍

1.总线的概念

总线是处理器与一个或者多个设备之间的通道。在设备模型中,所有的设备都通过总线相连。甚至是那些内部的虚拟“平台”总线。总线可以相互插入,比如一个USB控制器通常是一个PCI设备。设备模型展示了总线和它们所控制的设备之间的连接。

驱动程序

1.1 总线的使用逻辑首先调用

为了正确地注册到内核,所有的platform驱动程序都必须创建的主要结构体是struct platform_driver结构体。该结构体由许多回调函数和变量组成,向platform描述了platform驱动程序。所以我们需要写一个struct platform_driver结构体。然后在其中有两个比较重要的回调函数:probe和remove

  • int (*probe)(…)指向platform驱动程序中的探测函数的指针。当platform核心有一个它认为驱动程序需要控制的struct platform_device时,就会调用该函数。所以我们会在这个函数中写驱动程序的逻辑。
  • int (*remove)(…)指向一个移除函数的指针,当struct platform_device被从系统中移除,或者platform驱动程序正在从内核中卸载时,platform核心调用该函数。

为了把struct platform_driver注册到platform核心中,需要调用以struct platform_driver为参数的platform_driver_register函数。通常在platform驱动程序的模块化代码中完成该工程。

当platform驱动程序将要被卸载的时候,需要把struct platform_driver从内核注销。这是通过调用platform_driver_unregister完成的。

1.2 完整实现代码

#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.h> 
#include <linux/fcntl.h> 
#include <linux/timer.h> 
#include <linux/workqueue.h> 
#include <asm/current.h> 
static int major; 
static struct class *sr501_class; 
static struct gpio_desc *sr501_gpio; 
static int irq; 
static int sr501_data = 0;   
static wait_queue_head_t sr501_wq; 
/* 实现对应的open/read/write等函数,填入file_operations结构体                   */ 
static ssize_t sr501_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset) 

#if 0 
    int val; 
    int len = (size < 4)? size : 4; 
     
    val = gpiod_get_value(sr501_gpio);  
    copy_to_user(buf, &val, len); 
    return len; 
#else 
    int val; 
    int len = (size < 4)? size : 4; 
    /* 1. 有数据就copy_to_uesr */ 
    /* 2. 无数据就休眠:  放入某个链表 */ 
    wait_event_interruptible(sr501_wq, sr501_data);  
    copy_to_user(buf, &sr501_data, len); 
    sr501_data = 0; 
    return len; 
#endif 

 
static unsigned int sr501_drv_poll(struct file *fp, poll_table * wait) 

    return 0; 

/* 定义自己的file_operations结构体                                              */ 
static struct file_operations sr501_fops = { 
    .owner   = THIS_MODULE, 
    .read    = sr501_drv_read, 
    .poll    = sr501_drv_poll, 
}; 
static irqreturn_t sr501_isr(int irq, void *dev_id) 

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
    /* 1. 记录数据 */ 
    sr501_data = 1; 
    /* 2. 唤醒APP:去同一个链表把APP唤醒 */ 
    wake_up(&sr501_wq);  
    return IRQ_HANDLED; 

/* 1. 从platform_device获得GPIO 
 * 2. gpio=>irq 
 * 3. request_irq 
 */ 
static int sr501_probe(struct platform_device *pdev) 

    /* 1. 获得硬件信息 */ 
    sr501_gpio = gpiod_get(&pdev->dev, NULL, 0); 
    gpiod_direction_input(sr501_gpio); 
    irq = gpiod_to_irq(sr501_gpio); 
    request_irq(irq, sr501_isr, IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING, "sr501"NULL); 
    /* 注册file_operations    */ 
    major = register_chrdev(0, "sr501", &sr501_fops);   
 
    sr501_class = class_create(THIS_MODULE, "sr501_class"); 
    if (IS_ERR(sr501_class)) { 
        printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
        unregister_chrdev(major, "sr501"); 
        return PTR_ERR(sr501_class); 
    } 
    /* 2. device_create */ 
    device_create(sr501_class, NULL, MKDEV(major, 0), NULL"sr501"); 
 
    return 0; 

static int sr501_remove(struct platform_device *pdev) 

    device_destroy(sr501_class, MKDEV(major, 0)); 
    class_destroy(sr501_class); 
    unregister_chrdev(major, "sr501"); 

static const struct of_device_id qzk_sr501[] = { 
    { .compatible = "qzk,sr501" }, 
    { }, 
}; 
/* 1. 定义platform_driver */ 
static struct platform_driver sr501s_driver = { 
    .probe      = sr501_probe, 
    .remove     = sr501_remove, 
    .driver     = { 
        .name   = "qzk_sr501"
        .of_match_table = qzk_sr501, 
    }, 
}; 
/* 2. 在入口函数注册platform_driver */ 
static int __init sr501_init(void) 

    int err; 
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
    init_waitqueue_head(&sr501_wq); 
    err = platform_driver_register(&sr501s_driver);  
    return err; 

/* 3. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数 
 *     卸载platform_driver 
 */ 
static void __exit sr501_exit(void) 

    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__); 
 
    platform_driver_unregister(&sr501s_driver); 

/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */ 
 
module_init(sr501_init); 
module_exit(sr501_exit); 
MODULE_LICENSE("GPL"); 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.

2.修改设备树

2.1 修改思路

在上述驱动中of_device_id结构体中的.compatible = “qzk,sr501”,所以我们需要将设备树中添加一个这样的设备节点

2.2 修改代码如下:

Linux-4.9.88/arch/arm/boot/dts/100ask_imx6ull-14x14.dts

HDF驱动框架探路(六):linux总线机制imx6ull驱动sr501红外传感器-鸿蒙HarmonyOS技术社区

3.应用侧测试程序

3.1 测试程序逻辑

测试程序只需要不断的去read,等到中断收到返回时,就会获得返回。

3.2 测试程序完成实现代码

#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <unistd.h> 
#include <stdio.h> 
#include <string.h> 
 
int main(int argc, char **argv) 

    int fd; 
    int val; 
    fd = open("/dev/sr501", O_RDWR); 
    if (fd == -1) 
    { 
        printf("can not open file %s\n", argv[1]); 
        return -1; 
    } 
 
    while (1) 
    { 
        read(fd, &val, 4); 
        if (val == 0x1) { 
            printf("detect people\n"); 
        } 
    } 
     
    close(fd); 
     
    return 0; 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.

4.编译

4.1 配置交叉编译工具链

回到源码根目录执行source env.sh配置编译工具链

4.2 重新生成内核设备树

进入源码目录下的Linux-4.9.88文件夹后执行make dtbs重新生成设备树

4.3 将重新生成的设备树放入开发板的boot目录

cp 100ask_imx6ull-14x14.dtb /boot

4.4 make编译驱动程序

KERN_DIR =  ~/imx6ullpro/Linux-4.9.88 # 板子所用内核源码的目录 
all
    make -C $(KERN_DIR) M=`pwd` modules 
    $(CROSS_COMPILE)gcc -o sr501_test sr501_test.c 
clean: 
    make -C $(KERN_DIR) M=`pwd` modules clean 
    rm -rf modules.order 
obj-m += sr501_drv.o 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

 直接执行make脚本执行上述Makefile生成测试程序和驱动ko文件

4.5 加载驱动程序

insmod sr501_drv.ko 
  • 1.

4.6 执行测试程序

./sr501_test 
  • 1.

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-12-15 15:30:38

鸿蒙HarmonyOS应用

2023-03-16 15:18:16

2021-11-22 16:46:59

鸿蒙HarmonyOS应用

2023-03-20 16:05:49

HDF传感器驱动开发

2021-11-30 14:52:41

鸿蒙HarmonyOS应用

2021-09-07 15:48:28

鸿蒙HarmonyOS应用

2021-05-24 14:28:34

鸿蒙HarmonyOS应用

2023-03-22 09:23:53

I2C总线温度传感器

2021-11-26 15:34:27

鸿蒙HarmonyOS应用

2021-05-20 11:13:22

Linux红外文件

2022-09-30 13:50:07

设备开发鸿蒙

2021-12-15 10:02:25

鸿蒙HarmonyOS应用

2017-11-16 14:46:58

Linuxplatform总线驱动设备

2023-05-30 14:58:05

智能开发鸿蒙

2022-08-08 19:35:37

HDF驱动开发鸿蒙

2022-04-20 20:28:40

HDF 驱动框架鸿蒙操作系统

2017-02-10 15:32:47

2020-10-16 09:47:34

鸿蒙Liteos-a移植

2023-05-06 15:41:00

人体传感器数据鸿蒙

2009-08-04 10:46:04

点赞
收藏

51CTO技术栈公众号