一、概述

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 yhelpSelect if you want MMU-based virtualised addressing spacesupport 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_IRQhelpThis 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 CRYPTO
source "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 set
onlylove@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_TEST
CONFIG_ONLYLOVE_TEST=m
onlylove@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/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$

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$

4、添加配置项

config ONLYLOVE_ICM20608tristate "Icm20608 Device Support"helpThis supports user icm20608 device.

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$

5、添加Makefile编译项

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

6、编译内核

onlylove@ubuntu:~/my/linux/linux-imx-4.1.15$ make -j4
scripts/kconfig/conf  --silentoldconfig KconfigCHK     include/config/kernel.releaseCHK     include/generated/uapi/linux/version.hCHK     include/generated/utsrelease.h
make[1]: 'include/generated/mach-types.h' is up to date.CHK     include/generated/bounds.hCHK     include/generated/asm-offsets.hCALL    scripts/checksyscalls.shCHK     include/generated/compile.hCC      drivers/spi/spi-icm2068.oLD      drivers/spi/built-in.oLD      drivers/built-in.oLINK    vmlinuxLD      vmlinux.oMODPOST vmlinux.oGEN     .versionCHK     include/generated/compile.hUPD     include/generated/compile.hCC      init/version.oLD      init/built-in.oKSYM    .tmp_kallsyms1.oKSYM    .tmp_kallsyms2.oLD      vmlinuxSORTEX  vmlinuxSYSMAP  System.mapOBJCOPY arch/arm/boot/ImageBuilding modules, stage 2.MODPOST 27 modulesKernel: arch/arm/boot/Image is readyLZO     arch/arm/boot/compressed/piggy.lzoAS      arch/arm/boot/compressed/piggy.lzo.oLD      arch/arm/boot/compressed/vmlinuxOBJCOPY arch/arm/boot/zImageKernel: arch/arm/boot/zImage is ready
onlylove@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驱动

# 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
#

数据读取成功。

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

  1. Linux 驱动开发 三十五:Linux 内核时钟管理

    参考: linux时间管理,时钟中断,系统节拍_u010936265的博客-CSDN博客_系统节拍时钟中断 Linux内核时钟系统和定时器实现_anonymalias的专栏-CSDN博客_linux内 ...

  2. Linux 驱动开发 三十四:Linux 内核定时器原理

    参考文档: <Cortex -A7 MPCore Technical Reference Manual> 中 Chapter 9:Generic Timer. <ARM ® Arch ...

  3. 进程间通信方式_第四十九期-Linux内核中的进程概述(4)

    作者:熊轶翔@熊仙僧,中国科学院软件研究所智能软件研究中心 上一章我们学习了进程调度,进程调度的过程是由操作系统内核管理的.在Linux中还存在着另一种由内核管理且又与进程运行息息相关的操作,也是就在 ...

  4. Linux设备驱动开发详解-Note(5)---Linux 内核及内核编程(1)

    Linux 内核及内核编程(1) 成于坚持,败于止步 Linux 2.6 内核的特点 Linux 2.6 相对于 Linux 2.4 有相当大的改进,主要体现在如下几个方面. 1.新的调度器 2.6 ...

  5. Window XP驱动开发(二十四)虚拟串口设备驱动

    转载请标明是引用于 http://blog.csdn.net/chenyujing1234 欢迎大家拍砖 在我的一篇文章<<winCE中实现虚拟串口的方法 >>中,讲到在win ...

  6. 嵌入式Linux驱动笔记(二十九)------内存管理之伙伴算法(Buddy)分析

    你好!这里是风筝的博客, 欢迎和我一起交流. 我们知道,在一个通用操作系统里,频繁申请内存释放内存都会出现一个非常著名的内存管理问题:内存碎片. 学过操作系统的都知道,有很多行之有效的方法(比如:记录 ...

  7. Linux 驱动开发 四十八:Linux INPUT 子系统实验

    一.input 子系统简介 input 就是输入的意思,因此 input 子系统就是管理输入的子系统,是 Linux 内核针对某一类设备而创建的框架. 比如按键输入.键盘.鼠标.触摸屏等等这些都属于输 ...

  8. oracle驱动maven报错_在Maven仓库中添加Oracle JDBC驱动

    由于Oracle授权问题,Maven3不提供Oracle JDBC driver,为了在Maven项目中应用Oracle JDBC driver,必须手动添加到本地仓库. 一.首先要得到Oracle ...

  9. NVIDIA Jetson TX2 内核中添加 CP210x 串口驱动

    说明:本文的核心内容是围绕版本为 L4T 27.1 的内核编译过程.如果内核版本是 L4T 28.1 ,则可移步这篇文章: 编译 L4T 28.1,这两个版本的编译步骤几乎一样. 问题背景: 最近在 ...

最新文章

  1. python 遍历删除
  2. 我的小型网站搜索分词中遇到的问题
  3. Human centered design and design thinking
  4. 圣地亚哥的计算机科学在哪个学院,加州大学圣地亚哥分校计算机科学在哪个学院?...
  5. java的继承实例_Java继承和多态实例
  6. 拉格朗日差值 - 杜教板子
  7. python提取部分字符串三参数_python3 字符串属性(三)
  8. leetCode-995:K 连续位的最小翻转次数
  9. bootstrap-multiselect.js如何动态更新select里的数据
  10. 模糊综合评价模型详解
  11. 台达伺服b3参数_恩阳台达B3系列伺服安装
  12. Tanzu 学习系列之TKGm for vSphere 快速部署
  13. 前端xlsx导入与导出
  14. mysql是如何设置候选码_求关系模式中的候选键(软考,数据库)
  15. 【Markdown】Typora中文手册
  16. Android Init Language : init.rc
  17. Linux搭建FTP服务使用案例
  18. 计算机tpm1.2怎么启动,tpm2.0开启的方法
  19. kali工具熟悉——情报分析
  20. 线性表-顺序表的基本操作

热门文章

  1. 笨办法学python在线阅读_“笨办法”学Python(第3版)
  2. 浅析门户网站体育赛事CDN加速解决方案
  3. mysql从安全文件密码_MySQL安全输入密码的一些操作介绍_MySQL
  4. zookeeper 选举策略
  5. MATLAB模糊控制Suface三维图像导出svg高清矢量图方法
  6. Burpsuite1.7使用指南渗透测试方法大全
  7. JSON内容对比工具
  8. 数字化校园建设之实践一
  9. 不甘眼前,勇敢跳槽!毕业生销售转行软件测试的自学逆袭之路!
  10. 27.Python数据库操作(一)【内置数据库SQLite和ORM框架SQLAlchemy】