tshell_blog

ソフトウェアと車輪がついた乗り物のはなし

Raspberry Pi 4 + Ubuntu 20.04で複数のUARTを有効にする

UART configurationによるとRaspberry Pi 4では6つのUARTが利用できるはずですが,Ubuntu 20.04からは1つのUARTしか見えません。しかもデフォルトではシリアルコンソールに割り当てられているので,これを使って通信するアプリケーション(例えばルンバを動かすとか)を作るのはやめた方がよさそうです。

環境

設定ファイルの編集

以下2つのファイルを編集して,まずはUART0(8, 10pin) 以外のUARTを有効にしてみます。

syscfg.txtで有効にするUARTの数を決めて,usercfg.txtでdtoverlayを読み込ませます。

/boot/firmware/syscfg.txtの編集

以下のように``enable_uart‘‘を2に修正します。

# This file is intended to be modified by the pibootctl utility. User
# configuration changes should be placed in "usercfg.txt". Please refer to the
# README file for a description of the various configuration files on the boot
# partition.

enable_uart=2
dtparam=audio=on
dtparam=i2c_arm=on
dtparam=spi=on
cmdline=cmdline.txt

/boot/firmare/usercfg.txtの編集

dtoverlay=uart2 を追加します。

# Place "config.txt" changes (dtparam, dtoverlay, disable_overscan, etc.) in
# this file. Please refer to the README file for a description of the various
# configuration files on the boot partition.

dtoverlay=uart2

UARTが有効になったことの確認

syscfg.txt,usercfg.txtを編集して再起動すると/dev/ttyAMA1が見えるようになります。

ubuntu@ubuntu:~$ dmesg | grep ttyAMA
[    1.654046] fe201000.serial: ttyAMA0 at MMIO 0xfe201000 (irq = 14, base_baud = 0) is a PL011 rev2
[    1.654434] fe201400.serial: ttyAMA1 at MMIO 0xfe201400 (irq = 14, base_baud = 0) is a PL011 rev2

screen コマンドで/dev/ttyAMA1を開いて,通信できることを確認しましょう。 pin27がTx,pin28がRxです。

$ screen /dev/ttyAMA1 9600

screenから抜ける方法はいつも忘れますが,Ctrl + a, kです。

ほかのUARTも有効にする

syscfg.txt,usercfg.txtを以下のように設定すればほかのUARTも使えるようになります。

ubuntu@ubuntu:~$ cat /boot/firmware/syscfg.txt
# This file is intended to be modified by the pibootctl utility. User
# configuration changes should be placed in "usercfg.txt". Please refer to the
# README file for a description of the various configuration files on the boot
# partition.

enable_uart=6
dtparam=audio=on
dtparam=i2c_arm=on
dtparam=spi=on
cmdline=cmdline.txt
ubuntu@ubuntu:~$ cat /boot/firmware/usercfg.txt
# Place "config.txt" changes (dtparam, dtoverlay, disable_overscan, etc.) in
# this file. Please refer to the README file for a description of the various
# configuration files on the boot partition.

dtoverlay=uart2
dtoverlay=uart3
dtoverlay=uart4
dtoverlay=uart5
ubuntu@ubuntu:~$ dmesg | grep ttyAMA
[    1.662514] fe201000.serial: ttyAMA0 at MMIO 0xfe201000 (irq = 14, base_baud = 0) is a PL011 rev2
[    1.662963] fe201400.serial: ttyAMA1 at MMIO 0xfe201400 (irq = 14, base_baud = 0) is a PL011 rev2
[    1.663212] fe201600.serial: ttyAMA2 at MMIO 0xfe201600 (irq = 14, base_baud = 0) is a PL011 rev2
[    1.663448] fe201800.serial: ttyAMA3 at MMIO 0xfe201800 (irq = 14, base_baud = 0) is a PL011 rev2
[    1.663675] fe201a00.serial: ttyAMA4 at MMIO 0xfe201a00 (irq = 14, base_baud = 0) is a PL011 rev2

ピンアサインは以下の通りです。

UART Tx pin Rx pin
/dev/ttyS0 8 10
/dev/ttyAMA0 8 10
/dev/ttyAMA1 27 28
/dev/ttyAMA2 7 29
/dev/ttyAMA3 24 21
/dev/ttyAMA4 32 33

ttyAMA1で使われる27pinはReservedになっており,起動時には何かデータが出力されるようです。起動時に予期しないデータが出力されると困る場合はほかのUARTを使えばよさそうです。
ttyAMA4でしか確認していませんが,ttyAMA1以外であれば起動時にデータは出力されないはずです。