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

From wiki_veye
Jump to navigation Jump to search
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
[[Build drivers from source for rpi/zh|查看中文]]
 
[[Build drivers from source for rpi/zh|查看中文]]
  
The main resources in this section are the [https://www.raspberrypi.org/documentation/linux/kernel/building.md official piOS building method]. We use the method of cross-compilation.
+
The main resources in this section are the [https://www.raspberrypi.org/documentation/linux/kernel/building.md official piOS building method].  
  
The following operations are done on ubuntu PC.
+
We provide methods to compile locally on Raspberry Pi and cross-compile on PC. It is recommended to use Raspberry Pi local compilation, which is simpler and faster.
===Build cross-compilation environment on Ubuntu 64-bit operating system)===
 
<code>sudo apt install git bc bison flex libssl-dev make libc6-dev libncurses5-dev</code>
 
  
*32-bit piOS target version
+
===Building the Drivers Locally===
 +
The following operations are performed on the Raspberry Pi.
  
<code>sudo apt install crossbuild-essential-armhf</code>
+
The following is an example of piOS 5.15.32.
  
*64-bit piOS target version
+
====Prepare environment====
 +
<code>sudo apt install git bc bison flex libssl-dev make</code>
  
<code>sudo apt install crossbuild-essential-arm64</code>
+
====Download the kernel headers for the local piOS====
===Download the standard version of the piOS source code===
+
<code>sudo apt install raspberrypi-kernel-headers</code>
=====Confirm the piOS version of your raspberry Pi=====
 
  
*<code>Release version</code>
+
====Download drivers code from veye====
 +
<code>git clone <nowiki>https://github.com/veyeimaging/raspberrypi_v4l2.git</nowiki></code>
  
 +
====Confirm the version of piOS on your own Raspberry Pi====
 
<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 5.15.32-v8+ #1538 SMP PREEMPT Thu Mar 31 19:40:39 BST 2022 aarch64 GNU/Linux</code>
  
*<code>Code tag</code>
+
====Compile drivers====
 +
<code>cd ~/raspberrypi_v4l2/driver_source/cam_drv_src/rpi-5.15_all</code>
  
<code>$ cp /usr/share/doc/raspberrypi-bootloader/changelog.Debian.gz ./</code>
+
<code>make</code>
  
<code>gunzip changelog.Debian.gz</code>
+
Done.
  
Check the top lines and you will know the tag.
+
====Compile dts====
=====There are two ways to get the source code of the version you want:=====
+
<code>cd ~/raspberrypi_v4l2/driver_source/dts/rpi-5.15.y</code>
  
#using git to clone the corresponding branch and checkout the corresponding tag.
+
<code>./build_dtbo.sh</code>
  
<code>git clone --branch rpi-5.4.y <nowiki>https://github.com/raspberrypi/linux</nowiki></code>
+
Done.
  
<code>git checkout raspberrypi-kernel_1.20201022-1</code>
+
====Common errors====
  
''PS: Please replace the above two commands with your own corresponding versions.''
+
=====Missing build directory=====
 +
<code>make[1]: *** /lib/modules/[version]/build: No such file or directory. Stop.</code>
  
2. Manually download the version code of the corresponding tag directly from the link below.
+
The cmd <code>sudo apt install raspberrypi-kernel-headers</code> will simply install the latest kernel headers available on the mirror. Which indeed matches the latest available kernel on the mirror. But not necessarily the installed kernel on the system.
  
https://github.com/raspberrypi/linux/tags
+
The Raspbian maintainers always remove the older kernel headers from the repository index files. Not sure why they do that.  
====Patch our  code to kernel====
 
  
*Driver source code
+
Solutions:
  
camera driver path is : linux/drivers/media/i2c,copy our camera module drivers to this path.
+
======1. Upgrade piOS, and build again.======
 +
<code>sudo apt update</code>
  
*Modify the the Makefile and Kconfig files
+
<code>sudo apt full-upgrade</code>
  
Modify the Config and Makefile in the same path,add the corresponding camera driver.
+
<code>sudo apt install raspberrypi-kernel-headers</code>
  
*dts file
+
Specifically, for the Raspberry Pi 4 series, 32-bit PiOS will automatically switch to 64-bit mode after upgrading to the latest version. However, the raspberrypi-kernel-headers package is missing the build directory for the v8+ mode.
  
dts file path is :linux/arch/arm/boot/dts/overlays,copy our [camera]-overlay.dts files to this path.
+
For example:
  
*Modify dts Makefile
+
<code>$ uname -a</code>
  
Modify the Makefile in the same path,add the corresponding dts compilation option.
+
<code>Linux raspberrypi 6.1.21-v8+ #1642 SMP PREEMPT Mon Apr 3 17:24:16 BST 2023 aarch64 GNU/Linux</code>
===Building===
 
====Preparing====
 
  
*For Raspberry Pi 1, Zero and Zero W, and Raspberry Pi Compute Module 1 default (32-bit only) build configuration
+
There is no build directory under <code>/lib/modules/6.1.21-v8+/</code>.
  
<code>KERNEL=kernel</code>
+
If this happens, you can add <code>arm_64bit=0</code> to the <code>/boot/config.txt</code> file and then restart the Raspberry Pi to switch back to 32-bit mode.
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig</code>
+
======2. Install specific version kernel headers.======
 +
Download the deb package for the version of piOS you are currently using from this [https://archive.raspberrypi.org/debian/pool/main/r/raspberrypi-firmware/ link] and install it.
  
*For Raspberry Pi 2, 3, 3+ and Zero 2 W, and Raspberry Pi Compute Modules 3 and 3+ default 32-bit build configuration
+
For tag name, please determine according to the local piOS version and raspberrypi OS [https://github.com/raspberrypi/linux/tags tags].
  
<code>KERNEL=kernel7</code>
+
======3. Use rpi-source======
 +
Use rpi-source from <nowiki>https://github.com/RPi-Distro/rpi-source</nowiki> it sets up everything you need to build your own kernel (from the current running kernel by default).
 +
===Install the compiled driver===
 +
Once compiled, you get the specified version of the driver and dtbo file.
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig</code>
+
Refer to [[V4L2 mode for Raspberry Pi#Driver Installation|How to install driver]],
  
*For Raspberry Pi 4 and 400, and Raspberry Pi Compute Module 4 default 32-bit build configuration
+
<code>wget <nowiki>https://github.com/veyeimaging/raspberrypi_v4l2/releases/latest/download/raspberrypi_v4l2.tgz</nowiki></code>
  
<code>KERNEL=kernel7l</code>
+
<code>tar -xzvf raspberrypi_v4l2.tgz</code>
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig</code>
+
<code>cd raspberrypi_v4l2/release/</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>mkdir driver_bin/$(uname -r)</code>
  
<code>KERNEL=kernel8</code>
+
Put the driver and dtbo files compiled into this directory.
  
<code>make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig</code>
+
<code>chmod +x *</code>
====Add compilation options====
 
  
*32-bit version
+
<code>sudo ./install_driver.sh [camera module]</code>
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig</code>
+
camera module:could be veye327,csimx307,cssc132,veyecam2m,etc.
 
+
===References===
*64-bit version
+
https://www.raspberrypi.org/documentation/linux/kernel/building.md
 
 
<code>make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig</code>
 
 
 
Add the compilation options by the corresponding camera module, for 5.4 version kernel the path is driver-- > multimedia-- > i2C.
 
  
For 5.10 version kernel,the path is Device Drivers --> Multimedia Support --> Media ancillary drivers --> Camera sensor devices.
+
===Document History===
=====Build=====
 
  
*<code>32-bit version</code>
+
* 20260321
  
<code>make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j4</code>
+
Delete the section related to cross-compilation.
  
*<code>64-bit version</code>
+
*20230510
  
<code>make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- Image modules dtbs -j4</code>
+
Add  solution for"Missing build directory" error.
===Frequently Questions===
 
  
*disagrees about version of symbol module_layout
+
*20230326
  
After the system has been booted, <code>dmesg</code> can see this error reported when the driver of camera is loaded. It is caused by the mismatch between the original piOS version on the board and the compiled camera driver version.
+
Add description and solution for "Missing build directory" error.
  
It is recommended to refer to this [https://www.raspberrypi.com/documentation/computers/linux_kernel.html#cross-compiling-the-kernel link] to recompile from the code and install Image, dtb, modules as a whole.
+
*20220424
===References===
 
https://www.raspberrypi.org/documentation/linux/kernel/building.md
 
  
=== Document History ===
+
Add local build method.
  
* 20220411
+
*20220411
  
 
Add description of MV series.
 
Add description of MV series.

Latest revision as of 07:48, 21 March 2026

查看中文

The main resources in this section are the official piOS building method.

We provide methods to compile locally on Raspberry Pi and cross-compile on PC. It is recommended to use Raspberry Pi local compilation, which is simpler and faster.

1 Building the Drivers Locally

The following operations are performed on the Raspberry Pi.

The following is an example of piOS 5.15.32.

1.1 Prepare environment

sudo apt install git bc bison flex libssl-dev make

1.2 Download the kernel headers for the local piOS

sudo apt install raspberrypi-kernel-headers

1.3 Download drivers code from veye

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

1.4 Confirm the version of piOS on your own Raspberry Pi

$ uname -a

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

1.5 Compile drivers

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

make

Done.

1.6 Compile dts

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

./build_dtbo.sh

Done.

1.7 Common errors

1.7.1 Missing build directory

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

The cmd sudo apt install raspberrypi-kernel-headers will simply install the latest kernel headers available on the mirror. Which indeed matches the latest available kernel on the mirror. But not necessarily the installed kernel on the system.

The Raspbian maintainers always remove the older kernel headers from the repository index files. Not sure why they do that.

Solutions:

1.7.1.1 1. Upgrade piOS, and build again.

sudo apt update

sudo apt full-upgrade

sudo apt install raspberrypi-kernel-headers

Specifically, for the Raspberry Pi 4 series, 32-bit PiOS will automatically switch to 64-bit mode after upgrading to the latest version. However, the raspberrypi-kernel-headers package is missing the build directory for the v8+ mode.

For example:

$ uname -a

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

There is no build directory under /lib/modules/6.1.21-v8+/.

If this happens, you can add arm_64bit=0 to the /boot/config.txt file and then restart the Raspberry Pi to switch back to 32-bit mode.

1.7.1.2 2. Install specific version kernel headers.

Download the deb package for the version of piOS you are currently using from this link and install it.

For tag name, please determine according to the local piOS version and raspberrypi OS tags.

1.7.1.3 3. Use rpi-source

Use rpi-source from https://github.com/RPi-Distro/rpi-source it sets up everything you need to build your own kernel (from the current running kernel by default).

2 Install the compiled driver

Once compiled, you get the specified version of the driver and dtbo file.

Refer to How to install driver

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)

Put the driver and dtbo files compiled into this directory.

chmod +x *

sudo ./install_driver.sh [camera module]

camera module:could be veye327,csimx307,cssc132,veyecam2m,etc.

3 References

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

4 Document History

  • 20260321

Delete the section related to cross-compilation.

  • 20230510

Add solution for"Missing build directory" error.

  • 20230326

Add description and solution for "Missing build directory" error.

  • 20220424

Add local build method.

  • 20220411

Add description of MV series.