嵌入式linux学习-驱动(2) hello world 模块实现记录 基于RK3568

本文记录了一位开发者在RK3568开发板上进行驱动开发的过程。首先,详细展示了使用迅为提供的虚拟机编译内核的步骤,包括内核配置、编译和生成镜像。然后,编写了一个简单的Hello World驱动模块,并给出了对应的makefile。在开发板上,成功加载和卸载了这个模块,验证了驱动的功能。虽然在Ubuntu系统中可以使用modinfo查看模块信息,但在开发板的默认系统中未找到该命令。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天跟随 讯为的开发板实现了 hello world 的驱动,之前已经实现了很多次,这次还是重新实现了一次 本文是基于RK3568

1. 编迅为的内核

topeet@ubuntu:~/Linux/rk356x_linux$ ./build.sh kernel
processing option: kernel
============Start building kernel============
TARGET_ARCH          =arm64
TARGET_KERNEL_CONFIG =rockchip_linux_defconfig
TARGET_KERNEL_DTS    =rk3568-evb1-ddr4-v10-linux
TARGET_KERNEL_CONFIG_FRAGMENT =
==========================================
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  YACC    scripts/kconfig/zconf.tab.c
  LEX     scripts/kconfig/zconf.lex.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
#
# configuration written to .config
xxx
... 
  LD [M]  drivers/net/wireless/rockchip_wlan/rkwifi/bcmdhd/bcmdhd.o
  Building modules, stage 2.
  MODPOST 3 modules
  CC      drivers/net/wireless/marvell/mwifiex/mwifiex.mod.o
  CC      drivers/net/wireless/marvell/mwifiex/mwifiex_sdio.mod.o
  CC      drivers/net/wireless/rockchip_wlan/rkwifi/bcmdhd/bcmdhd.mod.o
  LD [M]  drivers/net/wireless/rockchip_wlan/rkwifi/bcmdhd/bcmdhd.ko
  LD [M]  drivers/net/wireless/marvell/mwifiex/mwifiex.ko
  LD [M]  drivers/net/wireless/marvell/mwifiex/mwifiex_sdio.ko
found ./arch/arm64/boot/dts/rockchip/.rk3568-evb1-ddr4-v10-linux.dtb.dts.tmp
found ./arch/arm64/boot/dts/rockchip/.rk3568-evb1-ddr4-v10-linux.dtb.dts.tmp
found ./arch/arm64/boot/dts/rockchip/.rk3568-evb1-ddr4-v10-linux.dtb.dts.tmp
found ./arch/arm64/boot/dts/rockchip/.rk3568-evb1-ddr4-v10-linux.dtb.dts.tmp
found ./arch/arm64/boot/dts/rockchip/.rk3568-evb1-ddr4-v10-linux.dtb.dts.tmp
found ./arch/arm64/boot/dts/rockchip/.rk3568-evb1-ddr4-v10-linux.dtb.dts.tmp
found ./arch/arm64/boot/dts/rockchip/.rk3568-evb1-ddr4-v10-linux.dtb.dts.tmp
  Image:  resource.img (with rk3568-evb1-ddr4-v10-linux.dtb logo.bmp logo_kernel.bmp) is ready
  Image:  boot.img (with Image  resource.img) is ready
  Image:  zboot.img (with Image.lz4  resource.img) is ready
fdt {
kernel {
resource {
FIT description: U-Boot FIT source file for arm
Created:         Tue Dec 20 22:33:35 2022
 Image 0 (fdt)
  Description:  unavailable
  Created:      Tue Dec 20 22:33:35 2022
  Type:         Flat Device Tree
  Compression:  uncompressed
  Data Size:    137945 Bytes = 134.71 KiB = 0.13 MiB
  Architecture: AArch64
  Load Address: 0xffffff00
  Hash algo:    sha256
  Hash value:   9cd38b1dd5c8dff695ae9928a6f96cc7d35dc3fd712907989d96c31a5ebb1aaf
 Image 1 (kernel)
  Description:  unavailable
  Created:      Tue Dec 20 22:33:35 2022
  Type:         Kernel Image
  Compression:  uncompressed
  Data Size:    26578952 Bytes = 25956.01 KiB = 25.35 MiB
  Architecture: AArch64
  OS:           Linux
  Load Address: 0xffffff01
  Entry Point:  0xffffff01
  Hash algo:    sha256
  Hash value:   86c70073ba22fb58d4121acf95e4f8a45772a3260da3686e07883adcc7c55a6d
 Image 2 (resource)
  Description:  unavailable
  Created:      Tue Dec 20 22:33:35 2022
  Type:         Multi-File Image
  Compression:  uncompressed
  Data Size:    176128 Bytes = 172.00 KiB = 0.17 MiB
  Hash algo:    sha256
  Hash value:   0f60a9def334606c948d16f093c7340c2a4c1a7cae7ffc04ac0d2b663cc2f318
 Default Configuration: 'conf'
 Configuration 0 (conf)
  Description:  unavailable
  Kernel:       kernel
  FDT:          fdt
grep: exceeded PCRE's backtracking limit
grep: exceeded PCRE's backtracking limit
grep: exceeded PCRE's backtracking limit
grep: exceeded PCRE's backtracking limit
grep: exceeded PCRE's backtracking limit
grep: exceeded PCRE's backtracking limit
grep: exceeded PCRE's backtracking limit
grep: exceeded PCRE's backtracking limit
 PLEASE CHECK BOARD GPIO POWER DOMAIN CONFIGURATION !!!!!
 <<< ESPECIALLY Wi-Fi/Flash/Ethernet IO power domain >>> !!!!!
 Check Node [pmu_io_domains] in the file: /home/topeet/Linux/rk356x_linux/kernel/arch/arm64/boot/dts/rockchip/rk3568-evb1-ddr4-v10-linux.dts

 请再次确认板级的电源域配置!!!!!!
 <<< 特别是Wi-Fi,FLASH,以太网这几路IO电源的配置 >>> !!!!!
 检查内核文件 /home/topeet/Linux/rk356x_linux/kernel/arch/arm64/boot/dts/rockchip/rk3568-evb1-ddr4-v10-linux.dts 的节点 [pmu_io_domains]
Running build_kernel succeeded.
topeet@ubuntu:~/Linux/rk356x_linux$

内核编译层面 迅为基本已经都做好了 我这边使用的是迅为提供的虚拟机。
接下来按照文档说明的开始抄代码
第一个驱动如下 :

#include <linux/module.h>
#include <linux/init.h>


static int __init hello_init(void)
{
    printk("init ... \n");
    return 0;
}

static void hello_exit(void)
{
    printk("exit ... \n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("jeason b");
MODULE_VERSION("1.0.0");

对应的makefile 如下:

obj-m += hello.o
KDIR:=/home/topeet/Linux/rk356x_linux/kernel
PWD?=$(shell pwd)
all:
	make -C $(KDIR) M=$(PWD) modules
clean:
	make -C $(KDIR) M=$(PWD) clean
	rm -f *.ko *.o *.mod.o *.mod.c *.symvers *.order

2.模块安装测试

[root@RK356X:~]# modinfo hello.ko
-/bin/sh: modinfo: not found
[root@RK356X:~]# insmod hello.ko
[ 6478.328524] init ...
[root@RK356X:~]# rmmod hello.ko
[ 6482.710927] exit ...
[root@RK356X:~]#

在 rk3568 开发板的默认的系统里面没找到 modinfo的指令 在ubuntu上查看模块信息

topeet@ubuntu:~/drive/hello$ ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers
topeet@ubuntu:~/drive/hello$
topeet@ubuntu:~/drive/hello$ modinfo hello.ko
filename:       /home/topeet/drive/hello/hello.ko
version:        1.0.0
author:         jeason b
license:        GPL v2
srcversion:     8C25FFFAB4F34B01755F9C1
depends:
name:           hello
vermagic:       4.19.232 SMP mod_unload aarch64
topeet@ubuntu:~/drive/hello$
topeet@ubuntu:~/drive/hello$ file hello.ko
hello.ko: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), BuildID[sha1]=9d25a5a92f3e27c8bc230f32ef6c54332b2d7a14, with debug_info, not stripped
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值