Difference between revisions of "Build drivers from source for rpi/zh"

From wiki_veye
Jump to navigation Jump to search
 
(5 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
我们提供了在树莓派上本地编译和在PC上交叉编译的方法。建议使用树莓派本地编译,这更简单,也更快捷。
 
我们提供了在树莓派上本地编译和在PC上交叉编译的方法。建议使用树莓派本地编译,这更简单,也更快捷。
  
=== 树莓派本地编译 ===
+
===树莓派本地编译===
 
以下操作均在树莓派上进行。
 
以下操作均在树莓派上进行。
  
 
以下以piOS 5.15.32为例:
 
以下以piOS 5.15.32为例:
  
==== 编译环境准备 ====
+
====编译环境准备====
 
<code>sudo apt install git bc bison flex libssl-dev make</code>
 
<code>sudo apt install git bc bison flex libssl-dev make</code>
  
==== 下载本机piOS的内核头文件 ====
+
====下载本机piOS的内核头文件====
 
<code>sudo apt install raspberrypi-kernel-headers</code>
 
<code>sudo apt install raspberrypi-kernel-headers</code>
  
==== 下载veye驱动代码 ====
+
====下载veye驱动代码====
 
<code>git clone <nowiki>https://github.com/veyeimaging/raspberrypi_v4l2.git</nowiki></code>
 
<code>git clone <nowiki>https://github.com/veyeimaging/raspberrypi_v4l2.git</nowiki></code>
  
Line 25: Line 25:
 
<code>Linux raspberrypi 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 GNU/Linux</code>
 
<code>Linux raspberrypi 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 GNU/Linux</code>
  
==== 编译驱动 ====
+
====编译驱动====
 
进入对应目录并编译
 
进入对应目录并编译
  
Line 34: Line 34:
 
完成。
 
完成。
  
==== 编译dts ====
+
====编译dts====
 
<code>cd ~/raspberrypi_v4l2/driver_source/dts/rpi-5.15.y</code>
 
<code>cd ~/raspberrypi_v4l2/driver_source/dts/rpi-5.15.y</code>
  
Line 41: Line 41:
 
完成。
 
完成。
  
==== 常见错误 ====
+
====常见错误====
  
===== 找不到build目录 =====
+
=====找不到build目录=====
 
<code>make[1]: *** /lib/modules/[version]/build: No such file or directory. Stop.</code>
 
<code>make[1]: *** /lib/modules/[version]/build: No such file or directory. Stop.</code>
  
Line 52: Line 52:
 
解决方法:
 
解决方法:
  
1.升级piOS并重新编译。
+
======1.升级piOS并重新编译。======
 
 
 
<code>sudo apt update</code>
 
<code>sudo apt update</code>
  
Line 60: Line 59:
 
<code>sudo apt install raspberrypi-kernel-headers</code>
 
<code>sudo apt install raspberrypi-kernel-headers</code>
  
2. 从这个[https://archive.raspberrypi.org/debian/pool/main/r/raspberrypi-firmware/ 链接]中查找到对应当前的版本的deb安装包并安装。
+
'''特别的''',对于rpi4系列,32bit的PiOS升级到最新版本后会自动启用64-bit模式。但是<code>raspberrypi-kernel-headers</code>包中缺少v8+模式的build目录。
 
 
tag请根据本机piOS版本与raspberrypi OS [https://github.com/raspberrypi/linux/tags tags]确定。
 
 
 
=== PC机上交叉编译 ===
 
以下操作均在ubuntu PC上进行。
 
====开发环境准备====
 
<code>sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev</code>
 
  
*32-bit piOS target version
+
比如:
 
 
<code>sudo apt install crossbuild-essential-armhf</code>
 
 
 
*64-bit piOS target version
 
 
 
<code>sudo apt install crossbuild-essential-arm64</code>
 
====下载标准版本====
 
=====首先,请确认自己的树莓派上的piOS版本:=====
 
 
 
*Release version
 
  
 
<code>$ uname -a</code>
 
<code>$ uname -a</code>
  
<code>Linux raspberrypi 5.4.72-v7l+ #1356 SMP Thu Oct 22 13:57:51 BST 2020 armv7l GNU/Linux</code>
+
<code>Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023 aarch64 GNU/Linux</code>
  
*Code tag
+
却没有<code>/lib/modules/6.1.21-v8+/build</code>目录。
  
<code>$ cp /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz ./</code>
+
此时可以在/boot/config.txt中添加<code>arm_64bit=0</code>然后重启,切换回32bit模式。
  
<code>gunzip changelog.Debian.gz</code>
+
=====2. 安装对应的raspberrypi-kernel-header的deb包。=====
 +
从这个[https://archive.raspberrypi.org/debian/pool/main/r/raspberrypi-firmware/ 链接]中查找到对应当前的版本的deb安装包并安装。
  
查看最上面一条,确定代码tag。
+
tag请根据本机piOS版本与raspberrypi OS [https://github.com/raspberrypi/linux/tags tags]确定。
=====有两种方法可以得到自己想要的版本的源码:=====
 
在PC上:
 
 
 
1. 采用git获取到对应branch上的对应tag版本代码
 
 
 
<code>git clone --branch rpi-5.4.y <nowiki>https://github.com/raspberrypi/linux</nowiki></code>
 
  
<code>git checkout raspberrypi-kernel_1.20201022-1</code>
+
=====3. 使用rpi-source=====
 +
使用[https://github.com/RPi-Distro/rpi-source rpi-source]工具,它会设置好构建自己内核所需的一切(默认情况下是从当前运行的内核构建)。
  
PS:以上两条命令请替换为自己对应的版本。
+
===安装编译后的驱动===
 
 
2. 直接从如下链接手动下载对应tag的版本代码
 
 
 
https://github.com/raspberrypi/linux/tags
 
====patch我们的驱动代码====
 
<code>git clone <nowiki>https://github.com/veyeimaging/raspberrypi_v4l2.git</nowiki></code>
 
 
 
*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编译选项。
 
====编译====
 
=====编译准备=====
 
不同树莓派的编译选项不同,区别如下:
 
 
 
*For Raspberry Pi 1, Zero and Zero W, and Raspberry Pi Compute Module 1 default (32-bit only) build configuration
 
 
 
<code>KERNEL=kernel</code>
 
 
 
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig</code>
 
 
 
*For Raspberry Pi 2, 3, 3+ and Zero 2 W, and Raspberry Pi Compute Modules 3 and 3+ default 32-bit build configuration
 
 
 
<code>KERNEL=kernel7</code>
 
 
 
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig</code>
 
 
 
*For Raspberry Pi 4 and 400, and Raspberry Pi Compute Module 4 default 32-bit build configuration
 
 
 
<code>KERNEL=kernel7l</code>
 
 
 
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig</code>
 
 
 
*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
 
 
 
<code>KERNEL=kernel8</code>
 
 
 
<code>make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig</code>
 
=====增加编译选项=====
 
 
 
*32-bit version
 
 
 
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig</code>
 
 
 
*64-bit version
 
 
 
<code>make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig</code>
 
 
 
增加对应camera模块驱动的编译选项,linux5.4版本路径为driver-->multimedia-->i2c;
 
 
 
5.10版本kernel的路径是Device Drivers --> Multimedia Support --> Media ancillary drivers --> Camera sensor devices。
 
=====编译输出=====
 
 
 
*32-bit version
 
 
 
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j4</code>
 
 
 
*64-bit version
 
 
 
<code>make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs -j4</code>
 
 
 
=== 安装编译后的驱动 ===
 
 
编译完成后,就得到了指定版本的驱动和dtbo文件。
 
编译完成后,就得到了指定版本的驱动和dtbo文件。
  
Line 203: Line 108:
 
https://www.raspberrypi.org/documentation/linux/kernel/building.md
 
https://www.raspberrypi.org/documentation/linux/kernel/building.md
  
=== 本文修改记录 ===
+
===本文修改记录===
 +
 
 +
* 20260321
 +
 
 +
删除交叉编译的相关章节。
 +
 
 +
*20230326
 +
 
 +
完善'''找不到build目录'''错误的处理方法。
  
* 20230326
+
*20230326
  
 
增加'''找不到build目录'''错误的描述和处理方法。
 
增加'''找不到build目录'''错误的描述和处理方法。
  
* 20220424
+
*20220424
  
 
增加在树莓派本地编译的支持方法。
 
增加在树莓派本地编译的支持方法。

Latest revision as of 07:47, 21 March 2026

English

本文主要参考资料为官方piOS编译方法

我们提供了在树莓派上本地编译和在PC上交叉编译的方法。建议使用树莓派本地编译,这更简单,也更快捷。

1 树莓派本地编译

以下操作均在树莓派上进行。

以下以piOS 5.15.32为例:

1.1 编译环境准备

sudo apt install git bc bison flex libssl-dev make

1.2 下载本机piOS的内核头文件

sudo apt install raspberrypi-kernel-headers

1.3 下载veye驱动代码

git clone https://github.com/veyeimaging/raspberrypi_v4l2.git

1.4 确认自己的树莓派上的piOS版本:

$ uname -a

Linux raspberrypi 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 GNU/Linux

1.5 编译驱动

进入对应目录并编译

cd ~/raspberrypi_v4l2/driver_source/cam_drv_src/rpi-5.15_all

make

完成。

1.6 编译dts

cd ~/raspberrypi_v4l2/driver_source/dts/rpi-5.15.y

./build_dtbo.sh

完成。

1.7 常见错误

1.7.1 找不到build目录

make[1]: *** /lib/modules/[version]/build: No such file or directory. Stop.

sudo apt install raspberrypi-kernel-headers只是简单地安装上可用的最新内核头文件。这不一定与当前你使用的piOS系统的内核匹配。

Raspbian维护者总是从存储库索引文件中删除旧的内核头文件。

解决方法:

1.7.1.1 1.升级piOS并重新编译。

sudo apt update

sudo apt full-upgrade

sudo apt install raspberrypi-kernel-headers

特别的,对于rpi4系列,32bit的PiOS升级到最新版本后会自动启用64-bit模式。但是raspberrypi-kernel-headers包中缺少v8+模式的build目录。

比如:

$ uname -a

Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr  3 17:24:16 BST 2023 aarch64 GNU/Linux

却没有/lib/modules/6.1.21-v8+/build目录。

此时可以在/boot/config.txt中添加arm_64bit=0然后重启,切换回32bit模式。

1.7.2 2. 安装对应的raspberrypi-kernel-header的deb包。

从这个链接中查找到对应当前的版本的deb安装包并安装。

tag请根据本机piOS版本与raspberrypi OS tags确定。

1.7.3 3. 使用rpi-source

使用rpi-source工具,它会设置好构建自己内核所需的一切(默认情况下是从当前运行的内核构建)。

2 安装编译后的驱动

编译完成后,就得到了指定版本的驱动和dtbo文件。

参考安装驱动步骤

wget https://github.com/veyeimaging/raspberrypi_v4l2/releases/latest/download/raspberrypi_v4l2.tgz

tar -xzvf raspberrypi_v4l2.tgz

cd raspberrypi_v4l2/release/

mkdir driver_bin/$(uname -r)

将编译得到的驱动的驱动和dtbo放到此目录下。

然后执行安装命令即可。

chmod +x *

sudo ./install_driver.sh [camera module]

camera module:可以是veye327,csimx307,cssc132,veyecam2m,veye_mvcam等。

3 参考资料

  • 树莓派的piOS编译方案:

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

4 本文修改记录

  • 20260321

删除交叉编译的相关章节。

  • 20230326

完善找不到build目录错误的处理方法。

  • 20230326

增加找不到build目录错误的描述和处理方法。

  • 20220424

增加在树莓派本地编译的支持方法。

  • 20220411

独立成篇,增加MV系列的描述。