1. 开发环境
- 电脑虚拟机安装ubuntu18.04
- orangepizero
- OrangePi_zero_ubuntu_xenial_server_linux5.3.5_v2.0.6.img
- 内核:linux5.3.5
- 源码下载(github中搜索 orangepi,即可根据型号找到对应的内核源码)
- orangepizero内核源码: https://gitee.com/orangepibase/mainline_kernel(已从github转到码云,加快下载速度)
- orangepizero交叉编译工具链: https://gitee.com/sunshengquan110/toolchainrk3399
2. 内核编译
2.1 内核配置
cd OrangePiH2_mainline/kernel
ls arch/arm/configs
acs5k_defconfig ezx_defconfig multi_v4t_defconfig s3c2410_defconfig
acs5k_tiny_defconfig footbridge_defconfig multi_v5_defconfig s3c6400_defconfig
am200epdkit_defconfig gemini_defconfig multi_v7_defconfig s5pv210_defconfig
aspeed_g4_defconfig h3600_defconfig mv78xx0_defconfig sama5_defconfig
aspeed_g5_defconfig h5000_defconfig mvebu_v5_defconfig shannon_defconfig
assabet_defconfig hackkit_defconfig mvebu_v7_defconfig shmobile_defconfig
at91_dt_defconfig hisi_defconfig mxs_defconfig simpad_defconfig
axm55xx_defconfig imote2_defconfig neponset_defconfig socfpga_defconfig
badge4_defconfig imx_v4_v5_defconfig netwinder_defconfig spear13xx_defconfig
bcm2835_defconfig imx_v6_v7_defconfig nhk8815_defconfig spear3xx_defconfig
cerfcube_defconfig integrator_defconfig nuc910_defconfig spear6xx_defconfig
clps711x_defconfig iop13xx_defconfig nuc950_defconfig spitz_defconfig
cm_x2xx_defconfig iop32x_defconfig nuc960_defconfig stm32_defconfig
cm_x300_defconfig iop33x_defconfig omap1_defconfig sun8iw7p1smp_defconfig
cns3420vb_defconfig ixp4xx_defconfig omap2plus_defconfig sunxi_defconfig
colibri_pxa270_defconfig jornada720_defconfig orangepi_defconfig tango4_defconfig
colibri_pxa300_defconfig keystone_defconfig orion5x_defconfig tbs_a711_defconfig
collie_defconfig ks8695_defconfig oxnas_v6_defconfig tct_hammer_defconfig
corgi_defconfig lart_defconfig palmz72_defconfig tegra_defconfig
davinci_all_defconfig lpc18xx_defconfig pcm027_defconfig trizeps4_defconfig
dove_defconfig lpc32xx_defconfig pleb_defconfig u300_defconfig
dram_0x00000000.config lpd270_defconfig prima2_defconfig u8500_defconfig
dram_0xc0000000.config lubbock_defconfig pxa168_defconfig versatile_defconfig
dram_0xd0000000.config magician_defconfig pxa255-idp_defconfig vexpress_defconfig
ebsa110_defconfig mainstone_defconfig pxa3xx_defconfig vf610m4_defconfig
efm32_defconfig milbeaut_m10v_defconfig pxa910_defconfig viper_defconfig
em_x270_defconfig mini2440_defconfig pxa_defconfig vt8500_v6_v7_defconfig
ep93xx_defconfig mmp2_defconfig qcom_defconfig xcep_defconfig
eseries_pxa_defconfig moxart_defconfig realview_defconfig zeus_defconfig
exynos_defconfig mps2_defconfig rpc_defconfig zx_defconfig
上面是一些对应芯片的默认配置,这里有一个坑:
- 按照我们使用的型号是orangepizero,所有我们应该选择orangepi_defconfig,但是经过测试,编译出来的驱动文件无法加载到内核
- 经过网上的资料查阅,和尝试最终使用此配置(sun8iw7p1smp_defconfig),成功编译出驱动并且加载成功
运行
make sun8iw7p1smp_defconfig ARCH=arm CROSS_COMPILE=“上面下载的交叉编译工具链位置”,参考如下
make sun8iw7p1smp_defconfig ARCH=arm CROSS_COMPILE=/home/ubuntu/OrangePiH2/toolchain/gcc-linaro-1.13.1-2012.02-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-
3. 编译驱动代码
3.1 修改官方提供的驱动makefile
ifeq ($(KERNELRELEASE), )
KERNELDIR := /lib/modules/$(shell uname -r)/build #修改为你内核源码的位置
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
clean:
rm -rf *.mk .tmp_versions Module.symvers *.mod.c *.o *.ko .*.cmd Module.markers modules.order
load:
insmod ch934x.ko
unload:
rmmod ch934x
install: default
mkdir -p /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/
cp -f ./ch934x.ko /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/
depmod -a
uninstall:
rm -rf /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/ch934x.ko
depmod -a
else
obj-m := ch934x.o
endif
修改为如下:
ifeq ($(KERNELRELEASE), )
KERNELDIR :=/home/ubuntu/OrangePiH2_mainline/kernel
PWD :=$(shell pwd)
default:
$(MAKE) -C $(KERNELDIR) M=$(PWD)
clean:
rm -rf *.mk .tmp_versions Module.symvers *.mod.c *.o *.ko .*.cmd Module.markers modules.order
load:
insmod ch934x.ko
unload:
rmmod ch934x
install: default
mkdir -p /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/
cp -f ./ch934x.ko /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/
depmod -a
uninstall:
rm -rf /lib/modules/$(shell uname -r)/kernel/drivers/usb/serial/ch934x.ko
depmod -a
else
obj-m := ch934x.o
endif
最终make ARCH=arm CROSS_COMPILE=/home/ubuntu/OrangePiH2/toolchain/gcc-linaro-1.13.1-2012.02-x86_64_arm-linux-gnueabi/bin/arm-linux-gnueabi-即可生成对应的驱动文件