Hello world ESP32-S3 with Rust

Updated on 6 January 2023
dev board ESP32-S3-DevKitC-1
chip ESP32-S3-WROOM-1-N8R2
features rust serial esp32s3
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 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.gpio5.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

UPLOAD_PORT?=/dev/cu.usbmodem14*
PROJECT_NAME=blinky-rust-esp32s3

.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 $(UPLOAD_PORT) target/xtensa-esp32s3-espidf/debug/$(PROJECT_NAME)

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


clean: ## Remove all build files
	cargo clean

Prototype

A photo of the actual setup.

Hello world ESP32-S3 with Rust prototype

Serial console

Serial output from the firmware.

Hello world ESP32-S3 with Rust serial console

Hello world ESP32-S3 with Rust browser

Use GPIO5 to add a simple external LED

Description

Install dependancies

  1. Install for Xtensa
     git clone https://github.com/esp-rs/rust-build.git
     cd rust-build
     ./install-rust-toolchain.sh
     . export-esp.sh
    
  2. Set $PATH
     export LIBCLANG_PATH="~/.espressif/tools/xtensa-esp32-elf-clang/esp-14.0.0-20220415-x86_64-apple-darwin/lib/"
     export PATH="~/.espressif/tools/xtensa-esp32-elf-gcc/8_4_0-esp-2021r2-patch3-x86_64-apple-darwin/bin/:~/.espressif/tools/xtensa-esp32s2-elf-gcc/8_4_0-esp-2021r2-patch3-x86_64-apple-darwin/bin/:~/.espressif/tools/xtensa-esp32s3-elf-gcc/8_4_0-esp-2021r2-patch3-x86_64-apple-darwin/bin/:$PATH"
    
  3. Install Cargo sub-commands
     cargo install cargo-generate
     cargo install ldproxy
     cargo install espflash
     cargo install espmonitor
    
  4. Generate the project
     $ cargo generate --vcs none --git https://github.com/esp-rs/esp-idf-template cargo
    
     🤷   Project Name : hello
     🔧   Destination: /Users/sayanee/Desktop/hello ...
     🔧   Generating template ...
     ✔ 🤷   STD support · true
     ✔ 🤷   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)? (beware: Dev Cont
     ✔ 🤷   Configure project to use Dev Containers (VS Code, GitHub Codespaces and Gitpod)? (beware: Dev Containers not available for esp-idf v4.3.2) · false
     ✔ 🤷   MCU · esp32s3
     [ 1/10]   Done: .cargo/config.toml
     [ 2/10]   Done: .cargo
     [ 3/10]   Done: .gitignore
     [ 4/10]   Done: .vscode
     [ 5/10]   Done: Cargo.toml
     [ 6/10]   Done: build.rs
     [ 7/10]   Done: rust-toolchain.toml
     [ 8/10]   Done: sdkconfig.defaults
     [ 9/10]   Done: src/main.rs
     [10/10]   Done: src
     🔧   Moving generated files into: `/Users/sayanee/Desktop/hello`...
     ✨   Done! New project created /Users/sayanee/Desktop/hello
    

Compile

  1. Change directory into the project name
     $ cd hello
    
  2. Edit file src/main.rs with the blinky code
  3. Edit Cargo.toml to add dependancies
  4. Compile / build the project
      $ cargo build
    

Upload the firmware

  1. Plug in the board into the USB port (not the UART port).
  2. Upload / Flash
      espflash /dev/cu.usbmodem14101 target/xtensa-esp32s3-espidf/debug/hello
    

View the logs

Start the serial monitor

  espmonitor /dev/cu.usbmodem14101

References