zoukankan      html  css  js  c++  java
  • Linux驱动开发之开篇--HelloWorld

      Linux驱动的编写,大致分为两个过程,第一个过程为测试阶段,即为某一具体的设备,添加必要的驱动模块,为了节省编译时间,需要将代码单独放在一处,在编译时,只需要要调用内核的头文件即可;第二个过程为布置阶段,即为某一具体设备,添加完整、可靠的驱动模块,即该过程将驱动模块编译到内核镜像文件中,它需要将驱动模块的代码,添加到内核源码树中,下面将逐一介绍。

    阶段一

    1 新建一目录,并编写测试代码

    mkdir -p develop/drivers/
    cd develop/drivers/ vim demo.c
    //driver/demo/demo.c 
    #include "linux/init.h"
    #include "linux/module.h"
    
    static int hello_init(void)
    {
            printk(KERN_ALERT"Hello World linux-driver-module
    ");//注意,必须是KERN_ALEAT及以上级别,不然不会打印
            return 0;
    }
    
    static int hello_exit(void)
    {
            printk(KERN_ALERT"Goodbye linux-driver-module
    ");
            return 0;
    }
    
    module_init(hello_init);
    module_exit(hello_init);
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("gjianw217@163.com");

    2 添加编译文件Makefile

    ARCH=arm
    CROSS_COMPILE=arm-linux-gnueabihf-
    APP_COMPILE=arm-linux-
    #obj-m := pwm_test-drv.o
    obj-m := hello-drv.o
    KDIR := /path/to/kernel/linux/
    PWD := $(shell pwd)
    default:
        make -C $(KDIR) M=$(PWD) ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) modules
    app:hello-app.c
        $(APP_COMPILE)gcc -o helloappe hello-app.c
    clean:
        $(MAKE) -C $(KDIR) M=$(PWD) clean

    3 编译并加载到板上

    make
    Download thd demo.ko into the Development Board
    arm/drivers/demo.ko

    4  驱动模块的测试

    cd /arm/drivers/
    insmod demo.ko
    lsmod   demo.ko
    rmmod demo.ko
    
    dmesg

    阶段二

    1 将驱动代码按类型放到内核源码树相应目录下(drivers/demo/demo.c)

    //driver/demo/demo.c 
    #include "linux/init.h"
    #include "linux/module.h"
    
    static int hello_init(void)
    {
            printk(KERN_ALERT"Hello World linux-driver-module
    ");
            return 0;
    }
    
    static int hello_exit(void)
    {
            printk(KERN_ALERT"Goodbye linux-driver-module
    ");
            return 0;
    }
    
    module_init(hello_init);
    module_exit(hello_init);
    
    MODULE_LICENSE("GPL");
    MODULE_AUTHOR("lpj");
    View Code

    2 在当前目录下,分别添加编译文件

     driver/demo/Makefile

    #driver module demo
    obj-$(CONFIG_HELLODRV) += hello.o

    driver/demo/Kconfig

    menu USER_DEVICE_DRIVERS //4中可以看到它
    config HELLODRV
    tristate "Hello"
    help
    This is a demo driver programming.
    endmenu

    3 修改上层目录文件,将其添加到内核中

    driver/Makefile

    obj-$(CONFIG_HELLODRV)+=demo/

    driver/Kconfig 

    source drivers/demo/Kconfig

    arch/arm/Kconfig

    source "drivers/demo/Kconfig"

    4进行系统配置并编译

    make menuconfig
    make

    5加载内核镜像文件到板上,使用命令检查

    lsmod 
    dmesg

    参考链接

    【1】Linux驱动模块编译进内核中

    【2】Linux内核驱动学习 

  • 相关阅读:
    FILTER(过滤器)
    HDFS优缺点
    python运算符
    python变量类型
    python变量存储
    python编码问题
    【一:定义】python 简介
    如何学一门新技术
    Django安装
    redis 安装及启动关闭
  • 原文地址:https://www.cnblogs.com/gjianw217/p/4497799.html
Copyright © 2011-2022 走看看