Blinky with Rust on ESP32-C3

Updated on 19 October 2023
dev board ESP32-C3-DevKitM-1
chip ESP32-C3-MINI-1-N4
features rust blinky esp32c3 riscv
This tutorial is more than 1 year old. If the steps below do not work, then please check the latest versions and the documentations of the individual tools used.

Before starting

Dependancies

Ensure the following dependancies are downloaded and available:

Buy the components

Code

Download code src/main.rs
use esp_idf_hal::{delay::FreeRtos, gpio::PinDriver, peripherals::Peripherals};
use esp_idf_sys as _;

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

    let peripherals = Peripherals::take().unwrap();
    let mut led_pin = PinDriver::output(peripherals.pins.gpio3).unwrap();

    loop {
        led_pin.set_low().unwrap();
        FreeRtos::delay_ms(1000);

        led_pin.set_high().unwrap();
        FreeRtos::delay_ms(1000);
        println!("blink");
    }
}

Makefile

PORT?=/dev/cu.usbserial-*
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 flash target/riscv32imc-esp-espidf/debug/$(PROJECT_NAME)

log: ## Start the logs
	cargo espmonitor $(PORT) --chip esp32c3

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 cargo-espmonitor
    
     cargo install --list
    
  3. Generate the project
     cargo generate esp-rs/esp-idf-template cargo
    

    With options:

    • project-name: blinky-rust-esp32c3
    • MCU target: esp32c3
    • Configure advanced template options: false

    Typical output:

     $  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: blinky-rust-esp32c3
     🔧   Destination: /Users/sayanee/Desktop/blinky-rust-esp32c3 ...
     🔧   project-name: blinky-rust-esp32c3 ...
     🔧   Generating template ...
     ✔ 🤷   Which MCU to target? · esp32c3
     ✔ 🤷   Configure advanced template options? · false
     🔧   Moving generated files into: `/Users/sayanee/Desktop/blinky-rust-esp32c3`...
     🔧   Initializing a fresh Git repository
     ✨   Done! New project created /Users/sayanee/Desktop/blinky-rust-esp32c3
    

Compile

  1. Change directory into the project name
      $ cd blinky
    
  2. Edit file src/main.rs with the blinky code.
  3. Edit Cargo.toml to add dependencies
[package]
name = "blinky-rust-esp32c3"
version = "0.1.0"
authors = ["Sayanee <[email protected]>"]
edition = "2021"
resolver = "2"
rust-version = "1.66"

[profile.release]
opt-level = "s"

[profile.dev]
debug = true
opt-level = "z"

[features]
pio = ["esp-idf-sys/pio"]

[dependencies]
esp-idf-sys = { version = "0.33.2", features = ["binstart"] }
esp-idf-hal = "0.42"

[build-dependencies]
embuild = "0.31.3"
  1. 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.usbserial-1410
    
  3. Upload / Flash
      espflash flash target/riscv32imc-esp-espidf/debug/blinky
    

View the logs

Start the serial monitor

  cargo espmonitor /dev/cu.usbserial-1410 --chip esp32c3

References

Watch