【Linux设备驱动】第一个驱动HelloWorld编写解析

第一个Linux设备驱动HelloWorld编写解析

如果你是初出茅庐的程序员,那么先阅读这一篇《编写驱动前应该知道哪些》做一个预备,再阅读下面代码,然后你就可以豁然开朗了

helloworld.c文件

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/version.h>

#include "include/common.h"

#define HELLOWORD_DRIVER_VERSION 20221111

static int times = 0;
module_param(times, int, S_IRUGO);
MODULE_PARM_DESC(times, "set this module times");


static int __init hello_init(void)
{
    printk("create hello module success!\n");

    return 0;
}

static void __exit hello_exit(void)
{
    printk("exit hello module success!\n");

}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("zywang");//声明是谁编写的该模块
MODULE_DESCRIPTION("xxx driver");//该模块是干嘛的描述
MODULE_LICENSE("GPL");//内核人事的特定许可

Makefile

CROSS_COMPILE ?= arm-linux-gnu-

#模块编译依赖kernel
ENV_KERNEL_DIR ?= $(PWD)/../../kernel
KDIR := ${ENV_KERNEL_DIR}

#所要编译模块名称
MODULE_NAME := hello

all: modules

.PHONY: modules clean

EXTRA_CFLAGS += -I$(PWD)/include

#模块来自1个源文件helloworld.c
obj-m := $(MODULE_NAME).o
$(MODULE_NAME)-objs := helloworld.o

# '@$(MAKE)' 不再将$(MAKE)指令输出到屏幕上
# '-C' 将当前工作目录转移到你所指定的位置
# 'M=$(PWD)' 返回到当前目录继续读入,执行当前Makefile
# '$@' 代表所有参数

modules:
    @$(MAKE) -C $(KDIR) M=$(shell pwd) $@

clean:
    @find . -name '*.o' -o -name '*~' -o -name '.depend' -o -name '.*.cmd' \
    -o -name '*.mod.c' -o -name '.tmp_versions' -o -name '*.ko' \
    -o -name '*.symvers' -o -name 'modules.order' | xargs rm -rf

以上实例代码已打包,有需要的读者可以自行下载,下载资源无需积分:linux设备驱动,helloworld驱动程序

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Van.Ghylivan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值