移植linux驱动代码到内核中

通常,我们测试和使用驱动代码有两种方法:

第一,单独编译成ko文件,使用模块编译命令insmod挂载到内核中使用;

第二,编译到内核当中,和内核一起启动和加载;

这里我大致介绍一下移植和编译驱动到linux内核的方法:

一,下载需要移植的代码,并把代码复制到kernel对应的drivers/ron子目录中;

二, 修改或是新建Makefile;

#
# Makefile for the robin core.
#
obj-$(CONFIG_KEY1_BUTTON) += button_input_drv.o

三,新建或是修改Kconfig,添加该部分的编译选项;

#
# robin subsystem configuration
#
menu "rk3399 key button support"
        config KEY1_BUTTON
        tristate "KEY1_BUTTON support"
---help---
        rk3399 key1 button gpio test
        haha this is first config menu item
        config KEY2_BUTTON
        tristate "KEY2_BUTTON support"
---help---
        rk3399 key1 button gpio test
        haha this is first config menu item
endmenu

四, 让编译器知道你自定义的 Makefile Kconfig 在该 ron 目录上级要修改 Makefile 和 Kconfig;

Makefile 中添加一行
obj-y += robin/

Kconfig 中添加一行

menu "Device Drivers"
...
source "drivers/robin/Kconfig"
endmenu

 五,启动配置菜

make menuconfig

robin@ubuntu:~/work/kernel-rockchip-nanopi4-linux-v4.4.y$ 
make menuconfig

移植linux驱动代码到内核中_第1张图片

检查配置结果

 vi .config  

移植linux驱动代码到内核中_第2张图片

六,  编译并烧录内核、重启开发板后调试;
root@SOM-RK3399v2:~# dmesg | grep robin
[linaro-7.3-2018.05 revision
d29120a424ecfbc167ef90065c0eeb7f91977701] (Linaro GCC 7.3-2018.05) ) #12 SMP
Sun Aug 29 02:06:34 PDT 2021
[ 0.000000] psci: probing for conduit method from DT.
[ 0.632480] call button_drvier_init()
[ 0.633165]
input: robin key1 button input dev as
/devices/virtual/input/input0
[ 0.633872] IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING:3 call
button_driver_probe()
[ 1.325048] usb usb1: Manufacturer: Linux 4.4.179-robin ehci_hcd
[ 1.387568] usb usb2: Manufacturer: Linux 4.4.179-robin ohci_hcd
[ 1.992571] usb usb3: Manufacturer: Linux 4.4.179-robin xhci-hcd
[ 1.998997] usb usb4: Manufacturer: Linux 4.4.179-robin xhci-hcd
root@SOM-RK3399v2:~# cat /proc/bus/input/devices
I: Bus=0000 Vendor=0100 Product=0010 Version=1001
N: Name="robin key1 button input dev"
P: Phys=rk3399 key1
S: Sysfs=/devices/virtual/input/input0
U: Uniq=
H: Handlers=kbd event0 cpufreq
B: PROP=0
B: EV=3
B: KEY=4
I: Bus=0000 Vendor=0000 Product=0000 Version=0000
N: Name="Typec_Headphone"
P: Phys=fusb302/typec
S: Sysfs=/devices/platform/ff3d0000.i2c/i2c-4/4-0022/input/input1
U: Uniq=
H: Handlers=event1
B: PROP=0
B: EV=21
B: SW=4
root@SOM-RK3399v2:~# cd /drv_module/
root@SOM-RK3399v2:/drv_module# ls
button_input_drv.ko button_input_test
root@SOM-RK3399v2:/drv_module# ./button_input_test /dev/input/event0
[ 195.858505] call button_handler()
key1 pressed

你可能感兴趣的:(linux驱动开发,linux)