This commit is contained in:
andromeda
2026-04-05 21:11:02 +02:00
commit 399d59d5e7
11 changed files with 1416 additions and 0 deletions

17
.cargo/config.toml Normal file
View File

@@ -0,0 +1,17 @@
[target.riscv32imac-unknown-none-elf]
runner = "sudo espflash flash --monitor --chip esp32c6"
[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 = ["core"]

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
target
result*

1014
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

39
Cargo.toml Normal file
View File

@@ -0,0 +1,39 @@
[package]
edition = "2024"
name = "esp32c6-play"
rust-version = "1.88"
version = "0.1.0"
[[bin]]
name = "esp32c6-play"
path = "./src/bin/main.rs"
[dependencies]
esp-hal = { version = "~1.0", features = ["esp32c6", "log-04"] }
esp-bootloader-esp-idf = { version = "0.4.0", features = ["esp32c6", "log-04"] }
log = "0.4.27"
critical-section = "1.2.0"
esp-backtrace = { version = "0.18.1", features = [
"esp32c6",
"panic-handler",
"println",
] }
esp-println = { version = "0.16.1", features = ["esp32c6", "log-04"] }
[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

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()
);
}

27
flake.lock generated Normal file
View File

@@ -0,0 +1,27 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1775373274,
"narHash": "sha256-rL4F2+/ixLRzbgewGRpxTqfQNmaHi+VbLTkUcedyZRw=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "bd48fcede9287ff448ecc84c4e94a4fca4bd0d72",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

24
flake.nix Normal file
View File

@@ -0,0 +1,24 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
};
outputs = {nixpkgs, ...}: let
pkgs = nixpkgs.legacyPackages.x86_64-linux;
esp-rs = pkgs.callPackage ./nix/esp-rs.nix {};
in {
devShells.x86_64-linux.default = pkgs.mkShell {
buildInputs = [
pkgs.pkg-config
pkgs.libudev-zero
pkgs.libusb1
pkgs.espflash
pkgs.esp-generate
pkgs.rust-analyzer
esp-rs
];
shellHook = ''
export RUSTUP_TOOLCHAIN=${esp-rs}
'';
};
};
}

178
nix/esp-rs.nix Normal file
View File

@@ -0,0 +1,178 @@
# MIT licensed
# code taken from https://git.sr.ht/~olk/esp-rs-nix/blob/main/esp-rs.nix
{
pkgs ? import <nixpkgs> {},
lib ? pkgs.lib,
}: let
versions = {
esp-rust = {
version = "1.93.0.0";
hash = "sha256-837shgsDonrmUqZsIhUoR+93IR8tUat67PiHfShilJc=";
};
rust-src = {
version = "1.93.0.0";
hash = "sha256-upKiKRDvubE8obME4OVewTr6PjS/p0QOqjpQfEWBfE0=";
};
gcc = {
version = "14.2.0_20241119";
xtensa-hash = "sha256-pX2KCnUoGZtgqFmZEuNRJxDMQgqYYPRpswL3f3T0nWE=";
riscv32-hash = "sha256-67O34FYUnVG2nfkfQj2yH874qDSYx4F/16xxPi0kNbY=";
};
defaultArch = "x86_64-unknown-linux-gnu";
gccArch = "x86_64-linux-gnu";
};
mkGccToolchain = {
name,
arch,
hash,
}:
pkgs.stdenv.mkDerivation {
pname = "esp-${name}-gcc";
version = versions.gcc.version;
src = pkgs.fetchzip {
url = "https://github.com/espressif/crosstool-NG/releases/download/esp-${versions.gcc.version}/${arch}-esp-elf-${versions.gcc.version}-${versions.gccArch}.tar.xz";
inherit hash;
};
installPhase = ''
runHook preInstall
mkdir -p $out
cp -r ./* $out/
runHook postInstall
'';
meta = with lib; {
description = "ESP32 ${name} GCC toolchain";
homepage = "https://github.com/espressif/crosstool-NG";
license = licenses.gpl2Plus;
platforms = ["x86_64-linux"];
};
};
esp-rust-build = pkgs.stdenv.mkDerivation {
pname = "esp-rust-build";
version = versions.esp-rust.version;
src = pkgs.fetchzip {
url = "https://github.com/esp-rs/rust-build/releases/download/v${versions.esp-rust.version}/rust-${versions.esp-rust.version}-${versions.defaultArch}.tar.xz";
hash = versions.esp-rust.hash;
};
nativeBuildInputs = with pkgs; [autoPatchelfHook];
buildInputs = with pkgs; [
stdenv.cc.cc
zlib
];
patchPhase = ''
patchShebangs ./install.sh
'';
installPhase = ''
runHook preInstall
mkdir -p $out
./install.sh --destdir=$out --prefix="" --disable-ldconfig --without=rust-docs-json-preview,rust-docs
runHook postInstall
'';
meta = with lib; {
description = "ESP32 Rust toolchain";
homepage = "https://github.com/esp-rs/rust-build";
license = with licenses; [
mit
asl20
];
platforms = ["x86_64-linux"];
};
};
esp-xtensa-gcc = mkGccToolchain {
name = "xtensa";
arch = "xtensa";
hash = versions.gcc.xtensa-hash;
};
esp-riscv32-gcc = mkGccToolchain {
name = "riscv32";
arch = "riscv32";
hash = versions.gcc.riscv32-hash;
};
rust-src = pkgs.fetchzip {
url = "https://github.com/esp-rs/rust-build/releases/download/v${versions.rust-src.version}/rust-src-${versions.rust-src.version}.tar.xz";
hash = versions.rust-src.hash;
};
esp-rs-base = pkgs.symlinkJoin {
name = "esp-rs-base-${versions.esp-rust.version}";
paths = [
esp-rust-build
esp-xtensa-gcc
esp-riscv32-gcc
];
meta = {
description = "Base ESP32 toolchain components";
platforms = ["x86_64-linux"];
};
};
in
pkgs.stdenv.mkDerivation {
pname = "esp-rs";
version = versions.esp-rust.version;
src = rust-src;
nativeBuildInputs = with pkgs; [
autoPatchelfHook
pkg-config
];
buildInputs = with pkgs; [
stdenv.cc.cc
zlib
];
autoPatchelfIgnoreMissingDeps = ["*"];
patchPhase = ''
patchShebangs ./install.sh
'';
installPhase = ''
runHook preInstall
# Start with the base toolchain
mkdir -p $out
cp -rL ${esp-rs-base}/* $out/
chmod -R u+w $out
# Install rust-src on top
./install.sh --destdir=$out --prefix="" --disable-ldconfig
runHook postInstall
'';
passthru = {
inherit esp-rust-build esp-xtensa-gcc esp-riscv32-gcc;
version = versions.esp-rust.version;
};
meta = with lib; {
description = "Complete ESP32 Rust development toolchain";
longDescription = ''
A complete toolchain for developing Rust applications on ESP32 microcontrollers.
Includes Rust compiler with ESP32 targets, Xtensa and RISC-V GCC toolchains,
and all necessary components for no_std ESP-HAL development.
'';
homepage = "https://github.com/esp-rs";
license = with licenses; [
mit
asl20
gpl2Plus
];
platforms = ["x86_64-linux"];
maintainers = [];
};
}

4
rust-toolchain.toml Normal file
View File

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

40
src/bin/main.rs Normal file
View File

@@ -0,0 +1,40 @@
#![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 log::info;
// 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.2.0
esp_println::logger::init_logger_from_env();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let _peripherals = esp_hal::init(config);
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.0.0/examples
}

1
src/lib.rs Normal file
View File

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