跳到主要内容

U-Boot - U-Boot 相关功能配置

目录结构

源码位于brandy/brandy-2.0/u-boot-2018目录,挑选常用的目录说明,结构如下:

├── arch	#芯片arch相关文件
├── board
│ └──sunxi #板级公共功能实现
├── cmd #通用命令
├── common #社区通用功能
├── configs #defconfig配置
├── drivers #驱动目录
├── env #启动变量
├── include #通用头文件
├── lib #通用函数实现
├── sprite #量产烧写相关功能

进入 U-Boot 控制台

在启动的时候一直按住 s 键,即可进入 U-boot 控制台。

编译烧录使用

编译uboot的快捷命令是:

muboot

编译产物有3个,如下:

u-boot-efex.bin	#烧录阶段使用,只存在于固件中。对应sun300iw1p1_efex_defconfig
u-boot-spinor-sun300iw1p1.bin #nor介质常电启动使用,快启不使用
u-boot-sun300iw1p1.bin #nand和emmc介质常电启动使用

配置说明

方案通过 device/config/chips/{IC}/configs/{BOARD}/BoardConfig_nor.mkBoardConfig.mk 文件的 LICHEE_BRANDY_DEFCONF 变量指定 uboot 的使用的 defconfig 文件。如:

LICHEE_BRANDY_DEFCONF:=sun300iw1p1_v821_defconfig

则此文件对应编译产物u-boot-sun300iw1p1.bin;编译时nor方案会自动指定配置为sun300iw1p1_v821_nor_defconfig,对应编译产物为u-boot-spinor-sun300iw1p1.bin

添加新的宏配置

操作步骤如下:

  1. 修改 Kconfig。不同目录下都有 Kconfig 文件,找一个合适的添加。如:添加 mytest 配置
config mytest
bool "this is mytest" #配置描述
default n #默认不使能
help
xxx #详细描述
  1. 在对应的 defconfig 文件使能新建的配置
CONFIG_MYTEST=y
  1. c 代码中使用CONFIG_MYTEST宏去管控代码

命令行调试

执行help,则可以看到支持的命令。列举几个命令使用方法:

cmp 内存比较命令,比较两段内存,以一个 word 进行比较 count=X,表示比较 X 个 word 字节的内存数据。

cmp addr1 addr2 count

fdt修改设备树命令;用法较多,参考:

fdt set    <path> <prop> [<val>]    - Set <property> [to <val>]
fdt mknode <path> <node> - Create a new node after <path>
fdt rm <path> [<prop>] - Delete the node or <property>

env相关命令

printenv	#打印所有env变量
setenv name value #设置变量名name的值为value
saveenv #保存env变量到flash

在 U-Boot 下配置内核设备树

fdt 命令在 U-Boot 中用于操作设备树(Device Tree),主要是用来解析、修改和查看设备树(FDT,Flattened Device Tree)的内容。

fdt - flattened device tree utility commands

Usage:
fdt addr [-c] <addr> [<length>] - Set the [control] fdt location to <addr>
fdt move <fdt> <newaddr> <length> - Copy the fdt to <addr> and make it active
fdt resize [<extrasize>] - Resize fdt to size + padding to 4k addr + some optional <extrasize> if needed
fdt print <path> [<prop>] - Recursive print starting at <path>
fdt list <path> [<prop>] - Print one level starting at <path>
fdt get value <var> <path> <prop> - Get <property> and store in <var>
fdt get name <var> <path> <index> - Get name of node <index> and store in <var>
fdt get addr <var> <path> <prop> - Get start address of <property> and store in <var>
fdt get size <var> <path> [<prop>] - Get size of [<property>] or num nodes and store in <var>
fdt set <path> <prop> [<val>] - Set <property> [to <val>]
fdt mknode <path> <node> - Create a new node after <path>
fdt rm <path> [<prop>] - Delete the node or <property>
fdt header - Display header info
fdt bootcpu <id> - Set boot cpuid
fdt memory <addr> <size> - Add/Update memory node
fdt rsvmem print - Show current mem reserves
fdt rsvmem add <addr> <size> - Add a mem reserve
fdt rsvmem delete <index> - Delete a mem reserves
fdt chosen [<start> <end>] - Add/update the /chosen branch in the tree
<start>/<end> - initrd start/end addr
NOTE: Dereference aliases by omitting the leading '/', e.g. fdt print ethernet0.
  1. 查看设备树的节点

    fdt print <node-path>

    这个命令打印设备树中指定路径()下的节点内容。 例如:

    fdt print /soc@2002000/vind@45800800/sensor@5812000

    image-20250513150557865

  2. 查看整个设备树

    fdt print

    这个命令会显示设备树的所有内容。

  3. 设置设备树节点的属性

    fdt set <node-path> <property-name> <value>

    这个命令修改指定节点的属性值。 例如:

    fdt set /soc@2002000/vind@45800800/sensor@5812000 status disabled

    image-20250513150641185

  4. 删除设备树的属性

    fdt rm <node-path> <property-name>

    这个命令删除设备树节点的某个属性。 例如:

    fdt rm /soc@2002000/vind@45800800/sensor@5812000 status

    image-20250513150720581

  5. 添加设备树的属性

    fdt set <node-path> <property-name> <value>

    如果属性不存在,会创建并设置它。 例如:

    fdt set /soc@2002000/vind@45800800/sensor@5812000 sensor0_twi_addr <0x7e>

    image-20250513150803833

U-Boot 使用 PWM 功能

配置启用 PWM 驱动

首先确认板级使用的 U-Boot 配置文件,这里以 100ask_avaota_f1 板级为例:

image-20250513152806384

可以看到配置项 LICHEE_BRANDY_DEFCONF:=sun300iw1p1_v821_defconfig 表示板级使用的 U-Boot 配置文件是 sun300iw1p1_v821_defconfig

前往 brandy/brandy-2.0/u-boot-2018/configs 查看配置,找到 sun300iw1p1_v821_defconfig,增加配置项开启 PWM 驱动

CONFIG_PWM_SUNXI=y

image-20250513153006898

配置 PWM 设备树

回到板级配置,找到 U-Boot 使用的设备树,增加 PWM 的配置项

&pwm8_pin_a {
allwinner,pins = "PD18";
allwinner,function = "pwm_8";
allwinner,muxsel = <0x4>;
allwinner,drive = <10>;
allwinner,pull = <2>;
};

&pwm8_pin_b {
allwinner,pins = "PD18";
allwinner,function = "io_disabled";
allwinner,muxsel = <0xf>;
allwinner,drive = <3>;
allwinner,pull = <2>;
};

&pwm8 {
pinctrl-names = "active", "sleep";
pinctrl-0 = <&pwm8_pin_a>;
pinctrl-1 = <&pwm8_pin_b>;
status = "okay";
};

然后需要加上使用到的 PWM 的 aliases

&aliases {
spi0 = &spi0;
spif = &spif;
pwm8 = &pwm8;
};

image-20250522105008468

测试 U-Boot 下 PWM 功能

首先输入 help 查看是否有测试命令 sunxi_pwm

image-20250513153720418

然后切换设备树,将设备树切换回 U-Boot 的设备树

提示

在启动过程中,设备树会切换到 Kernel 内核使用的设备树,此时运行 sunxi_pwm 命令会报错

error:fdt err returned FDT_ERR_BADPATH

如果日志中打印 change working_fdt 则表示当前设备树已经切换到内核设备树了

查看日志找到 U-Boot 设备树的地址,找到 change working_fdt 这一行,前面是 U-Boot 的设备树地址,后面是 Kernel 的设备树地址。所以这里 U-Boot 的设备树地址是 0x838deea0

image-20250513153909369

fdt addr 0x838deea0 # 改成上面日志的地址
危险

根据日志里 U-Boot 设备树的地址配置 U-Boot 设备树,请不要参考下面的错误示例,直接复制使用上面的命令!U-Boot 设备树的地址是自动配置的,每一个固件都不一样!

image-20250522104626816

查看刚才配置的 pwm8 是否正确

image-20250513154850560

执行测试,输入

sunxi_pwm 8 50000 100000

image-20250513154957035

使用示波器查看结果

image-20250513155130049