購買產品:PL2303HX USB轉TTL傳輸線 ,PL2303HXD USB轉TTL傳輸線 。
剛拿到 Pi 3 如果安裝 2016-02-26-raspbian-jessie 的映像檔後想使用序列埠連線 ,會發現出現亂碼,該怎麼辦?
這是因為原本 Pi 3 內建的硬體 UART 被 BCM2837 SoC 拿去給 Bluetooth 晶片組使用,而原本的 UART 輸出腳位(GPIOs 14 & 15)改成用 mini-uart port。意思是原本硬體 UART 有獨立的 clock divisor,因此 baud rate 可以維持在 115200,可是 mini-uart 使用系統核心時脈,實際只能跑到 72000 左右的 baud rate,因此當使用 115200 的 baud rate 連線就會出現亂碼。
解決的方法為:
步驟一 ,增加一個 pi3-disable-bt-overlay 的 device tree overlay 。這個 overlay 會停用藍牙,並且將 UART0/ttyAMA0 再設定給 GPIOs 14 & 15。
建議將 image 寫入 SD 卡後就直接修改 /boot/config.txt
,在檔案最後增加兩行。
force_turbo=1
dtoverlay=pi3-disable-bt
其中,force_turbo=1
是強制 CPU 的時脈維持 1.2GHz 的時脈。預設值為 0 時表示時脈為 ondemand ,會隨著負載而調整。
而 dtoverlay=pi3-disable-bt
表示載入 pi3-disable-bt
這個 device tree overlay,將會停用藍牙並將 UART0/ttyAMA0 設定到 GPIOs 14 & 15 腳位。
其實做完步驟一 以後就可以用 115200 的 baud rate 透過序列埠連線進 Pi 了。可是雖然載入 pi3-disable-bt-overlay
,但藍牙的服務依然是開啟的,因此還是要進入系統將藍牙服務停掉。
$ sudo systemctl disable hciuart
最後,因為使用 force_turbo=1
這個設定,整個 SoC 溫度會很高,因此再更新系統(包括 firmware 和 dtb)後就可以把這個設定註解掉,重開機生效。
$ sudo apt-get update
$ sudo apt-get upgrade
更多資訊:
1. 要如何看 CPU 時脈?
可以用 cat
指令查看以下三個檔案如下,分別為最高時脈、目前時脈、最低時脈。
$ cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq
$ cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq
$ cat /sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq
2. 如何看目前的 Device Tree?
首先要安裝 device tree compiler 。
$ sudo apt-get install device-tree-compiler
再用 dtc
指令查看。
$ dtc -I fs /proc/device-tree
結果會像
/dts-v1/;
/ {
model = "Raspberry Pi 3 Model B Rev 1.2";
compatible = "brcm,bcm2710", "brcm,bcm2709";
memreserve = <0x3b000000 0x4000000>;
#address-cells = <0x1>;
#size-cells = <0x1>;
interrupt-parent = <0x1>;
...
soc {
compatible = "simple-bus";
ranges = <0x7e000000 0x3f000000 0x1000000>;
#address-cells = <0x1>;
phandle = <0x27>;
#size-cells = <0x1>;
linux,phandle = <0x27>;
...
uart0_pins {
phandle = <0x34>;
brcm,function = <0x4>;
brcm,pins = <0xe 0xf>;
brcm,pull = <0x0 0x2>;
linux,phandle = <0x34>;
};
uart1_pins {
phandle = <0xf>;
brcm,function = <0x2>;
brcm,pins = <0xe 0xf>;
brcm,pull = <0x0 0x2>;
linux,phandle = <0xf>;
};
...
參考資料:
* CONFIG.TXT
* DEVICE TREES, OVERLAYS AND PARAMETERS
* Raspberry Pi Device Tree Overlays
* Raspberry Pi 3 UART Overlay Workaround
* Raspberry Pi 3 compatibility (BT disable & serial port remap fix)
* RPi Serial Connection
* Raspberry Pi Serial Communication: What, Why, and a Touch of How
* More on Raspberry Pi serial ports