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内核驱动学习 

  • 相关阅读:
    HDU 2955 Robberies(01背包)
    HDU 2602 Bone Collector(01背包)
    HUST 1352 Repetitions of Substrings(字符串)
    HUST 1358 Uiwurerirexb jeqvad(模拟解密)
    HUST 1404 Hamming Distance(字符串)
    HDU 4520 小Q系列故事――最佳裁判(STL)
    HDU 2058 The sum problem(枚举)
    【破解】修改程序版权、添加弹窗
    HDU 1407 测试你是否和LTC水平一样高(枚举)
    HDU 1050 Moving Tables(贪心)
  • 原文地址:https://www.cnblogs.com/gjianw217/p/4497799.html
Copyright © 2011-2022 走看看