文章目录
前言
上篇文章《ARM DIY 硬件调试》介绍了 DIY ARM 板的基础硬件焊接,包括电源、SOC、SD 卡座等,板子已经可以跑起来了。
但是发现串口乱码,今天就来解决串口乱码问题。
串口乱码问题定位
串口出现乱码,通常是波特率设置的不对,仔细检查了下
设备树配置的波特率
c
chosen {
stdout-path = "serial0:115200n8";
};
和 xshell 的串口参数
都是 115200,配置没有问题。
想到是不是晶振实际频率和配置的不一致。
板子上焊接的是 26MHz,设备树中配置的是 24000000,这样导致系统时钟不正确,最终产生的串口波特率不是 115200,所以乱码。
内核修改晶振频率
内核修改晶振频率直接修改上述红框中设备树参数就行了
c
osc24M: osc24M_clk {
#clock-cells = <0>;
compatible = "fixed-clock";
// clock-frequency = <24000000>;
clock-frequency = <26000000>; // 晶振频率实际为 26MHz
clock-accuracy = <50000>;
clock-output-names = "osc24M";
};
上电发现内核串口输出已经正常,但是 uboot 串口输出还是乱码
bash
Qµ!ݿN¡¬,º¢§ʖʘʛS#⑭®ª¨ J¥*T ¤VնӝջᏎݵ6£¤¤Q¨©#ճ鎖ƘڐʘʛW#⑥ª&ٴ7䙵Rų䝼w儑 £¥u¡ĭ±셲Re³¢<<-*A
ƕ#Q¬®KՐIAª9כD:Ѣ#N¥®EA<<陝£{A¡cѺ앥CA¶כA¦(Ωº ^Ųݷ¶LɐY½V썐®ͱ-CAµkպ䝵K#Ű¢ن*§³)^Ѡʳ*V隂NNѱa♂QH鞂VJաa⑂2ݐ)
[Ἥ
ݹC]ҡ±/썐©]ي᳃ٵՁ¦Wݴ#
ٹªɹӝ
A±-娝·Kþ$þӝŲ/Gѷ;A²º¯7aªA´9+;ō¶*sAµ<<$麅´VU¢𩑖骩)ݷűź#˅¡¢ِº厙Қa£*αUю)ͳ=+썐ߥ±Ꭼҙ&ښ¢.¯取6ɐ.º厥<<#]³ª¨ґ帩ɱ
ªِ²폁²6բ1Uю麩Sɐ¬𒅒峩ѿ*ѻ鸷A¹V䍐
[A¦A²¸3⑂6¤ɱ{A¤-/7ՐE͵ӝCͱݰύ%傧U ѡz+썐]Jٱºن#٠ 0.000000] Booting Linux on physical CPU 0x0
[ 0.000000] Linux version 5.3.5 (liyongjun@Box) (gcc version 12.3.0 (Buildroot 2023.08-rc1-102-g51dbde549e)) #3 SMP Thu Aug 17 04:19:40 CST 2023
[ 0.000000] CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=10c5387d
[ 0.000000] CPU: div instructions available: patching division code
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[ 0.000000] OF: fdt: Machine model: Lichee Pi Zero with Dock
[ 0.000000] Memory policy: Data cache writealloc
[ 0.000000] cma: Reserved 16 MiB at 0x41c00000
[ 0.000000] psci: probing for conduit method from DT.
[ 0.000000] psci: Using PSCI v0.1 Function IDs from DT
[ 0.000000] percpu: Embedded 15 pages/cpu s30412 r8192 d22836 u61440
[ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 16256
[ 0.000000] Kernel command line: console=ttyS0,115200 panic=5 console=tty0 rootwait root=/dev/mmcblk0p2 earlyprintk rw
[ 0.000000] Dentry cache hash table entries: 8192 (order: 3, 32768 bytes, linear)
[ 0.000000] Inode-cache hash table entries: 4096 (order: 2, 16384 bytes, linear)
[ 0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
uboot 修改晶振频率
那就接着修改 uboot 晶振频率配置,同样,修改设备树参数
bash
osc24M: osc24M_clk {
#clock-cells = <0>;
compatible = "fixed-clock";
// clock-frequency = <24000000>;
clock-frequency = <26000000>;
clock-accuracy = <50000>;
clock-output-names = "osc24M";
};
上电,发现 uboot 串口打印还是乱码。查看 uboot 编译选项,确认 uboot 启用了设备树,并且修改的设备树参数也已经被 C 代码解析到了是 26000000,不过串口还是乱码。
最终通过修改 .h 文件中的 CONFIG_SYS_NS16550_CLK
参数,成功修复了 uboot 串口乱码的问题
ns16550 是很多 SOC 使用的串口芯片 IP。
在上面截图的最后可以看到 #define COUNTER_FREQUENCY 24000000
这个配置,这个参数仍然保持 24000000 而 uboot 串口也不会乱码,说明 uboot 的串口时钟设置并不像 kernel 那样基于 CPU 时钟,而是有自己单独的一个参数 CONFIG_SYS_NS16550_CLK
,这也解释了为什么一开始配置 uboot 设备树的 CPU 时钟仍然解决不了串口打印乱码的问题。
最终 uboot 和 kernel 的串口打印都正常了
bash
U-Boot SPL 2022.01 (Aug 19 2023 - 23:03:28 +0800)
DRAM: 64 MiB
Trying to boot from MMC1
U-Boot 2022.01 (Aug 19 2023 - 23:03:28 +0800) Allwinner Technology
CPU: Allwinner V3s (SUN8I 1681)
Model: Lichee Pi Zero
DRAM: 64 MiB
WDT: Not starting watchdog@1c20ca0
MMC: mmc@1c0f000: 0
Loading Environment from FAT... Unable to read "uboot.env" from mmc0:1... In: serial@1c28000
Out: serial@1c28000
Err: serial@1c28000
Net: No ethernet found.
Hit any key to stop autoboot: 0
switch to partitions #0, OK
mmc0 is current device
Scanning mmc 0:1...
Found U-Boot script /boot.scr
292 bytes read in 2 ms (142.6 KiB/s)
## Executing script at 41900000
4183712 bytes read in 349 ms (11.4 MiB/s)
9041 bytes read in 4 ms (2.2 MiB/s)
Kernel image @ 0x41000000 [ 0x000000 - 0x3fd6a0 ]
## Flattened Device Tree blob at 41800000
Booting using the fdt blob at 0x41800000
Loading Device Tree to 42dfa000, end 42dff350 ... OK
Starting kernel ...
[ 0.000000] Booting Linux on physical CPU 0x0
[ 0.000000] Linux version 5.3.5 (liyongjun@Box) (gcc version 12.3.0 (Buildroot 2023.08-rc1-102-g51dbde549e)) #3 SMP Thu Aug 17 04:19:40 CST 2023
[ 0.000000] CPU: ARMv7 Processor [410fc075] revision 5 (ARMv7), cr=10c5387d
[ 0.000000] CPU: div instructions available: patching division code
[ 0.000000] CPU: PIPT / VIPT nonaliasing data cache, VIPT aliasing instruction cache
[ 0.000000] OF: fdt: Machine model: Lichee Pi Zero with Dock
[ 0.000000] Memory policy: Data cache writealloc
[ 0.000000] cma: Reserved 16 MiB at 0x41c00000
[ 0.000000] psci: probing for conduit method from DT.
[ 0.000000] psci: Using PSCI v0.1 Function IDs from DT
[ 0.000000] percpu: Embedded 15 pages/cpu s30412 r8192 d22836 u61440
[ 0.000000] Built 1 zonelists, mobility grouping on. Total pages: 16256
[ 0.000000] Kernel command line: console=ttyS0,115200 panic=5 console=tty0 rootwait root=/dev/mmcblk0p2 earlyprintk rw
[ 0.000000] Dentry cache hash table entries: 8192 (order: 3, 32768 bytes, linear)
[ 0.000000] Inode-cache hash table entries: 4096 (order: 2, 16384 bytes, linear)
[ 0.000000] mem auto-init: stack:off, heap alloc:off, heap free:off
番外篇
在一开始没有找到解决 uboot 串口打印乱码问题办法的时候,又想通过 uboot 本身来看出一些端倪,怎么办呢?我想到了一个办法:因为实际晶振频率从 24MHz 变成了 26MHz,那么串口波特率就会从 115200 变成(115200 / 24 * 26 = )124800,那就把 xshell 的串口波特率设置成 124800
这样串口打印也不乱码了
bash
U-Boot 2022.01 (Aug 19 2023 - 23:03:28 +0800) Allwinner Technology
CPU: Allwinner V3s (SUN8I 1681)
Model: Lichee Pi Zero
DRAM: 64 MiB
WDT: Not starting watchdog@1c20ca0
MMC: mmc@1c0f000: 0
Loading Environment from FAT... Unable to read "uboot.env" from mmc0:1... In: serial@1c28000
Out: serial@1c28000
Err: serial@1c28000
Net: No ethernet found.
Hit any key to stop autoboot: 0
=>
=>
=> printenv
arch=arm
baudrate=115200
board=sunxi
board_name=sunxi
boot_a_script=load ${devtype} ${devnum}:${distro_bootpart} ${scriptaddr} ${prefix}${script}; source ${scriptaddr}
boot_efi_binary=load ${devtype} ${devnum}:${distro_bootpart} ${kernel_addr_r} efi/boot/bootarm.efi; if fdt addr ${fdt_addr_r}; then bootefi ${kernel_addr_r} ${fdt_addr_r};else bootefi ${kernel_addr_r} ${fdtcontroladdr};fi
boot_efi_bootmgr=if fdt addr ${fdt_addr_r}; then bootefi bootmgr ${fdt_addr_r};else bootefi bootmgr;fi
boot_extlinux=sysboot ${devtype} ${devnum}:${distro_bootpart} any ${scriptaddr} ${prefix}${boot_syslinux_conf}
boot_prefixes=/ /boot/
boot_script_dhcp=boot.scr.uimg
boot_scripts=boot.scr.uimg boot.scr
boot_syslinux_conf=extlinux/extlinux.conf
boot_targets=fel mmc0 pxe dhcp
bootcmd=run distro_bootcmd
bootcmd_dhcp=devtype=dhcp; if dhcp ${scriptaddr} ${boot_script_dhcp}; then source ${scriptaddr}; fi;setenv efi_fdtfile ${fdtfile}; if test -z "${fdtfile}" -a -n "${soc}"; then setenv efi_fdtfile ${soc}-${board}${boardver}.dtb; fi; setenv efi_old_vci ${bootp_vci};setenv efi_old_arch ${bootp_arch};setenv bootp_vci PXEClient:Arch:00010:UNDI:003000;setenv bootp_arch 0xa;if dhcp ${kernel_addr_r}; then tftpboot ${fdt_addr_r} dtb/${efi_fdtfile};if fdt addr ${fdt_addr_r}; then bootefi ${kernel_addr_r} ${fdt_addr_r}; else bootefi ${kernel_addr_r} ${fdtcontroladdr};fi;fi;setenv bootp_vci ${efi_old_vci};setenv bootp_arch ${efi_old_arch};setenv efi_fdtfile;setenv efi_old_arch;setenv efi_old_vci;
bootcmd_fel=if test -n ${fel_booted} && test -n ${fel_scriptaddr}; then echo '(FEL boot)'; source ${fel_scriptaddr}; fi
bootcmd_mmc0=devnum=0; run mmc_boot
bootcmd_pxe=dhcp; if pxe get; then pxe boot; fi
bootdelay=2
bootm_size=0x2e00000
console=ttyS0,115200
cpu=armv7
dfu_alt_info_ram=kernel ram 0x41000000 0x1000000;fdt ram 0x41800000 0x100000;ramdisk ram 0x41C00000 0x4000000
distro_bootcmd=for target in ${boot_targets}; do run bootcmd_${target}; done
efi_dtb_prefixes=/ /dtb/ /dtb/current/
fdt_addr_r=0x41800000
fdtcontroladdr=43d71610
fdtfile=sun8i-v3s-licheepi-zero.dtb
fdtoverlay_addr_r=0x41B00000
kernel_addr_r=0x41000000
load_efi_dtb=load ${devtype} ${devnum}:${distro_bootpart} ${fdt_addr_r} ${prefix}${efi_fdtfile}
loadaddr=0x42000000
mmc_boot=if mmc dev ${devnum}; then devtype=mmc; run scan_dev_for_boot_part; fi
mmc_bootdev=0
partitions=name=loader1,start=8k,size=32k,uuid=${uuid_gpt_loader1};name=loader2,size=984k,uuid=${uuid_gpt_loader2};name=esp,size=128M,bootable,uuid=${uuid_gpt_esp};name=system,size=-,uuid=${uuid_gpt_system};
pxefile_addr_r=0x41A00000
ramdisk_addr_r=0x41C00000
scan_dev_for_boot=echo Scanning ${devtype} ${devnum}:${distro_bootpart}...; for prefix in ${boot_prefixes}; do run scan_dev_for_extlinux; run scan_dev_for_scripts; done;run scan_dev_for_efi;
scan_dev_for_boot_part=part list ${devtype} ${devnum} -bootable devplist; env exists devplist || setenv devplist 1; for distro_bootpart in ${devplist}; do if fstype ${devtype} ${devnum}:${distro_bootpart} bootfstype; then run scan_dev_for_boot; fi; done; setenv devplist
scan_dev_for_efi=setenv efi_fdtfile ${fdtfile}; if test -z "${fdtfile}" -a -n "${soc}"; then setenv efi_fdtfile ${soc}-${board}${boardver}.dtb; fi; for prefix in ${efi_dtb_prefixes}; do if test -e ${devtype} ${devnum}:${distro_bootpart} ${prefix}${efi_fdtfile}; then run load_efi_dtb; fi;done;run boot_efi_bootmgr;if test -e ${devtype} ${devnum}:${distro_bootpart} efi/boot/bootarm.efi; then echo Found EFI removable media binary efi/boot/bootarm.efi; run boot_efi_binary; echo EFI LOAD FAILED: continuing...; fi; setenv efi_fdtfile
scan_dev_for_extlinux=if test -e ${devtype} ${devnum}:${distro_bootpart} ${prefix}${boot_syslinux_conf}; then echo Found ${prefix}${boot_syslinux_conf}; run boot_extlinux; echo SCRIPT FAILED: continuing...; fi
scan_dev_for_scripts=for script in ${boot_scripts}; do if test -e ${devtype} ${devnum}:${distro_bootpart} ${prefix}${script}; then echo Found U-Boot script ${prefix}${script}; run boot_a_script; echo SCRIPT FAILED: continuing...; fi; done
scriptaddr=0x41900000
serial#=12c0000127c26729
soc=sunxi
stderr=serial@1c28000
stdin=serial@1c28000
stdout=serial@1c28000
uuid_gpt_esp=c12a7328-f81f-11d2-ba4b-00a0c93ec93b
uuid_gpt_system=69dad710-2ce4-4e3c-b16c-21a1d49abed3
Environment size: 4258/131068 bytes
=>
然后通过 uboot 的串口打印以及环境变量来找寻更多信息,帮助解决问题。