Openwrt character device driver development example

Chapter 4 Character Device Driver and Application Template Test

Character Device Driver Package Test

1. Put the template mydrv in the openwrt source specific directory openwrt/package/kernel/

mydrv template structure

2. Configure compilation support for the driver

  $ cd openwrt
  $ make menuconfig
  # 选择 Kernel modules -> Other modules -> kmod-mydrv
  # 按 y 选中
  # 保存退出

3. Character device driver package Makefile introduction

  include $(TOPDIR)/rules.mk
  include $(INCLUDE_DIR)/kernel.mk
  include $(INCLUDE_DIR)/package.mk #以上三个用来关联OpenWrt

  PKG_NAME:=mydrv #字符设备驱动模块名称
  PKG_RELEASE:=1 #版本号

  define KernelPackage/mydrv #内核模块
    SUBMENU:=Other modules #进行归类
    TITLE:=mydrv #标题
    FILES:=$(PKG_BUILD_DIR)/mydrv.ko #模块文件
    KCONFIG:=
  endef

  define KernelPackage/mydrv/description
    This is a mydrv drivers #描述
  endef

  MAKE_OPTS:= \ #编译前指定工具和内核arch等
    ARCH="$(LINUX_KARCH)" \
    CROSS_COMPILE="$(TARGET_CROSS)" \
    SUBDIRS="$(PKG_BUILD_DIR)"

  define Build/Prepare #编译前准备工作
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
  endef

  define Build/Compile #进行编译
    $(MAKE) -C "$(LINUX_DIR)" \
    $(MAKE_OPTS) modules
  endef

  $(eval $(call KernelPackage,mydrv))

Makefile in the src directory: obj-m += mydrv.otell the kernel that the module object file name added is mydrv.o

4. Compile the driver package

  $ cd openwrt
  $ make package/kernel/mydrv/compile V=99 #如果make有问题,试试root用户

The compiled firmware is in the directory openwrt/bin/ramips/packages/base/under the name ofkmod-mydrv_3.18.29-1_ramips_24kec.ipk

5. Load the driver module into the kernel and unload it

  # 将模块拷贝到 widora
  $ opkg install kmod-mydrv_3.14.25-1_ramips_24kec.ipk
  # 安装完成后在目录 `/lib/modules/3.18.29/`下可以找到驱动
  $ insmod mydrv.ko #加载
  $ rmmod mydrv.ko #卸载

load test

6. The driver is automatically loaded after booting

  $ cd /etc/modules.d
  $ vi 61-mydrv #61表示不使用外接usb类的自启动程序,分隔符‘-’后面接驱动模块名称!
  # 在创建的该文件中写入: mydrv 即驱动的名称。
  # 最后,必须保证 mydrv.ko 驱动模块文件放在`/lib/modules/3.14.25/`中
  $ reboot
  $ lsmod #令查询当前已经加载的驱动模块

Application Package Testing

1. Put the template mydrv_appapplication package in a specific directory of OpenWrtopenwrt/package/

2. Configure the compilation of OpenWrt support packages

  $ cd openwrt
  $ make menuconfig
  # 选择 Utilities -> mydrv_app -> y 选中

3. Introduction to the source code of the application software package

  #include <stdio.h>
  #include <string.h>
  #include <fcntl.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <sys/ioctl.h>

  int main(int argc , char** argv)
  {
    int fd;
    int num = 1;
    //打开驱动模块
    fd = open("dev/mydrv" , O_RDWR|O_NONBLOCK);
    if(fd < 0)
    {
        printf("can't open /dev/mydrv\n");
        return -1;
    }
    //函数测试
    write(fd,&num,1);
    read(fd,&num,1);
    ioctl(fd,1,1);
    close(fd);
    return 0;
  }

4. Compile the application package

  $ cd openwrt
  $ make package/mydrv_app/compile V=99
  $ make package/mydrv_app/install V=99
  $ make package/index V=99
  # 编译后的固件存放在目录 `openwrt/bin/ramips/packages/base`下,名称为`mydrv_app_1_ramips_24kec.ipk`

5. Installation and use of the application

  # 将程序拷贝到 widora
  $ opkg install mydrv_app_l_ramips_24kec.ipk
  $ mydrv_app #运行程序

installation test

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844404&siteId=291194637