Linux環境でRP2040への書き込みが失敗する問題の解決方法
※この記事はLinux環境でPlatformIO + RP2040を使用し、マイコンへの書き込みが失敗している方向けの記事です。
問題の概要
MacBookでは問題なく動作していたRP2040の書き込みが、Ubuntu環境では失敗してしまいました。原因はLinux環境でのpicotoolの権限設定にありました。この記事では、その解決方法を備忘録として残します。
picotool単体をインストールしたい方はこちらの記事をご覧ください
私の環境
earlephilhower版のRP2040を使用したPlatformIOプロジェクトの設定ファイルです。
[env:pico]
platform = https://github.com/maxgerhardt/platform-raspberrypi.git
board = pico
framework = arduino
board_build.core = earlephilhower
board_build.f_cpu = 200000000L
build_flags =
-std=c++20
-std=gnu++20
build_unflags =
-std=gnu++20
lib_deps =
https://github.com/bakercp/PacketSerial.git
https://github.com/khoih-prog/RPI_PICO_TimerInterrupt.git
エラーの内容
PlatformIOでuploadを実行すると、以下のようなエラーが発生しました。
Linking .pio/build/pico/firmware.elf
Generating UF2 image
picotool uf2 convert -t elf ".pio/build/pico/firmware.elf" ".pio/build/pico/firmware.uf2"
Retrieving maximum program size .pio/build/pico/firmware.elf
Checking size .pio/build/pico/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [ ] 3.9% (used 10228 bytes from 262144 bytes)
Flash: [ ] 3.2% (used 66680 bytes from 2093056 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, jlink, pico-debug, picoprobe, picotool, raspberrypi-swd
CURRENT: upload_protocol = picotool
Looking for upload port...
Using manually specified: picotool
Forcing reset using 1200bps open/close on port picotool
No new RPxxxx device found yet, waiting..
No new RPxxxx device found yet, waiting..
No new RPxxxx device found yet, waiting..
No new RPxxxx device found yet, waiting..
No new RPxxxx device found yet, waiting..
No new RPxxxx device found yet, waiting..
Warning: Picotool did not detect any RPxxxx devices in BOOTSEL mode. Upload might fail.
Uploading .pio/build/pico/firmware.elf
No accessible RP-series devices in BOOTSEL mode were found.
but:
RP2040 device at bus 3, address 35 appears to be in BOOTSEL mode, but picotool
was unable to connect. Maybe try 'sudo' or check your permissions.
*** [upload] Error 249
エラーの原因
重要なのは以下のメッセージです:
RP2040 device at bus 3, address 35 appears to be in BOOTSEL mode, but picotool
was unable to connect. Maybe try 'sudo' or check your permissions.
デバイスは認識されているものの、権限エラーで接続できない状態です。PlatformIOにはpicotoolが含まれていますが、Linux環境ではudevルールが自動設定されないため、手動で設定する必要があります。
解決方法
ステップ1: picotoolリポジトリの取得
まず、picotoolの公式リポジトリをクローンします。
git clone https://github.com/raspberrypi/picotool.git
cd picotool/udev
ステップ2: udevルールファイルの確認
picotool/udevディレクトリに60-picotool.rulesがあることを確認してください。
ファイルの内容:
# Copy this file to /etc/udev/rules.d/
# You can reload the udev rules with "udevadm control --reload"
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="2e8a", \
ATTRS{idProduct}=="0003", \
TAG+="uaccess", \
MODE="660", \
GROUP="plugdev"
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="2e8a", \
ATTRS{idProduct}=="0009", \
TAG+="uaccess", \
MODE="660", \
GROUP="plugdev"
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="2e8a", \
ATTRS{idProduct}=="000a", \
TAG+="uaccess", \
MODE="660", \
GROUP="plugdev"
SUBSYSTEM=="usb", \
ATTRS{idVendor}=="2e8a", \
ATTRS{idProduct}=="000f", \
TAG+="uaccess", \
MODE="660", \
GROUP="plugdev"
このファイルは、Raspberry Pi製のUSBデバイス(RP2040など)へのアクセス権限を設定するものです。
ステップ3: udevルールのインストール
ルールファイルを/etc/udev/rules.d/にコピーします。
sudo cp 60-picotool.rules /etc/udev/rules.d/
ステップ4: udevルールの適用
新しいルールを読み込ませ、接続されているデバイスに適用します。
# udevルールを再読み込み
sudo udevadm control --reload-rules
# 接続中のデバイスに新ルールを適用
sudo udevadm trigger
ステップ5: 動作確認
RP2040をBOOTSELモードで接続し、以下のコマンドで確認できます。
(picotoolを単体でインストールしていないと認識されない可能性あり)
picotool info
sudoなしで実行できれば成功です!
PlatformIOでアップロード
それでは実際にアップロードしてみましょう。成功すると以下のような出力になります。
Processing pico (platform: https://github.com/maxgerhardt/platform-raspberrypi.git; board: pico; framework: arduino)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Verbose mode can be enabled via `-v, --verbose` option
CONFIGURATION: https://docs.platformio.org/page/boards/raspberrypi/pico.html
PLATFORM: Raspberry Pi RP2040 (1.18.0+sha.a1df30f) > Raspberry Pi Pico
HARDWARE: RP2040 200MHz, 256KB RAM, 2MB Flash
DEBUG: Current (blackmagic) External (blackmagic, cmsis-dap, jlink, pico-debug, picoprobe, raspberrypi-swd)
PACKAGES:
- framework-arduinopico @ 1.50401.0+sha.0affea4
- tool-mklittlefs-rp2040-earlephilhower @ 5.100300.230216 (10.3.0)
- tool-openocd-rp2040-earlephilhower @ 5.140200.250530 (14.2.0)
- tool-picotool-rp2040-earlephilhower @ 5.140200.250530 (14.2.0)
- tool-pioasm-rp2040-earlephilhower @ 5.140200.250530 (14.2.0)
- toolchain-rp2040-earlephilhower @ 5.140200.250530 (14.2.0)
Flash size: 2.00MB
Sketch size: 2.00MB
Filesystem size: 0.00MB
PSRAM size: 0.00MB
Maximium Sketch size: 2093056 EEPROM start: 0x101ff000 Filesystem start: 0x101ff000 Filesystem end: 0x101ff000
LDF: Library Dependency Finder -> https://bit.ly/configure-pio-ldf
LDF Modes: Finder ~ chain, Compatibility ~ soft
Found 72 compatible libraries
Scanning dependencies...
Dependency Graph
|-- PacketSerial @ 1.4.0+sha.92c4bae
|-- RPI_PICO_TimerInterrupt @ 1.3.1+sha.45c4aca
|-- CugoSDK
|-- Servo(rp2040) @ 1.0.0
Building in release mode
Retrieving maximum program size .pio/build/pico/firmware.elf
Checking size .pio/build/pico/firmware.elf
Advanced Memory Usage is available via "PlatformIO Home > Project Inspect"
RAM: [ ] 3.9% (used 10228 bytes from 262144 bytes)
Flash: [ ] 3.2% (used 66680 bytes from 2093056 bytes)
Configuring upload protocol...
AVAILABLE: blackmagic, cmsis-dap, jlink, pico-debug, picoprobe, picotool, raspberrypi-swd
CURRENT: upload_protocol = picotool
Looking for upload port...
Already found 1 device(s) RPxxxx device in BOOTSEL mode, not trying to do 1200bps reset.
Uploading .pio/build/pico/firmware.elf
Loading into Flash: [ ] 0%
Loading into Flash: [= ] 5%
Loading into Flash: [=== ] 10%
Loading into Flash: [==== ] 15%
Loading into Flash: [====== ] 20%
Loading into Flash: [======= ] 26%
Loading into Flash: [========= ] 31%
Loading into Flash: [========== ] 36%
Loading into Flash: [============ ] 41%
Loading into Flash: [============= ] 46%
Loading into Flash: [=============== ] 52%
Loading into Flash: [================= ] 57%
Loading into Flash: [================== ] 62%
Loading into Flash: [==================== ] 67%
Loading into Flash: [===================== ] 72%
Loading into Flash: [======================= ] 78%
Loading into Flash: [======================== ] 83%
Loading into Flash: [========================== ] 88%
Loading into Flash: [=========================== ] 93%
Loading into Flash: [============================= ] 98%
Loading into Flash: [==============================] 100%
Verifying Flash: [ ] 0%
Verifying Flash: [= ] 5%
Verifying Flash: [=== ] 10%
Verifying Flash: [==== ] 15%
Verifying Flash: [====== ] 20%
Verifying Flash: [======= ] 26%
Verifying Flash: [========= ] 31%
Verifying Flash: [========== ] 36%
Verifying Flash: [============ ] 41%
Verifying Flash: [============= ] 46%
Verifying Flash: [=============== ] 52%
Verifying Flash: [================= ] 57%
Verifying Flash: [================== ] 62%
Verifying Flash: [==================== ] 67%
Verifying Flash: [===================== ] 72%
Verifying Flash: [======================= ] 78%
Verifying Flash: [======================== ] 83%
Verifying Flash: [========================== ] 88%
Verifying Flash: [=========================== ] 93%
Verifying Flash: [============================= ] 98%
Verifying Flash: [==============================] 100%
OK
The device was rebooted to start the application.
まとめ
Linux環境でRP2040を使用する際は、udevルールの設定が必須です。
WindowsやMacでは自動的に処理されますが、Linuxでは手動設定が必要になります。
この設定を行うことで、sudoなしでRP2040への書き込みが可能になります。
お疲れ様でした!