新闻  |   论坛  |   博客  |   在线研讨会
yocto环境学习--新建driver recipe
电子禅石 | 2020-12-07 19:23:10    阅读:1243   发布文章

1.建立目录

yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp$ cd sources/meta-elmo

yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/sources/meta-elmo$ mkdir recipes-kernel
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/sources/meta-elmo$ mkdir recipes-kernel/chardriver


2.建立bb文件
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/sources/meta-elmo/recipes-kernel/chardriver$ vi chardriver_0.1.bb
#yocto recipe to build a kernel module out of the kernel tree

DESCRIPTION = "Chardriver kernel module out of the kernel tree"
LICENSE = "CLOSED"

PROVIDES = "virtual/kernel-module-chardriver"
RPROVIDES_${PN} = "virtual/kernel-module-driver"
RPROVIDES_${PN}-dev = "virtual/kernel-module-driver"

#PR = "r0"

inherit module

SRC_URI = "file://chardriver.c \
                file://Makefile \
"

S = "${WORKDIR}"
~                  


3.编写Makefile

yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/sources/meta-elmo/recipes-kernel/chardriver$ mkdir files
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/sources/meta-elmo/recipes-kernel/chardriver$ vi files/Makefile
obj-m := chardriver.o

SRC := $(shell pwd)

all:
        $(MAKE) -C $(KERNEL_SRC) M=$(SRC)

modules_install:
        $(MAKE) -C $(KERNEL_SRC) M=$(SRC) modules_install

clean:
        rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c
        rm -f Module.markers Module.symvers modules.order
        rm -rf .tmp_versions Modules.symvers


4.编写驱动C文件
#include <linux/module.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/mm.h>
#include <linux/cdev.h>
#include <linux/errno.h>
#include <linux/sched.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>
#include <linux/device.h>

#define simple_MAJOR 200  

static unsigned char simple_inc=0;
static char demoBuffer[256];

int simple_open(struct inode *inode,struct file *filp)
{
    if(simple_inc>0) return -ERESTARTSYS;
    simple_inc++;
    memset(demoBuffer,0,256);
    return 0;
}
int simple_release(struct inode *inode,struct file *filp)
{
        if(simple_inc<=0){
                printk("close error");
                return -EFAULT;
        }
        simple_inc--;
    return 0;
}
ssize_t simple_read(struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
{
        if(copy_to_user(buf,demoBuffer,count)){
                count=-EFAULT;
        }
        return count;
}

size_t simple_write(struct file *file,const char __user *buf,size_t count,loff_t *f_pos)
{
        if (copy_from_user(demoBuffer + *f_pos,buf,count)){
                count = -EFAULT;
        }
        return count;
}


static const struct file_operations simple_fops={
        .owner=THIS_MODULE,
        .open=simple_open,
        .release=simple_release,
        .read=simple_read,
        .write=simple_write,
};

int simple_init_module(void)
{
        int ret;
        struct class *simple_class;

        memset(demoBuffer,0,256);
        ret=register_chrdev(simple_MAJOR,"simple",&simple_fops);
        if(ret<0){
                printk("Unable to register character device %d!/n",simple_MAJOR);
                return ret;
        }
        simple_class = class_create(THIS_MODULE, "simple_driver");
        device_create(simple_class, NULL, MKDEV(simple_MAJOR,0), NULL, "simple_device");

        printk("simple_init_module OK!\n");
        return 0;
}

void simple_cleanup_module(void)
{
        unregister_chrdev(simple_MAJOR,"simple");
        printk("simple_cleanup_module!\n");
}
module_init(simple_init_module);
module_exit(simple_cleanup_module);

MODULE_LICENSE("Dual BSD/GPL");


5.编译
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ bitbake chardriver
Loading cache: 100% |###################################################################################################################################| ETA:  00:00:00
Loaded 2567 entries from dependency cache.
Parsing recipes: 100% |#################################################################################################################################| Time: 00:00:00
Parsing of 2051 .bb files complete (2050 cached, 1 parsed). 2566 targets, 157 skipped, 3 masked, 0 errors.
NOTE: Resolving any missing task queue dependencies

Build Configuration:
BB_VERSION        = "1.26.0"
BUILD_SYS         = "x86_64-linux"
NATIVELSBSTRING   = "Ubuntu-14.04"
TARGET_SYS        = "arm-poky-linux-gnueabi"
MACHINE           = "imx6ulevk"
DISTRO            = "poky"
DISTRO_VERSION    = "1.8"
TUNE_FEATURES     = "arm armv7a vfp neon callconvention-hard cortexa7"
TARGET_FPU        = "vfp-neon"
meta              
meta-yocto        = "(nobranch):83aa565d93aacae484976562ef1ae8dbbb6b2bc0"
meta-oe           
meta-multimedia   = "(nobranch):10d3c8f85280a0bf867a8e4f84bcda81c290d28e"
meta-fsl-arm      = "(nobranch):c5326c90d6754c8630504ae29244907b0841c3a9"
meta-fsl-arm-extra = "(nobranch):436b86e3421736216412bdbb21cd9cb08c758c75"
meta-fsl-demos    = "(nobranch):836bdf5a9d500ed258f1ddc07d89eca74a704098"
meta-bsp          
meta-sdk          = "(nobranch):2f59ef1080f616f391b1c488fb2204e570f90239"
meta-browser      = "(nobranch):4b27058a8275a5310161459c9bb8f4c52a77762c"
meta-gnome        
meta-networking   
meta-python       
meta-ruby         
meta-filesystems  = "(nobranch):10d3c8f85280a0bf867a8e4f84bcda81c290d28e"
meta-qt5          = "(nobranch):fc026381545650e10fdd488d5b2a3b2d78f87793"
meta-elmo         = "<unknown>:<unknown>"

NOTE: Preparing RunQueue
NOTE: Executing SetScene Tasks
NOTE: Executing RunQueue Tasks
NOTE: Tasks Summary: Attempted 434 tasks of which 420 didn't need to be rerun and all succeeded.
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$



编译过程文件在tmp/work/imx6ulevk-poky-linux-gnueabi/chardriver/0.1-r0/
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ ls tmp/work/imx6ulevk-poky-linux-gnueabi/chardriver/0.1-r0/
built-in.o    chardriver.ko     chardriver.mod.o  chardriver.spec   deploy-rpms  license-destdir  modules.order   package         pkgdata  sysroot-destdir
chardriver.c  chardriver.mod.c  chardriver.o      configure.sstate  image        Makefile         Module.symvers  packages-split  pseudo   temp
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$
查看tmp/work/imx6ulevk-poky-linux-gnueabi/chardriver/0.1-r0/chardriver.c 是跟我们写的驱动是一样的。


现在驱动编译是OK了,但查看镜像却没有我们的驱动,也是因为我们没把驱动加入系统
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ sudo mount -o loop -t ext3 tmp/deploy/images/imx6ulevk/fsl-image-qt5-imx6ulevk.ext3 /home/work/rootfs/
[sudo] password for yangzhiwen:
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ ls /home/work/rootfs/lib/modules/3.14.38-6UL_ga+ge4944a5/extra/
cryptodev.ko
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$

6.现在我们添加驱动
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ vi ../sources/meta-elmo/conf/layer.conf

# We have a conf and classes directory, add to BBPATH
BBPATH .= ":${LAYERDIR}"

# We have recipes-* directories, add to BBFILES
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb \
        ${LAYERDIR}/recipes-*/*/*.bbappend"

MACHINE_ESSENTIAL_EXTRA_RRECOMMENDS += "chardev"    #添加此2行代码,此行表示将驱动编译进系统,下面那行表示系统运行里加载驱动
KERNEL_MODULE_AUTOLOAD += "chardev"


IMAGE_INSTALL_append = "elmo-example"

BBFILE_COLLECTIONS += "elmo"
BBFILE_PATTERN_elmo = "^${LAYERDIR}/"
BBFILE_PRIORITY_elmo = "6"
~      

7.再编译和查看
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ bitbake fsl-image-qt5

yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ ls tmp/work/imx6ulevk-poky-linux-gnueabi/chardev/1.0-r0/
built-in.o    configure.sstate  image            package         pseudo                      sstate-install-package_qa         sstate-install-populate_sysroot
chardev.c     COPYING           license-destdir  packages-split  sstate-install-package      sstate-install-package_write_rpm  sysroot-destdir
chardev.spec  deploy-rpms       Makefile         pkgdata         sstate-install-packagedata  sstate-install-populate_lic       temp
yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ sudo mount -o loop -t ext3 tmp/deploy/images/imx6ulevk/fsl-image-qt5-imx6ulevk-20160127022253.rootfs.ext3 /home/data/rootfs/

yangzhiwen@yzw-kingsee:/home/work/workspace/fsl-release-bsp/build_qt5$ ls /home/data/rootfs/lib/modules/3.14.38-6UL_ga+ge4944a5/extra/
chardev.ko  cryptodev.ko

8.烧录到卡后log

Starting udev
udevd[156]: starting version 182
random: nonblocking pool is initialized
FAT-fs (mmcblk1p1): Volume was not properly unmounted. Some data may be corrupt. Please run fsck.
simple_init_module OK!

OK

https://blog.csdn.net/yangzhiwen56/article/details/50592845?locationNum=13

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
属于自己的技术积累分享,成为嵌入式系统研发高手。
推荐文章
最近访客