HDF驱动框架探路(一):工欲善其事,必先利其器(linux驱动开发) 原创 精华

Mr_qzk
发布于 2021-11-19 16:50
浏览
15收藏

【本文正在参与优质创作者激励】
老规矩还是将最终希望跑出来的效果如下:
HDF驱动框架探路(一):工欲善其事,必先利其器(linux驱动开发)-鸿蒙开发者社区

@toc

前言

  • 从本文开始会为记录一个驱动小白的成长道路。当你已经具备了成熟的驱动开发经验的话,那么你可以直接跳过,本文对你毫无用处。作为一个没有接触过任何驱动开发的小白来说,想要搞清楚openharmony的HDF驱动框架的话,我大概梳理了一下,不对之处各位大佬留言指出。想要彻底搞清楚HDF框架我认为第一步必须需要搞清楚linux驱动,因为linux内核是被广泛使用的,无论android,ios,还是openharmony。因为linux内核最基础的用户态和内核态的概念最存在于各种驱动开发中的。
  • 所以有了上述的认识,正如标题所示工欲善其事,必先利其器。这个器就是首先得知道linux驱动开发。那么本文就是讲解linux驱动开发的。
  • 虽然本文是来介绍linux驱动开发的,不过我们还是先来了解一下HDF的概念,只需要知道这个概念,应为我们现在的工作都是为这个概念服务的。那么:什么是HDF驱动框架?HDF(Hardware Driver Foundation)驱动框架,为驱动开发者提供驱动框架能力,包括驱动加载、驱动服务管理和驱动消息机制。旨在构建统一的驱动架构平台,为驱动开发者提供更精准、更高效的开发环境,力求做到一次开发,多系统部署。

什么是驱动开发?

这个看似不是问题的问题却很重要,我们必须需要从这一步开始理清楚,见下图:

HDF驱动框架探路(一):工欲善其事,必先利其器(linux驱动开发)-鸿蒙开发者社区

  • 从上图中可以看出单片机裸机程序是可以直接控制硬件的,而在linux应用程序中这一招就行不通了。linux必须通过驱动程序来控制操作硬件,OK,暂且先知道这么多,更详细的问题比如linux为什么要这样子做,我们先留着这些疑问,饭要一口一口吃。

总体架构图

HDF驱动框架探路(一):工欲善其事,必先利其器(linux驱动开发)-鸿蒙开发者社区

1.应用程序

写应用程序的目的主要是为了测试驱动程序,测试的方法为调用glibc的open、read、write函数。
代码见下:

#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;
    char buf[1024];
    int len;
    int ret;

    if (argc < 2) {
        printf("Usage: %s -w <string>\n", argv[0]);
        printf("       %s -r\n",argv[0]);
        return -1;
    }

    fd = open("/dev/hello_linux_dri", O_RDWR);
    if (fd == -1)
    {
        printf("can not open file /dev/hello_linux_dri\n");
        return -1;
    }
    printf("open /dev/hello_linux_dri success\n");
    if ((0 == strcmp(argv[1], "-w")) && (argc  == 3))
    {
        len = strlen(argv[2]) + 1;
        len = len < 1024 ? len : 1024;
        ret = write(fd, argv[2], len);
        printf("write driver : %d\n", ret);
    } else {
        len = read(fd, buf, 1024);
        printf("read driver : %d\n", len);
        buf[1023] = '\0';
        printf("APP read : %s\n", buf);
    }

    close(fd);

    return 0;
}

2.驱动程序总体逻辑

驱动程序属于模块写法:预先注册自己的的函数任务,以便服务于将来的某个请求,然后它的初始化函数就立即结束

2.1 环境确认

因为驱动程序是调用的内核头文件,所以首先需要确认头文件有没有,下面第一条命令是确认有没有,没有的话,使用第二条进行下载。

apt-cache search linux-headers-$(uname -r)
sudo apt-get install linux-headers-$(uname -r)

2.2 驱动框架代码搭建

代码见下图:

#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>

static int major = 0;
static int ker_val = 123;
static struct class *hello_for_class;
static ssize_t hello_read(struct file *file, char __user *buf, size_t size, loff_t *offset)
{
    printk("%s %s line %d\n",__FILE__,__FUNCTION__, __LINE__);
    return 4;
}

static ssize_t hello_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
    printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
    return 4;
}
int __init hello_init(void)
{
    printk("hello_linux_dri init\n");
    return 0;
}
void __exit hello_exit(void)
{
    printk("hello_linux_dri exit\n");
    return;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

上述就是驱动代码的一个基本框架

3.注册驱动

使用file_operations结构体来填充register_chrdev函数来注册驱动

static struct file_operations hello_linux_fops = {
    .owner = THIS_MODULE,
    .read = hello_read,
    .write = hello_write,
};
int __init hello_init(void)
{
    printk("hello_linux_dri init\n");
    major = register_chrdev(0,"hello_linux_dri", &hello_linux_fops);
    return 0;
}

4.注销驱动

使用unregister_chrdev函数来注销注册中的驱动信息

void __exit hello_exit(void)
{
    printk("hello_linux_dri exit\n");
    unregister_chrdev(major, "hello_linux_dri");
    return;
}

5.自动生成设备文件来使用驱动

5.1 生成设备文件

使用class_create和device_create来进行生成,代码如下:

int __init hello_init(void)
{
    printk("hello_linux_dri init\n");
    major = register_chrdev(0,"hello_linux_dri", &hello_linux_fops);
    hello_for_class = class_create(THIS_MODULE, "hello_class");
    device_create(hello_for_class, NULL, MKDEV(major, 0), NULL, "hello_linux_dri");

    return 0;
}

5.2 撤销设备文件

使用device_destroy和class_destroy进行撤销,代码如下:

void __exit hello_exit(void)
{
    printk("hello_linux_dri exit\n");

    device_destroy(hello_for_class, MKDEV(major, 0));
    class_destroy(hello_for_class);
    unregister_chrdev(major, "hello_linux_dri");

    return;
}

效果如下:
HDF驱动框架探路(一):工欲善其事,必先利其器(linux驱动开发)-鸿蒙开发者社区
完整代码见附件

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
hello_linux_dri.zip 182B 91次下载
已于2021-11-19 16:50:54修改
14
收藏 15
回复
举报
2条回复
按时间正序
/
按时间倒序
红叶亦知秋
红叶亦知秋

好文,收藏了

回复
2021-11-22 09:53:00
zhushangyuan_
zhushangyuan_

同驱动小白  好好跟着你的系列去学习 

回复
2022-1-14 00:17:56
回复
    相关推荐