For Driver course (www.ittraining.com.tw)
1) 安裝toolchain (Cross compiler)
# For 32 位元Linux
export PATH=/home/student/pi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin:$PATH
# For 64 位元Linux
export PATH=/home/student/pi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi-x64/bin:$PATH
root@ubuntu:/home/student/pi# arm-linux-gnueabihf-gcc -v
..
gcc version 4.8.3 20140303 (prerelease) (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03)
2) Linux kernel 編譯
#進入kernel source tree
cd linux
# 產生 .config
For Raspberry Pi 2, Pi 3, Pi 3+, and Compute Module 3 default build configuration
KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
For Raspberry Pi 4
KERNEL=kernel7l
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2711_defconfig
#編譯 Linux kernel、kernel module 及 device tree
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j 2
# 安裝 kernel modulemake ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- INSTALL_MOD_PATH=../modules modules_install
sudo cp arch/arm/boot/zImage /boot/
$KERNEL.img
sudo cp arch/arm/boot/dts/*.dtb /boot/
sudo cp arch/arm/boot/dts/overlays/*.dtb* /boot/overlays/
#複製 kernel modules 到 Raspberry Pi
tar zcvf modules.tar.gz modules
scp modules.tar.gz root@192.168.1.141:/root
tar zxf modules.tar.gz
解開後將 <kernel version> 目錄放到 /lib/modules/
----------------------------------------------------
在 Raspberry Pi 編譯kernel module
1.) copy kernel source to Raspberry Pi
scp linux-rpi-4.14.98-v7.tar.gz root@192.168.1.157:/root
tar zxf linux-rpi-4.14.98-v7.tar.gz
2.)建立連結到 kernel source
cd /lib/modules/4.14.98-v7
ln -sf /root/linux-rpi-4.14.98-v7 build
------------------------------------------------------------------------------------
3.) build driver module from source
cd kernel_driver/01_LED
.
├── chr_led.c
├── Makefile
└── test.c
make
├── chr_led.c
├── chr_led.ko
├── chr_led.mod.c
├── chr_led.mod.o
├── chr_led.o
├── Makefile
├── modules.order
├── Module.symvers
└── test.c
發生 fixdep error! Exec format error
進入kernel source 重新產生ARM版本的工具集
cd /root/linux-rpi-4.14.98-v7
make scripts
4.) 測試 module
#載入模組sudo insmod chr_led.ko
#顯示所有模組
lsmod
#用user應用程式測試driver
gcc test.c -o test
sudo ./test ==> 你會看見LED 在閃礫
#卸載模組
rmmod chr_led
沒有留言 :
張貼留言