一、概述

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 SRCARCHstringoption env="SRCARCH"source "arch/$SRCARCH/Kconfig"
  • 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 yhelp  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.#choiceprompt "ARM system type"default ARCH_VERSATILE if !MMUdefault ARCH_MULTIPLATFORM if MMUconfig ARCH_MULTIPLATFORMbool "Allow multiple platforms to be selected"depends on MMUselect ARCH_WANT_OPTIONAL_GPIOLIBselect ARM_HAS_SG_CHAINselect ARM_PATCH_PHYS_VIRTselect AUTO_ZRELADDRselect CLKSRC_OFselect COMMON_CLKselect GENERIC_CLOCKEVENTSselect MIGHT_HAVE_PCIselect MULTI_IRQ_HANDLERselect SPARSE_IRQselect USE_OF……config ARCH_VERSATILEbool "ARM Ltd. Versatile family"select ARCH_WANT_OPTIONAL_GPIOLIBselect ARM_AMBAselect ARM_TIMER_SP804select ARM_VICselect CLKDEV_LOOKUPselect GENERIC_CLOCKEVENTSselect HAVE_MACH_CLKDEVselect ICSTselect PLAT_VERSATILEselect PLAT_VERSATILE_CLOCKselect PLAT_VERSATILE_SCHED_CLOCKselect VERSATILE_FPGA_IRQhelp  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"endmenumenu "Floating point emulation"comment "At least one emulation must be selected"endmenu……menu "Userspace binary formats"source "fs/Kconfig.binfmt"endmenumenu "Power management options"source "kernel/power/Kconfig"endmenusource "net/Kconfig"source "drivers/Kconfig"source "drivers/firmware/Kconfig"source "fs/Kconfig"source "arch/arm/Kconfig.debug"source "security/Kconfig"source "crypto/Kconfig"if CRYPTOsource "arch/arm/crypto/Kconfig"endifsource "lib/Kconfig"source "arch/arm/kvm/Kconfig"
  • source:包含一个配置文件;
  • menu:定义一个菜单项;
  • choice:定义一个选项组;
  • config:定义一个配置项;
  • comment:定义一个注释;

3、其他配置文件

五、测试

1、添加菜单

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

menu "onlylove test"endmenu

2、添加 bool 类型配置

menu "onlylove test"config ONLYLOVE_TESTbool "onlylove test Management Support"endmenu

3、添加 tristate 类型配置

menu "onlylove test"config ONLYLOVE_TESTtristate "onlylove test Management Support"endmenu

4、添加 string 类型配置

menu "onlylove test"config ONLYLOVE_TESTstring "onlylove test Management Support"endmenu

5、添加 hex 类型配置

menu "onlylove test"config ONLYLOVE_TESThex "onlylove test Management Support"endmenu

6、添加 int 类型值

menu "onlylove test"config ONLYLOVE_TESTint "onlylove test Management Support"endmenu

7、查看配置是否生效

1、添加菜单和配置项

menu "onlylove test"config ONLYLOVE_TESTtristate "onlylove test Management Support"endmenu

2、查看旧配置文件

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ cat .config | grep ONLYLOVE_TEST# CONFIG_ONLYLOVE_TEST is not setonlylove@ubuntu:~/my/linux/linux-imx-4.1.15$

3、选中 onlylove test Management Support 配置项

4、查看新配置文件

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ cat .config | grep ONLYLOVE_TESTCONFIG_ONLYLOVE_TEST=monlylove@ubuntu:~/my/linux/linux-imx-4.1.15$

六、向 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/spionlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ ls -l Kconfig -rw-rw-r-- 1 onlylove onlylove 20563 May 24  2019 Kconfigonlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$

3、添加驱动文件

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ pwd/home/onlylove/my/linux/linux-imx-4.1.15/drivers/spionlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$ ls -l spi-icm2068.cls: cannot access 'spi-icm2068.c': No such file or directoryonlylove@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.conlylove@ubuntu:~/my/linux/linux-imx-4.1.15/drivers/spi$

4、添加配置项

config ONLYLOVE_ICM20608        tristate "Icm20608 Device Support"        help          This supports user icm20608 device.

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ cat .config | grep ONLYLOVE_ICM20608CONFIG_ONLYLOVE_ICM20608=yonlylove@ubuntu:~/my/linux/linux-imx-4.1.15$

5、添加Makefile编译项

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

6、编译内核

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ make -j4scripts/kconfig/conf  --silentoldconfig Kconfig  CHK     include/config/kernel.release  CHK     include/generated/uapi/linux/version.h  CHK     include/generated/utsrelease.hmake[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 readyonlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ 

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

7、驱动测试

1、Linux 启动日志


以上日志可以确定 icm20608 驱动加载成功。icm20608 设备 ID 读取成功。

2、app程序调用icm20608驱动

# lsicm20608_app# ls -l /dev/icm20608crw-rw----    1 root     root       10,  62 Jan  1 00:00 /dev/icm20608## ./icm20608_app /dev/icm20608data[0] = 6 data[1] = 13 data[2] = 0 data[3] = 43 data[4] = 6 data[5] = 2067原始值:gx = 6, gy = 13, gz = 0ax = 43, ay = 6, az = 2067temp = 1715实际值:act gx = 0.37°/S, act gy = 0.79°/S, act gz = 0.00°/Sact ax = 0.02g, act ay = 0.00g, act az = 1.01gact temp = 30.17°Cdata[0] = 7 data[1] = 13 data[2] = 0 data[3] = 39 data[4] = 8 data[5] = 2063原始值:gx = 7, gy = 13, gz = 0ax = 39, ay = 8, az = 2063temp = 1708实际值:act gx = 0.43°/S, act gy = 0.79°/S, act gz = 0.00°/Sact ax = 0.02g, act ay = 0.00g, act az = 1.01gact temp = 30.15°Cdata[0] = 8 data[1] = 12 data[2] = -1 data[3] = 43 data[4] = 11 data[5] = 2067原始值:gx = 8, gy = 12, gz = -1ax = 43, ay = 11, az = 2067temp = 1711实际值:act gx = 0.49°/S, act gy = 0.73°/S, act gz = -0.06°/Sact ax = 0.02g, act ay = 0.01g, act az = 1.01gact temp = 30.16°C^C#

数据读取成功。