[esp] Add Rust no-std example.

This commit is contained in:
Shaun Reed 2025-09-21 09:35:04 -04:00
parent bb28b1e2ef
commit 79c6da4c60
9 changed files with 189 additions and 0 deletions

View File

@ -0,0 +1,16 @@
[build]
target = "xtensa-esp32-espidf"
[target.xtensa-esp32-espidf]
linker = "ldproxy"
runner = "espflash flash --monitor"
rustflags = [ "--cfg", "espidf_time64"]
[unstable]
build-std = ["std", "panic_abort"]
[env]
MCU="esp32"
# Note: this variable is not used by the pio builder (`cargo build --features pio`)
ESP_IDF_VERSION = "v5.2.3"

View File

@ -0,0 +1,42 @@
name: Continuous Integration
on:
push:
branches:
- main
paths-ignore:
- "**/README.md"
pull_request:
workflow_dispatch:
env:
CARGO_TERM_COLOR: always
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
rust-checks:
name: Rust Checks
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
action:
- command: build
args: --release
- command: fmt
args: --all -- --check --color always
- command: clippy
args: --all-targets --all-features --workspace -- -D warnings
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Rust
uses: esp-rs/xtensa-toolchain@v1.6
with:
default: true
buildtargets: esp32
ldproxy: true
- name: Enable caching
uses: Swatinem/rust-cache@v2
- name: Run command
run: cargo ${{ matrix.action.command }} ${{ matrix.action.args }}

4
esp/rust/02_esp-idf-no-std/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/.vscode
/.embuild
/target
/Cargo.lock

View File

@ -0,0 +1,49 @@
[package]
name = "esp-idf-no-std"
version = "0.1.0"
authors = ["Shaun Reed <shaunrd0@gmail.com>"]
edition = "2021"
resolver = "2"
rust-version = "1.77"
[[bin]]
name = "esp-idf-no-std"
harness = false # do not use the built in cargo test harness -> resolve rust-analyzer errors
[profile.release]
opt-level = "s"
[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z"
[features]
default = []
experimental = ["esp-idf-svc/experimental"]
[dependencies]
log = "0.4"
esp-idf-svc = "0.51"
esp-idf-hal = "0.45.2"
anyhow = "1.0.98"
# --- Optional Embassy Integration ---
# esp-idf-svc = { version = "0.51", features = ["critical-section", "embassy-time-driver", "embassy-sync"] }
# If you enable embassy-time-driver, you MUST also add one of:
# a) Standalone Embassy libs ( embassy-time, embassy-sync etc) with a foreign async runtime:
# embassy-time = { version = "0.4.0", features = ["generic-queue-8"] } # NOTE: any generic-queue variant will work
# b) With embassy-executor:
# embassy-executor = { version = "0.7", features = ["executor-thread", "arch-std"] }
# NOTE: if you use embassy-time with embassy-executor you don't need the generic-queue-8 feature
# --- Temporary workaround for embassy-executor < 0.8 ---
# esp-idf-svc = { version = "0.51", features = ["embassy-time-driver", "embassy-sync"] }
# critical-section = { version = "1.1", features = ["std"], default-features = false }
[build-dependencies]
embuild = "0.33"

View File

@ -0,0 +1,3 @@
fn main() {
embuild::espidf::sysenv::output();
}

View File

@ -0,0 +1,2 @@
[toolchain]
channel = "esp"

View File

@ -0,0 +1,10 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=8000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granularity for thread sleeps (10 ms by default).
#CONFIG_FREERTOS_HZ=1000
# Workaround for https://github.com/espressif/esp-idf/issues/7631
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
#CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n

View File

@ -0,0 +1,62 @@
use esp_idf_hal::delay::{FreeRtos, BLOCK};
use esp_idf_hal::i2c::*;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::prelude::*;
const SSD1306_ADDRESS: u8 = 0x3c;
fn main() -> anyhow::Result<()> {
esp_idf_hal::sys::link_patches();
let peripherals = Peripherals::take()?;
let i2c = peripherals.i2c0;
let sda = peripherals.pins.gpio21;
let scl = peripherals.pins.gpio22;
println!("Starting I2C SSD1306 test");
let config = I2cConfig::new().baudrate(100.kHz().into());
let mut i2c = I2cDriver::new(i2c, sda, scl, &config)?;
// initialze the display - don't worry about the meaning of these bytes - it's specific to SSD1306
i2c.write(SSD1306_ADDRESS, &[0, 0xae], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xd4], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x80], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xa8], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x3f], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xd3], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x00], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x40], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x8d], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x14], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xa1], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xc8], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xda], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x12], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x81], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xcf], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xf1], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xdb], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x40], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xa4], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xa6], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0xaf], BLOCK)?;
i2c.write(SSD1306_ADDRESS, &[0, 0x20, 0x00], BLOCK)?;
// fill the display
for _ in 0..64 {
let data: [u8; 17] = [
0x40, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff,
];
i2c.write(SSD1306_ADDRESS, &data, BLOCK)?;
}
loop {
// we are sleeping here to make sure the watchdog isn't triggered
FreeRtos::delay_ms(500);
i2c.write(SSD1306_ADDRESS, &[0, 0xa6], BLOCK)?;
FreeRtos::delay_ms(500);
i2c.write(SSD1306_ADDRESS, &[0, 0xa7], BLOCK)?;
}
}

View File

@ -3,6 +3,7 @@
```bash ```bash
shaunrd0/klips/esp/rust shaunrd0/klips/esp/rust
├── 01_es-idf-std # Template project for using ESP-IDF with std enabled. ├── 01_es-idf-std # Template project for using ESP-IDF with std enabled.
├── 02_es-idf-no-std # Template project for ESP-IDF using no std.
└── README.md └── README.md
``` ```