RaspberryPi PICO用のRust環境構築

RaspberryPi PICO用のRust開発環境を構築していきます。

# Rust Toolchainのインストール

Rustを試す用のディレクトリを作って、以下のページに書いている方法でインストールします。

https://rustup.rs

```shell-session:
% mkdir rust
% cd rust
% curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
info: downloading installer

Welcome to Rust!

This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.

Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:

  /Users/nomura/.rustup

This can be modified with the RUSTUP_HOME environment variable.

The Cargo home directory located at:

  /Users/nomura/.cargo

This can be modified with the CARGO_HOME environment variable.

The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:

  /Users/nomura/.cargo/bin

This path will then be added to your PATH environment variable by
modifying the profile files located at:

  /Users/nomura/.profile
  /Users/nomura/.zshenv

You can uninstall at any time with rustup self uninstall and
these changes will be reverted.

Current installation options:


   default host triple: x86_64-apple-darwin
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes

1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

info: profile set to 'default'
info: default host triple is x86_64-apple-darwin
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2021-12-02, rust version 1.57.0 (f1edd0429 2021-11-29)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
 17.9 MiB /  17.9 MiB (100 %)  10.9 MiB/s in  1s ETA:  0s
info: downloading component 'rust-std'
 23.6 MiB /  23.6 MiB (100 %)  10.8 MiB/s in  2s ETA:  0s
info: downloading component 'rustc'
 63.3 MiB /  63.3 MiB (100 %)  10.4 MiB/s in  6s ETA:  0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
 17.9 MiB /  17.9 MiB (100 %)   1.8 MiB/s in  6s ETA:  0s
info: installing component 'rust-std'
 23.6 MiB /  23.6 MiB (100 %)  11.7 MiB/s in  2s ETA:  0s
info: installing component 'rustc'
 63.3 MiB /  63.3 MiB (100 %)  14.8 MiB/s in  4s ETA:  0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-apple-darwin'

  stable-x86_64-apple-darwin installed - rustc 1.57.0 (f1edd0429 2021-11-29)


Rust is installed now. Great!

To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).

To configure your current shell, run:
source $HOME/.cargo/env
% source $HOME/.cargo/env
% 
```

## ツール群のバージョン確認

インストールされたコマンドのバージョン

```shell-session:
% 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)
```
## ターゲット用ツールチェインのインストール

RP2040のアーキテクチャはデュアルARM Cortex-M0+ @ 133 MHzなので、Cortex-M0+がターゲットとなります。

```shell-session:thumbv6m-none-eabi
% rustup target install thumbv6m-none-eabi
info: downloading component 'rust-std' for 'thumbv6m-none-eabi'
info: installing component 'rust-std' for 'thumbv6m-none-eabi'
```

## cargoサブコマンドのインストール

flip-link、elf2uf2-rs、cargo-generate、cargo-binutilsをインストールします。

|コマンド|説明||
|:-:|:-:|:-:|
|flip-link|組み込みプログラムにゼロコストのスタックオーバーフロー保護を追加|https://github.com/knurling-rs/flip-link|
|elf2uf2-rs|elf形式をuf2形式に変換するツール|https://github.com/raspberrypi/pico-sdk/tree/master/tools/elf2uf2|
|cargo-generate|テンプレートを使ってRustの新規プロジェクトを作るツール|https://github.com/cargo-generate/cargo-generate|
|carge-binutils|Rustツールチェインに付属のLLVMツールを呼び出す|https://github.com/rust-embedded/cargo-binutils|



```shell-session:flip-link
% cargo install flip-link           
    Updating crates.io index
  Downloaded flip-link v0.1.5
  Downloaded 1 crate (21.0 KB) in 0.99s
  Installing flip-link v0.1.5
  Downloaded getrandom v0.2.3
  Downloaded log v0.4.14
  Downloaded memchr v2.4.1
  Downloaded env_logger v0.9.0
  Downloaded object v0.26.2
  Downloaded libc v0.2.112
  Downloaded cfg-if v1.0.0
  Downloaded 7 crates (976.8 KB) in 0.78s
   Compiling libc v0.2.112
   Compiling cfg-if v1.0.0
   Compiling log v0.4.14
   Compiling memchr v2.4.1
   Compiling env_logger v0.9.0
   Compiling getrandom v0.2.3
   Compiling object v0.26.2
   Compiling flip-link v0.1.5
    Finished release [optimized] target(s) in 55.54s
  Installing /Users/nomura/.cargo/bin/flip-link
   Installed package `flip-link v0.1.5` (executable `flip-link`)
```

```shell-session:elf2uf2-rs
% cargo install elf2uf2-rs 
    Updating crates.io index
  Downloaded elf2uf2-rs v1.3.6
  Downloaded 1 crate (51.1 KB) in 0.58s
  Installing elf2uf2-rs v1.3.6
  Downloaded lazy_static v1.4.0
  Downloaded byteorder v1.4.3
  Downloaded memoffset v0.6.5
  Downloaded once_cell v1.9.0
  Downloaded quote v1.0.14
  Downloaded proc-macro-error-attr v1.0.4
  Downloaded syn v1.0.85
  Downloaded unicode-xid v0.2.2
  Downloaded version_check v0.9.4
  Downloaded scopeguard v1.1.0
  Downloaded proc-macro-error v1.0.4
  Downloaded strsim v0.10.0
  Downloaded time v0.1.44
  Downloaded textwrap v0.14.2
  Downloaded void v1.0.2
  Downloaded static_assertions v1.1.0
  Downloaded either v1.6.1
  Downloaded crossbeam-deque v0.8.1
  Downloaded crossbeam-channel v0.5.1
  Downloaded core-foundation-sys v0.8.3
  Downloaded clap_derive v3.0.5
  Downloaded cfg-if v0.1.10
  Downloaded crossbeam-utils v0.8.5
  Downloaded crossbeam-epoch v0.9.5
  Downloaded serialport v4.0.1
  Downloaded pbr v1.0.4
  Downloaded termcolor v1.1.2
  Downloaded synstructure v0.12.6
  Downloaded rayon v1.5.1
  Downloaded rayon-core v1.9.1
  Downloaded zerocopy-derive v0.3.1
  Downloaded nix v0.16.1
  Downloaded mach v0.1.2
  Downloaded assert_into v1.1.0
  Downloaded zerocopy v0.6.1
  Downloaded sysinfo v0.20.5
  Downloaded clap v3.0.5
  Downloaded autocfg v1.0.1
  Downloaded hashbrown v0.11.2
  Downloaded os_str_bytes v6.0.0
  Downloaded num_cpus v1.13.1
  Downloaded indexmap v1.8.0
  Downloaded proc-macro2 v1.0.36
  Downloaded heck v0.4.0
  Downloaded mach v0.2.3
  Downloaded IOKit-sys v0.1.5
  Downloaded atty v0.2.14
  Downloaded bitflags v1.3.2
  Downloaded CoreFoundation-sys v0.1.4
  Downloaded 49 crates (2.4 MB) in 1.52s
   Compiling libc v0.2.112
   Compiling autocfg v1.0.1
   Compiling proc-macro2 v1.0.36
   Compiling unicode-xid v0.2.2
   Compiling crossbeam-utils v0.8.5
   Compiling lazy_static v1.4.0
   Compiling cfg-if v1.0.0
   Compiling syn v1.0.85
   Compiling version_check v0.9.4
   Compiling crossbeam-epoch v0.9.5
   Compiling CoreFoundation-sys v0.1.4
   Compiling scopeguard v1.1.0
   Compiling rayon-core v1.9.1
   Compiling memchr v2.4.1
   Compiling IOKit-sys v0.1.5
   Compiling core-foundation-sys v0.8.3
   Compiling bitflags v1.3.2
   Compiling nix v0.16.1
   Compiling sysinfo v0.20.5
   Compiling hashbrown v0.11.2
   Compiling either v1.6.1
   Compiling void v1.0.2
   Compiling cfg-if v0.1.10
   Compiling heck v0.4.0
   Compiling byteorder v1.4.3
   Compiling strsim v0.10.0
   Compiling once_cell v1.9.0
   Compiling termcolor v1.1.2
   Compiling textwrap v0.14.2
   Compiling static_assertions v1.1.0
   Compiling assert_into v1.1.0
   Compiling memoffset v0.6.5
   Compiling indexmap v1.8.0
   Compiling rayon v1.5.1
   Compiling proc-macro-error-attr v1.0.4
   Compiling proc-macro-error v1.0.4
   Compiling crossbeam-channel v0.5.1
   Compiling quote v1.0.14
   Compiling num_cpus v1.13.1
   Compiling mach v0.1.2
   Compiling atty v0.2.14
   Compiling mach v0.2.3
   Compiling time v0.1.44
   Compiling os_str_bytes v6.0.0
   Compiling pbr v1.0.4
   Compiling crossbeam-deque v0.8.1
   Compiling serialport v4.0.1
   Compiling synstructure v0.12.6
   Compiling clap_derive v3.0.5
   Compiling zerocopy-derive v0.3.1
   Compiling zerocopy v0.6.1
   Compiling clap v3.0.5
   Compiling elf2uf2-rs v1.3.6
    Finished release [optimized] target(s) in 36.08s
  Installing /Users/nomura/.cargo/bin/elf2uf2-rs
   Installed package `elf2uf2-rs v1.3.6` (executable `elf2uf2-rs`)
```

```shell-session:cargo-generate
% cargo install cargo-generate
    Updating crates.io index
  Downloaded cargo-generate v0.12.0
  Downloaded 1 crate (74.3 KB) in 1.12s
  Installing cargo-generate v0.12.0
  Downloaded anyhow v1.0.52
  Downloaded aho-corasick v0.7.18
  Downloaded cc v1.0.72
  Downloaded dirs-sys v0.3.6
  Downloaded dirs v4.0.0
  Downloaded dialoguer v0.9.0
  Downloaded console v0.15.0
  Downloaded generic-array v0.12.4
  Downloaded globset v0.4.8
  Downloaded form_urlencoded v1.0.1
  Downloaded heck v0.3.3
  Downloaded git2 v0.13.25
  Downloaded fnv v1.0.7
  Downloaded chrono v0.4.19
  Downloaded jobserver v0.1.24
  Downloaded liquid v0.23.1
  Downloaded matches v0.1.9
  Downloaded liquid-lib v0.23.1
  Downloaded liquid-core v0.23.2
  Downloaded kstring v1.0.6
  Downloaded ignore v0.4.18
  Downloaded opaque-debug v0.2.3
  Downloaded percent-encoding v2.1.0
  Downloaded proc-macro-hack v0.5.19
  Downloaded rand_core v0.6.3
  Downloaded regex v1.5.4
  Downloaded smallvec v1.7.0
  Downloaded remove_dir_all v0.7.0
  Downloaded smartstring v0.2.9
  Downloaded proc-quote v0.4.0
  Downloaded terminal_size v0.1.17
  Downloaded openssl-sys v0.9.72
  Downloaded sha-1 v0.8.2
  Downloaded semver v1.0.4
  Downloaded structopt v0.3.25
  Downloaded rhai_codegen v1.2.0
  Downloaded pest v2.1.3
  Downloaded serde_derive v1.0.133
  Downloaded same-file v1.0.6
  Downloaded remove_dir_all v0.5.3
  Downloaded proc-quote-impl v0.3.2
  Downloaded pkg-config v0.3.24
  Downloaded pest_meta v2.1.3
  Downloaded rand_chacha v0.3.1
  Downloaded rand v0.8.4
  Downloaded pest_generator v2.1.3
  Downloaded ppv-lite86 v0.2.16
  Downloaded path-dedot v3.0.14
  Downloaded path-absolutize v3.0.11
  Downloaded number_prefix v0.4.0
  Downloaded num-traits v0.2.14
  Downloaded indicatif v0.16.2
  Downloaded libssh2-sys v0.2.23
  Downloaded strsim v0.8.0
  Downloaded thread_local v1.1.3
  Downloaded typenum v1.15.0
  Downloaded zeroize v1.4.3
  Downloaded walkdir v2.3.2
  Downloaded vec_map v0.8.2
  Downloaded rhai v1.3.0
  Downloaded regex-syntax v0.6.25
  Downloaded unicode-width v0.1.9
  Downloaded ucd-trie v0.1.3
  Downloaded thiserror-impl v1.0.30
  Downloaded block-buffer v0.7.3
  Downloaded ansi_term v0.12.1
  Downloaded num-integer v0.1.44
  Downloaded url v2.2.2
  Downloaded anymap2 v0.13.0
  Downloaded textwrap v0.11.0
  Downloaded libgit2-sys v0.12.26+1.3.0
  Downloaded libz-sys v1.1.3
  Downloaded thiserror v1.0.30
  Downloaded serde v1.0.133
  Downloaded doc-comment v0.3.3
  Downloaded unicode-normalization v0.1.19
  Downloaded toml v0.5.8
  Downloaded unicode-bidi v0.3.7
  Downloaded tinyvec v1.5.1
  Downloaded tinyvec_macros v0.1.0
  Downloaded structopt-derive v0.4.18
  Downloaded tempfile v3.2.0
  Downloaded pest_derive v2.1.0
  Downloaded fake-simd v0.1.2
  Downloaded clap v2.34.0
  Downloaded block-padding v0.1.5
  Downloaded idna v0.2.3
  Downloaded maplit v1.0.2
  Downloaded liquid-derive v0.23.1
  Downloaded bstr v0.2.17
  Downloaded home v0.5.3
  Downloaded itertools v0.10.3
  Downloaded sanitize-filename v0.3.0
  Downloaded digest v0.8.1
  Downloaded byte-tools v0.3.1
  Downloaded ahash v0.7.6
  Downloaded unicode-segmentation v1.8.0
  Downloaded time v0.1.43
  Downloaded 98 crates (7.7 MB) in 2.89s (largest was `libgit2-sys` at 1.5 MB)
   Compiling libc v0.2.112
   Compiling proc-macro2 v1.0.36
   Compiling unicode-xid v0.2.2
   Compiling syn v1.0.85
   Compiling autocfg v1.0.1
   Compiling memchr v2.4.1
   Compiling pkg-config v0.3.24
   Compiling cfg-if v1.0.0
   Compiling version_check v0.9.4
   Compiling once_cell v1.9.0
   Compiling serde_derive v1.0.133
   Compiling regex-syntax v0.6.25
   Compiling proc-macro-hack v0.5.19
   Compiling serde v1.0.133
   Compiling ucd-trie v0.1.3
   Compiling unicode-width v0.1.9
   Compiling maplit v1.0.2
   Compiling lazy_static v1.4.0
   Compiling log v0.4.14
   Compiling tinyvec_macros v0.1.0
   Compiling percent-encoding v2.1.0
   Compiling either v1.6.1
   Compiling ppv-lite86 v0.2.16
   Compiling matches v0.1.9
   Compiling unicode-segmentation v1.8.0
   Compiling crossbeam-utils v0.8.5
   Compiling anymap2 v0.13.0
   Compiling bitflags v1.3.2
   Compiling unicode-bidi v0.3.7
   Compiling doc-comment v0.3.3
   Compiling same-file v1.0.6
   Compiling ansi_term v0.12.1
   Compiling remove_dir_all v0.5.3
   Compiling fnv v1.0.7
   Compiling anyhow v1.0.52
   Compiling strsim v0.8.0
   Compiling vec_map v0.8.2
   Compiling static_assertions v1.1.0
   Compiling semver v1.0.4
   Compiling zeroize v1.4.3
   Compiling number_prefix v0.4.0
   Compiling smallvec v1.7.0
   Compiling home v0.5.3
   Compiling heck v0.4.0
   Compiling thread_local v1.1.3
   Compiling path-dedot v3.0.14
   Compiling num-traits v0.2.14
   Compiling num-integer v0.1.44
   Compiling proc-macro-error-attr v1.0.4
   Compiling proc-macro-error v1.0.4
   Compiling ahash v0.7.6
   Compiling pest v2.1.3
   Compiling textwrap v0.11.0
   Compiling tinyvec v1.5.1
   Compiling itertools v0.10.3
   Compiling form_urlencoded v1.0.1
   Compiling walkdir v2.3.2
   Compiling heck v0.3.3
   Compiling smartstring v0.2.9
   Compiling path-absolutize v3.0.11
   Compiling pest_meta v2.1.3
   Compiling unicode-normalization v0.1.19
   Compiling aho-corasick v0.7.18
   Compiling bstr v0.2.17
   Compiling getrandom v0.2.3
   Compiling time v0.1.43
   Compiling terminal_size v0.1.17
   Compiling atty v0.2.14
   Compiling dirs-sys v0.3.6
   Compiling remove_dir_all v0.7.0
   Compiling jobserver v0.1.24
   Compiling quote v1.0.14
   Compiling rand_core v0.6.3
   Compiling regex v1.5.4
   Compiling idna v0.2.3
   Compiling clap v2.34.0
   Compiling dirs v4.0.0
   Compiling cc v1.0.72
   Compiling rand_chacha v0.3.1
   Compiling proc-quote-impl v0.3.2
   Compiling url v2.2.2
   Compiling rand v0.8.4
   Compiling console v0.15.0
   Compiling globset v0.4.8
   Compiling sanitize-filename v0.3.0
   Compiling openssl-sys v0.9.72
   Compiling libz-sys v1.1.3
   Compiling libssh2-sys v0.2.23
   Compiling libgit2-sys v0.12.26+1.3.0
   Compiling chrono v0.4.19
   Compiling indicatif v0.16.2
   Compiling ignore v0.4.18
   Compiling tempfile v3.2.0
   Compiling dialoguer v0.9.0
   Compiling pest_generator v2.1.3
   Compiling proc-quote v0.4.0
   Compiling liquid-derive v0.23.1
   Compiling pest_derive v2.1.0
   Compiling rhai_codegen v1.2.0
   Compiling thiserror-impl v1.0.30
   Compiling structopt-derive v0.4.18
   Compiling rhai v1.3.0
   Compiling thiserror v1.0.30
   Compiling structopt v0.3.25
   Compiling kstring v1.0.6
   Compiling toml v0.5.8
   Compiling liquid-core v0.23.2
   Compiling liquid-lib v0.23.1
   Compiling liquid v0.23.1
   Compiling git2 v0.13.25
   Compiling cargo-generate v0.12.0
    Finished release [optimized] target(s) in 3m 46s
  Installing /Users/nomura/.cargo/bin/cargo-generate
   Installed package `cargo-generate v0.12.0` (executable `cargo-generate`)
```

```shell-session:cargo-binutils
% cargo install cargo-binutils
    Updating crates.io index
  Downloaded cargo-binutils v0.3.4
  Downloaded 1 crate (25.0 KB) in 0.85s
  Installing cargo-binutils v0.3.4
  Downloaded itoa v1.0.1
  Downloaded backtrace v0.3.63
  Downloaded semver v0.9.0
  Downloaded rustc-demangle v0.1.21
  Downloaded miniz_oxide v0.4.4
  Downloaded failure v0.1.8
  Downloaded object v0.27.1
  Downloaded ryu v1.0.9
  Downloaded serde_json v1.0.74
  Downloaded semver v0.10.0
  Downloaded semver-parser v0.7.0
  Downloaded failure_derive v0.1.8
  Downloaded gimli v0.26.1
  Downloaded rustc-cfg v0.4.0
  Downloaded rustc_version v0.2.3
  Downloaded cargo_metadata v0.11.4
  Downloaded adler v1.0.2
  Downloaded addr2line v0.17.0
  Downloaded 18 crates (1.4 MB) in 1.18s
   Compiling proc-macro2 v1.0.36
   Compiling unicode-xid v0.2.2
   Compiling syn v1.0.85
   Compiling memchr v2.4.1
   Compiling libc v0.2.112
   Compiling serde_derive v1.0.133
   Compiling serde v1.0.133
   Compiling autocfg v1.0.1
   Compiling cc v1.0.72
   Compiling failure_derive v0.1.8
   Compiling gimli v0.26.1
   Compiling adler v1.0.2
   Compiling semver-parser v0.7.0
   Compiling serde_json v1.0.74
   Compiling cfg-if v1.0.0
   Compiling rustc-demangle v0.1.21
   Compiling ryu v1.0.9
   Compiling itoa v1.0.1
   Compiling unicode-width v0.1.9
   Compiling anyhow v1.0.52
   Compiling strsim v0.8.0
   Compiling bitflags v1.3.2
   Compiling regex-syntax v0.6.25
   Compiling ansi_term v0.12.1
   Compiling vec_map v0.8.2
   Compiling miniz_oxide v0.4.4
   Compiling semver v0.9.0
   Compiling textwrap v0.11.0
   Compiling backtrace v0.3.63
   Compiling rustc_version v0.2.3
   Compiling object v0.27.1
   Compiling aho-corasick v0.7.18
   Compiling addr2line v0.17.0
   Compiling atty v0.2.14
   Compiling quote v1.0.14
   Compiling clap v2.34.0
   Compiling regex v1.5.4
   Compiling synstructure v0.12.6
   Compiling failure v0.1.8
   Compiling rustc-cfg v0.4.0
   Compiling semver v0.10.0
   Compiling toml v0.5.8
   Compiling cargo_metadata v0.11.4
   Compiling cargo-binutils v0.3.4
    Finished release [optimized] target(s) in 1m 06s
  Installing /Users/nomura/.cargo/bin/cargo-cov
  Installing /Users/nomura/.cargo/bin/cargo-nm
  Installing /Users/nomura/.cargo/bin/cargo-objcopy
  Installing /Users/nomura/.cargo/bin/cargo-objdump
  Installing /Users/nomura/.cargo/bin/cargo-profdata
  Installing /Users/nomura/.cargo/bin/cargo-readobj
  Installing /Users/nomura/.cargo/bin/cargo-size
  Installing /Users/nomura/.cargo/bin/cargo-strip
  Installing /Users/nomura/.cargo/bin/rust-ar
  Installing /Users/nomura/.cargo/bin/rust-cov
  Installing /Users/nomura/.cargo/bin/rust-ld
  Installing /Users/nomura/.cargo/bin/rust-lld
  Installing /Users/nomura/.cargo/bin/rust-nm
  Installing /Users/nomura/.cargo/bin/rust-objcopy
  Installing /Users/nomura/.cargo/bin/rust-objdump
  Installing /Users/nomura/.cargo/bin/rust-profdata
  Installing /Users/nomura/.cargo/bin/rust-readobj
  Installing /Users/nomura/.cargo/bin/rust-size
  Installing /Users/nomura/.cargo/bin/rust-strip
   Installed package `cargo-binutils v0.3.4` (executables `cargo-cov`, `cargo-nm`, `cargo-objcopy`, `cargo-objdump`, `cargo-profdata`, `cargo-readobj`, `cargo-size`, `cargo-strip`, `rust-ar`, `rust-cov`, `rust-ld`, `rust-lld`, `rust-nm`, `rust-objcopy`, `rust-objdump`, `rust-profdata`, `rust-readobj`, `rust-size`, `rust-strip`)
```

# Lチカしてみる

## プロジェクトの新規作成

プロジェクトのテンプレートを取得してプロジェクトを新規作成します。

https://github.com/rp-rs/rp2040-project-template

```shell-session:Project新規作成
% cargo generate --git https://github.com/rp-rs/rp2040-project-template --branch main --name led-blink
⚠️   Unable to load config file: /Users/nomura/.cargo/cargo-generate.toml
🔧   Generating template ...
[ 1/16]   Done: .cargo/config.toml
[ 2/16]   Done: .cargo
[ 4/16]   Done: .github/workflows
[ 5/16]   Done: .github
[ 6/16]   Done: .gitignore
[ 7/16]   Done: .vscode/settings.json
[ 8/16]   Done: .vscode
[ 9/16]   Done: CODE_OF_CONDUCT.md
[10/16]   Done: Cargo.toml
[11/16]   Done: README.md
[12/16]   Done: build.rs
[13/16]   Done: debug_probes.md
[14/16]   Done: memory.x
[15/16]   Done: src/main.rs
[16/16]   Done: src
🔧   Moving generated files into: `/Users/nomura/rust/led-blink`...
✨   Done! New project created /Users/nomura/rust/led-blink
% cd led-blink
```

Cargo.tomlの[Package]-[name]を変更してプロジェクトの名前を変更します。

```toml:Cargo.toml
[package]
(省略)
name = "led-blink"
(省略)
```

## ビルド実行

```shell-session:build
 % cargo build
    Updating crates.io index
  Downloaded byteorder v1.3.4
  Downloaded critical-section v0.2.5
  Downloaded debug-helper v0.3.12
  Downloaded crc-any v2.4.1
  Downloaded num v0.3.1
  Downloaded paste v1.0.6
  Downloaded panic-probe v0.3.0
  Downloaded num_enum v0.5.6
  Downloaded num-rational v0.3.2
  Downloaded volatile-register v0.2.1
  Downloaded usbd-hid-macros v0.5.2
  Downloaded rp2040-hal v0.3.0
  Downloaded rp-pico v0.2.0
  Downloaded num-iter v0.1.42
  Downloaded num-complex v0.3.1
  Downloaded nb v1.0.0
  Downloaded usbd-serial v0.1.1
  Downloaded usbd-hid-descriptors v0.1.1
  Downloaded rp2040-boot2 v0.2.0
  Downloaded num_enum_derive v0.5.6
  Downloaded usbd-hid v0.5.2
  Downloaded vcell v0.1.3
  Downloaded defmt-rtt v0.3.1
  Downloaded cortex-m v0.7.4
  Downloaded embedded-time v0.12.1
  Downloaded defmt-parser v0.3.0
  Downloaded defmt v0.3.0
  Downloaded cortex-m-rt-macros v0.7.0
  Downloaded rp2040-pac v0.2.1
  Downloaded ssmarshal v1.0.0
  Downloaded encode_unicode v0.3.6
  Downloaded pio v0.1.0
  Downloaded embedded-hal v0.2.6
  Downloaded nb v0.1.3
  Downloaded cortex-m-rt v0.7.1
  Downloaded defmt-macros v0.3.1
  Downloaded bitfield v0.13.2
  Downloaded bare-metal v1.0.0
  Downloaded bare-metal v0.2.5
  Downloaded usb-device v0.2.8
  Downloaded arrayvec v0.7.2
  Downloaded 41 crates (1.4 MB) in 1.95s
   Compiling proc-macro2 v1.0.36
   Compiling unicode-xid v0.2.2
   Compiling syn v1.0.85
   Compiling autocfg v1.0.1
   Compiling cortex-m v0.7.4
   Compiling semver-parser v0.7.0
   Compiling nb v1.0.0
   Compiling version_check v0.9.4
   Compiling void v1.0.2
   Compiling vcell v0.1.3
   Compiling serde v1.0.133
   Compiling defmt v0.3.0
   Compiling bitfield v0.13.2
   Compiling cortex-m-rt v0.7.1
   Compiling defmt-macros v0.3.1
   Compiling debug-helper v0.3.12
   Compiling byteorder v1.3.4
   Compiling usb-device v0.2.8
   Compiling critical-section v0.2.5
   Compiling rp2040-pac v0.2.1
   Compiling defmt-parser v0.3.0
   Compiling either v1.6.1
   Compiling arrayvec v0.7.2
   Compiling bare-metal v1.0.0
   Compiling encode_unicode v0.3.6
   Compiling cfg-if v1.0.0
   Compiling bitflags v1.3.2
   Compiling paste v1.0.6
   Compiling defmt-rtt v0.3.1
   Compiling rand_core v0.6.3
   Compiling panic-probe v0.3.0
   Compiling led-blink v0.1.0 (/Users/nomura/rust/led-blink)
   Compiling nb v0.1.3
   Compiling volatile-register v0.2.1
   Compiling semver v0.9.0
   Compiling num-traits v0.2.14
   Compiling num-integer v0.1.44
   Compiling num-iter v0.1.42
   Compiling num-rational v0.3.2
   Compiling proc-macro-error-attr v1.0.4
   Compiling proc-macro-error v1.0.4
   Compiling crc-any v2.4.1
   Compiling itertools v0.10.3
   Compiling usbd-hid-descriptors v0.1.1
   Compiling embedded-hal v0.2.6
   Compiling rustc_version v0.2.3
   Compiling usbd-serial v0.1.1
   Compiling rp2040-boot2 v0.2.0
   Compiling bare-metal v0.2.5
   Compiling quote v1.0.14
   Compiling num-complex v0.3.1
   Compiling num v0.3.1
   Compiling embedded-time v0.12.1
   Compiling ssmarshal v1.0.0
   Compiling cortex-m-rt-macros v0.7.0
   Compiling num_enum_derive v0.5.6
   Compiling usbd-hid-macros v0.5.2
   Compiling num_enum v0.5.6
   Compiling usbd-hid v0.5.2
   Compiling pio v0.1.0
   Compiling rp2040-hal v0.3.0
   Compiling rp-pico v0.2.0
    Finished dev [optimized + debuginfo] target(s) in 37.28s
```

ビルドされたファイルの確認

```shell-session:実行ファイルの確認
 % ls -l target/thumbv6m-none-eabi/debug/led-blink
-rwxr-xr-x  2 nomura  staff  1887172  1  8 15:04 target/thumbv6m-none-eabi/debug/led-blink
```

## ビルドされたファイルを実行する

ビルドされたファイルは、elf形式なのでuf2形式にしてRaspberryPi PICOにコピーする必要があります。

まず.cargo/config.tomlで設定されているRunnerを変更します。
Runnerをデフォルトのprobe-runからelf2uf2へ変更します。

```shell-session:
% vi .cargo/config.toml
```

```toml:.cargo/config.toml
(省略)
# runner = "probe-run --chip RP2040"
runner = "elf2uf2-rs -d"
(省略)
```

cargo runコマンドでファイルをRaspberryPi PICOに転送します。
転送が終わるとプログラムが自動的に実行されてLEDが点滅します。

```shell-session:
 % cargo run
    Finished dev [optimized + debuginfo] target(s) in 0.26s
     Running `elf2uf2-rs -d target/thumbv6m-none-eabi/debug/led-blink`
Found pico uf2 disk /Volumes/RPI-RP2
Transfering program to pico
30.00 KB / 30.00 KB [==================================================================================================================================================================] 100.00 % 9.88 MB/s 
```

## Lチカ動作動画

https://youtu.be/272LLhPhir0

# 参考サイト

https://qiita.com/ochaochaocha3/items/1969d76debd6d3b42269

https://rustup.rs

https://github.com/rp-rs

https://github.com/cargo-generate

https://github.com/rust-embedded