Build drivers from source for rpi/zh

From wiki_veye
Revision as of 17:07, 11 April 2022 by Admin (talk | contribs)
Jump to navigation Jump to search

English

本文主要参考资料为官方piOS编译方法。我们采用交叉编译的方法,5.4.72版本32bitOS为例进行说明。

以下操作均在ubuntu PC上进行。

1 开发环境准备

sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev

  • 32-bit piOS target version

sudo apt install crossbuild-essential-armhf

  • 64-bit piOS target version

sudo apt install crossbuild-essential-arm64

2 下载标准版本

2.1 首先,请确认自己的树莓派上的piOS版本:
  • Release version

$ uname -a

Linux raspberrypi 5.4.72-v7l+ #1356 SMP Thu Oct 22 13:57:51 BST 2020 armv7l GNU/Linux

  • Code tag

$ cp /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz ./

gunzip changelog.Debian.gz

查看最上面一条,确定代码tag。

2.2 有两种方法可以得到自己想要的版本的源码:

在PC上:

1. 采用git获取到对应branch上的对应tag版本代码

git clone --branch rpi-5.4.y https://github.com/raspberrypi/linux

git checkout raspberrypi-kernel_1.20201022-1

PS:以上两条命令请替换为自己对应的版本。

2. 直接从如下链接手动下载对应tag的版本代码

https://github.com/raspberrypi/linux/tags

3 patch我们的驱动代码

  • Driver source code

camera驱动源码路径为: linux/drivers/media/i2c,将camera驱动的源码放到对应目录。

  • Modify the the Makefile and Kconfig files

修改同路径下的Config和Makefile,增加对应camera驱动。

  • dts file

dts文件路径为:linux/arch/arm/boot/dts/overlays,将[camera]-overlay.dts文件放到此路径。

  • Modify dts Makefile

修改同路径下的Makefile,增加对应dts编译选项。

4 编译

4.1 编译准备

不同树莓派的编译选项不同,区别如下:

  • For Raspberry Pi 1, Zero and Zero W, and Raspberry Pi Compute Module 1 default (32-bit only) build configuration

KERNEL=kernel

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig

  • For Raspberry Pi 2, 3, 3+ and Zero 2 W, and Raspberry Pi Compute Modules 3 and 3+ default 32-bit build configuration

KERNEL=kernel7

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig

  • For Raspberry Pi 4 and 400, and Raspberry Pi Compute Module 4 default 32-bit build configuration

KERNEL=kernel7l

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig

  • For Raspberry Pi 3, 3+, 4, 400 and Zero 2 W, and Raspberry Pi Compute Modules 3, 3+ and 4 default 64-bit build configuration

KERNEL=kernel8

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig

4.2 增加编译选项
  • 32-bit version

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig

  • 64-bit version

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig

增加对应camera模块驱动的编译选项,linux5.4版本路径为driver-->multimedia-->i2c;

5.10版本kernel的路径是Device Drivers --> Multimedia Support --> Media ancillary drivers --> Camera sensor devices。

4.3 编译输出
  • 32-bit version

make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j4

  • 64-bit version

make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs -j4

5 常见问题

  • disagrees about version of symbol module_layout

系统启动过后,dmesg可以看到camera的驱动加载的时候报这个错误。由于板子上原有的piOS版本与编译后的camera驱动版本不匹配导致。

建议参考这个链接,从代码重新编译,并整体安装Image、dtb、modules。

6 参考资料

  • 树莓派的piOS编译方案:

https://www.raspberrypi.org/documentation/linux/kernel/building.md