树莓派打开GPIO的TTL串口

     树莓派火了有很长一段时间了,最近决定入坑树莓派。今年最新发布了3B+版本,所以就入手了该版本的树莓派,经过几天等待,快递到达,下载最新系统烧写到SD卡,成功启动后能正常SSH。
     把玩两天发现这就是一台小主机啊!与PC安装debian差不多,可以直接从源安装许多软件,确实非常方便。最初的试用新鲜感过了后就回归研究了。首先想要用用串口,发现GPIO引脚上有TTL串口,于是直接插上串口线连接电脑,神奇的现象发生了:居然没有任何反应......
  what?这不是应有的剧本啊!!不是应该直接能通信吗?难道板子是坏的?短暂的伤神后决定应该去网上搜一下是否有出类似问题的先例,在这里先感谢一下网上各位大神的无私奉献!!经过一番搜索,发现没有一个可以直接解决的办法,最终经过比较和验证终于问题得到解决,因而在这里将解决办法记录一下,方便下次查询。
    树莓派3B+使用的是BCM2837B0四核A53 ARM64处理器,网上资料显示其有两个串口。一个是硬件串口默认连接了蓝牙,另一个是mini串口,我暂时不知道连接在哪里,反正就是没有连接到GPIO来直接输出。通过搜索发现官方论坛的一篇文章有谈到这个问题,虽然是讨论Pi3的,但是我认为3B+可能也适用,所以就照做了一下。
    论坛原文地址: https://www.raspberrypi.org/forums/viewtopic.php?f=107&t=138223
    其中有一段描述如下:
Re: Overlay to remap Pi 3 UART
    Quote
Mon Mar 07, 2016 10:30 am
I've pushed the overlay (pi3-miniuart-bt) to the source trees. It will appear in the next release, or you can download prebuilt one here. The commit message says:
    Switch Pi3 Bluetooth function to use the mini-UART (ttyS0) and restore
    UART0/ttyAMA0 over GPIOs 14 & 15. Note that this may reduce the maximum
    usable baudrate.

    It is also necessary to edit /lib/systemd/system/hciuart.server and
    replace ttyAMA0 with ttyS0.

    If cmdline.txt uses the alias serial0 to refer to the user-accessable port
    then the firmware will replace with the appropriate port whether or not
    this overlay is used.

To use, add this to your config.txt:

Code: Select all
dtoverlay=pi3-miniuart-bt


     看起来就是需要使用设备树将mini串口重映射给蓝牙,把硬件串口重映射到GPIO,于是照做,在config.txt文件的末尾添加了上面的这一行,开机发现没有反应,这...... 于是只能暂时放弃了......

    直到有一天发现FreeBSD官方支持PI3B,虽然是支持PI3B,但还是那句话,先不管板子是否支持,暂且认为它可以支持,就下载了BSD的镜像dd到SD卡。加载,居然发现串口有启动信息输出!!!简直了!!这就是我想要的!虽然只能启动到uboot就不能再继续了,这个应该是FreeBSD还不支持3B+导致的,这个暂且不管,板子太新还不支持也不是我能解决得了的,先研究一下为什么它的镜像能让串口有输出。首先查看了FreeBSD的镜像构成,与pi官方的构成类似,有一个FAT32分区和一个FreeBSD分区:
jiangxianxu@debian:/data/work/raspberrypi$ sudo fdisk -l FreeBSD-12.0-CURRENT-arm64-aarch64-RPI3-20180514-r333606.img
Disk FreeBSD-12.0-CURRENT-arm64-aarch64-RPI3-20180514-r333606.img: 2.5 GiB, 2684354560 bytes, 5242880 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device                                                                                                      Boot  Start     End Sectors  Size Id Type
FreeBSD-12.0-CURRENT-arm64-aarch64-RPI3-20180514-r333606.img1 *      2079  104390  102312   50M  c W95 FAT32 (LBA)
FreeBSD-12.0-CURRENT-arm64-aarch64-RPI3-20180514-r333606.img2      104391 5242859 5138469  2.5G a5 FreeBSD
jiangxianxu@debian:/data/work/raspberrypi$


将第一个分区持载到我们的系统中:
jiangxianxu@debian:/data/work/raspberrypi$ sudo mount -o loop,offset=1064448,sizelimit=52383744 FreeBSD-12.0-CURRENT-arm64-aarch64-RPI3-20180514-r333606.img /mnt/iso1/
jiangxianxu@debian:/data/work/raspberrypi$ ll /mnt/iso1/
total 12652
-rwxr-xr-x 1 root root    5888 May 15 04:10 armstub8.bin
-rwxr-xr-x 1 root root   24240 Mar 27 00:02 bcm2710-rpi-3-b.dtb
-rwxr-xr-x 1 root root   52064 Mar 27 00:02 bootcode.bin
-rwxr-xr-x 1 root root      75 May 15 04:10 config.txt
drwxr-xr-x 3 root root    4096 May 15 05:31 EFI
-rwxr-xr-x 1 root root    2595 Mar 27 00:02 fixup_cd.dat
-rwxr-xr-x 1 root root    6569 Mar 27 00:02 fixup.dat
-rwxr-xr-x 1 root root    9722 Mar 27 00:02 fixup_db.dat
-rwxr-xr-x 1 root root    9726 Mar 27 00:02 fixup_x.dat
-rwxr-xr-x 1 root root    1494 Mar 27 00:02 LICENCE.broadcom
drwxr-xr-x 2 root root    4096 May 15 05:31 overlays
-rwxr-xr-x 1 root root     123 May 15 04:07 README
-rwxr-xr-x 1 root root  672804 Mar 27 00:02 start_cd.elf
-rwxr-xr-x 1 root root 4967492 Mar 27 00:02 start_db.elf
-rwxr-xr-x 1 root root 2824484 Mar 27 00:02 start.elf
-rwxr-xr-x 1 root root 3911492 Mar 27 00:02 start_x.elf
-rwxr-xr-x 1 root root  430040 May 15 04:07 u-boot.bin
jiangxianxu@debian:/data/work/raspberrypi$


    经过一番对比确定它可以从串口输出启动信息的原因与该分区中的config.txt有关:
jiangxianxu@debian:/data/work/raspberrypi$ cat /mnt/iso1/config.txt
init_uart_clock=3000000
enable_uart=1
kernel=u-boot.bin
kernel7=u-boot.bin
jiangxianxu@debian:/data/work/raspberrypi$

    其中有一条关键的配置:enable_uart=1,于是马上行动,将该行添加到我自己的config.txt文件,开机,发现还是没有反应,真尼玛是不给人活路啊,又一次无路可走。但是心里一直在牵挂着这个事情,突然有一天想到是不是虽然串口可以用了,但是内核没有打印到串口来?因为内核是需要cmdline告知它串口的,马上检查cmdline.txt,果然如此,于是修改该文件,修改内容对比如下:
pi@raspberrypi:~ $ diff /boot/cmdline_uart.txt /boot/cmdline_bak.txt
1c1
< dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=PARTUUID=89f4525b-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
---
> dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=PARTUUID=89f4525b-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
pi@raspberrypi:~ $

    也就是将console=serial0改成了console=ttyAMA0(这里又要感谢网上大大们了,通过前面的搜索已经知道了硬件串口是ttyAMA0),再去开机,神奇的事情发生了,真的有内核启动信息输出了!!!发现新天地!!
    但是后面又发现了,这样做之后WIFI不能用......于是联想到是不是与蓝牙被抢了串口有关?马上翻出之前重映射串口的方法加上,最终将config.txt文件改了,打开串口的同时又重映射串口,文件对比如下:
pi@raspberrypi:~ $ diff /boot/config.txt /boot/config_old.txt
57,59d56
<
< dtoverlay=pi3-miniuart-bt
< enable_uart=1
pi@raspberrypi:~ $


    修改完成后开机,OK,一切都那么自然,串口正常输出,WIFI正常使用!!!!但是蓝牙好像不能用......只是我平时也不用蓝牙,也就暂时不管它了。

    啰嗦了这么多,总结一下让硬件串口输了到GPIO TTL引脚需要做的事情如下:
1、在config.txt中增加dtoverlay=pi3-miniuart-bt重映射串口
2、在config.txt中增加enable_uart=1打开串口
3、在cmdline.txt中将console=serial0改成console=ttyAMA0
通过这3个步骤就已经可以让串口正常使用了,可以用于内核调试,uboot调试等等......
 




猜你喜欢

转载自blog.csdn.net/u013451404/article/details/80385496