Getting started with embedded Linux starts with modifying the serial port baud rate

Keywords: Embedded system

environment

Hardware: bananapi m1
Software: buildroot (uboot-2018.07 + linux-4.18.12)

Default baud rate

compile

$ make bananapi_m1_defconfig
$ make

After compilation, the SD card burns the image sdcard.img, inserts BPI and starts. By default, the baud rate of the serial port of uboot and kernel is 115200. When we set the serial port tool to 115200 and 8n1, we can see the log at system startup, including the log printing of uboot and kernel.

Modify baud rate

How to modify the baud rate? It is also divided into two parts, uboot and kernel.
First look at uboot and use the make uboot menuconfig command

In the path → Device Drivers → Serial drivers, change the Default baudrate to 57600. Then recompile uboot.
In fact, it is to modify the config in output/build/uboot-2018.07/.config_BAUDRATE

#
# Serial drivers
#
CONFIG_BAUDRATE=57600                                                                                                      
CONFIG_REQUIRE_SERIAL_CONSOLE=y
CONFIG_SPECIFY_CONSOLE_INDEX=y
CONFIG_SERIAL_PRESENT=y
CONFIG_SPL_SERIAL_PRESENT=y
CONFIG_CONS_INDEX=1
CONFIG_DM_SERIAL=y

Then modify the serial port baud rate of the kernel. Different boards, different images and modified positions are different. I know two kinds

1. Modify dts

Here I see that the compiled image uses the file sun7i-a20-bananapi.dtb, so I modify output/build/linux-4.18.12/arch/arm/boot/dts/sun7i-a20-bananapi.dts

    chosen {
        // stdout-path = "serial0:115200n8";
        stdout-path = "serial0:57600n8";
    };

Recompile the kernel, compile it completely to generate sdcard.img, burn it again, change the baud rate of the serial port tool to 57600, and start it

It can be seen that the uboot phase is normal, and there is garbled code in the kernel phase. Obviously, the serial port baud rate of the kernel has not been successfully modified. Where is the problem?

2. Modify boot.scr

After carefully looking at the startup log of uboot, I didn't see the print of loading sun7i-a20-bananapi.dtb, but I saw this sentence Found U-Boot script /boot.scr. What is this sentence? According to the data, boot.scr is compiled and generated through boot.cmd.
boot.cmd

setenv bootargs console=ttyS0,115200 earlyprintk root=/dev/mmcblk0p2 rootwait                                               
  
mmc dev 0
fatload mmc 0 $kernel_addr_r zImage
fatload mmc 0 $fdt_addr_r sun7i-a20-bananapi.dtb

bootz $kernel_addr_r - $fdt_addr_r

You can find the answer instantly when you see the contents of boot.cmd. Here, the baud rate is passed from uboot to the kernel in the form of startup parameters. This can also be confirmed by executing the command cat /proc/cmdline after the system starts

Welcome to Bananapi M1
buildroot login: root
# cat /proc/cmdline
console=ttyS0,115200 earlyprintk root=/dev/mmcblk0p2 rootwait
#

Console = ttys0115200 earlyprintk root = / dev / mmcblk0p2 rootwait is the content of boot.scr.
Let's revise it
boot.cmd

setenv bootargs console=ttyS0,57600 earlyprintk root=/dev/mmcblk0p2 rootwait                                               
  
mmc dev 0
fatload mmc 0 $kernel_addr_r zImage
fatload mmc 0 $fdt_addr_r sun7i-a20-bananapi.dtb

bootz $kernel_addr_r - $fdt_addr_r

compile

$  mkimage -A arm -T script -O linux -d boot.cmd boot.scr

Instead of recompiling and burning the entire image, we can compile and replace the boot.scr file separately. Start after replacement

It can be seen that both uboot and kernel normally print log s at the baud rate of 57600, and the baud rate is also 57600 through cat /proc/cmdline.

# cat /proc/cmdline
console=ttyS0,57600 earlyprintk root=/dev/mmcblk0p2 rootwait

Posted by PlasmaDragon on Mon, 11 Oct 2021 10:30:27 -0700