openwrt教程(OpenWRT驱动开发)

路由器教程
路由器教程
路由器教程
657
文章
0
评论
2023年2月6日09:00:37 路由器教程 阅读 10,730

一、OpenWRT中的驱动

Openwrt源码中,所有扩展的软件包到在package目录下,自己添加的应用放在该目录下。

所有扩展的内核驱动package/kernel目录下,自己添加的驱动放在该目录下。

openwrt教程(OpenWRT驱动开发)

二、添加驱动步骤

  • 在package/kernel目录下添加一个helloworld文件夹
  • 在helloworld添加一个Makefile文件。具体Makefile怎么写,我们先看两个kernel中自带的驱动的Makefile文件。
  • I2C驱动

    ## Copyright (C) 2008 OpenWrt.org## This is free software, licensed under the GNU General Public License v2.# See /LICENSE for more information.#   I2C驱动#包含上两级目录配置include $(TOPDIR)/rules.mk          # TOPDIR : /openwrtinclude $(INCLUDE_DIR)/kernel.mk    # INCLUDE_DIR: /openwrt/includePKG_NAME:=i2c-gpio-custom           #驱动名字PKG_RELEASE:=2                      #Makefile的版本号include $(INCLUDE_DIR)/package.mk   #包含上一级目录配置# make menuconfig时在配置界面上显示的方式define KernelPackage/i2c-gpio-custom SUBMENU:=I2C support                 #显示的子选项 TITLE:=Custom GPIO-based I2C device  #显示的内容 DEPENDS:=@GPIO_SUPPORT +kmod-i2c-core +kmod-i2c-gpio  #依赖于其他模块 FILES:=$(PKG_BUILD_DIR)/i2c-gpio-custom.ko    #生成的目标文件 KCONFIG:=endefdefine KernelPackage/i2c-gpio-custom/descriptionKernel module for register a custom i2c-gpio platform device. #帮助时显示的内容endefEXTRA_KCONFIG:=    CONFIG_I2C_GPIO_CUSTOM=m    #编译成模块# 无需更改EXTRA_CFLAGS:=    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG))))    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) # 无需更改MAKE_OPTS:=    ARCH="$(LINUX_KARCH)"            #CPU架构    CROSS_COMPILE="$(TARGET_CROSS)"  #交叉编译工具   SUBDIRS="$(PKG_BUILD_DIR)"    EXTRA_CFLAGS="$(EXTRA_CFLAGS)"    $(EXTRA_KCONFIG)#编译前的准备define Build/Prepare   mkdir -p $(PKG_BUILD_DIR)   $(CP) ./src/* $(PKG_BUILD_DIR)/  #PKG_BUILD_DIR:编译时的目录,对应于驱动就是 build_dir/target-**/linux-**/endef#编译define Build/Compile   $(MAKE) -C "$(LINUX_DIR)"        $(MAKE_OPTS)        modulesendef#完成前面定义后,必须使用 eval 函数实现各种定义, KernelPackage代表是内核驱动模块,Package表示应用程序$(eval $(call KernelPackage,i2c-gpio-custom))

    RTC驱动

    ## Copyright (C) 2006-2009 OpenWrt.org## This is free software, licensed under the GNU General Public License v2.# See /LICENSE for more information.#   RTC驱动include $(TOPDIR)/rules.mkinclude $(INCLUDE_DIR)/kernel.mkPKG_NAME:=rtc-rv5c386aPKG_RELEASE:=1include $(INCLUDE_DIR)/package.mkdefine KernelPackage/rtc-rv5c386a SUBMENU:=Other modules DEPENDS:=@TARGET_brcm47xx TITLE:=Driver for RTC RV5C386A (used in WL-700gE and WL-HDD) AUTOLOAD:=$(call AutoLoad,70,rtc) FILES:=$(PKG_BUILD_DIR)/rtc.koendefdefine Build/Prepare   mkdir -p $(PKG_BUILD_DIR)   $(CP) ./src/* $(PKG_BUILD_DIR)/endef#有时候CROSS_COMPILE、ARCH、SUBDIRS、EXTRA_CFLAGS会被独立成一个变量,然后再去引用define Build/Compile   $(MAKE) -C "$(LINUX_DIR)"        CROSS_COMPILE="$(TARGET_CROSS)"        ARCH="$(LINUX_KARCH)"        SUBDIRS="$(PKG_BUILD_DIR)"        EXTRA_CFLAGS="$(BUILDFLAGS)"        modulesendef$(eval $(call KernelPackage,rtc-rv5c386a))

    根据上面的注释和解释,并对比两份Makefile,我们发现写的基本结构都是相似的,所以我们可以根据上面的模板来自行写一份。

    以I2C为模板:

    ## Copyright (C) 2008 OpenWrt.org## This is free software, licensed under the GNU General Public License v2.# See /LICENSE for more information.#include $(TOPDIR)/rules.mkinclude $(INCLUDE_DIR)/kernel.mkPKG_NAME:=helloworldPKG_RELEASE:=2include $(INCLUDE_DIR)/package.mkdefine KernelPackage/helloworld SUBMENU:=Other modules TITLE:=HelloWorld FILES:=$(PKG_BUILD_DIR)/helloworld.ko KCONFIG:=endefdefine KernelPackage/helloworld/descriptionKernel module for register helloworld.endefEXTRA_KCONFIG:=    CONFIG_HELLOWORLD=mEXTRA_CFLAGS:=    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG))))    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) MAKE_OPTS:=    ARCH="$(LINUX_KARCH)"    CROSS_COMPILE="$(TARGET_CROSS)"    SUBDIRS="$(PKG_BUILD_DIR)"    EXTRA_CFLAGS="$(EXTRA_CFLAGS)"    $(EXTRA_KCONFIG)define Build/Prepare   mkdir -p $(PKG_BUILD_DIR)   $(CP) ./src/* $(PKG_BUILD_DIR)/endefdefine Build/Compile   $(MAKE) -C "$(LINUX_DIR)"        $(MAKE_OPTS)        modulesendef$(eval $(call KernelPackage,helloworld))

    基本上就是I2C改成helloworld就OK了!

    3、在helloworld文件夹下再新建一个src文件夹,在src下新建一个Kconfig文件。文件内容:

    ## Copyright (C) 2008 OpenWrt.org## This is free software, licensed under the GNU General Public License v2.# See /LICENSE for more information.#include $(TOPDIR)/rules.mkinclude $(INCLUDE_DIR)/kernel.mkPKG_NAME:=helloworldPKG_RELEASE:=2include $(INCLUDE_DIR)/package.mkdefine KernelPackage/helloworld SUBMENU:=Other modules TITLE:=HelloWorld FILES:=$(PKG_BUILD_DIR)/helloworld.ko KCONFIG:=endefdefine KernelPackage/helloworld/descriptionKernel module for register helloworld.endefEXTRA_KCONFIG:=    CONFIG_HELLOWORLD=mEXTRA_CFLAGS:=    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=m,%,$(filter %=m,$(EXTRA_KCONFIG))))    $(patsubst CONFIG_%, -DCONFIG_%=1, $(patsubst %=y,%,$(filter %=y,$(EXTRA_KCONFIG)))) MAKE_OPTS:=    ARCH="$(LINUX_KARCH)"    CROSS_COMPILE="$(TARGET_CROSS)"    SUBDIRS="$(PKG_BUILD_DIR)"    EXTRA_CFLAGS="$(EXTRA_CFLAGS)"    $(EXTRA_KCONFIG)define Build/Prepare   mkdir -p $(PKG_BUILD_DIR)   $(CP) ./src/* $(PKG_BUILD_DIR)/endefdefine Build/Compile   $(MAKE) -C "$(LINUX_DIR)"        $(MAKE_OPTS)        modulesendef$(eval $(call KernelPackage,helloworld))

    再新建一个Makefile文件,内容为:

    obj-${CONFIG_HELLOWORLD}   += helloworld.o

    zui后新建一个helloworld.c的驱动文件,内容为:

    #include <linux/module.h>#include <linux/kernel.h>//insmod会执行该函数static int __init helloworld_init(void){   printk("helloworld driver init !n");   return 0;}//rmmod会执行该函数static int __exit helloworld_exit(void){   printk("helloworld driver exit !n");   return 0;}//声明出入口函数module_init(helloworld_init);module_exit(helloworld_exit);MODULE_AUTHOR("YANG");MODULE_LICENSE("GPL");

    4、接下来就是编译了先make menuconfig将驱动配置一下。

    openwrt教程(OpenWRT驱动开发)openwrt教程(OpenWRT驱动开发)

    openwrt教程(OpenWRT驱动开发)openwrt教程(OpenWRT驱动开发)

    openwrt教程(OpenWRT驱动开发)openwrt教程(OpenWRT驱动开发)

    保存后执行makepackage/kernel/helloworld/compile V=99

    5、编译出来的驱动模块在openwrt/bin/ramips/packages/kernel/kmod-helloworld_4.4.7-2_ramips_24kec.ipk

    以上教程由“WiFi之家网”整理收藏!

    相关推荐