組み込み Rust を始める(probe-rs と最小プロジェクト)
このページは、組み込み Rust を「実機で動かす」ところまでを最短距離で案内する入門ガイドです。 主に Cortex-M 系 MCU(STM32 / nRF52 / RP2040 など)を想定しますが、考え方は他のターゲットでも役立ちます。
ゴール(このページでできるようになること)
Section titled “ゴール(このページでできるようになること)”- Rust のクロスコンパイル用ターゲットを追加できる
cargo generateでテンプレからプロジェクトを作れるprobe-rsでフラッシュ書き込み・実行できるdefmt+ RTT によるログ出力の基本が分かる- よくある詰まりポイントを切り分けられる
前提(ハードウェア・環境)
Section titled “前提(ハードウェア・環境)”推奨ハードウェア
Section titled “推奨ハードウェア”- デバッグプローブ(例: ST-Link / J-Link / CMSIS-DAP など)
- 対応ボード(例: Nucleo, Discovery, nRF DK 等)
以降の手順は「プローブで書き込み・デバッグできる」構成を前提にします。 USB シリアル(UART)だけでも開発は可能ですが、最初はプローブがある方が圧倒的に楽です。
1. 必要ツールのインストール
Section titled “1. 必要ツールのインストール”1.1 Rust(rustup)
Section titled “1.1 Rust(rustup)”Rust を未導入なら rustup を使うのが標準です。
導入確認:
rustc -Vcargo -V1.2 クロスコンパイル用ターゲット
Section titled “1.2 クロスコンパイル用ターゲット”Cortex-M4F/M7F などでよく使う例(FPU あり):
rustup target add thumbv7em-none-eabihfどれを選ぶかは MCU のコア種別 + FPU の有無 で判断します。
- Cortex-M0/M0+:
thumbv6m-none-eabi - Cortex-M3:
thumbv7m-none-eabi - Cortex-M4/M7(FPUなし):
thumbv7em-none-eabi - Cortex-M4F/M7F(FPUあり):
thumbv7em-none-eabihf
1.3 probe-rs(フラッシュ・デバッグ)
Section titled “1.3 probe-rs(フラッシュ・デバッグ)”cargo install probe-rs-tools確認:
probe-rs --versionprobe-rs list-probes1.4 cargo-generate(テンプレから雛形作成)
Section titled “1.4 cargo-generate(テンプレから雛形作成)”cargo install cargo-generate2. プロジェクトを作る
Section titled “2. プロジェクトを作る”ゼロから memory.x やリンカ設定を書くのは、最初はつらいです。
テンプレを使って「動く構成」を作るのがおすすめです。
cargo generate --git https://github.com/rust-embedded/cortex-m-quickstart3. 最小構成の理解
Section titled “3. 最小構成の理解”3.1 Cargo.toml(依存関係)
Section titled “3.1 Cargo.toml(依存関係)”[package]name = "blinky"version = "0.1.0"edition = "2021"
[dependencies]cortex-m = "0.7"cortex-m-rt = "0.7"defmt = "1"defmt-rtt = "1"panic-probe = { version = "1", features = ["print-defmt"] }3.2 src/main.rs(最小プログラム)
Section titled “3.2 src/main.rs(最小プログラム)”#![no_std]#![no_main]
use cortex_m_rt::entry;
use defmt_rtt as _;use panic_probe as _;
#[entry]fn main() -> ! { defmt::info!("Hello, embedded Rust!");
loop { // メイン処理をここに書く }}重要な3点:
#![no_std]: 標準ライブラリなしで動かす#![no_main]: OS 前提のfn main()を使わない#[entry] fn main() -> !: 終了しないエントリポイント
3.3 memory.x(メモリマップ)
Section titled “3.3 memory.x(メモリマップ)”メーカーのハードウェアマニュアルを読み MCU に合わせて必ず確認してください。
MEMORY{ FLASH : ORIGIN = 0x08000000, LENGTH = 512K RAM : ORIGIN = 0x20000000, LENGTH = 128K}3.4 .cargo/config.toml
Section titled “3.4 .cargo/config.toml”[build]target = "thumbv7em-none-eabihf"
[target.thumbv7em-none-eabihf]runner = "probe-rs run --chip STM32F407VGTx"rustflags = [ "-C", "link-arg=-Tlink.x",]4. ビルドする
Section titled “4. ビルドする”cargo buildリリースビルド:
cargo build --release5. 書き込み・実行
Section titled “5. 書き込み・実行”チップ名を調べる
Section titled “チップ名を調べる”probe-rs chip listcargo run --release6. よくあるトラブルシューティング
Section titled “6. よくあるトラブルシューティング”probe-rs list-probes で何も出ない
Section titled “probe-rs list-probes で何も出ない”- プローブが接続されているか確認
- USB ケーブルが充電専用でないか確認
- Linux の場合、udev ルールの導入を検討
error: linking with … failed
Section titled “error: linking with … failed”memory.xが存在しパスが正しいか確認- ターゲットとリンカ設定の整合を確認
can’t find crate for core
Section titled “can’t find crate for core”rustup target add thumbv7em-none-eabihf実行するとすぐ HardFault
Section titled “実行するとすぐ HardFault”memory.xの FLASH/RAM サイズ・アドレスをデータシートと照合- HAL の初期化が正しいか確認
defmt のログが出ない
Section titled “defmt のログが出ない”defmt_rtt as _;があるか確認defmt::info!("boot");を main の最初に置く
7. 次の一歩
Section titled “7. 次の一歩”- GPIO 出力(LED 点滅)
- タイマ割り込み(一定周期処理)
- UART / I2C / SPI(センサ読み取り)
- 低消費電力、ウォッチドッグ
- ログ設計(defmt のレベル、panic の扱い)
「あなたのボード名 + HAL crate + blinky」でサンプルを探し、最小構成に HAL を足す形で進めるのがおすすめです。