0
0
Fork 0

setup rust project using esp-generate

This commit is contained in:
Jannik Beyerstedt 2026-04-25 15:07:54 +02:00
commit 8cfb908678
10 changed files with 1825 additions and 0 deletions

17
.cargo/config.toml Normal file
View file

@ -0,0 +1,17 @@
[target.riscv32imac-unknown-none-elf]
runner = "espflash flash --monitor --chip esp32c5"
[env]
ESP_LOG="info"
[build]
rustflags = [
# Required to obtain backtraces (e.g. when using the "esp-backtrace" crate.)
# NOTE: May negatively impact performance of produced code
"-C", "force-frame-pointers",
]
target = "riscv32imac-unknown-none-elf"
[unstable]
build-std = ["alloc", "core"]

1
.clippy.toml Normal file
View file

@ -0,0 +1 @@
stack-size-threshold = 1024

26
.gitignore vendored Normal file
View file

@ -0,0 +1,26 @@
# will have compiled files and executables
debug/
target/
# Editor configuration
.vscode/
.zed/
.helix/
.nvim.lua
# These are backup files generated by rustfmt
**/*.rs.bk
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
# RustRover
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Ignore .DS_Store file in mac
**/.DS_Store

1568
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

65
Cargo.toml Normal file
View file

@ -0,0 +1,65 @@
[package]
edition = "2024"
name = "esp32-c_its-companion"
rust-version = "1.88"
version = "0.1.0"
[[bin]]
name = "esp32-c_its-companion"
path = "./src/bin/main.rs"
[dependencies]
esp-hal = { version = "~1.1.0", features = ["esp32c5", "log-04", "unstable"] }
esp-rtos = { version = "0.3.0", features = [
"esp-alloc",
"esp-radio",
"esp32c5",
"log-04",
] }
esp-bootloader-esp-idf = { version = "0.5.0", features = ["esp32c5", "log-04"] }
log = "0.4.27"
critical-section = "1.2.0"
embedded-io = "0.7.1"
esp-alloc = "0.10.0"
esp-backtrace = { version = "0.19.0", features = [
"esp32c5",
"panic-handler",
"println",
] }
esp-println = { version = "0.17.0", features = ["esp32c5", "log-04"] }
esp-radio = { version = "0.18.0", features = [
"esp-alloc",
"esp32c5",
"log-04",
"unstable",
"wifi",
] }
smoltcp = { version = "0.13.0", default-features = false, features = [
"log",
"medium-ethernet",
"multicast",
"proto-dhcpv4",
"proto-dns",
"proto-ipv4",
"socket-dns",
"socket-icmp",
"socket-raw",
"socket-tcp",
"socket-udp",
] }
# For fine tuning these settings, please refer to https://doc.rust-lang.org/cargo/reference/profiles.html
[profile.dev]
# The default debug profile is too slow and too big for resource-constrained devices.
# Always build with some optimizations enabled.
opt-level = "s"
[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2 # prefer slower builds but better debugging experience
lto = 'fat'
opt-level = 's'

View file

@ -12,6 +12,15 @@ rustup toolchain install nightly --component rust-src
rustup target add riscv32imac-unknown-none-elf rustup target add riscv32imac-unknown-none-elf
# cargo install esp-generate --locked # cargo install esp-generate --locked
# cargo install esp-config --features=tui --locked
cargo install espflash --locked cargo install espflash --locked
``` ```
(Project was setup using esp-generate with the options `--chip esp32c5 -o esp32c5-wroom-1 -o alloc -o vscode -o log -o unstable-hal -o wifi -o esp-backtrace`.)
### Build and Flash
Connect your ESP32-C5 and run:
```
cargo run --release
```

70
build.rs Normal file
View file

@ -0,0 +1,70 @@
fn main() {
linker_be_nice();
// make sure linkall.x is the last linker script (otherwise might cause problems with flip-link)
println!("cargo:rustc-link-arg=-Tlinkall.x");
}
fn linker_be_nice() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 {
let kind = &args[1];
let what = &args[2];
match kind.as_str() {
"undefined-symbol" => match what.as_str() {
what if what.starts_with("_defmt_") => {
eprintln!();
eprintln!(
"💡 `defmt` not found - make sure `defmt.x` is added as a linker script and you have included `use defmt_rtt as _;`"
);
eprintln!();
}
"_stack_start" => {
eprintln!();
eprintln!("💡 Is the linker script `linkall.x` missing?");
eprintln!();
}
what if what.starts_with("esp_rtos_") => {
eprintln!();
eprintln!(
"💡 `esp-radio` has no scheduler enabled. Make sure you have initialized `esp-rtos` or provided an external scheduler."
);
eprintln!();
}
"embedded_test_linker_file_not_added_to_rustflags" => {
eprintln!();
eprintln!(
"💡 `embedded-test` not found - make sure `embedded-test.x` is added as a linker script for tests"
);
eprintln!();
}
"free"
| "malloc"
| "calloc"
| "get_free_internal_heap_size"
| "malloc_internal"
| "realloc_internal"
| "calloc_internal"
| "free_internal" => {
eprintln!();
eprintln!(
"💡 Did you forget the `esp-alloc` dependency or didn't enable the `compat` feature on it?"
);
eprintln!();
}
_ => (),
},
// we don't have anything helpful for "missing-lib" yet
_ => {
std::process::exit(1);
}
}
std::process::exit(0);
}
println!(
"cargo:rustc-link-arg=--error-handling-script={}",
std::env::current_exe().unwrap().display()
);
}

4
rust-toolchain.toml Normal file
View file

@ -0,0 +1,4 @@
[toolchain]
channel = "stable"
components = ["rust-src"]
targets = ["riscv32imac-unknown-none-elf"]

64
src/bin/main.rs Normal file
View file

@ -0,0 +1,64 @@
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
#![deny(clippy::large_stack_frames)]
use esp_backtrace as _;
use esp_hal::clock::CpuClock;
use esp_hal::main;
use esp_hal::time::{Duration, Instant};
use esp_hal::timer::timg::TimerGroup;
use log::info;
extern crate alloc;
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
#[allow(
clippy::large_stack_frames,
reason = "it's not unusual to allocate larger buffers etc. in main"
)]
#[main]
fn main() -> ! {
// generator version: 1.3.0
// generator parameters: --chip esp32c5 -o esp32c5-wroom-1 -o alloc -o vscode -o log -o unstable-hal -o wifi -o esp-backtrace
esp_println::logger::init_logger_from_env();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
// The following pins are used to bootstrap the chip. They are available
// for use, but check the datasheet of the module for more information on them.
// - GPIO2
// - GPIO3
// - GPIO7
// - GPIO25
// - GPIO26
// - GPIO27
// - GPIO28
esp_alloc::heap_allocator!(#[esp_hal::ram(reclaimed)] size: 65536);
let timg0 = TimerGroup::new(peripherals.TIMG0);
let sw_interrupt =
esp_hal::interrupt::software::SoftwareInterruptControl::new(peripherals.SW_INTERRUPT);
esp_rtos::start(timg0.timer0, sw_interrupt.software_interrupt0);
let (mut _wifi_controller, _interfaces) =
esp_radio::wifi::new(peripherals.WIFI, Default::default())
.expect("Failed to initialize Wi-Fi controller");
loop {
info!("Hello world!");
let delay_start = Instant::now();
while delay_start.elapsed() < Duration::from_millis(500) {}
}
// for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.1.0/examples
}

1
src/lib.rs Normal file
View file

@ -0,0 +1 @@
#![no_std]