如何在板上编译驱动

From BananaPro/Pi
Revision as of 10:06, 30 June 2016 by Support-Tab (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Other languages:
English • ‎中文(简体)‎

如何制作 Linux Kernel Headers

在制作Linux Kernel Headers前,要先安装必要的工具和库:

# apt-get install build-essential libncurses5-dev

获取Linux内核源码

# git clone https://github.com/LeMaker/linux-actions.git

生成 .config文件

# cd linux-actions 
# make O=/usr/src/linux-headers-$(uname -r) atm705a_s500_defconfig

将中间文件(包括 .config)重定向到"O"所指向的目录。

生成 Linux Kernel Headers

# cd linux-actions
# make O=/usr/src/linux-headers-$(uname -r) modules_prepare

modules_prepare - Set up for building external modules
/usr/src/linux-headers-$(uname-r) 即最终的Linux Kernel Headers.在使用前,需在/lib/modules/$(uname-r)目录下创建软链接build,并指向/usr/src/linux-headers-$(uname-r). 如下:
Lnbuild.png

删除原/usr/src/linux-headers-$(uname-r)目录中的source软链接, 最终可用的Linux Kernel Headers如下图所示:
Module.symvers.png

如何在板上编译驱动

在板子上编译驱动,需将内核源码路径指向Linux Kernel Headers. 详情参考下述步骤:

创建驱动程序

创建测试驱动helloworld,如下图所示:
Leds-driver.png
helloworld.c: 驱动程序.

#include<linux/init.h>           
#include<linux/module.h>     
static int __init hello_init(void) 
{
     printk(KERN_WARNING "hello,world.\n");   
     return 0;
}
 
static void __exit hello_exit(void) 
{
     printk(KERN_WARNING "goodbye world\n");
 
}
 
module_init(hello_init);  
module_exit(hello_exit); 
MODULE_LICENSE("GPL");   
MODULE_AUTHOR("lemaker");   
MODULE_DESCRIPTION("Just for test");

Makefile:

KDIR := /lib/modules/`uname -r`/build # Point to Linux Kernel Headers
PWD := $(shell pwd)
 
obj-m := helloworld.o
 
default:
        make -C $(KDIR) M=$(PWD) modules
 
clean:
        rm -rf *.o *.cmd *.ko *.mod.c *.tmp_versions *.order *.symvers

编译驱动程序

# cd test
# make

执行上述命令,输出ko驱动文件,如下图所示:
Koout.png