Post

Cross-Compiling a Raspberry Pi Driver

Cross-Compiling a Raspberry Pi Driver

This article is migrated from Medium and translated by Gemini pro 2.5.


After a bunch of trial and error, combined with asking GPT in Mandarin and then in English (which gave me slightly different answers), I finally succeeded in cross-compiling a usable RPi3 Driver.

Although there were still some warnings during compilation, I’m just happy that I can successfully install the driver. I’ll deal with the warnings later.

Preparing the Build Environment

  1. You can only compile a Linux driver in a Linux environment. You can either set up a separate Ubuntu system or use the WSL service on Windows. I’ve tested both, and they work.

  2. Install the cross-compilation toolchain.

1
2
sudo apt-get update
sudo apt-get install crossbuild-essential-arm64
  1. Download the corresponding Kernel source.
1
2
git clone --depth=1 https://github.com/raspberrypi/linux
cd linux
  1. Prepare the required build modules/packages.
1
2
3
4
5
# Default config file
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcm2711_defconfig

# How to adjust if needed
make menuconfig

The bcm2711_defconfig file might have a slightly different name. You can check the folder mentioned in the error message for the latest filename. These two commands will create a .config file.

Since I don’t really know how to adjust the Kernel config, I just pressed Enter for all the default answers whenever the process asked for user confirmation.

Alternatively, you can copy the .config file directly from your Raspberry Pi. The command to download files from the Pi via SSH is as follows:

1
scp ssh_name@ssh_address:remote/date/path local/target/path

Once you have the .config file, you can use the following commands to prepare the rest of the required modules:

1
2
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- prepare
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules_prepare
  1. Modify the Makefile’s target path.
1
2
3
4
5
6
7
8
9
10
KERNEL_PATH=~/Download/linux
MODULE_PATH=$(PWD)

obj-m := hello.o

all:
        make -C $(KERNEL_PATH) M=$(MODULE_PATH) ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- modules

clean:
        make -C $(KERNEL_PATH) M=$(MODULE_PATH) ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- clean

There were still some warnings during compilation:

Warning

The contents of the cross-compiled driver (modinfo):

modinfo

Comparing it with the driver compiled directly on the Pi:

Comparation

Although I ran into some compilation warnings, installing (insmod) and removing (rmmod) the driver both work normally.

This post is licensed under CC BY 4.0 by the author.