PC:
Ubuntu 14.04.6 LTS (GNU/Linux 4.4.0-142-generic x86_64)
树莓派:
Linux raspberrypi 4.19.118-v7+ #1311 SMP Mon Apr 27 14:21:24 BST 2020 armv7l
wyj@raspberrypi:~ $ uname -a
Linux raspberrypi 4.19.118-v7+ #1311 SMP Mon Apr 27 14:21:24 BST 2020 armv7l GNU/Linux
我这里的版本是4.19内核,接下来就需要pc端下载树莓派kernel对应版本的linux kernel,版本号一定要对应
找到相同版本的树莓派内核并下载
https://github.com/raspberrypi/linux/tree/rpi-4.19.y
wuyujun@wuyujun-virtual-machine:~/rasp_bap$ git clone https://github.com/raspberrypi/linux.git Rpi_linux
这里没有涉及到版本问题直接从下面网址下载https://github.com/raspberrypi/tools
git clone git://github.com/raspberrypi/tools.git RpiTools
因为git下载速度有点慢,下载交叉编译工具可以直接下载zip包,但是树莓派内核需要用git下载下来,用zip下载解压之后编译会出现奇怪的问题,困扰了我一天,后来重新用git clone下载树莓派内核编译就没问题了。
解压后,kernel在Rpi_linux目录 编译器等工具在tools-master目录
wuyujun@wuyujun-virtual-machine:~/rasp_bap$ sudo vim ~/.bashrc
在最后根据自己安装的路径加入环境变量
export PATH=$PATH:/home/wuyujun/rasp_bap/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin
使文件生效以及查看是否成功
wuyujun@wuyujun-virtual-machine:~/rasp_bap$ source ~/.bashrc
wuyujun@wuyujun-virtual-machine:~/rasp_bap$ echo $PATH
wuyujun@wuyujun-virtual-machine:~$ arm-linux-gnueabihf-gcc -v
wuyujun@wuyujun-virtual-machine:~$ which arm-linux-gnueabihf-gcc
/home/wuyujun/rasp_bap/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc
树莓派端执行如下命令获取内核配置
wyj@raspberrypi:~ $ sudo modprobe configs
wyj@raspberrypi:~ $ ls /proc/config.gz
会在/proc/目录下生成config.gz文件,将其复制到PC端下载好的树莓派kernel目录下面
wuyujun@wuyujun-virtual-machine:~/rasp_bap/Rpi_linux$ zcat config.gz > .config
PC进入Rpi_linux依次执行如下命令编译树莓派内核
wuyujun@wuyujun-virtual-machine:~/rasp_bap/Rpi_linux$ KERNEL=kernel7
wuyujun@wuyujun-virtual-machine:~/rasp_bap/Rpi_linux$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs
出现如下问题:
Makefile:590: include/config/auto.conf: 没有那个文件或目录
Makefile:621: include/config/auto.conf.cmd: 没有那个文件或目录
YACC scripts/kconfig/zconf.tab.c
/bin/sh: 1: bison: not found
make[2]: *** [scripts/kconfig/zconf.tab.c] 错误 127
make[1]: *** [syncconfig] 错误 2
make: *** [include/config/auto.conf.cmd] 错误 2
Makefile:590: include/config/auto.conf: 没有那个文件或目录
Makefile:621: include/config/auto.conf.cmd: 没有那个文件或目录
YACC scripts/kconfig/zconf.tab.c
LEX scripts/kconfig/zconf.lex.c
/bin/sh: 1: flex: not found
安装缺少的软件
wuyujun@wuyujun-virtual-machine:~/rasp_bap/Rpi_linux$ sudo apt-get install bison -y
wuyujun@wuyujun-virtual-machine:~/rasp_bap/Rpi_linux$ sudo apt-get install flex
还有出现的问题就上面说的下载zip之后编译内核出现目标缺少依赖的问题,删掉原先的重新用git下载树莓派内核来编译就好了。
再次编译
wuyujun@wuyujun-virtual-machine:~/rasp_bap/Rpi_linux$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs
创建目录
wuyujun@wuyujun-virtual-machine:~/rasp_bap$ mkdir -p driver/hello_world
wuyujun@wuyujun-virtual-machine:~/rasp_bap$ cd driver/hello_world/
编写hello world驱动程序
/*********************************************************************************
* Copyright: (C) 2020 Wu Yujun<[email protected]>
* All rights reserved.
*
* Filename: kernel_hello.c
* Description: This file is kernel printk hello world
*
* Version: 1.0.0(2020年07月08日)
* Author: Wu Yujun <[email protected]>
* ChangeLog: 1, Release initial version on "2020年07月08日 21时22分55秒"
*
********************************************************************************/
#include
#include
#include
static __init int hello_init(void)
{
printk(KERN_ALERT "Hello, World!\n");
return 0;
}
static __exit int hello_exit(void)
{
printk(KERN_ALERT "Good Bye!\n");
return 0;
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_AUTHOR("WuYuJun<[email protected]>");
MODULE_DESCRIPTION("Linux Kernel hello module");
MODULE_LICENSE("Dual BSD/GPL");
编写Makefile
LINUX_SRC = ${shell pwd}/../../Rpi_linux/
CROSS_COMPILE=/home/wuyujun/rasp_bap/tools-master/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
PWD := $(shell pwd)
EXTRA_CFLAGS+=-DMODULE
obj-m += kernel_hello.o
modules:
echo ${LINUX_SRC}
make -C $(LINUX_SRC) M=$(PWD) modules
make clear
clear:
rm -f *.o *.cmd *.mod.c
rm -rf *~ core .depend .tmp_versions Module.symvers modules.order -f
rm -f .*ko.cmd .*.o.cmd .*.o.d
make编译之后传到树莓派上,insmod加载驱动,rmmod卸载驱动,并用demsg查看内核日志
wyj@raspberrypi:~ $ sudo insmod kernel_hello.ko
wyj@raspberrypi:~ $ sudo rmmod kernel_hello
wyj@raspberrypi:~ $ dmesg
参考:https://blog.csdn.net/shanglin163/article/details/88828427