From 1651c68771a00318a7936ee65fdc97d75da9ddd4 Mon Sep 17 00:00:00 2001 From: Daniel Bulant Date: Mon, 11 May 2026 18:23:21 +0200 Subject: [PATCH] working logger example (kind of) --- esp32/.cargo/config.toml | 2 +- esp32/Cargo.lock | 57 ++++++++++++++++++++++++++ esp32/Cargo.toml | 1 + esp32/build.rs | 88 +++++++++++++++++++++++++++------------- esp32/memory.x | 17 -------- esp32/src/bin/logger.rs | 3 +- 6 files changed, 121 insertions(+), 47 deletions(-) delete mode 100644 esp32/memory.x diff --git a/esp32/.cargo/config.toml b/esp32/.cargo/config.toml index d017de9..63b5981 100644 --- a/esp32/.cargo/config.toml +++ b/esp32/.cargo/config.toml @@ -6,7 +6,7 @@ runner = "espflash flash --monitor" target = "xtensa-esp32-none-elf" rustflags = [ "-C", "link-arg=-Wl,-Tlinkall.x", - "-C", "link-arg=-nostartfiles", + "-C", "link-arg=-nostartfiles", ] [unstable] diff --git a/esp32/Cargo.lock b/esp32/Cargo.lock index f15781f..5a69db9 100644 --- a/esp32/Cargo.lock +++ b/esp32/Cargo.lock @@ -579,6 +579,12 @@ dependencies = [ "embedded-nal", ] +[[package]] +name = "embedded-storage" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21dea9854beb860f3062d10228ce9b976da520a73474aed3171ec276bc0c032" + [[package]] name = "enumset" version = "1.1.12" @@ -606,6 +612,23 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "esp-bootloader-esp-idf" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35ffc117c3a9859835d89d0e90f5ee9886ce2264a71a849a7a22ab5308f6653c" +dependencies = [ + "cfg-if", + "document-features", + "embedded-storage", + "esp-config", + "esp-hal-procmacros", + "esp-metadata-generated", + "esp-rom-sys", + "jiff", + "strum", +] + [[package]] name = "esp-config" version = "0.7.0" @@ -739,6 +762,7 @@ dependencies = [ "embedded-hal-compat", "embedded-io 0.7.1", "embedded-io-async 0.7.0", + "esp-bootloader-esp-idf", "esp-hal", "log", "owned_str", @@ -978,6 +1002,30 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "jiff" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f00b5dbd620d61dfdcb6007c9c1f6054ebd75319f163d886a9055cec1155073d" +dependencies = [ + "jiff-static", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", +] + +[[package]] +name = "jiff-static" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e000de030ff8022ea1da3f466fbb0f3a809f5e51ed31f6dd931c35181ad8e6d7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.117", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -1123,6 +1171,15 @@ dependencies = [ "critical-section", ] +[[package]] +name = "portable-atomic-util" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618" +dependencies = [ + "portable-atomic", +] + [[package]] name = "proc-macro-crate" version = "3.5.0" diff --git a/esp32/Cargo.toml b/esp32/Cargo.toml index 13bf743..7d850c9 100644 --- a/esp32/Cargo.toml +++ b/esp32/Cargo.toml @@ -14,6 +14,7 @@ codegen-units = 1 [target.'cfg(target_arch = "xtensa")'.dependencies] arrayvec = { version = "0.7.6", default-features = false } embedded-io = "0.7.1" +esp-bootloader-esp-idf = { version = "0.5.0", features = ["esp32"] } esp-hal = { version = "1.0.0", features = ["esp32", "rt"] } [target.'cfg(target_arch = "arm")'.dependencies] diff --git a/esp32/build.rs b/esp32/build.rs index d534cc3..4c168f1 100644 --- a/esp32/build.rs +++ b/esp32/build.rs @@ -1,31 +1,63 @@ -//! This build script copies the `memory.x` file from the crate root into -//! a directory where the linker can always find it at build time. -//! For many projects this is optional, as the linker always searches the -//! project root directory -- wherever `Cargo.toml` is. However, if you -//! are using a workspace or have a more complicated build setup, this -//! build script becomes required. Additionally, by requesting that -//! Cargo re-run the build script whenever `memory.x` is changed, -//! updating `memory.x` ensures a rebuild of the application with the -//! new memory settings. - -use std::env; -use std::fs::File; -use std::io::Write; -use std::path::PathBuf; - fn main() { - // Put `memory.x` in our output directory and ensure it's - // on the linker search path. - let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); - File::create(out.join("memory.x")) - .unwrap() - .write_all(include_bytes!("memory.x")) - .unwrap(); - println!("cargo:rustc-link-search={}", out.display()); + let args: Vec = std::env::args().collect(); + if args.len() > 1 { + let kind = &args[1]; + let what = &args[2]; - // By default, Cargo will re-run a build script whenever - // any file in the project changes. By specifying `memory.x` - // here, we ensure the build script is only re-run when - // `memory.x` is changed. - println!("cargo:rerun-if-changed=memory.x"); + 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!(); + } + _ => (), + }, + _ => { + std::process::exit(1); + } + } + + std::process::exit(0); + } + + println!( + "cargo:rustc-link-arg=-Wl,--error-handling-script={}", + std::env::current_exe().unwrap().display() + ); } diff --git a/esp32/memory.x b/esp32/memory.x deleted file mode 100644 index ef19dff..0000000 --- a/esp32/memory.x +++ /dev/null @@ -1,17 +0,0 @@ -MEMORY { - BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100 - FLASH : ORIGIN = 0x10000100, LENGTH = 2048K - 0x100 - - /* Pick one of the two options for RAM layout */ - - /* OPTION A: Use all RAM banks as one big block */ - /* Reasonable, unless you are doing something */ - /* really particular with DMA or other concurrent */ - /* access that would benefit from striping */ - RAM : ORIGIN = 0x20000000, LENGTH = 264K - - /* OPTION B: Keep the unstriped sections separate */ - /* RAM: ORIGIN = 0x20000000, LENGTH = 256K */ - /* SCRATCH_A: ORIGIN = 0x20040000, LENGTH = 4K */ - /* SCRATCH_B: ORIGIN = 0x20041000, LENGTH = 4K */ -} diff --git a/esp32/src/bin/logger.rs b/esp32/src/bin/logger.rs index d0f0d7a..ccf5b1b 100644 --- a/esp32/src/bin/logger.rs +++ b/esp32/src/bin/logger.rs @@ -3,9 +3,10 @@ use arrayvec::ArrayString; use core::fmt::Write as _; -use embedded_io::Write as _; use esp_hal::{clock::CpuClock, uart::Config, uart::Uart}; +esp_bootloader_esp_idf::esp_app_desc!(); + #[panic_handler] fn panic(_: &core::panic::PanicInfo) -> ! { loop {}