单片机
返回首页

Linux内核移植初探

2025-01-08 来源:elecfans

内核移植的梯度:


初级:根据芯片公司的参考配置,编译开发板内核并了解执行过程


中极:添加内核驱动的方式方法


高级:修改或添加BSP包


linux内核特性:


可移植性强、支持的硬件平台广泛;超强的网络功能;多任务多用户系统;模块化的设计


五大子系统:


进程管理;内存管理;文件系统;网络协议;设备管理

内核获取路径:芯片厂商、内核源码官方


linux内核的目录结构层次结构:

平台相关目录树:arch目录下


平台无关目录树:其它


crypto目录:算法、加密涉及的源码目录


Documentation目录:内核官方文档


fs目录:文件系统的信息


ipc目录:进程间通信的机制


mm目录:内存


driver:驱动相关


内核源码开发的头文件命名规范


#include :与CPU体系结构(arch)相关的头文件

#include :平台无关的头文件

#include :与某款芯片公司相关的头文件

#include :与开发板配套的头文件


配置内核


1.配置哪些目录需要编译


2.配置哪些文件需要编译


配置方法:Makefile

主目录Makefile:


包含体系结构下的Makefile


# Use LINUXINCLUDE when you must reference the include/ directory.

# Needed to be compatible with the O= option

LINUXINCLUDE    := -I$(srctree)/arch/$(hdr-arch)/include

                   -Iarch/$(hdr-arch)/include/generated -Iinclude

                   $(if $(KBUILD_SRC), -I$(srctree)/include)

                   -include include/generated/autoconf.h

   hdr-arch := $(SRCARCH)


#包含体系架构下的Makefile

include $(srctree)/arch/$(SRCARCH)/Makefile


ARCH ?= arm

CROSS_COMPILE ?= /opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-

# Architecture as present in compile.h

UTS_MACHINE := $(ARCH)

SRCARCH := $(ARCH)


# Additional ARCH settings for x86

ifeq ($(ARCH),i386)

SRCARCH := x86

endif

ifeq ($(ARCH),x86_64)

SRCARCH := x86

endif


各个子目录的Makefile

例:arch/arm/mach-s5pv210下的Makefile


# arch/arm/mach-s5pv210/Makefile

#

# Copyright (c) 2010 Samsung Electronics Co., Ltd.

#         http://www.samsung.com/

#

# Licensed under GPLv2


obj-y                :=

obj-m                :=

obj-n                :=

obj-                :=


# Core support for S5PV210 system


obj-$(CONFIG_CPU_S5PV210)    += cpu.o init.o clock.o dma.o

obj-$(CONFIG_CPU_S5PV210)    += setup-i2c0.o

obj-$(CONFIG_S5PV210_PM)    += pm.o sleep.o

obj-$(CONFIG_CPU_FREQ)        += cpufreq.o


# machine support


obj-$(CONFIG_MACH_AQUILA)    += mach-aquila.o

obj-$(CONFIG_MACH_SMDKV210)    += mach-smdkv210.o

obj-$(CONFIG_MACH_SMDKC110)    += mach-smdkc110.o

obj-$(CONFIG_MACH_GONI)        += mach-goni.o

obj-$(CONFIG_MACH_TORBRECK)    += mach-torbreck.o


# device support


obj-y                += dev-audio.o

obj-$(CONFIG_S3C64XX_DEV_SPI)    += dev-spi.o


obj-$(CONFIG_S5PV210_SETUP_FB_24BPP)    += setup-fb-24bpp.o

obj-$(CONFIG_S5PV210_SETUP_FIMC)    += setup-fimc.o

obj-$(CONFIG_S5PV210_SETUP_I2C1)     += setup-i2c1.o

obj-$(CONFIG_S5PV210_SETUP_I2C2)     += setup-i2c2.o

obj-$(CONFIG_S5PV210_SETUP_IDE)        += setup-ide.o

obj-$(CONFIG_S5PV210_SETUP_KEYPAD)    += setup-keypad.o

obj-$(CONFIG_S5PV210_SETUP_SDHCI)       += setup-sdhci.o

obj-$(CONFIG_S5PV210_SETUP_SDHCI_GPIO)    += setup-sdhci-gpio.o


obj - y  :=   编译进内核

obj - m :=   以模块形式编译

obj - n  :=   不编译 

obj -     :=   不编译


 哪些文件需要编译?


  CONFIG_XXX 


  配置单:


    在/arch/arm/configs默认目录下

  配置过程:


    1、导出需要的默认配置文件到主目录下并更名为.config

    2、交叉编译器的修改

    3、体系结构(System Type)的选择:S5PV210



ARCH ?= arm

CROSS_COMPILE ?= /opt/FriendlyARM/toolschain/4.5.1/bin/arm-linux-

    4、配置单(.config)增删改查--->使用make menuconfig实现图形化更改配置


                      Kconfig----> make menuconfig ---> .config ---->Makefile


Kconfig语法:

source:相当于include描述一包含关系

menu、endmenu:用来定义菜单如:


menu 'System Info'

end menu

执行make menuconfig 则:

.config中增加一条:CONFIG_ABC = y

即config ----> CONFIG_ABC =y---->Makefile中的obj-$(CONFIG_ABC) += xx.o


通过make menuconfig中的-----相应选项的help----->找到相应选项的Kconfig---->Makefile中对应的文件 .c


  例如:

     找menuconfig下的Samsung SoC serial support定义的(Kconfig)位置:


            法一:选中这个选项键盘单击h按键

            法二:搜索grep -nR 'Samsung SoC serial support'


    同样在Kconfig所在目录下的Makefile文件即可找到对应的.c文件                                              


通过Makefile  .o ----->找到Kconfig:同上↑


config:用来配置菜单子目录的内容


menu 'System Info'

config ABC

     bool 'This is a test config'

   help 

     Support test config

endmenu


执行make menuconfig 则:

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'


      ---->source “arch/$SRCARCH/Kconfig”      


config ARM

        bool

        default y

        select HAVE_AOUT

        select HAVE_DMA_API_DEBUG

        select HAVE_IDE

        select HAVE_MEMBLOCK

.......

...........

config HAVE_PWM

        bool

config MIGHT_HAVE_PCI

        bool

config SYS_SUPPORTS_APM_EMULATION

        bool

config HAVE_SCHED_CLOCK

        bool

config GENERIC_GPIO

        bool

............

................

menu 'Power management options'

source 'kernel/power/Kconfig'

config ARCH_SUSPEND_POSSIBLE

        depends on !ARCH_S5P64X0 && !ARCH_S5PC100

        depends on CPU_ARM920T || CPU_ARM926T || CPU_SA1100 ||

                CPU_V6 || CPU_V6K || CPU_V7 || CPU_XSC3 || CPU_XSCALE

        def_bool y

endmenu

source 'net/Kconfig'

source 'drivers/Kconfig'

source 'fs/Kconfig'

source 'arch/arm/Kconfig.debug'

source 'security/Kconfig'

source 'crypto/Kconfig'

source 'lib/Kconfig'


        --->source 'net/Kconfig'

   --->source 'drivers/Kconfig'

   --->source 'fs/Kconfig'

   --->source 'arch/arm/Kconfig.debug'

   --->source 'security/Kconfig'

   --->source 'crypto/Kconfig'

   --->source 'lib/Kconfig'


添加驱动到Linux内核中的步骤:以myled.c字符驱动为例

1.可以在/drivers/char/目录下新建文件夹mydriver,然后复制myled.c到mydriver目录

2、在mydriver目录下创建Makefile文件



obj-$(CONFIG_MYLED) += myled.o

3、在上层的Makefile文件添加一句obj-y += mydriver/这样上层Makefile文件就会找到mydriver目录下的Makefile文件

4、在mydriver目录下创建Kconfig文件


menu 'My Personal Device Driver'

config MYLED    

    bool 'Support myled device driver'

    help

           Support led driver for S5PV210

endmenu


 5、在上层的Kconfig文件添加一句source 'driver/char/mydriver/Kconfig'这样上层Kconfig文件就会找到mydriver目录下的Kconfig文件

 6、执行make menuconfig 选中My Personal Device Driver --->Support myled device driver之后执行make uImage 重新编译内核即可


内核编译过程


make :


  make Image     make zImage   make uImage (专为uboot启动准备的内核镜像 )      //编译的是obj-y    


  make modules   //编译的是obj-m


make uImage (vmlinux-->Image-->vmlinux-->zImage-->uImage) 


直接执行make uImage 报错解决办法:

  进入u-boot源码,在编译完成的uboot源码中进入tool目录,找到mkimage文件,将其复制到/bin根目录


vmlinux :OS elf file  ---OBJCOPY拷贝生成Image

Image:未压缩,所以比较大

zImage:经过压缩的Image文件 


自定义BSP的过程


进入单片机查看更多内容>>
相关视频
  • 【TI MSPM0 应用实战】智能小车+工业角度编码器+血氧仪+烟雾探测器!硬核参考设计详解!

  • 2022 Digi-Key KOL 系列: 你见过1GHz主频的单片机吗?Teensy 4.1开发板介绍

  • TI 新一代 C2000™ 微控制器:全方位助力伺服及马达驱动应用

  • MSP430电容触摸技术 - 防水Demo演示

  • 直播回放: Microchip Timberwolf™ 音频处理器在线研讨会

  • 基于灵动MM32W0系列MCU的指夹血氧仪控制及OTA升级应用方案分享

精选电路图
  • 1瓦线性调频增强器

  • 家用电器遥控器

  • 12V 转 28V DC-DC 变换器(基于 LM2585)

  • 红外开关

  • DS1669数字电位器

  • HA1377 桥式放大器 BCL 电容 17W(汽车音频)

    相关电子头条文章