Closed7

「基礎から学ぶ 組込みRust」環境構築メモ

nozonozo

まずは書籍の手順に沿って環境構築する。

Build Tools for Visual Studio 2022を以下からダウンロードして、「C++によるデスクトップ開発」をチェックしてインストールする。
https://aka.ms/vs/17/release/vs_BuildTools.exe

rustupをデフォルトでインストールする。
https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe

Powershellでバージョンを確認する。

> rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.57.0 (f1edd0429 2021-11-29)`
> rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
> cargo --version
cargo 1.57.0 (b2e52d7ca 2021-10-21)

クロスビルドツールチェインをインストールする。

> rustup target add thumbv7em-none-eabihf
info: downloading component 'rust-std' for 'thumbv7em-none-eabihf'
info: installing component 'rust-std' for 'thumbv7em-none-eabihf'

ファーム書き込みツールをインストールする。

> cargo install cargo-generate hf2-cli cargo-hf2
...
   Installed package `cargo-generate v0.11.1` (executable `cargo-generate.exe`)
...
   Installed package `hf2-cli v0.3.3` (executable `hf2.exe`)
...
   Installed package `cargo-hf2 v0.3.3` (executable `cargo-hf2.exe`)
     Summary Successfully installed cargo-generate, hf2-cli, cargo-hf2!

wio-terminalをブートローダモードで接続してビルド・書き込みする。
書き込みに成功してLチカする。

> git clone https://github.com/tomoyuki-nakabayashi/wio-terminal-blink-rs.git
> cd wio-terminal-blink-rs
> cargo hf2
...
   Compiling wio-terminal-blink-rs v0.1.0 (C:\Users\user\wio-terminal-blink-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 35.52s
    Searching for a connected device with known vid/pid pair.
    Trying  Ok(Some("Seeed Studio")) Ok(Some("Wio Terminal"))
    Flashing "\\\\?\\C:\\Users\\user\\wio-terminal-blink-rs\\target\\thumbv7em-none-eabihf\\debug\\wio-terminal-blink-rs"
    Finished in 0.824s
nozonozo

WSL2でも環境構築できないか試してみる。

WSLのバージョンは以下の通り。

> wsl --status
既定の配布: docker-desktop
既定のバージョン: 2

Linux 用 Windows サブシステムの最終更新日: 2021/10/23
Windows Subsystem for Linux カーネルは、'wsl --update' を使用して手動で更新できますが、システム設定が 原因で自動更新が発生することはありません。
カーネルの自動更新を受け取るには、 Windows Update の設定を有効にしてください:' Windowsの更新に、その他のMicrosoftの製品の更新情報を受け取る'。
詳細については、 https://aka.ms/wsl2kernel.
 を参照してください
カーネル バージョン: 5.10.16

Ubuntu-20.04をインストールする。

> wsl --install -d Ubuntu-20.04

Ubuntu上でツールをインストールする。(権限関連が面倒なのでrootで)

# apt update && apt upgrade
# apt install gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev
# apt list gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev
Listing... Done
gcc/focal,now 4:9.3.0-1ubuntu2 amd64 [installed]
libsdl2-dev/focal,now 2.0.10+dfsg1-3 amd64 [installed]
libssl-dev/focal-updates,now 1.1.1f-1ubuntu2.10 amd64 [installed]
libusb-1.0-0-dev/focal,now 2:1.0.23-2build1 amd64 [installed]
minicom/focal,now 2.7.1-1.1 amd64 [installed]
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# source $HOME/.cargo/env
# rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.57.0 (f1edd0429 2021-11-29)`
# rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
# cargo --version
cargo 1.57.0 (b2e52d7ca 2021-10-21)
# rustup target add thumbv7em-none-eabihf
info: downloading component 'rust-std' for 'thumbv7em-none-eabihf'
info: installing component 'rust-std' for 'thumbv7em-none-eabihf'
# cargo install cargo-generate hf2-cli cargo-hf2
# cargo install --list
cargo-generate v0.11.1:
    cargo-generate
cargo-hf2 v0.3.3:
    cargo-hf2
hf2-cli v0.3.3:
    hf2

wio-terminalへの書き込みで失敗しているように見える。

# git clone https://github.com/tomoyuki-nakabayashi/wio-terminal-blink-rs.git
# cd wio-terminal-blink-rs
# cargo hf2
...
   Compiling wio-terminal-blink-rs v0.1.0 (/root/wio-terminal-blink-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 19.17s
    Searching for a connected device with known vid/pid pair.
thread 'main' panicked at 'Are you sure device is plugged in and in bootloader mode?', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/cargo-hf2-0.3.3/src/main.rs:98:16
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

wio-terminalを接続した状態でPowershellで以下のコマンドを実行するとCOM4として接続されていることがわかる。

> Get-WMIObject Win32_SerialPort | Select-Object Name,DeviceID,Description

Name                         DeviceID Description
----                         -------- -----------
USB シリアル デバイス (COM4) COM4     USB シリアル デバイス

Ubuntu側で/dev/ttyS4みたいに見えてほしいけど存在しない。
(wio-terminal接続前後で/dev/に変化なし)

# ls /dev/ttyS*
/dev/ttyS0  /dev/ttyS1  /dev/ttyS2  /dev/ttyS3

WSL2ではシリアルUSBデバイスをサポートしていないみたい。

https://docs.microsoft.com/en-us/windows/wsl/faq#can-i-access-the-gpu-in-wsl-2--are-there-plans-to-increase-hardware-support-

As of right now WSL 2 does not include serial support, or USB device support. We are investigating the best way to add these features.

一応、microsoft公式のブログでUSBデバイスを認識させる方法を公開しているようである。

https://devblogs.microsoft.com/commandline/connecting-usb-devices-to-wsl/

カーネルが 5.10.60.1 以降である必要があるみたいなのでWSLをアップデートする。

> wsl --update
更新をチェック中...
更新をダウンロード中...
更新をインストール中...
この変更は、次回の WSL 再起動時に有効になります。強制的に再起動するには、'wsl --shutdown' を実行してください。
カーネル バージョン: 5.10.60.1
> wsl --shutdown

Ubuntu側でも 5.10.60.1 に見えている。

$ uname -r
5.10.60.1-microsoft-standard-WSL2

Windows側でusbipd-winをインストールする。

> winget install --interactive --exact dorssel.usbipd-win
> usbipd -v
usbipd-win
1.3.0+37.Branch.master.Sha.89a4e59f384d08d953380913735f59821e63fa1f

Ubuntu側で以下を実行して、visudoでsecure_pathパスに/usr/lib/linux-tools/5.4.0-77-genericを追加する。

$ sudo apt install linux-tools-5.4.0-77-generic hwdata
$ sudo visudo
$ sudo grep secure_path /etc/sudoers
Defaults        secure_path="/usr/lib/linux-tools/5.4.0-77-generic:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin"

wio-terminalを接続した状態で、Windows側から以下のコマンドを実行するとwio-terminalが見える。

> usbipd wsl list
BUSID  DEVICE                                                        STATE
1-7    USB シリアル デバイス (COM4), USB 大容量記憶装置, USB 入力デバイス, Wio Terminal   Not attached
1-8    G433 Gaming Headset, USB 入力デバイス                               Not attached
1-10   USB 入力デバイス                                                    Not attached
1-14   USB 入力デバイス                                                    Not attached
4-1    USB 入力デバイス                                                    Not attached
5-4    USB 大容量記憶装置                                                   Not attached

次のコマンドを実行すると sudo コマンドがないと言われてしまう。

> usbipd wsl attach --busid 1-7
/bin/sh: sudo: not found
usbipd: error: Failed to attach device with ID '1-7'.

WSLの既定のディストリビューションがUbuntuになっていないかったことが原因っぽい。
修正して再度実行する。

> wsl -l
Linux 用 Windows サブシステム ディストリビューション:
docker-desktop (既定)
Ubuntu-20.04
docker-desktop-data
> wsl --set-default Ubuntu-20.04
> usbipd wsl attach --busid 1-7

Ubuntu側で次のコマンドを実行するとwio-terminalが見えている。

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 002: ID 2886:002d Seeed Studio Wio Terminal
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ ls /dev/ttyACM0
/dev/ttyACM0

この状態でUbuntu側でwio-terminalへの書き込みをすると今度は成功した。

# cd wio-terminal-blink-rs/
# cargo hf2
    Finished dev [unoptimized + debuginfo] target(s) in 0.21s
    Searching for a connected device with known vid/pid pair.
    Trying  Ok(Some("Seeed Studio")) Ok(Some("Wio Terminal"))
    Flashing "/root/wio-terminal-blink-rs/target/thumbv7em-none-eabihf/debug/wio-terminal-blink-rs"
    Finished in 0.797s
nozonozo

ついでにDocker on WSL2からも書き込めるか試してみる。

適当にRust環境のDockerfileを書いてビルドする。

$ docker --version
Docker version 20.10.11, build dea9396
$ cat Dockerfile
FROM rust:1.57

RUN apt-get update && apt-get install -y gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev usbutils
RUN rustup target add thumbv7em-none-eabihf && cargo install cargo-generate hf2-cli cargo-hf2
$ docker build -t my-rust .

WSL上でwio-terminalが見える状態にした上で、docker run を--device=/dev/ttyACM0オプションをつけて実行する。

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 007: ID 2886:002d Seeed Studio Wio Terminal
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
$ ls /dev/ttyACM0
/dev/ttyACM0
$ docker run -it --rm --device=/dev/ttyACM0 my-rust bash

Docker内からwio-terminalが見えていそうではある。

root@f166c07e68d8:~# lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux 5.10.60.1-microsoft-standard-WSL2 vhci_hcd USB/IP Virtual Host Controller
Bus 001 Device 007: ID 2886:002d Seeed Studio Wio Terminal
Bus 001 Device 001: ID 1d6b:0002 Linux 5.10.60.1-microsoft-standard-WSL2 vhci_hcd USB/IP Virtual Host Controller

Lチカサンプルをビルドして書き込んでみるが、エラーがでてしまう。

root@f166c07e68d8:~# git clone https://github.com/tomoyuki-nakabayashi/wio-terminal-blink-rs.git
root@f166c07e68d8:~# cd wio-terminal-blink-rs
root@f166c07e68d8:~/wio-terminal-blink-rs# cargo hf2
...
   Compiling wio-terminal-blink-rs v0.1.0 (/root/wio-terminal-blink-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 19.12s
    Searching for a connected device with known vid/pid pair.
thread 'main' panicked at 'Are you sure device is plugged in and in bootloader mode?', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/cargo-hf2-0.3.3/src/main.rs:98:16
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

一応RUST_BACKTRACE=fullをつけて実行した結果は以下になる。

root@f166c07e68d8:~/wio-terminal-blink-rs# RUST_BACKTRACE=full cargo hf2
    Finished dev [unoptimized + debuginfo] target(s) in 0.02s
    Searching for a connected device with known vid/pid pair.
thread 'main' panicked at 'Are you sure device is plugged in and in bootloader mode?', /usr/local/cargo/registry/src/github.com-1ecc6299db9ec823/cargo-hf2-0.3.3/src/main.rs:98:16
stack backtrace:
   0:     0x563113c842cc - std::backtrace_rs::backtrace::libunwind::trace::hf6a6dfd7da937cb0
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/../../backtrace/src/backtrace/libunwind.rs:90:5
   1:     0x563113c842cc - std::backtrace_rs::backtrace::trace_unsynchronized::hc596a19e4891f7f3
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x563113c842cc - std::sys_common::backtrace::_print_fmt::hb16700db31584325
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x563113c842cc - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h231c4190cfa75162
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:46:22
   4:     0x563113ca8adc - core::fmt::write::h2a1462b5f8eea807
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/fmt/mod.rs:1163:17
   5:     0x563113c7feb5 - std::io::Write::write_fmt::h71ddfebc68685972
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/io/mod.rs:1696:15
   6:     0x563113c85c50 - std::sys_common::backtrace::_print::hcc197d4bebf2b369
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:49:5
   7:     0x563113c85c50 - std::sys_common::backtrace::print::h335a66af06738c7c
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:36:9
   8:     0x563113c85c50 - std::panicking::default_hook::{{closure}}::h6fac9ac9c8b79e52
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:210:50
   9:     0x563113c85805 - std::panicking::default_hook::h341c1030c6a1161b
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:227:9
  10:     0x563113c86304 - std::panicking::rust_panic_with_hook::h50680ff4b44510c6
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:624:17
  11:     0x563113c85de0 - std::panicking::begin_panic_handler::{{closure}}::h9371c0fbb1e8465a
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:521:13
  12:     0x563113c84774 - std::sys_common::backtrace::__rust_end_short_backtrace::h9b3efa22a5768c0f
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/sys_common/backtrace.rs:139:18
  13:     0x563113c85d49 - rust_begin_unwind
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:517:5
  14:     0x563113b27021 - core::panicking::panic_fmt::h23b9203e89cc61cf
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:100:14
  15:     0x563113ca6fa1 - core::panicking::panic_display::h0b2b4cc4e8e79e2e
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/panicking.rs:64:5
  16:     0x563113b26f1b - core::option::expect_failed::h111c62e88059319b
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/option.rs:1638:5
  17:     0x563113b49a95 - cargo_hf2::main::h627a470898be686e
  18:     0x563113b45543 - std::sys_common::backtrace::__rust_begin_short_backtrace::h416d789ed8b472e6  19:     0x563113b4e809 - std::rt::lang_start::{{closure}}::hb0c2b3d3f9ca3409
  20:     0x563113c83fab - core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &F>::call_once::hc56adab7a77ec6e3
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/core/src/ops/function.rs:259:13
  21:     0x563113c83fab - std::panicking::try::do_call::h29f013120c5abc65
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:403:40
  22:     0x563113c83fab - std::panicking::try::h86d5b2b66caec4cf
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:367:19
  23:     0x563113c83fab - std::panic::catch_unwind::h7dd136d787f51397
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panic.rs:133:14
  24:     0x563113c83fab - std::rt::lang_start_internal::{{closure}}::h4a199351e630a8a5
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/rt.rs:128:48
  25:     0x563113c83fab - std::panicking::try::do_call::h20ceb9e5dff838c6
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:403:40
  26:     0x563113c83fab - std::panicking::try::hc2abb46a5e41bd43
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panicking.rs:367:19
  27:     0x563113c83fab - std::panic::catch_unwind::h52c3eb4408ad6dfb
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/panic.rs:133:14
  28:     0x563113c83fab - std::rt::lang_start_internal::hd15a47be08101c28
                               at /rustc/f1edd0429582dd29cccacaf50fd134b05593bd9c/library/std/src/rt.rs:128:20
  29:     0x563113b49f82 - main
  30:     0x7f4c47ac2d0a - __libc_start_main
  31:     0x563113b277fa - _start
  32:                0x0 - <unknown>

問題切り分けのために、書き込みが成功しているWSL上のUbuntuと同じ環境になるようにしたコンテナでも試してみる。

$ docker run -it --rm --device=/dev/ttyACM0 ubuntu bash
root@b60aebd2063c:~# cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
root@b60aebd2063c:~# apt update && apt upgrade
root@b60aebd2063c:~# apt install gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev usbutils
root@b60aebd2063c:~# apt list gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev usbutils
Listing... Done
gcc/focal,now 4:9.3.0-1ubuntu2 amd64 [installed]
libsdl2-dev/focal,now 2.0.10+dfsg1-3 amd64 [installed]
libssl-dev/focal-updates,now 1.1.1f-1ubuntu2.10 amd64 [installed]
libusb-1.0-0-dev/focal,now 2:1.0.23-2build1 amd64 [installed]
minicom/focal,now 2.7.1-1.1 amd64 [installed]
usbutils/focal,now 1:012-2 amd64 [installed]
root@b60aebd2063c:~# apt install curl
root@b60aebd2063c:~# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
root@b60aebd2063c:~# source $HOME/.cargo/env
root@b60aebd2063c:~# rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.57.0 (f1edd0429 2021-11-29)`
root@b60aebd2063c:~# rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
root@b60aebd2063c:~# cargo --version
cargo 1.57.0 (b2e52d7ca 2021-10-21)
root@b60aebd2063c:~# rustup target add thumbv7em-none-eabihf
info: downloading component 'rust-std' for 'thumbv7em-none-eabihf'
info: installing component 'rust-std' for 'thumbv7em-none-eabihf'
root@b60aebd2063c:~# cargo install cargo-generate hf2-cli cargo-hf2
root@b60aebd2063c:~# cargo install --list
cargo-generate v0.11.1:
    cargo-generate
cargo-hf2 v0.3.3:
    cargo-hf2
hf2-cli v0.3.3:
    hf2

ビルド・書き込みでエラーが発生する。
そうなると、dockerの--device=/dev/ttyACM0オプションで何か情報が欠けてpass throughされている?

root@b60aebd2063c:~# lsusb
Bus 002 Device 001: ID 1d6b:0003
Bus 001 Device 008: ID 2886:002d Seeed Studio Wio Terminal
Bus 001 Device 001: ID 1d6b:0002
root@b60aebd2063c:~# apt install git
root@b60aebd2063c:~# git clone https://github.com/tomoyuki-nakabayashi/wio-terminal-blink-rs.git
root@b60aebd2063c:~# cd wio-terminal-blink-rs
root@b60aebd2063c:~/wio-terminal-blink-rs# cargo hf2
...
   Compiling wio-terminal-blink-rs v0.1.0 (/root/wio-terminal-blink-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 20.35s
    Searching for a connected device with known vid/pid pair.
thread 'main' panicked at 'Are you sure device is plugged in and in bootloader mode?', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/cargo-hf2-0.3.3/src/main.rs:98:16
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

成功したWSL2と失敗したコンテナでそれぞれlsusb -v -d 2886:002dを実行した結果の差分は以下の通り。
確かに文字列データが抜けているようにみえる?

$ diff -u lsusb-wsl2.log lsusb-doc-ubuntu.log
--- lsusb-wsl2.log      2021-12-19 20:37:55.306267172 +0900
+++ lsusb-doc-ubuntu.log        2021-12-19 20:33:06.136263582 +0900
@@ -5,9 +5,9 @@
   bLength                18
   bDescriptorType         1
   bcdUSB               2.10
-  bDeviceClass          239 Miscellaneous Device
+  bDeviceClass          239
   bDeviceSubClass         2
-  bDeviceProtocol         1 Interface Association
+  bDeviceProtocol         1
   bMaxPacketSize0        64
   idVendor           0x2886
   idProduct          0x002d
@@ -31,9 +31,9 @@
       bDescriptorType        11
       bFirstInterface         0
       bInterfaceCount         2
-      bFunctionClass          2 Communications
-      bFunctionSubClass       2 Abstract (modem)
-      bFunctionProtocol       1 AT-commands (v.25ter)
+      bFunctionClass          2
+      bFunctionSubClass       2
+      bFunctionProtocol       1
       iFunction               0
     Interface Descriptor:
       bLength                 9
@@ -41,9 +41,9 @@
       bInterfaceNumber        0
       bAlternateSetting       0
       bNumEndpoints           1
-      bInterfaceClass         2 Communications
-      bInterfaceSubClass      2 Abstract (modem)
-      bInterfaceProtocol      1 AT-commands (v.25ter)
+      bInterfaceClass         2
+      bInterfaceSubClass      2
+      bInterfaceProtocol      1
       iInterface              0
       CDC Header:
         bcdCDC               1.10
@@ -75,7 +75,7 @@
       bInterfaceNumber        1
       bAlternateSetting       0
       bNumEndpoints           2
-      bInterfaceClass        10 CDC Data
+      bInterfaceClass        10
       bInterfaceSubClass      0
       bInterfaceProtocol      0
       iInterface              0
@@ -105,9 +105,9 @@
       bInterfaceNumber        2
       bAlternateSetting       0
       bNumEndpoints           2
-      bInterfaceClass         8 Mass Storage
-      bInterfaceSubClass      6 SCSI
-      bInterfaceProtocol     80 Bulk-Only
+      bInterfaceClass         8
+      bInterfaceSubClass      6
+      bInterfaceProtocol     80
       iInterface              0
       Endpoint Descriptor:
         bLength                 7
@@ -135,7 +135,7 @@
       bInterfaceNumber        3
       bAlternateSetting       0
       bNumEndpoints           2
-      bInterfaceClass         3 Human Interface Device
+      bInterfaceClass         3
       bInterfaceSubClass      0
       bInterfaceProtocol      0
       iInterface              0
@@ -175,7 +175,7 @@
       bInterfaceNumber        4
       bAlternateSetting       0
       bNumEndpoints           2
-      bInterfaceClass       255 Vendor Specific Class
+      bInterfaceClass       255
       bInterfaceSubClass     42
       bInterfaceProtocol      1
       iInterface              0
nozonozo

書き込みエラーが発生する箇所を確認する。
以下のようにhf2 infoコマンドでもエラーが発生する。

# lsusb
Bus 002 Device 001: ID 1d6b:0003
Bus 001 Device 002: ID 2886:002d Seeed Studio Wio Terminal
Bus 001 Device 001: ID 1d6b:0002
# hf2 -p 0x002d -v 0x2886 info
thread 'main' panicked at 'Are you sure device is plugged in and in bootloader mode?: HidApiError { message: "hid_error is not implemented yet" }', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/hf2-cli-0.3.3/src/main.rs:17:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

エラーメッセージからapi.openの部分でエラーになっている。

https://github.com/jacobrosenthal/hf2-rs/blob/master/hf2-cli/src/main.rs#L17

    let api = HidApi::new().expect("Couldn't find system usb");

    let d = if let (Some(v), Some(p)) = (args.vid, args.pid) {
        api.open(v, p)
            .expect("Are you sure device is plugged in and in bootloader mode?")
    } else {

api.openの部分はhidapiのクレート?の機能なので、hf2-cliのバグということではなさそう。
デバイスのvidとpidを指定してopenしようとしているところでpanicしているので、やっぱりdockerのpass throughが変なことになっていそう。

https://crates.io/crates/hidapi

nozonozo

実機のLinuxでも確認してみる。

以下のインストーラでUbuntuをインストールする。

https://jp.ubuntu.com/download/thank-you?version=20.04.3&architecture=amd64&platform=desktop

インストールした環境は以下の通り。

$ cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
$ uname -r
5.11.0-43-generic

ツールをインストールする。

# apt update && apt upgrade
# apt install gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev usbutils
# apt list gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev usbutils | grep installed

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

gcc/focal,now 4:9.3.0-1ubuntu2 amd64 [installed]
libsdl2-dev/focal,now 2.0.10+dfsg1-3 amd64 [installed]
libssl-dev/focal-updates,now 1.1.1f-1ubuntu2.10 amd64 [installed]
libusb-1.0-0-dev/focal,now 2:1.0.23-2build1 amd64 [installed]
minicom/focal,now 2.7.1-1.1 amd64 [installed]
usbutils/focal,now 1:012-2 amd64 [installed]
# curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# source $HOME/.cargo/env
# rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.57.0 (f1edd0429 2021-11-29)`
# rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
# cargo --version
cargo 1.57.0 (b2e52d7ca 2021-10-21)
# rustup target add thumbv7em-none-eabihf
info: downloading component 'rust-std' for 'thumbv7em-none-eabihf'
info: installing component 'rust-std' for 'thumbv7em-none-eabihf'
# cargo install cargo-generate hf2-cli cargo-hf2
# cargo install --list
cargo-generate v0.11.1:
    cargo-generate
cargo-hf2 v0.3.3:
    cargo-hf2
hf2-cli v0.3.3:
    hf2

rustサンプルをビルドして書き込む。書き込みに成功する。

# lsusb
Bus 002 Device 003: ID 04f2:b2b9 Chicony Electronics Co., Ltd Lenovo Integrated Camera UVC
Bus 002 Device 004: ID 2886:002d
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
# git clone https://github.com/tomoyuki-nakabayashi/wio-terminal-blink-rs.git
# cd wio-terminal-blink-rs
# cargo hf2
...
   Compiling wio-terminal-blink-rs v0.1.0 (/root/wio-terminal-blink-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 1m 40s
    Searching for a connected device with known vid/pid pair.
    Trying  Ok(Some("Seeed Studio")) Ok(Some("Wio Terminal"))
    Flashing "/root/wio-terminal-blink-rs/target/thumbv7em-none-eabihf/debug/wio-terminal-blink-rs"
    Finished in 0.402s

Dockerでも試してみる。以下を参考にDockerをインストールする。

https://docs.docker.com/engine/install/ubuntu/

# apt list docker-ce docker-ce-cli containerd.io
Listing... Done
containerd.io/focal,now 1.4.12-1 amd64 [installed]
docker-ce-cli/focal,now 5:20.10.12~3-0~ubuntu-focal amd64 [installed]
docker-ce/focal,now 5:20.10.12~3-0~ubuntu-focal amd64 [installed]
# docker --version
Docker version 20.10.12, build e91ed57

Dockerイメージを作成する。

# cat Dockerfile
FROM ubuntu:20.04

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update \
    && apt-get install -y git curl gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev usbutils
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y \
    && . $HOME/.cargo/env \
    && rustup target add thumbv7em-none-eabihf \
    && cargo install cargo-generate hf2-cli cargo-hf2
# docker build -t ubuntu-rust .

イメージからコンテナを作成して環境を確認する。

# docker run -it --rm --device=/dev/ttyACM0 ubuntu-rust bash
# apt list gcc minicom libusb-1.0-0-dev libsdl2-dev libssl-dev usbutils
Listing... Done
gcc/focal,now 4:9.3.0-1ubuntu2 amd64 [installed]
libsdl2-dev/focal,now 2.0.10+dfsg1-3 amd64 [installed]
libssl-dev/focal-updates,now 1.1.1f-1ubuntu2.10 amd64 [installed]
libusb-1.0-0-dev/focal,now 2:1.0.23-2build1 amd64 [installed]
minicom/focal,now 2.7.1-1.1 amd64 [installed]
usbutils/focal,now 1:012-2 amd64 [installed]
root@aa596f64cca7:~# rustup --version
rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.57.0 (f1edd0429 2021-11-29)`
root@aa596f64cca7:~# rustc --version
rustc 1.57.0 (f1edd0429 2021-11-29)
root@aa596f64cca7:~# cargo --version
cargo 1.57.0 (b2e52d7ca 2021-10-21)
root@aa596f64cca7:~# cargo install --list
cargo-generate v0.11.1:
    cargo-generate
cargo-hf2 v0.3.3:
    cargo-hf2
hf2-cli v0.3.3:
    hf2

ビルドして書き込みを実行する。失敗する。
Windows関係なく事象は再現するみたい。

root@aa596f64cca7:~# lsusb
Bus 002 Device 003: ID 04f2:b2b9
Bus 002 Device 006: ID 2886:002d
Bus 002 Device 002: ID 8087:0024
Bus 002 Device 001: ID 1d6b:0002
Bus 001 Device 002: ID 8087:0024
Bus 001 Device 001: ID 1d6b:0002
root@aa596f64cca7:~# git clone https://github.com/tomoyuki-nakabayashi/wio-terminal-blink-rs.git
root@aa596f64cca7:~# cd wio-terminal-blink-rs
root@aa596f64cca7:~/wio-terminal-blink-rs# cargo hf2
...
   Compiling wio-terminal-blink-rs v0.1.0 (/root/wio-terminal-blink-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 1m 44s
    Searching for a connected device with known vid/pid pair.
thread 'main' panicked at 'Are you sure device is plugged in and in bootloader mode?', /root/.cargo/registry/src/github.com-1ecc6299db9ec823/cargo-hf2-0.3.3/src/main.rs:98:16
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
nozonozo

そもそもの話、dockerの--device=オプションに渡すものは/dev/ttyACM0だけでよかったのか。
wio-terminalの接続前後で/dev/配下は以下のように変化していた。/dev/ttyACM0だけでは足りなさそう。

# diff ls-dev-res.detached-wio-terminal ls-dev-res.attached-wio-terminal
20a21
> hidraw0
67a69,70
> sdb
> serial
68a72
> sg1
139a144
> ttyACM0
176a182
> usb

ひとまず--privilegedで権限を渡してビルド・書き込みしてみる。
成功した。別にdockerのバグでもなんでもなかった。

# docker run -it --rm --privileged ubuntu-rust bash
root@4a177a40fdb6:/# lsusb
Bus 002 Device 003: ID 04f2:b2b9
Bus 002 Device 008: ID 2886:002d
Bus 002 Device 002: ID 8087:0024
Bus 002 Device 001: ID 1d6b:0002
Bus 001 Device 002: ID 8087:0024
Bus 001 Device 001: ID 1d6b:0002
root@4a177a40fdb6:/# git clone https://github.com/tomoyuki-nakabayashi/wio-terminal-blink-rs.git
root@4a177a40fdb6:/# cd wio-terminal-blink-rs
root@4a177a40fdb6:/wio-terminal-blink-rs# cargo hf2
...
   Compiling wio-terminal-blink-rs v0.1.0 (/wio-terminal-blink-rs)
    Finished dev [unoptimized + debuginfo] target(s) in 1m 41s
    Searching for a connected device with known vid/pid pair.
    Trying  Ok(Some("Seeed Studio")) Ok(Some("Wio Terminal"))
    Flashing "/wio-terminal-blink-rs/target/thumbv7em-none-eabihf/debug/wio-terminal-blink-rs"
    Finished in 0.405s
このスクラップは2021/12/21にクローズされました