0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ubuntu 24.04 on Raspberry Pi 4 で GPIO14/15 の UART を使う手順メモ

Posted at

はじめに

Raspberry PiにUbuntuを入れて、ROS2を使ってUARTでマイコンやLiDARの読み取りをしようと思ったのですが、どうやらUARTを有効化するには少し設定が必要なようでした。今回はその設定手順を実際に検証してまとめ直しました。Raspberry Piでロボットを作ろうと思っていて、UARTを有効にする方法を探しておられる方の力になれば幸いです。

UbuntuにはRaspberry Pi OSのようなハードウェア設定ができるツールがない!

Raspberry Pi OS では raspi-config からサクッと UART を有効化できますが、

Ubuntu 24.04 for Raspberry Pi だと raspi-config使えません。
image.png

この記事では、

  • Ubuntu 公式サイトから配布されているUbuntu 24.04 for Raspberry Pi イメージRaspberry Pi 4B に書き込んで起動し、f
  • Bluetooth は有効のまま、GPIO14/15 (ピン 8/10) の UART を /dev/ttyS0 として使う

までの手順をまとめます。

Raspberry PiのUARTの詳細な説明については公式ドキュメントを読んでいただけると複雑な設定などもできるかと思います。


環境

  • ボード:Raspberry Pi 4 Model B
  • OS:Ubuntu 24.04 for Raspberry Pi
  • UART ピン:
    • GPIO14 → TXD0(ボード上のピン 8)
    • GPIO15 → RXD0(ボード上のピン 10)

全体の流れ

Ubuntu で UART を使えるようにするには

  1. ブート引数からシリアルコンソールを外す(UART をログに使わないようにする)
  2. config.txt で UART を有効化する
  3. 再起動後 /dev/ttyS0 を使って動作確認

の3ステップで進めていきます。

Ubuntu の Raspberry Pi イメージでは、

cmdline.txtconfig.txt/boot/firmware 配下にあります。

1. 設定ファイルのバックアップ

まずは念のためバックアップを取っておきます。

何かおかしくなったら .bak を戻せば復旧できます。

cd /boot/firmware

sudo cp -p cmdline.txt cmdline.txt.bak
sudo cp -p config.txt  config.txt.bak

2. シリアルコンソールを無効化(cmdline.txt)

Ubuntu のデフォルトでは、UART にカーネルコンソールがぶら下がっていることがあります。

これを外さないと、UARTを自分のアプリから自由に使えません。まずはそれを解除していきます。下記のコマンドで設定ファイルを編集します。

sudo nano /boot/firmware/cmdline.txt

長い文字列が表示されるかと思います。cmdline.txt は 1 行だけの長い文字列になっているはずです。
その中からconsole=serial0,115200という部分を探して 削除 します。(ない場合もあるので、なければパス)

変更前の例:

... console=serial0,115200 console=tty1 root=LABEL=writable ...

変更後:

... console=tty1 root=LABEL=writable ...

console=tty1 は HDMI につながるコンソールなので残して OK です。

保存して nano を終了します(Ctrl+OEnterCtrl+X)。


3. UART を有効化(config.txt)

続いて config.txt を編集して UART を有効にします。

sudo nano /boot/firmware/config.txt

ファイルのどこか([all] セクションの中など)に、

次の 1 行が入っていることを確認、または追記します:

enable_uart=1

全体はこんな感じになります。今回はpi4のセクションのところにenable_uart=1を記しました。


[all]
arm_64bit=1
kernel=vmlinuz
cmdline=cmdline.txt
initramfs initrd.img followkernel

# Enable the audio output, I2C and SPI interfaces on the GPIO header. As these
# parameters related to the base device-tree they must appear *before* any
# other dtoverlay= specification
dtparam=audio=on
dtparam=i2c_arm=on
dtparam=spi=on

# Comment out the following line if the edges of the desktop appear outside
# the edges of your display
disable_overscan=1

# If you have issues with audio, you may try uncommenting the following line
# which forces the HDMI output into HDMI mode instead of DVI (which doesn't
# support audio output)
#hdmi_drive=2

# Enable the KMS ("full" KMS) graphics overlay, leaving GPU memory as the
# default (the kernel is in control of graphics memory with full KMS)
dtoverlay=vc4-kms-v3d
disable_fw_kms_setup=1

# Autoload overlays for any recognized cameras or displays that are attached
# to the CSI/DSI ports. Please note this is for libcamera support, *not* for
# the legacy camera stack
camera_auto_detect=1
display_auto_detect=1

# Config settings specific to arm64
dtoverlay=dwc2

[pi4]
max_framebuffers=2
arm_boost=1
enable_uart=1

[pi3+]
# Use a smaller contiguous memory area, specifically on the 3A+ to avoid an
# OOM oops on boot. The 3B+ is also affected by this section, but it shouldn't
# cause any issues on that board
dtoverlay=vc4-kms-v3d,cma-128

[pi02]
# The Zero 2W is another 512MB board which is occasionally affected by the same
# OOM oops on boot.
dtoverlay=vc4-kms-v3d,cma-128

[cm4]
# Enable the USB2 outputs on the IO board (assuming your CM4 is plugged into
# such a board)
dtoverlay=dwc2,dr_mode=host

[all]

今回は Bluetooth を切らない前提なので、

dtoverlay=disable-bt などは 書きません

  • この設定で、GPIO14/15 側に mini-UART (ttyS0) が割り当てられた状態で使えます。
  • PL011 を使いたい場合は Bluetooth を切る必要がありますが、それは今回のスコープ外。
  • (PL011は1MbpsのUARTを使える嬉しさがあります。しかしBluetoothのペリフェラルと同居しているので、もしPL011が使いたい場合はBluetoothをOFFにする必要があります。)

保存して終了します。


4. 再起動とデバイス確認

設定を反映させるために再起動します。

sudo reboot

起動後、UART デバイスを確認します。

ls -l /dev/ttyS0

5. 権限設定(必要なら)

通常ユーザーで UART を触りたい場合は、そのユーザーを dialout グループに追加します。

sudo adduser $USER dialout

実行後、一度ログアウトしてログインし直すと反映されます。


6. 配線と minicom での動作確認

minicomでUARTの受信ができるかチェックしてみましょう。

何かしらのデバイスを繋いでラズパイのUARTのピンに値を送りつけてみてください。minicomで確認することができるはずです。

6.1 GPIO 配線

  • ピン 6 : GND
  • ピン 8 : GPIO14 / TXD0
  • ピン 10: GPIO15 / RXD0

例えば USB シリアル変換器とつなぐ場合:

  • Pi GND (ピン6) ↔ USBシリアル GND
  • Pi TXD0 (ピン8) ↔ USBシリアル RX
  • Pi RXD0 (ピン10) ↔ USBシリアル TX

クロス接続を忘れずに。

image.png

シリアル変換器のおすすめ

6.2 minicom のインストール

sudo apt update
sudo apt install minicom

6.3 minicom で通信テスト

ボーレートは115200にしていますが、各自任意の値にしてください。(LD19 LiDARを使う場合は230400だった)

sudo minicom -D /dev/ttyS0 -b 115200
  • USB シリアル変換器側でパソコンからラズパイに何か文字を送る
  • または TX と RX をショートして、自分が打ったキーが戻ってくるか確認

まとめ

お疲れ様でした。
Ubuntu 24.04 for Raspberry Pi で、raspi-config なしに GPIO14/15 の UART を使うには

  1. /boot/firmware/cmdline.txt から console=serial0,115200 を削除
  2. /boot/firmware/config.txtenable_uart=1 を追加
  3. 再起動
  4. /dev/ttyS0 を UART デバイスとして使用
  5. 必要なら dialout グループにユーザーを追加
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?