Tech Blog

組込み Rust を RISC-V で試してみる

Cover Image for 組込み Rust を RISC-V で試してみる

RISC-V マイコンボード M5Stack Stamp-C3U が余っていたので、 Embedded Rust Trainings for Espressif と Lチカ をサクッと試してみる

今回の環境

TL;DR

1.環境設定

gh repo clone esp-rs/std-training
(out)Cloning into 'std-training'...
(out)remote: Enumerating objects: 4636, done.
(out)remote: Counting objects: 100% (893/893), done.
(out)remote: Compressing objects: 100% (124/124), done.
(out)remote: Total 4636 (delta 797), reused 769 (delta 769), pack-reused 3743 (from 2)
(out)Receiving objects: 100% (4636/4636), 1.10 MiB | 1.33 MiB/s, done.
(out)Resolving deltas: 100% (2864/2864), done.
cd std-training
devcontainer open .

2.Hello, Board!

Enter the hardware-check directory

cd intro/hardware-check

Config file

cp cfg.toml.example cfg.toml
  // intro/hardware-check/cfg.toml
  [hardware-check]
- wifi_ssid = "FBI Surveillance Van"
- wifi_psk = "hunter2"
+ wifi_ssid = "Your Wifi name"
+ wifi_psk = "Your Wifi password"

Build

cargo build
   Compiling compiler_builtins v0.1.140
   Compiling core v0.0.0 (/home/esp/.rustup/toolchains/nightly-2025-01-01-aarch64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
   Compiling proc-macro2 v1.0.93
...

   Compiling toml-cfg v0.1.3
   Compiling hardware-check v0.1.0 (/workspace/intro/hardware-check)
    Finished `dev` profile [optimized + debuginfo] target(s) in 1m 52s

Connect

web-flash --chip esp32c3 target/riscv32imc-esp-espidf/debug/hardware-check

ESP Web Flasher - Connect

Flash

ESP Web Flasher - Device Dashboard

ESP Web Flasher - Install ESP Application

Monitor

ESP Web Flasher - Device Dashboard

ESP Web Flasher - Logs

3.Blink

Arduino / Built-in Examples / Blink を参考にする

Create new project

mkdir -p ../../examples
cargo generate esp-rs/esp-idf-template cargo --destination ../../examples
⚠️   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: blink
🔧   Destination: examples/blink ...
🔧   project-name: blink ...
🔧   Generating template ...
✔ 🤷   Which MCU to target? · esp32c3
✔ 🤷   Configure advanced template options? · true
✔ 🤷   ESP-IDF version (master = UNSTABLE) · v5.3
✔ 🤷   Configure project to use Dev Containers (VS Code and GitHub Codespaces)? · false
✔ 🤷   Configure project to support Wokwi simulation with Wokwi VS Code extension? · true
✔ 🤷   Add CI files for GitHub Action? · false
🔧   Moving generated files into: `examples/blink`...
🔧   Initializing a fresh Git repository
✨   Done! New project created examples/blink
cd ../../examples/blink

Dependencies

cargo add anyhow@1.0.95 esp-idf-svc@0.50.1 
    Updating crates.io index
      Adding anyhow v1.0.95 to dependencies
             Features:
             + std
             - backtrace
      Adding esp-idf-svc v0.50.1 to dependencies
             Features:
             + alloc
             + binstart
             + critical-section
             + embassy-sync
             + embassy-time-driver
             + futures-io
             + std
             - alloc_handler
             - embedded-storage
             - experimental
             - libstart
             - native
             - nightly
             - panic_handler
             - pio
             - wake-from-isr
    Updating crates.io index
     Locking 168 packages to latest compatible versions
      Adding esp-idf-svc v0.50.1 (available: v0.51.0)
cargo add rgb-led --path ../../common/lib/rgb-led
      Adding rgb-led (local) to dependencies
    Updating crates.io index
     Locking 5 packages to latest compatible versions
      Adding bytemuck v1.21.0
 Downgrading esp-idf-hal v0.45.2 -> v0.45.0 (available: v0.45.2)
 Downgrading log v0.4.25 -> v0.4.22 (available: v0.4.25)
      Adding rgb v0.8.50
      Adding rgb-led v0.1.0 (/workspace/common/lib/rgb-led)

Coding

  // examples/blink/src/main.rs
+ use anyhow::{Result};
+ use esp_idf_svc::hal::prelude::Peripherals;
+ use rgb_led::{RGB8, WS2812RMT};
+
- fn main() {
+ fn main() -> Result<()> {
      // It is necessary to call this function once. Otherwise some patches to the runtime
      // implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
      esp_idf_svc::sys::link_patches();
+     let peripherals = Peripherals::take().unwrap();

-     // Bind the log crate to the ESP Logging facilities
-     esp_idf_svc::log::EspLogger::initialize_default();
+     // Onboard RGB LED pin
+     // Rust ESP Board gpio2,  ESP32-C3-DevKitC-02 gpio8
+     let mut led = WS2812RMT::new(peripherals.pins.gpio2, peripherals.rmt.channel0)?;

-     log::info!("Hello, world!");
+     // the loop runs over and over again forever
+     loop {
+         // set the LED to Red
+         led.set_pixel(RGB8::new(50, 0, 0))?;
+         // wait for a second
+         std::thread::sleep(std::time::Duration::from_millis(1000));
+         // turn the LED off
+         led.set_pixel(RGB8::new(0, 0, 0))?;
+         // wait for a second
+         std::thread::sleep(std::time::Duration::from_millis(1000));
+     }
  }

Build

cargo build
  Downloaded document-features v0.2.10
  Downloaded litrs v0.4.1
  Downloaded embassy-time-driver v0.1.0
...

   Compiling rgb-led v0.1.0 (/workspace/common/lib/rgb-led)
   Compiling blink v0.1.0 (/workspace/examples/blink)
    Finished `dev` profile [optimized + debuginfo] target(s) in 42.35s

Flash

web-flash --chip esp32c3 target/riscv32imc-esp-espidf/debug/blink

ESP Web Flasher - Device Dashboard

ESP Web Flasher - Install ESP Application

Blinky

Blinky

参考にしたページ