辅助工具
项 | 描述 |
---|---|
MobaXterm | 提供wsl终端和串口终端 |
WSL | 提供Windows下Linux环境 |
以下前提:
1.已经安装MobaXterm 和 WSL(下方linux命令均在WSL中执行)
2.开发板已烧录Linux系统和根文件系统,且支持rz/sz命令
内核编译
1.解压板商修改的内核源码
cd ~
cp /mnt/d/6818xxx.tar.gz ~
tar -xzvf 6818xxx.tar.gz
vim ~/6818xxx/kernel/include/linux/compiler-gcc5.h #解决wsl编译器版本高问题
文件compiler-gcc5.h的内容
#ifndef __LINUX_COMPILER_H
#error "Please don't include <linux/compiler-gcc5.h> directly, include <linux/compiler.h> instead."
#endif
#define __used __attribute__((__used__))
#define __must_check __attribute__((warn_unused_result))
#define __compiler_offsetof(a, b) __builtin_offsetof(a, b)
#define __cold __attribute__((__cold__))
#define __UNIQUE_ID(prefix) __PASTE(__PASTE(__UNIQUE_ID_, prefix), __COUNTER__)
#ifndef __CHECKER__
# define __compiletime_warning(message) __attribute__((warning(message)))
# define __compiletime_error(message) __attribute__((error(message)))
#endif /* __CHECKER__ */
#define unreachable() __builtin_unreachable()
#define __noclone __attribute__((__noclone__))
#define __visible __attribute__((externally_visible))
#define asm_volatile_goto(x...) do { asm goto(x); asm (""); } while (0)
#ifdef CONFIG_ARCH_USE_BUILTIN_BSWAP
#define __HAVE_BUILTIN_BSWAP32__
#define __HAVE_BUILTIN_BSWAP64__
#define __HAVE_BUILTIN_BSWAP16__
#endif /* CONFIG_ARCH_USE_BUILTIN_BSWAP */
2.配置内核源码
sudo apt-get install ncurses-dev #安装终端图形库
cd ~/6818xxx/kernel
make xxx6818_defconfig #准备新的配置文件
make menuconfig #使用内核配置菜单生成配置,窗口先拉大点
3.编译内核源码
cd ~/6818xxx
./mk -a -j=16 #使用脚本编译所有,16线程编译
模块编译
1.安装交叉编译工具链
cd ~
cp /mnt/d/arm-linux-gnueabi-5.4.0.tar.xz ~
sudo tar xvjf arm-linux-gnueabi-5.4.0.tar.xz
mkdir opt
mv /usr/local/arm ~/opt/
vim ~/.bashrc #配置环境变量
source ~/.bashrc
文件~/.bashrc
最后一行添加如下内容:
export PATH="$HOME/opt/arm/5.4.0/usr/bin/:$PATH"
2.编写驱动及Makefile
cd ~
mkdir hello
vim hello.c
vim Makefile
驱动文件内容:
#include <linux/kernel.h>
#include <linux/module.h>
static int __init xxx6818_hello_module_init(void)
{
printk("<0>""Hello, hello module is installed !\n");
return 0;
}
static void __exit xxx6818_hello_module_cleanup(void)
{
printk("<0>""Good-bye, hello module was removed!\n");
}
module_init(xxx6818_hello_module_init);
module_exit(xxx6818_hello_module_cleanup);
MODULE_LICENSE("GPL");
Makefile内容:
ifneq ($(KERNELRELEASE),)
obj-m:=hello.o
else
KERNELDIR:=/home/long/6818xxx/kernel
CROSS_COMPILE:=/home/long/opt/arm/5.4.0/usr/bin/arm-none-linux-gnueabi-
PWD:=$(shell pwd)
default:
$(MAKE) ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *.order .*.cmd *.ko *.mod.c *.symvers
endif
install: Hello.ko
cp --target-dir=/mnt/d/ hello.ko
3.编译驱动模块
make
make install
4.驱动模块拷贝到设备
启动开发板,打开MobaXterm连接开发板串口,右键“Send file using Z-modem"选择d盘下ko文件
5.安装测试驱动模块
insmod hello.ko
lsmod |grep hello
rmmod hello
其他
1.内核配置菜单说明
项 | 描述 |
---|---|
<> | 三态配置(选/不选/模块) |
[] | 布尔配置(选/不选) |
() | 字符串配置 |
—> | 还有子菜单 |
按键Y | 选择 |
按键N | 不选 |
按键M | 作为模块 |
空格键 | 切换Y/N/M状态 |
2.内核配置菜单需要至少19*80的命令行窗口