一、环境
1.本机
虚拟机搭建ubuntu140.4 64bit
2.目标板
Debian
3.交叉编译器
arm-linux-gcc
友善版本为4.9.3
git clone https://github.com/friendlyarm/prebuilts.git
sudo mkdir -p /opt/FriendlyARM/toolchain
sudo tar xf prebuilts/gcc-x64/arm-cortexa9-linux-gnueabihf-4.9.3.tar.xz -C /opt/FriendlyARM/toolchain/
二、步骤
1.确定本机版本
$ uname -r
3.16.0-30-generic
2.确定目标板版本
$ uname -r
3.4.39-s5p4418
3.下载源码树
git clone https://github.com/friendlyarm/linux-3.4.y.git cd linux-3.4.y git checkout nanopi2-lollipop-mr1
4.编译源码树
make nanopi2_linux_defconfig
touch .scmversion
make uImage
的这之前可能需要准备mkimage
5.编写hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void)
{
printk(KERN_ALERT "Hello, world\n");
return 0;
}
static void hello_exit(void)
{
printk(KERN_ALERT"Goodbye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
6.编写Makefile
obj-m := hello.o
KERNELDIR := /home/inmen/FriendlyARM/linux-3.4.y
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
其中KERNELDIR需要修改,定位到本机的源码树目录
7.生成ko文件
执行make
8.检查ko文件信息
$ modinfo hello.ko
filename: /home/inmen/nanopi2Driver/hello.ko
license: Dual BSD/GPL
depends:
vermagic: 3.4.39-s5p4418 SMP preempt mod_unload ARMv7 p2v8
注意:vermagic中信息应与目标板信息一致,否则不能挂载
9.在目标板挂载驱动模块
三、参考资料
【友善官司方资料】