树莓派3B串口配置教程
前言
为什么需要配置串口?直接不能用吗?
树莓派有两个串口,一个是硬件串口(PL011 UART),一个是迷你串口(mini-uart),在默认情况下,蓝牙模块会使用PL011串口也就是硬件串口,而迷你串口(mini-uart)用于控制台输出。迷你UART的波特率与VC4 GPU上VPU的核心频率相关联。这意味着当VPU频率调节器改变核心频率时,UART的波特率也会发生变化。这使得UART在默认状态下受限使用。
配置
本教程将参考官方文档教大家配置
树莓派系统: raspbian-stretch
官方文档(英文)
- 官方文档(英文)
- 树莓派中也有配置文档
到达overlays文件夹
1 | cd /boot/overlays |
查看全部文档
1 | cat README |
查看单个文档
1 | dtoverlay -h pi3-miniuart-bt |
这里把文档贴出来
Name: pi3-miniuart-bt
Info: 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.N.B. It is also necessary to edit /lib/systemd/system/hciuart.service and replace ttyAMA0 with ttyS0, unless you have a system with udev rules that create /dev/serial0 and /dev/serial1, in which case use/dev/serial1 instead because it will always be correct. Furthermore,you must also set core_freq=250 in config.txt or the miniuart will notwork.
Usage: dtoverlay=pi3-miniuart-bt
Params:
教程开始
1. 通过SSH连接树莓派
2. 查看串口状态
1 | ls -l /dev |
结果将会看到所有接口 找到以下串口
1 | lrwxrwxrwx 1 root root 7 Nov 21 12:14 serial0 -> ttyS0 |
3. 关闭板载蓝牙
1 | sudo systemctl disable hciuart |
4.修改 hciuart.service
文件路径 /lib/systemd/system/hciuart.service
将文件中 ttyAMA0 替换为 ttyS0
如果hciuart.service中没有ttyAMA0可以跳过本步骤!
1 | sudo vim /lib/systemd/system/hciuart.service |
5. 修改 cmdline.txt
文件路径 /boot/cmdline.txt (修改前注意备份)
删除掉 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
1 | cd /boot |
修改为以下配置 (参考)
1 | dwc_otg.lpm_enable=0 console=tty1 root=PARTUUID=d71c341c-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles |
6. 修改 config.txt
文件路径 /boot/config.txt (修改前注意备份)
在尾部添加一行 dtoverlay=pi3-miniuart-bt
如果存在 dtoverlay=pi3-disable-bt 请在前面加 # 注释掉
据说如果同时存在两行串口会不能使用
1 | cd /boot |
7. 重启
重启
1 | sudo reboot |
重启后查看串口接口
1 | ls -l /dev |
如果结果一样证明已经配置成功,诺出现不能开机,请自行恢复备份文件!