この記事はmicro:bit Advent Calendar 2023 Advent Calendar 2023の10日目です。

概要

micro:bit v2でRust Discoveryの写経をしました。感想などの共有です。

Rust Discovery

 

環境構築

開発環境構築はこちらに記載されています。

Setting up a development environment

 

私は次の環境で写経、動作確認しました。

    • MacBook 2017 macOS Ventura バージョン13.6

 

    micro:bit v2

写経したところ

Rust Discoveryは所定の章でコードが書かれています。
今のところ、私が写経、動作確認したのはつぎになります。

章確認5. LED roulette✅7. UART✅8. I2C✅9. LED compass
10. Punch-o-meter

写経の方針

写経の方針はつぎのようにしました。

ターゲット

Rust Discoveryはmicro:bit v2かmicro:bit v1で確認できます。
私はmicro:bit v2を持っていたのでmicro:bit v2で進めました。
micro:bit v2かmicro:bit v1でビルドの際に手順が少し違うようです。

Embed.tomlの編集

Embed.tomlのchipをmicro:bitのハードウェアバージョンにより選択します。
私の確認環境はmicro:bit v2のため、つぎの設定としました。

[default.general]
chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2
# chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1

[default.reset]
halt_afterwards = true

[default.rtt]
enabled = false

[default.gdb]
enabled = true

参照先:

 

標準ライブラリのコンパイル済みバージョン(実際には縮小版)のダウンロード

ビルドの前に標準ライブラリのコンパイル済みバージョン(実際には縮小版)のダウンロードが必要とのことです。
ダウンロードする際のコマンドがmicro:bitのハードウェアバージョンにより異なります。

# For micro:bit v2
$ rustup target add thumbv7em-none-eabihf

# For micro:bit v1
$ rustup target add thumbv6m-none-eabi

参照先:

 

ビルド

ビルドのコマンドがmicro:bitのハードウェアバージョンにより異なります。

# For micro:bit v2
$ cargo build --features v2 --target thumbv7em-none-eabihf

# For micro:bit v1
$ cargo build --features v1 --target thumbv6m-none-eabi

参照先:

 

フラッシュ

フラッシュのコマンドがmicro:bitのハードウェアバージョンにより異なります。

# For micro:bit v2
$ cargo embed --features v2 --target thumbv7em-none-eabihf

# For micro:bit v1
$ cargo embed --features v1 --target thumbv6m-none-eabi

参照先:

 

ディレクトリ構成

写経のやり方は好み、いろいろやり方ははあると思いますが今回私は下図のようにRust Discoveryの各章で書かれているコード毎にディレクトリを作成し写経するようにしました。

src_tree.png

Rust Discovery micro:bitのGitHubリポジトリはこちらにあります。

 

srcディレクトリ以下に各章のRust Discoveryの各章のコードが格納されています。
写経する際はこちらのディレクトリを自分の作業用ディレクトリにコピーし、Rust Discoveryの各章で書かれているコードを写経していきました。

こちらは参考までに私の写経用のGitHubリポジトリです。

 

感想

Rust Discoveryの記載のとおりに環境構築、動作確認ができました。
もしmicro:bitをお持ちの方でRustに興味のある方は写経してみては如何でしょうか。