• i.MX 6ULL 驱动开发 二十九:向 Linux 内核中添加自己编写驱动


    一、概述

    Linux 内核编译流程如下:

    1、配置 Linux 内核。

    2、编译 Linux 内核。

    说明:进入 Linux 内核源码,使用 make help 参看相关配置。

    二、make menuconfig 工作原理

    1、menuconfig 它本身是一个软件,只提供图形界面配置的一些逻辑,并不负责提供内容。

    2、menuconfig 是内核源码树的各目录下的 kconfig 提供的。

    3、menuconfig 中所有选中配置项的相关值会保存到配置文件中(默认配置文件为 .config)。

    4、在编译内核时,Makefile 根据相关配置项选择需要编译的源码。

    三、Kconfig 语法

    参考文档:Documentation/kbuild/kconfig-language.txt

    Linux 驱动开发 六十五:《kconfig-language.txt》翻译_lqonlylove的博客-CSDN博客

    四、Linux 内核中 Kconfig 分析

    1、顶层 Kconfig 内容

    #
    # For a description of the syntax of this configuration file,
    # see Documentation/kbuild/kconfig-language.txt.
    #
    mainmenu "Linux/$ARCH $KERNELVERSION Kernel Configuration"
    
    config SRCARCH
    	string
    	option env="SRCARCH"
    
    source "arch/$SRCARCH/Kconfig"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • mainmenu标题栏
    • config:定义配置项
      • string数据类型
      • env:导入 Kconfig环境变量
    • source:读取的配置文件位置。
      在这里插入图片描述

    2、arch/arm/Kconfig 内容

    ……
    source "init/Kconfig" # 包含配置文件
    
    source "kernel/Kconfig.freezer"
    
    menu "System Type" # 定义配置菜单栏
    
    config MMU	# 在 System Type 下定义配置项
    	bool "MMU-based Paged Memory Management Support"
    	default y
    	help
    	  Select if you want MMU-based virtualised addressing space
    	  support by paged memory management. If unsure, say 'Y'.
    
    #
    # The "ARM system type" choice list is ordered alphabetically by option
    # text.  Please add new entries in the option alphabetic order.
    #
    choice
    	prompt "ARM system type"
    	default ARCH_VERSATILE if !MMU
    	default ARCH_MULTIPLATFORM if MMU
    
    config ARCH_MULTIPLATFORM
    	bool "Allow multiple platforms to be selected"
    	depends on MMU
    	select ARCH_WANT_OPTIONAL_GPIOLIB
    	select ARM_HAS_SG_CHAIN
    	select ARM_PATCH_PHYS_VIRT
    	select AUTO_ZRELADDR
    	select CLKSRC_OF
    	select COMMON_CLK
    	select GENERIC_CLOCKEVENTS
    	select MIGHT_HAVE_PCI
    	select MULTI_IRQ_HANDLER
    	select SPARSE_IRQ
    	select USE_OF
    
    ……
    
    config ARCH_VERSATILE
    	bool "ARM Ltd. Versatile family"
    	select ARCH_WANT_OPTIONAL_GPIOLIB
    	select ARM_AMBA
    	select ARM_TIMER_SP804
    	select ARM_VIC
    	select CLKDEV_LOOKUP
    	select GENERIC_CLOCKEVENTS
    	select HAVE_MACH_CLKDEV
    	select ICST
    	select PLAT_VERSATILE
    	select PLAT_VERSATILE_CLOCK
    	select PLAT_VERSATILE_SCHED_CLOCK
    	select VERSATILE_FPGA_IRQ
    	help
    	  This enables support for ARM Ltd Versatile board.
    	  
    ……
    
    menu "Multiple platform selection"
    	depends on ARCH_MULTIPLATFORM	# Multiple platform selection 依赖 ARCH_MULTIPLATFORM 配置项
    
    comment "CPU Core family selection"
    
    ……
    
    #
    # This is sorted alphabetically by mach-* pathname.  However, plat-*
    # Kconfigs may be included either alphabetically (according to the
    # plat- suffix) or along side the corresponding mach-* source.
    #
    source "arch/arm/mach-mvebu/Kconfig"
    ……
    
    menu "Bus support"
    ……
    menu "Kernel Features"
    
    menu "CPU Power Management"
    
    source "drivers/cpufreq/Kconfig"
    
    source "drivers/cpuidle/Kconfig"
    
    endmenu
    
    menu "Floating point emulation"
    
    comment "At least one emulation must be selected"
    
    endmenu
    ……
    
    menu "Userspace binary formats"
    
    source "fs/Kconfig.binfmt"
    
    endmenu
    
    menu "Power management options"
    
    source "kernel/power/Kconfig"
    
    endmenu
    
    source "net/Kconfig"
    
    source "drivers/Kconfig"
    
    source "drivers/firmware/Kconfig"
    
    source "fs/Kconfig"
    
    source "arch/arm/Kconfig.debug"
    
    source "security/Kconfig"
    
    source "crypto/Kconfig"
    if CRYPTO
    source "arch/arm/crypto/Kconfig"
    endif
    
    source "lib/Kconfig"
    
    source "arch/arm/kvm/Kconfig"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • source:包含一个配置文件;
    • menu:定义一个菜单项;
    • choice:定义一个选项组;
    • config:定义一个配置项;
    • comment:定义一个注释;

    3、其他配置文件

    五、测试

    1、添加菜单

    为了方便测试,在顶层 Kconfig 下添加一个 bool 配置项,配置项内容如下:

    menu "onlylove test"
    
    endmenu
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    2、添加 bool 类型配置

    menu "onlylove test"
    
    config ONLYLOVE_TEST
    	bool "onlylove test Management Support"
    
    endmenu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    3、添加 tristate 类型配置

    menu "onlylove test"
    
    config ONLYLOVE_TEST
    	tristate "onlylove test Management Support"
    
    endmenu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    4、添加 string 类型配置

    menu "onlylove test"
    
    config ONLYLOVE_TEST
    	string "onlylove test Management Support"
    
    endmenu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    5、添加 hex 类型配置

    menu "onlylove test"
    
    config ONLYLOVE_TEST
    	hex "onlylove test Management Support"
    
    endmenu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    6、添加 int 类型值

    menu "onlylove test"
    
    config ONLYLOVE_TEST
    	int "onlylove test Management Support"
    
    endmenu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    7、查看配置是否生效

    1、添加菜单和配置项

    menu "onlylove test"
    
    config ONLYLOVE_TEST
    	tristate "onlylove test Management Support"
    
    endmenu
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、查看旧配置文件

    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ cat .config | grep ONLYLOVE_TEST
    # CONFIG_ONLYLOVE_TEST is not set
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$
    
    • 1
    • 2
    • 3

    3、选中 onlylove test Management Support 配置项
    在这里插入图片描述
    4、查看新配置文件

    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ cat .config | grep ONLYLOVE_TEST
    CONFIG_ONLYLOVE_TEST=m
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$
    
    • 1
    • 2
    • 3

    六、向 Linux 内核中添加自己编写驱动

    1、确定驱动所属类型

    • ICM-20608 属于 SPI 设备。

    2、找到对应Kconfig文件

    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ pwd
    /home/onlylove/my/linux/linux-imx-4.1.15/drivers/spi
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ ls -l Kconfig 
    -rw-rw-r-- 1 onlylove onlylove 20563 May 24  2019 Kconfig
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、添加驱动文件

    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ pwd
    /home/onlylove/my/linux/linux-imx-4.1.15/drivers/spi
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ ls -l spi-icm2068.c
    ls: cannot access 'spi-icm2068.c': No such file or directory
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$  cp -f ~/my/imx6ull_drive/13_icm20608_spi/spi-icm2068.c ./
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ ls -l spi-icm2068.c
    -rw-rw-r-- 1 onlylove onlylove 12758 Nov 13 00:25 spi-icm2068.c
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    4、添加配置项

    config ONLYLOVE_ICM20608
            tristate "Icm20608 Device Support"
            help
              This supports user icm20608 device.
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ cat .config | grep ONLYLOVE_ICM20608
    CONFIG_ONLYLOVE_ICM20608=y
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$
    
    • 1
    • 2
    • 3

    5、添加Makefile编译项

    obj-$(CONFIG_ONLYLOVE_ICM20608)         += spi-icm2068.o
    
    • 1

    6、编译内核

    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ make -j4
    scripts/kconfig/conf  --silentoldconfig Kconfig
      CHK     include/config/kernel.release
      CHK     include/generated/uapi/linux/version.h
      CHK     include/generated/utsrelease.h
    make[1]: 'include/generated/mach-types.h' is up to date.
      CHK     include/generated/bounds.h
      CHK     include/generated/asm-offsets.h
      CALL    scripts/checksyscalls.sh
      CHK     include/generated/compile.h
      CC      drivers/spi/spi-icm2068.o
      LD      drivers/spi/built-in.o
      LD      drivers/built-in.o
      LINK    vmlinux
      LD      vmlinux.o
      MODPOST vmlinux.o
      GEN     .version
      CHK     include/generated/compile.h
      UPD     include/generated/compile.h
      CC      init/version.o
      LD      init/built-in.o
      KSYM    .tmp_kallsyms1.o
      KSYM    .tmp_kallsyms2.o
      LD      vmlinux
      SORTEX  vmlinux
      SYSMAP  System.map
      OBJCOPY arch/arm/boot/Image
      Building modules, stage 2.
      MODPOST 27 modules
      Kernel: arch/arm/boot/Image is ready
      LZO     arch/arm/boot/compressed/piggy.lzo
      AS      arch/arm/boot/compressed/piggy.lzo.o
      LD      arch/arm/boot/compressed/vmlinux
      OBJCOPY arch/arm/boot/zImage
      Kernel: arch/arm/boot/zImage is ready
    onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    日志 CC drivers/spi/spi-icm2068.o 表示 spi-icm2068 驱动编译成功。

    7、驱动测试

    1、Linux 启动日志

    在这里插入图片描述
    以上日志可以确定 icm20608 驱动加载成功。icm20608 设备 ID 读取成功。

    2、app程序调用icm20608驱动

    # ls
    icm20608_app
    # ls -l /dev/icm20608
    crw-rw----    1 root     root       10,  62 Jan  1 00:00 /dev/icm20608
    #
    # ./icm20608_app /dev/icm20608
    data[0] = 6 data[1] = 13 data[2] = 0 data[3] = 43 data[4] = 6 data[5] = 2067
    
    原始值:
    gx = 6, gy = 13, gz = 0
    ax = 43, ay = 6, az = 2067
    temp = 1715
    实际值:act gx = 0.37°/S, act gy = 0.79°/S, act gz = 0.00°/S
    act ax = 0.02g, act ay = 0.00g, act az = 1.01g
    act temp = 30.17°C
    data[0] = 7 data[1] = 13 data[2] = 0 data[3] = 39 data[4] = 8 data[5] = 2063
    
    原始值:
    gx = 7, gy = 13, gz = 0
    ax = 39, ay = 8, az = 2063
    temp = 1708
    实际值:act gx = 0.43°/S, act gy = 0.79°/S, act gz = 0.00°/S
    act ax = 0.02g, act ay = 0.00g, act az = 1.01g
    act temp = 30.15°C
    data[0] = 8 data[1] = 12 data[2] = -1 data[3] = 43 data[4] = 11 data[5] = 2067
    
    原始值:
    gx = 8, gy = 12, gz = -1
    ax = 43, ay = 11, az = 2067
    temp = 1711
    实际值:act gx = 0.49°/S, act gy = 0.73°/S, act gz = -0.06°/S
    act ax = 0.02g, act ay = 0.01g, act az = 1.01g
    act temp = 30.16°C
    ^C
    #
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    数据读取成功。

  • 相关阅读:
    算法金 | 奇奇怪怪的正则化
    489 - Hangman Judge (UVA)
    计算机毕业设计Java校园闲置物品交换平台系统(源码+系统+mysql数据库+Lw文档)
    git的基本使用
    lstm 回归实战、 分类demo
    Java注释
    定制你的【Spring Boot Starter】,加速开发效率
    微信小程序——Git版本管理
    Nginx 安装配置
    Java Pattern.matcher()方法具有什么功能呢?
  • 原文地址:https://blog.csdn.net/OnlyLove_/article/details/128193600