Setup basic esp32c3 embassy

This commit is contained in:
Mona Mayrhofer 2025-06-26 16:04:37 +02:00
commit c1b98bb7d0
No known key found for this signature in database
GPG key ID: 3E2BDA732A957188
11 changed files with 1990 additions and 0 deletions

17
.cargo/config.toml Normal file
View file

@ -0,0 +1,17 @@
[target.riscv32imc-unknown-none-elf]
runner = "espflash flash --monitor --chip esp32c3"
[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 = "riscv32imc-unknown-none-elf"
[unstable]
build-std = ["alloc", "core"]

14
.envrc Normal file
View file

@ -0,0 +1,14 @@
if ! has nix_direnv_version || ! nix_direnv_version 3.0.6; then
source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/3.0.6/direnvrc" "sha256-RYcUJaRMf8oF5LznDrlCXbkOQrywm0HDv1VjYGaJGdM="
fi
watch_file flake.nix
watch_file flake.lock
mkdir -p "$PWD/.devenv"
DEVENV_ROOT_FILE="$PWD/.devenv/root"
printf %s "$PWD" > "$DEVENV_ROOT_FILE"
if ! use flake . --override-input devenv-root "file+file://$DEVENV_ROOT_FILE"
then
echo "devenv could not be built. The devenv environment was not loaded. Make the necessary changes to devenv.nix and hit enter to try again." >&2
fi

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
/target
.devenv/
.direnv/

1543
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

68
Cargo.toml Normal file
View file

@ -0,0 +1,68 @@
[package]
name = "m5stamp-c3-ws2812"
version = "0.1.0"
edition = "2024"
[[bin]]
name = "uwu"
path = "./src/bin/main.rs"
[dependencies]
embassy-net = { version = "0.6.0", features = [
"dhcpv4",
"medium-ethernet",
"tcp",
"udp",
] }
embedded-io = "0.6.1"
embedded-io-async = "0.6.1"
esp-alloc = "0.7.0"
esp-hal = { version = "1.0.0-beta.0", features = ["esp32c3", "unstable"] }
esp-println = { version = "0.13.0", features = ["esp32c3", "log"] }
log = { version = "0.4.21" }
smoltcp = { version = "0.12.0", default-features = false, features = [
"medium-ethernet",
"multicast",
"proto-dhcpv4",
"proto-dns",
"proto-ipv4",
"socket-dns",
"socket-icmp",
"socket-raw",
"socket-tcp",
"socket-udp",
] }
# for more networking protocol support see https://crates.io/crates/edge-net
bleps = { git = "https://github.com/bjoernQ/bleps", package = "bleps", rev = "a5148d8ae679e021b78f53fd33afb8bb35d0b62e", features = [
"async",
"macros",
] }
critical-section = "1.2.0"
embassy-executor = { version = "0.7.0", features = ["task-arena-size-20480"] }
embassy-time = { version = "0.4.0", features = ["generic-queue-8"] }
esp-hal-embassy = { version = "0.7.0", features = ["esp32c3"] }
esp-wifi = { version = "0.13.0", features = [
"ble",
"builtin-scheduler",
"coex",
"esp-alloc",
"esp32c3",
"log",
"wifi",
] }
heapless = { version = "0.8.0", default-features = false }
static_cell = { version = "2.1.0", features = ["nightly"] }
[profile.dev]
# Rust debug is too slow.
# For debug builds always builds with some optimization
opt-level = "s"
[profile.release]
codegen-units = 1 # LLVM can perform better optimizations using a single thread
debug = 2
debug-assertions = false
incremental = false
lto = 'fat'
opt-level = 's'
overflow-checks = false

40
build.rs Normal file
View file

@ -0,0 +1,40 @@
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() {
"_defmt_timestamp" => {
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!();
}
_ => (),
},
// 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()
);
}

100
flake.nix Normal file
View file

@ -0,0 +1,100 @@
{
description = "Description for the project";
inputs = {
devenv-root = {
url = "file+file:///dev/null";
flake = false;
};
flake-parts.url = "github:hercules-ci/flake-parts";
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
devenv.url = "github:cachix/devenv";
nix2container.url = "github:nlewo/nix2container";
nix2container.inputs.nixpkgs.follows = "nixpkgs";
devenv.inputs.nixpkgs.follows = "nixpkgs";
mk-shell-bin.url = "github:rrbutani/nix-mk-shell-bin";
nixos-hardware.url = "github:NixOS/nixos-hardware/master";
# crane = {
# url = "github:ipetkov/crane";
# inputs.nixpkgs.follows = "nixpkgs-old";
# };
fenix.url = "github:nix-community/fenix/monthly";
fenix.inputs.nixpkgs.follows = "nixpkgs";
crane.url = "github:ipetkov/crane";
};
nixConfig = {
extra-trusted-public-keys = "devenv.cachix.org-1:w1cLUi8dv3hnoSPGAuibQv+f9TZLr6cv/Hm9XgU50cw=";
extra-substituters = "https://devenv.cachix.org";
};
outputs =
inputs@{
flake-parts,
devenv-root,
nixpkgs,
...
}:
flake-parts.lib.mkFlake { inherit inputs; } {
imports = [
inputs.devenv.flakeModule
];
systems = [
"x86_64-linux"
"i686-linux"
"x86_64-darwin"
"aarch64-linux"
"aarch64-darwin"
];
perSystem =
{
config,
self',
inputs',
pkgs,
system,
...
}:
{
devenv.shells.default =
let
fnx = inputs'.fenix.packages;
rustToolchain = fnx.combine [
fnx.complete.cargo
fnx.complete.clippy
fnx.complete.rust-src
fnx.complete.rustc
fnx.complete.rustfmt
fnx.targets.riscv32imc-unknown-none-elf.latest.rust-std
];
in
{
name = "my-project";
packages = [
pkgs.dioxus-cli
pkgs.espflash
pkgs.cargo-espflash
pkgs.cargo-expand
# Required for rust-src
rustToolchain
#pkgs.rust-rust-analyzer
inputs'.fenix.packages.rust-analyzer
#rustToolchain.rust-src
pkgs.slsnif
];
# DO NOT ENABLE RUST because devenv messes up the ability
# to build rust-src
# languages.rust = {};
scripts.serial-sniff.exec = ''
slsnif -u -n -s 115200 /dev/ttyACM0
'';
};
};
};
}

4
rust-toolchain.toml Normal file
View file

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

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

@ -0,0 +1,51 @@
#![no_std]
#![no_main]
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_hal::clock::CpuClock;
use esp_hal::timer::systimer::SystemTimer;
use esp_hal::timer::timg::TimerGroup;
use log::info;
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
extern crate alloc;
#[esp_hal_embassy::main]
async fn main(spawner: Spawner) {
// generator version: 0.3.1
esp_println::logger::init_logger_from_env();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(size: 72 * 1024);
let timer0 = SystemTimer::new(peripherals.SYSTIMER);
esp_hal_embassy::init(timer0.alarm0);
info!("Embassy initialized!");
let timer1 = TimerGroup::new(peripherals.TIMG0);
let _init = esp_wifi::init(
timer1.timer0,
esp_hal::rng::Rng::new(peripherals.RNG),
peripherals.RADIO_CLK,
)
.unwrap();
// TODO: Spawn some tasks
let _ = spawner;
loop {
info!("Hello world!");
Timer::after(Duration::from_secs(1)).await;
}
// for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0-beta.0/examples/src/bin
}

1
src/lib.rs Normal file
View file

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

149
template.yaml Normal file
View file

@ -0,0 +1,149 @@
options:
- !Option
name: unstable-hal
display_name: Enable unstable HAL features.
help: "This configuration enables unstable esp-hal features.
These come with no stability guarantees, and could be changed or removed at any time."
- !Option
name: alloc
display_name: Enable allocations via the esp-alloc crate.
help: esp-alloc comes with no stability guarantees at this time.
- !Option
name: wifi
display_name: Enable Wi-Fi via the esp-wifi crate.
help: esp-wifi comes with no stability guarantees at this time.
requires:
- alloc
- unstable-hal
chips:
- esp32
- esp32c2
- esp32c3
- esp32c6
- esp32s2
- esp32s3
- !Option
name: ble
display_name: Enable BLE via the esp-wifi crate.
help: esp-wifi comes with no stability guarantees at this time.
requires:
- alloc
- unstable-hal
chips:
- esp32
- esp32c2
- esp32c3
- esp32c6
- esp32h2
- esp32s3
- !Option
name: embassy
display_name: Add embassy framework support.
help: esp-hal-embassy comes with no stability guarantees at this time.
requires:
- unstable-hal
- !Option
name: probe-rs
display_name: Use probe-rs to flash and monitor instead of espflash.
help: probe-rs is a debugger that connects to the chips over JTAG. It can be used to flash and
monitor, and it can also be used to interactively debug an application, or run tests on the
hardware. Semihosting or RTT-based technologies like defmt-rtt require probe-rs.
chips:
- esp32c6
- esp32h2
- esp32s3
- !Option
name: probe-rs
display_name: Use probe-rs to flash and monitor instead of espflash.
help: probe-rs is a debugger that connects to the chips over JTAG. It can be used to flash and
monitor, and it can also be used to interactively debug an application, or run tests on the
hardware. Semihosting or RTT-based technologies like defmt-rtt require probe-rs.
probe-rs requires a debug probe like esp-prog, and will not work with USB-UART adapters that
often come on development boards.
chips:
- esp32
- esp32s2
- esp32c2
- esp32c3
- !Category
name: flashing-probe-rs
display_name: Flashing, logging and debugging (probe-rs)
requires:
- probe-rs
options:
- !Option
name: defmt
display_name: Use defmt to print messages.
selection_group: log-frontend
- !Option
name: panic-rtt-target
display_name: Use panic-rtt-target as the panic handler.
selection_group: panic-handler
requires:
- probe-rs
- !Category
name: flashing-espflash
display_name: Flashing, logging and debugging (espflash)
requires:
- "!probe-rs"
options:
- !Option
name: log
display_name: Use the log crate to print messages.
selection_group: log-frontend
requires:
- "!probe-rs"
- !Option
name: defmt
display_name: Use defmt to print messages.
selection_group: log-frontend
- !Option
name: esp-backtrace
display_name: Use esp-backtrace as the panic handler.
selection_group: panic-handler
requires:
- "!probe-rs"
- !Category
name: optional
display_name: Options
options:
- !Option
name: wokwi
display_name: Add support for Wokwi simulation using VS Code Wokwi extension.
chips:
- esp32
- esp32c3
- esp32c6
- esp32h2
- esp32s2
- esp32s3
- !Option
name: dev-container
display_name: Add support for VS Code Dev Containers and GitHub Codespaces.
- !Option
name: ci
display_name: Add GitHub Actions support with some basic checks.
- !Category
name: editor
display_name: Optional editor config files for rust-analyzer
options:
- !Option
name: helix
display_name: Add rust-analyzer settings for Helix Editor
- !Option
name: vscode
display_name: Add rust-analyzer settings for Visual Studio Code