###### tags: `raspberrypi` # Raspberry Pi Pico で Baremetal Rust プログラムの実行 こんな記事があった。 Raspberry Pi Pico で Rust のプログラムを動かしている人がいるらしい。 https://www.cnx-software.com/2021/02/05/raspberry-pi-pico-gets-supports-for-rust-rt-thread-os-and-freertos/ https://github.com/rp-rs ## 開発環境 Raspberry Pi 4 (8GB) Ubuntu Server (Ubuntu 20.04.1 LTS) Rust / Cargo 1.49.0 ## rp2040 crate RP2040 マイコンの creat がある🥰。 https://docs.rs/rp2040/0.1.0/rp2040/ ## L チカプログラム 上の記事の L チカプログラムを動作させてみる。 pico-blink-rs https://github.com/rp-rs/pico-blink-rs 0.2.0 を使った。 ### Rust プログラムのビルド メインのプログラムは、https://github.com/rp-rs/pico-blink-rs/blob/develop/src/main.rs にあるように、1 秒ごとの L チカの無限ループになっている。 clone してビルドしてみる。 ```bash=bash $ cargo build --release Updating crates.io index Updating git repository `https://github.com/rp-rs/rp2040-pac` Downloaded crc-any v2.3.5 Downloaded syn v1.0.60 Downloaded rp2040-boot2 v0.1.2 Downloaded nb v1.0.0 Downloaded cortex-m-rt-macros v0.1.8 Downloaded r0 v0.2.2 Downloaded vcell v0.1.3 Downloaded debug-helper v0.3.10 Downloaded cortex-m v0.7.1 Downloaded bitfield v0.13.2 Downloaded bare-metal v0.2.5 Downloaded cortex-m-rt v0.6.13 Downloaded void v1.0.2 Downloaded nb v0.1.3 Downloaded panic-halt v0.2.0 Downloaded embedded-hal v0.2.4 Downloaded volatile-register v0.2.0 Downloaded 17 crates (593.7 KB) in 1.52s Compiling semver-parser v0.7.0 Compiling proc-macro2 v1.0.24 Compiling cortex-m v0.7.1 Compiling unicode-xid v0.2.1 Compiling nb v1.0.0 error[E0463]: can't find crate for `core` | = note: the `thumbv6m-none-eabi` target may not be installed error: aborting due to previous error For more information about this error, try `rustc --explain E0463`. error: could not compile `nb` To learn more, run the command again with --verbose. warning: build failed, waiting for other jobs to finish... error: build failed ``` エラーになったので、`thumbv6m-none-eabi` target をインストールする。 ```bash=bash $ rustup target add thumbv6m-none-eabi info: downloading component 'rust-std' for 'thumbv6m-none-eabi' info: installing component 'rust-std' for 'thumbv6m-none-eabi' info: using up to 500.0 MiB of RAM to unpack components ``` まだエラーになる。 ```bash=bash $ cargo build --release Compiling nb v1.0.0 Compiling void v1.0.2 Compiling vcell v0.1.3 Compiling rp2040-pac v0.1.1 (https://github.com/rp-rs/rp2040-pac?branch=main#ad2bdba6) Compiling bitfield v0.13.2 Compiling pico-blink-rs v0.2.0 (/home/ubuntu/dev/pico/pico-blink-rs) Compiling r0 v0.2.2 Compiling panic-halt v0.2.0 Compiling semver v0.9.0 Compiling proc-macro2 v1.0.24 Compiling cortex-m v0.7.1 Compiling crc-any v2.3.5 Compiling syn v1.0.60 Compiling cortex-m-rt v0.6.13 Compiling volatile-register v0.2.0 Compiling nb v0.1.3 Compiling embedded-hal v0.2.4 Compiling rustc_version v0.2.3 Compiling rp2040-boot2 v0.1.2 Compiling bare-metal v0.2.5 Compiling quote v1.0.8 Compiling cortex-m-rt-macros v0.1.8 error[E0609]: no field `gpio25` on type `PADS_BANK0` --> src/main.rs:126:18 | 126 | p.PADS_BANK0.gpio25.write(|w| { | ^^^^^^ unknown field error[E0609]: no field `gpio25_ctrl` on type `IO_BANK0` --> src/main.rs:133:16 | 133 | p.IO_BANK0.gpio25_ctrl.write(|w| { | ^^^^^^^^^^^ unknown field error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0609`. error: could not compile `pico-blink-rs` To learn more, run the command again with --verbose. ``` DOCS.RS の rp2040-0.1.0 のページには `pads_bank0` の `gpio25` と `io_bank0` の `gpio25_ctrl` がありそうな感じがするので、`Cargo.toml` でバージョンを指定してみる。 https://docs.rs/rp2040/0.1.0/rp2040/pads_bank0/index.html https://docs.rs/rp2040/0.1.0/rp2040/io_bank0/index.html `cargo.toml` ```toml #rp2040-pac = { git = "https://github.com/rp-rs/rp2040-pac", branch="main" } rp2040-pac = "0.1.0" ``` ```bash=bash $ cargo build --release Updating crates.io index Downloaded rp2040-pac v0.1.1 Downloaded 1 crate (471.8 KB) in 1.66s Compiling semver-parser v0.7.0 Compiling proc-macro2 v1.0.24 Compiling unicode-xid v0.2.1 Compiling cortex-m v0.7.1 Compiling debug-helper v0.3.10 Compiling nb v1.0.0 Compiling syn v1.0.60 Compiling void v1.0.2 Compiling vcell v0.1.3 Compiling cortex-m-rt v0.6.13 Compiling bitfield v0.13.2 Compiling rp2040-pac v0.1.1 Compiling r0 v0.2.2 Compiling pico-blink-rs v0.2.0 (/home/ubuntu/dev/pico/pico-blink-rs) Compiling panic-halt v0.2.0 Compiling crc-any v2.3.5 Compiling semver v0.9.0 Compiling nb v0.1.3 Compiling volatile-register v0.2.0 Compiling embedded-hal v0.2.4 Compiling rustc_version v0.2.3 Compiling rp2040-boot2 v0.1.2 Compiling bare-metal v0.2.5 Compiling quote v1.0.8 Compiling cortex-m-rt-macros v0.1.8 Finished release [optimized + debuginfo] target(s) in 1m 40s ``` 無事ビルドが通った。 `target/thumbv6m-none-eabi/release/pico-blink-rs` がプログラム本体。 ```bash $ file target/thumbv6m-none-eabi/release/pico-blink-rs target/thumbv6m-none-eabi/release/pico-blink-rs: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), statically linked, with debug_info, not stripped ``` ### ELF ファイルを UF2 ファイルに変換 `pico-blink-rs` を `elf2uf2` で UF2 ファイルに変換する。 まず `elf2uf2` を作成する。こちらのページを参考にした。 https://beta-notes.way-nifty.com/blog/2021/02/post-2fff25.html ```bash=bash $ cd pico-sdk/tools/el2uf2 mkdir build cd build export PICO_SDK_PATH=../../../../pico-sdk cmake .. make sudo cp elf2uf2 /usr/local/bin/ ``` `elf2uf2` を使って ELF を uf2 に変換する。 ```bash=bash $ elf2uf2 target/thumbv6m-none-eabi/release/pico-blink-rs target/thumbv6m-none-eabi/release/pico-blink-rs.uf2 ``` `picotool info` でファイルが生成されていることを確認する。 ```bash=bash $ picotool info target/thumbv6m-none-eabi/release/pico-blink-rs.uf2 File target/thumbv6m-none-eabi/release/pico-blink-rs.uf2: Program Information none ``` ### 作成したプログラムのコピー Pico に `pico-blink-rs.uf2` をコピーすると Rust での L チカが確認できる。 ```bash=bash $ dmesg | tail [22275.279236] sda: sda1 [22275.285711] sd 0:0:0:0: [sda] Attached SCSI removable disk $ sudo mount /dev/sda1 /mnt/pico $ sudo cp pico-blink-rs.uf2 /mnt/pico $ sudo sync $ sudo umount /mnt/pico ``` ## rp2040 の crates https://crates.io/search?q=rp2040 rp-pico の方がダウンロード数が多いかも。