11.linux驱动模版

整体结构

一共 3 部分:

  1. 设备树节点(DTS)
  2. 内核驱动(平台驱动 + 字符设备,最简版)
  3. 用户层测试APP
    环境:Linux 5.x / 6.x,ZYNQ / STM32MP1 通用。

一、第一步:添加设备树节点 dts

打开板级 dts,在根节点下添加:

myhello_dev {
    compatible = "mycompany,hello_dev";
    status = "okay";
};
  • compatible 是驱动和设备匹配的关键字,必须两边一致。
  • 本例只做字符设备,不需要寄存器地址,所以不用 reg。

编译生成 dtb,更新到开发板。


二、第二步:驱动代码 hello_dev.c(两层最简封装)

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

// ---------------- 字符设备变量 ----------------
#define DEV_NAME "hello_dev"
static dev_t devno;
static struct cdev cdev;
static struct class *dev_class;

// ---------------- 文件操作接口(应用调用open/read/write) ----------------
static int hello_open(struct file *file)
{
    pr_info("hello dev opened\n");
    return 0;
}

static ssize_t hello_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    char k_buf[] = "Hello World from kernel driver!\n";
    int len = strlen(k_buf);

    // 内核数据拷贝到用户空间
    copy_to_user(buf, k_buf, len);
    return len;
}

static int hello_release(struct file *file)
{
    pr_info("hello dev closed\n");
    return 0;
}

static struct file_operations hello_fops = {
    .owner = THIS_MODULE,
    .open  = hello_open,
    .read  = hello_read,
    .release = hello_release,
};

// ---------------- 平台驱动:设备匹配成功后执行probe ----------------
static int hello_probe(struct platform_device *pdev)
{
    int ret;

    // 1. 申请设备号
    ret = alloc_chrdev_region(&devno, 0, 1, DEV_NAME);
    if (ret < 0)
    {
        pr_err("alloc devno failed\n");
        return ret;
    }

    // 2. 初始化字符设备,绑定文件操作集
    cdev_init(&cdev, &hello_fops);
    cdev_add(&cdev, devno, 1);

    // 3. 自动在 /dev 下生成设备节点
    dev_class = class_create(THIS_MODULE, DEV_NAME);
    device_create(dev_class, NULL, devno, NULL, DEV_NAME);

    pr_info("hello driver probed success\n");
    return 0;
}

static int hello_remove(struct platform_device *pdev)
{
    // 销毁设备节点
    device_destroy(dev_class, devno);
    class_destroy(dev_class);

    // 删除字符设备
    cdev_del(&cdev);
    unregister_chrdev_region(devno, 1);

    pr_info("hello driver removed\n");
    return 0;
}

// 匹配表:和dts里compatible严格对应
static const struct of_device_id hello_of_match[] = {
    { .compatible = "mycompany,hello_dev" },
    { /* Sentinel */ }
};
MODULE_DEVICE_TABLE(of, hello_of_match);

// 注册平台驱动
static struct platform_driver hello_driver = {
    .probe  = hello_probe,
    .remove = hello_remove,
    .driver = {
        .name = "hello_plat_driver",
        .of_match_table = hello_of_match,
    },
};

module_platform_driver(hello_driver);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Hello world platform char driver");
MODULE_AUTHOR("Newbie");

分层说明

  1. 底层封装:file_operations,向上给应用提供 open/read;
  2. 上层框架封装:platform_driver,和设备树做匹配,实现软硬件分离;
    硬件信息只留在DTS,C代码里没有写死任何硬件地址。

三、Makefile

obj-m += hello_dev.o
KERNELDIR ?= /home/xxx/linux-kernel
PWD := $(shell pwd)

all:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) clean

编译得到:hello_dev.ko


四、第三步:用户层应用 app.c

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

int main(void)
{
    int fd;
    char buf[128];

    fd = open("/dev/hello_dev", O_RDONLY);
    if (fd < 0)
    {
        perror("open");
        return -1;
    }

    read(fd, buf, sizeof(buf));
    printf("Recv: %s\n", buf);

    close(fd);
    return 0;
}

交叉编译:

arm-linux-gnueabihf-gcc app.c -o app

五、开发板上完整测试流程

加载驱动

insmod hello_dev.ko

dts匹配成功,会自动执行 probe,自动生成 /dev/hello_dev

运行应用程序

chmod +x app
./app

运行结果

终端打印:

Recv: Hello World from kernel driver!

dmesg 查看内核日志:

hello dev opened
hello dev closed

卸载驱动

rmmod hello_dev

六、软硬件分离关键点复盘

硬件描述只放在 DTS:

compatible = "mycompany,hello_dev";

驱动不写死硬件信息,只依靠 of_device_id 做字符串匹配;

一旦匹配成功,内核自动调用 probe 函数初始化字符设备;

应用只操作 /dev/xxx 文件,完全不关心底层硬件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值