记Linux第一个驱动模块(参考正点和野火)

10 篇文章 1 订阅
10 篇文章 0 订阅

记Linux第一个驱动模块(参考正点和野火):

1. Linux开发准备:

1.1 一份新的Linux源码(和板子的内核是一样的)

1.2 vscode软件

2. 配置vscode:

2.1 配置插件

1)、 C/C++,这个肯定是必须的。
2)、 C/C++ Snippets,即 C/C++重用代码块。
3)、 C/C++ Advanced Lint,即 C/C++静态检测 。
4)、 Code Runner,即代码运行。
5)、 Include AutoComplete,即自动头文件包含。
6)、 Rainbow Brackets,彩虹花括号,有助于阅读代码。
7)、 One Dark Pro, VSCode 的主题。
8)、 GBKtoUTF8,将 GBK 转换为 UTF8。
9)、 ARM,即支持 ARM汇编语法高亮显示。
10)、 Chinese(Simplified),即中文环境。
11)、 vscode-icons, VSCode图标插件,主要是资源管理器下各个文件夹的图标。
12)、 compareit,比较插件,可以用于比较两个文件的差异。
13)、DeviceTree,设备树语法插件。
14)、 TabNine,一款 AI 自动补全插件,强烈推荐,谁用谁知道!

2.2 建立工程:

2.2.1 建立工程

1.新建一个文件夹
2.用vscode->打开文件夹
3.将工作区另存为

2.2.2 创建sttings.json文件

内容如下:

{

    "search.exclude": {

        "**/node_modules": true,

        "**/bower_components": true,

        "**/*.o":true,

        "**/*.su":true, 

        "**/*.cmd":true,

        "Documentation":true    

    },

    "files.exclude": {

        "**/.git": true,

        "**/.svn": true,

        "**/.hg": true,

        "**/CVS": true,

        "**/.DS_Store": true,  

        "**/*.o":true,

        "**/*.su":true, 

        "**/*.cmd":true,

        "Documentation":true

    } }
2.2.2 创建c_cpp_properties.json文件
{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/home/book/lichee-pi/linux_kernels/linux-5.10/include", //全为linux的目录
                "/home/book/lichee-pi/linux_kernels/linux-5.10/arch/arm/include",
                "/home/book/lichee-pi/linux_kernels/linux-5.10/arch/arm/include/generated/"
            ],
            "defines": [],
            "compilerPath": "/home/book/lichee-pi/buildroot-2018.08.2/output/host/bin/arm-linux-gnueabihf-gcc", //gcc的位置 用buildroot里面的
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "linux-gcc-arm"
        }
    ],
    "version": 5
}
2.2.3 test.c文件

内容如下:

#include<linux/init.h>
#include<linux/module.h>
#include<linux/kernel.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("wk");
static int __init test_init(void)
{
	printk(KERN_ALERT "Hello,world\n");
	return 0;
}
static void __exit test_exit(void)
{
	printk(KERN_ALERT"Goodbye,cruel world\n");
 
}
module_init(test_init);
module_exit(test_exit);
2.2.4 建立makefile文件:
KERNELDIR := /home/book/lichee-pi/linux_kernels/linux2 #为内核的目录
CURRENT_PATH := $(shell pwd) 
ARCH=arm
CROSS_COMPILE=/home/book/lichee-pi/buildroot-2018.08.2/output/host/bin/arm-linux-gnueabihf- #配置gcc的目录 使用bulidroot一致的
export ARCH CROSS_COMPILE
obj-m := test.o #生成模块的名字

build: kernel_modules

kernel_modules:
	$(MAKE) -C $(KERNELDIR) M=${CURRENT_PATH} modules
clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
2.2.5 结果如图:

在这里插入图片描述
如果有和我一样建立个linux_driver文件夹的话 在make时需要在终端使用 cd linux_driver 后再使用make进行编译。

3.编译工程:

make

如果有和我一样建立个linux_driver文件夹的话 在make时需要在终端使用 cd linux_driver 后再使用make进行编译。

4. 将文件放入开发板

5.加载模块:

insmod test.ko

会出现提示:

# insmod test.ko
[   16.074621] test: loading out-of-tree module taints kernel.
[   16.081418] Hello,world

6. 查询加载的模块:

# lsmod
Module                  Size  Used by    Tainted: G
test                   16384  0

7.卸载模块:

# rmmod test 
[  292.187514] Goodbye,cruel world
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值