RustでESP32組み込み開発のための環境構築

2024年7月5日
しろくま
RustでESP32組み込み開発のための環境構築

現在、以下の記事のデバイスの開発を行なっている。

これまで、ESP32のファームウェア開発はC++とPlatformIOを使っていたが、 今回はRustでESP32プログラムを書こうと思い、RustでESP32プログラムを書くための環境構築を行った。

Rust環境の構築

以下のサイトを参考にしながら進めた。

Rustup

まず、Rustの環境構築から始めた。rustupを使ってRustの環境構築を行うことにした。

以下のコマンドでrustupのインストールを行った。

$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

RustRover

JetBrains系のAll Products Packユーザーなので、今回はRust用IDEのRustRoverを利用することにした。

espup

ESP関係の開発を進めるために、espupを利用する。

以下のコマンドでインストールした。

$ cargo install espup
$ espup install

ホームディレクトリにexport-esp.shファイルが置かれるので、.zshrcに以下を追記した。

source $HOME/export-esp.sh

プロジェクトの作成

  1. esp-template: no_std向け、ベアメタル環境
  2. esp-idf-template: std向け、ESP-IDFを使用する場合、利用できる

今回は、2のESP-IDFプロジェクトを生成したかったので、 esp-idf-templateのテンプレートを利用する。

まず、cargo-generateをインストールした。

cargo install cargo-generate

GitHubリポジトリを作成し、そのディレクトリに移動。 以下のコマンドで、esp-idf-templateからテンプレートを作成した。

cargo generate esp-rs/esp-idf-template cargo

実行結果:

$ cargo generate esp-rs/esp-idf-template cargo
⚠️   Favorite `esp-rs/esp-idf-template` not found in config, using it as a git repository: <https://github.com/esp-rs/esp-idf-template.git>
🤷   Project Name: magical_painting
🔧   Destination: /Users/junichi/ghq/github.com/sat0b/magical_painting/magical_painting ...
🔧   project-name: magical_painting ...
🔧   Generating template ...
✔ 🤷   Which MCU to target? · esp32
✔ 🤷   Configure advanced template options? · false
🔧   Moving generated files into: `/Users/junichi/ghq/github.com/sat0b/magical_painting/magical_painting`...
🔧   Initializing a fresh Git repository
✨   Done! New project created /Users/junichi/ghq/github.com/sat0b/magical_painting/magical_painting

対象のリポジトリ(magical_painting)の下に、magical_paintingができてしまったので、手動で全て一つ上の階層に移動した。

起動時のエラー

まず、以下のコマンドで実行したところ、以下のエラーが発生した。

$ cargo run
   Compiling esp-idf-sys v0.35.0
   Compiling esp-idf-hal v0.44.0
   Compiling esp-idf-svc v0.49.0
   Compiling magical_painting v0.1.0 (/Users/junichi/ghq/github.com/sat0b/magical_painting)
error: linker `ldproxy` not found
  |
  = note: No such file or directory (os error 2)

error: could not compile `magical_painting` (bin "magical_painting") due to 1 previous error

ldproxyが見つからないエラーが出たので、ldproxyをインストールした。

$ cargo install ldproxy

再度、実行したところ、今度は以下のエラーが発生した。

$ cargo run
   Compiling magical_painting v0.1.0 (/Users/junichi/ghq/github.com/sat0b/magical_painting)
    Finished dev [optimized + debuginfo] target(s) in 2.70s
     Running `espflash flash --monitor target/xtensa-esp32-espidf/debug/magical_painting`
error: could not execute process `espflash flash --monitor target/xtensa-esp32-espidf/debug/magical_painting` (never executed)

Caused by:
  No such file or directory (os error 2)

espflashが実行できないと表示されているので、espflashをインストールした。

$ cargo install espflash

動作確認

再度実行したところ、以下のような出力が得られ、正常に動作することを確認できた。

$ cargo run
    Finished dev [optimized + debuginfo] target(s) in 0.33s
     Running `espflash flash --monitor target/xtensa-esp32-espidf/debug/magical_painting`
[2024-07-04T12:06:56Z INFO ] Detected 2 serial ports
[2024-07-04T12:06:56Z INFO ] Ports which match a known common dev board are highlighted
[2024-07-04T12:06:56Z INFO ] Please select a port
[2024-07-04T12:06:58Z INFO ] Serial port: '/dev/tty.usbserial-1130'
[2024-07-04T12:06:58Z INFO ] Connecting...
[2024-07-04T12:06:58Z INFO ] Using flash stub

(中略)

I (450) main_task: Started on CPU0
I (460) main_task: Calling app_main()
I (460) magical_painting: Hello, world!
I (460) main_task: Returned from app_main()

次の記事はこちら。


前の記事
前の記事はありません。
次の記事
次の記事はありません。