一、硬件平臺
MT7620(A9內核)
二、軟件平臺
1、Ubuntu 12.04
2、openwrt 官方15.05版本SDK開發包
三、功能說明
近期在整理openwrt的驅動,但是網上的資料,大部分都是在 openwr 源代碼 package 目錄下,建立文件夾,然後在 openwrt 目錄下編譯。此方法雖然可以編譯,但是維護起來不是特別方便。比如單獨編譯一個模塊,還需要依賴於 openwrt 的SDK包。這樣,每次需要增加驅動模塊,都要在SDK包下建立目錄。
個人比較傾向於驅動模塊單獨建立文件夾,路徑不做限制。這樣方便代碼維護管理,而且在git或者 svn 上傳下載方便,與 openwrt 的源碼SDK包分離開。
本文章,主要介紹的就是,在任意地方,建立自己的文件夾和編寫驅動,然後編譯出驅動模塊。最大的優勢在於,代碼的路徑不受限制。如果需要修改某一個驅動,僅僅下載該驅動文件,不需要更新整個 openwrt SDK開發包。
四、操作步驟
1、編寫C文件
在任意文件夾,例如本文章的路徑爲 /home/test/ 編寫代碼,名稱爲“hello.c”。
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
/* 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