meishaoming / blog

MIT License
1 stars 2 forks source link

raspberry pi cm3 with wifi bt #40

Open meishaoming opened 5 years ago

meishaoming commented 5 years ago

raspberry pi cm3 with wifi bt

在使用 CM3 来做一个项目,要用到 WiFi&BT。Pi 3B 上使用的 WiFi&BT 芯片是 BCM43438,在 CM3 上参照 Pi 3B 也用了这颗芯片。本文记录调试过程中查到的一些知识点。

UART 的 RTS 和 CTS

RTS (Require To Send) 是输出信号,当它输出低电平时,表示告诉对端设备:我可以接收数据,你可以发送了。

CTS (Clear To Send) 是输入信号,当检查到它为低电平时,表示对方可以接收设备,我可以发送了。

所以跟 TX/RX 信号一样,RTS 和 CTS 信号线也是与对端交叉连接。

主控端 设备端
TX RX
RX TX
RTS CTS
CTS RTS

raspberry pi 3b 的 uart

树莓派硬件上有两个 UART,分别是

硬件上一般会使用 PL011 UART 来连接 BT,mini UART 作为 linux console output。

mini UART 的时钟源来自 VPU ,所以 VPU 的频率变化会引起 mini UART 的波特率变化。当在 config.txt 里加上了 enable_uart=1,VPU 的固件(start.elf)会把 VPU 的频率固定在 250MHz,使得 mini UART 的波特率保持一致。

UART 的输出 pin 脚可以配置。Pi 3B 的配置情况是:

UART 还可以配置 flow control 引脚 RTS/CTS。在 Pi 3B 中 GPIOs 28-31 作为 PCM 功能,BT 没有接 RTS 和 CTS。而在 Zero W 中,GPIO 30 和 31 用作 RTS 和 CTS 。

参考上一小节 RTS/CTS 的作用,当不使用 flow control 功能时,设备端(BCM43438)的 RTS 引脚悬空,CTS 引脚接地。

参考 THE RASPBERRY PI UARTS

raspberry pi WiFi & BT

raspberry pi 3b 使用了 BCM43438 这个 WiFi + BT 的芯片。

WiFi 功能使用了 sdio1:pin 34-39 (clk,cmd,d0~d3) 。

BT 使用了 ttyAMA0: pin 32(tx), 33(rx)。gpio43 作为 GPCLK2 功能输出 32.768KHz 时钟给 BT。

gpio43 作为 bt_pins (/proc/device-tree/soc/gpio@7e200000/bt_pins) ,这样 hciuart 服务才能运行:

pi-bluetooth.hciuart.service

[Unit]
Description=Configure Bluetooth Modems connected by UART
ConditionPathIsDirectory=/proc/device-tree/soc/gpio@7e200000/bt_pins
Requires=dev-serial1.device
After=dev-serial1.device

[Service]
Type=forking
ExecStart=/usr/bin/btuart

[Install]
WantedBy=multi-user.target

btuart 使用 hciattach 工具,在串口 /dev/ttyAMA0 上发送 HCI 命令。对端(BCM43438)会回应,根据回应的内容触发加载相应的驱动,并加载 firmware 到 BCM43438 中的蓝牙模块中:

...
9月 24 03:42:28 raspberrypi btuart[297]: bcm43xx_init
9月 24 03:42:28 raspberrypi btuart[297]: Flash firmware /lib/firmware/brcm/BCM4345C0.hcd
9月 24 03:42:28 raspberrypi btuart[297]: Set BDADDR UART: b8:27:eb:eb:f5:3d
9月 24 03:42:28 raspberrypi btuart[297]: Set Controller UART speed to 3000000 bit/s
...

要了解这里的详细过程,得读一读 hciattach 的源代码。

meishaoming commented 5 years ago

网络配置

给 eth0 配置两个接口,一个 static,一个dhcp

创建文件:/etc/network/interfaces.d/eth0-static-ip

内容:

auto eth0:1
allow-hotplug eth0:1
iface eth0:1 inet static
    address 192.168.9.23
    netmask 255.255.255.0
    metric 500

/etc/dhcpcd.conf 中增加:

interface eth0
metric 200
static domain_name_servers=114.114.114.114

interface wlan0
metric 300
static domain_name_servers=114.114.114.114

metric 的值越小、优先级越高。这里是 eth0 优先。

重启即可

meishaoming commented 5 years ago

修改 pi 密码

pi 的默认密码是:raspberry

passwd

修改 apt 软件源

使用 清华源

修改 /etc/apt/sources.list

#deb http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi
# Uncomment line below then 'apt-get update' to enable 'apt-get source'
#deb-src http://raspbian.raspberrypi.org/raspbian/ stretch main contrib non-free rpi
deb http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib
#deb-src http://mirrors.tuna.tsinghua.edu.cn/raspbian/raspbian/ stretch main non-free contrib

系统常用软件安装

sudo apt update
sudo apt install -y vim tmux git silversearcher-ag strace

git 配置

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global core.editor vim

配置、安装 python 相关组件

sudo apt install -y python-pip

sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U

sudo pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

vim 配置

echo "set nu" > ~/.vimrc
echo "set shiftwidth=4" >> ~/.vimrc
echo "set tabstop=4" >> ~/.vimrc
echo "set et"  >> ~/.vimrc
echo "syntax enable"  >> ~/.vimrc
echo "set hlsearch " >> ~/.vimrc
echo "map <C-h> :tabp<CR>" >> ~/.vimrc
echo "map <C-l> :tabn<CR>" >> ~/.vimrc

每天晚上 一点重启

sudo vim /etc/crontab

00 1    * * *   root    reboot

sudo systemctl restart cron

meishaoming commented 5 years ago

image

meishaoming commented 5 years ago

/etc/config/ssh_config

    GSSAPIAuthentication no
    Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
    MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
    SendEnv LANG LC_*
    HashKnownHosts yes
    GSSAPIAuthentication yes
    HostKeyAlgorithms ssh-rsa,ssh-dss