Blinky with Rust on ESP32-C3

dev board ESP32-C3-DevKitM-1
chip ESP32-C3-MINI-1-N4
features rust blinky esp32c3

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Buy the components

Code

Download code src/main.rs
use std::thread;
use std::time::Duration;
use esp_idf_sys as _;
use embedded_hal::digital::blocking::OutputPin;
use esp_idf_hal::peripherals::Peripherals;

fn main() {
  esp_idf_sys::link_patches();

  let peripherals = Peripherals::take().unwrap();
  let mut led = peripherals.pins.gpio3.into_output().unwrap();
  let n = 1;

  while n == 1 {
    led.set_high().unwrap();
    thread::sleep(Duration::from_millis(1000));

    led.set_low().unwrap();
    thread::sleep(Duration::from_millis(1000));

    println!("blink");
  }
}

Makefile

PORT?=/dev/cu.SLAB_USBtoUART*
PROJECT_NAME=blinky-rust-esp32c3

.PHONY: default compile upload log clean

default: clean compile upload

help: ## Show help message
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m\033[0m\n"} /^[$$()% 0-9a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

compile: ## Compile rust
	cargo clean
	cargo build

upload: ## Upload the firmware to the board
	espflash $(PORT) target/riscv32imc-esp-espidf/debug/$(PROJECT_NAME)

log: ## Start the logs
	espmonitor $(PORT)

clean: ## Remove all build files
	cargo clean

Prototype

A photo of the actual setup.

Blinky with Rust on ESP32-C3 prototype

Schematic

Wire up the hardware accordingly

Blinky with Rust on ESP32-C3 schematic

Serial console

Serial output from the firmware.

Blinky with Rust on ESP32-C3 serial console

Description

Install dependancies

  1. Install for RISC-V
     rustup target add riscv32imc-unknown-none-elf
    
  2. Install Cargo sub-commands
     cargo install cargo-generate
     cargo install ldproxy
     cargo install espup
     cargo install espflash
    
     cargo install --list
    
  3. Generate the project
     cargo generate --vcs none --git https://github.com/esp-rs/esp-idf-template cargo
    

    Typical output:

     🤷   Project Name: blinky-rust-esp32c3
     🔧   Destination: /Users/sayanee/Desktop/blinky-rust-esp32c3 ...
     🔧   project-name: blinky-rust-esp32c3 ...
     🔧   Generating template ...
     ✔ 🤷   STD support · true
     ✔ 🤷   MCU · esp32c3
     ✔ 🤷   ESP-IDF native build version (v4.3.2 = previous stable, v4.4 = stable, mainline = UNSTABLE) · v4.4
     ? 🤷   Configure project to use Dev Containers (VS Code, GitHub Codespaces and Gitpod)?
     ✔ 🤷   Configure project to use Dev Containers (VS Code, GitHub Codespaces and Gitpod)?
     🔧   Moving generated files into: `/Users/sayanee/Desktop/blinky-rust-esp32c3`...
     ✨   Done! New project created /Users/sayanee/Desktop/blinky-rust-esp32c3
    

Compile

  1. Change directory into the project name
     $ cd blinky-rust-esp32c3
    
  2. Edit file src/main.rs with the blinky code.
  3. Edit Cargo.toml to add dependencies
      [dependencies]
      embedded-hal = "1.0.0-alpha.8"
      esp-idf-sys = { version = "0.31.11", features = ["binstart"] }
      esp-idf-hal = "0.38.1"
    
  4. Compile / build the project
      $ cargo build
    

Upload the firmware

  1. Plug in the board into the USB port.
  2. Check the port address

     $ ls /dev/cu.*
     /dev/cu.SLAB_USBtoUART  /dev/cu.usbserial-1410
    
  3. Upload / Flash
      espflash /dev/cu.SLAB_USBtoUART target/riscv32imc-esp-espidf/debug/blinky-rust-esp32c3
    

View the logs

Start the serial monitor

  espmonitor /dev/cu.SLAB_USBtoUART

References