openwrt 编译驱动模块(在openwrt源代码外部任意位置编写代码,独立模块化编译.ko)

一、硬件平台

        MT7620(A9内核)


二、软件平台

       1、Ubuntu 12.04 

       2、openwrt 官方15.05版本SDK开发包


三、功能说明

   本文章主要介绍,如何编译 openwrt 驱动模块。
    近期在整理openwrt的驱动,但是网上的资料,大部分都是在 openwr 源代码 package 目录下,建立文件夹,然后在 openwrt 目录下编译。此方法虽然可以编译,但是维护起来不是特别方便。比如单独编译一个模块,还需要依赖于 openwrt 的SDK包。这样,每次需要增加驱动模块,都要在SDK包下建立目录。
    个人比较倾向于驱动模块单独建立文件夹,路径不做限制。这样方便代码维护管理,而且在git或者 svn 上传下载方便,与 openwrt 的源码SDK包分离开。
    本文章,主要介绍的就是,在任意地方,建立自己的文件夹和编写驱动,然后编译出驱动模块。最大的优势在于,代码的路径不受限制。如果需要修改某一个驱动,仅仅下载该驱动文件,不需要更新整个 openwrt SDK开发包。

四、操作步骤

      1、编写C文件

    在任意文件夹,例如本文章的路径为 /home/test/   编写代码,名称为“hello.c”。

#include 
#include 
#include 
/* hello_init ---- 初始化函数,当模块装载时被调用,如果成功装载返回0 否则返回非0值 */
static int __init hello_init(void)
{    
    printk("I bear a charmed life.\n");    
	return 0;
}

/* hello_exit ---- 退出函数,当模块卸载时被调用 */
static void __exit hello_exit(void)
{    
	printk("Out, out, brief candle\n");
}

module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");

    2、编写makefile

编译使用的makefile文件,文件名称为“makefile”。

说明:

本文中,openwrt 的SDK开发包存放路径为 /home/sky/develop/openWrt/openwrt/

openwrt 下载的 kernel ,存放路径为 /home/sky/develop/openWrt/openwrt/build_dir/target-mipsel_24kec+dsp_glibc-2.21/linux-ramips_mt7620/linux-3.18.29/

openwrt 的gcc 交叉编译工具链,路径为:/home/sky/develop/openWrt/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_glibc-2.21/bin/

对于本文中openwrt 的SDK包:

(1)交叉编译工具链为 glibc2.21

(2)目标芯片选择为mt7620

在makefile中,说明了gcc 和 openwrt 的SDK包路径,makefile 内容如下:

######################################## hello ################################
obj-m = hello.o
PWD=$(shell pwd)
KERNEL_DIR=/home/sky/develop/openWrt/openwrt/build_dir/target-mipsel_24kec+dsp_glibc-2.21/linux-ramips_mt7620/linux-3.18.29/
TOOLCHAIN="/home/sky/develop/openWrt/openwrt/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_glibc-2.21/bin/mipsel-openwrt-linux-gnu-"

###############################################################################
all:
	make -C $(KERNEL_DIR) \
	ARCH=mips \
	CROSS_COMPILE=$(TOOLCHAIN) \
	M=$(PWD) \
	modules
	
clean:
	rm -f *.ko
	rm -f *.o
	rm -f *.mod.c
	rm -f *.mod.o
	rm -f *.order
	rm -f *.sysvers
  
#make command:
#make
#make clean


3、编译

在终端中,进行编译,输入命令 :  make  

需要清空,则输入命令: make clean





你可能感兴趣的:(Linux,openwrt,驱动)