From d3a212878231edb7ef89ffb517b60101545c5482 Mon Sep 17 00:00:00 2001 From: Sachymetsu Date: Sat, 27 Dec 2025 17:04:38 +0000 Subject: [PATCH] feat: Optional defmt dep, reset panic handler --- Cargo.toml | 59 +++++++++++++++++++++++++------------------- src/allocator.rs | 3 ++- src/main.rs | 3 +++ src/panic_handler.rs | 15 +++++++++++ 4 files changed, 53 insertions(+), 27 deletions(-) create mode 100644 src/panic_handler.rs diff --git a/Cargo.toml b/Cargo.toml index b6ec12a..bd489a7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,50 +7,58 @@ license = "MIT OR Apache-2.0" version = "0.1.0" edition = "2024" +[features] +default = ["defmt"] +defmt = [ + "dep:defmt", + "dep:defmt-rtt", + "dep:panic-probe", + "postcard/defmt", + "postcard/use-defmt", + "sachy-sntp/defmt", + "sachy-mdns/defmt", + "sachy-fmt/defmt", + "embassy-embedded-hal/defmt", + "embassy-sync/defmt", + "embassy-executor/defmt", + "embassy-time/defmt", + "embassy-time/defmt-timestamp-uptime", + "embassy-rp/defmt", + "embassy-net/defmt", + "cyw43/defmt", + "cyw43-pio/defmt", +] + [dependencies] cortex-m = { version = "0.7.6", features = ["inline-asm"] } cortex-m-rt = "0.7.0" -defmt = "1.0" -defmt-rtt = "1.0" -panic-probe = { version = "1.0", features = ["print-defmt"] } +defmt = { version = "1.0", optional = true } +defmt-rtt = { version = "1.0", optional = true } +panic-probe = { version = "1.0", features = ["print-defmt"], optional = true } critical-section = "1.2.0" static_cell = "2.1" heapless = { version = "0.8" } chrono = { version = "0.4", default-features = false } portable-atomic = { version = "1.5", features = ["critical-section"] } serde = { version = "1.0.210", default-features = false, features = ["alloc"] } -postcard = { version = "1.1.3", features = [ - "alloc", - "defmt", - "use-defmt", -], default-features = false } +postcard = { version = "1.1.3", features = ["alloc"], default-features = false } rlsf = { version = "0.2.1", default-features = false } sachy-sntp = { git = "https://tangled.org/sachy.dev/sachy-embed-core", package = "sachy-sntp", default-features = false, features = [ - "defmt", "embassy-net", "chrono", ] } -sachy-mdns = { git = "https://tangled.org/sachy.dev/sachy-embed-core", package = "sachy-mdns", features = [ - "defmt", -] } -sachy-fmt = { git = "https://tangled.org/sachy.dev/sachy-embed-core", package = "sachy-fmt", features = [ - "defmt", -] } +sachy-mdns = { git = "https://tangled.org/sachy.dev/sachy-embed-core", package = "sachy-mdns" } +sachy-fmt = { git = "https://tangled.org/sachy.dev/sachy-embed-core", package = "sachy-fmt" } -embassy-embedded-hal = { version = "0.5.0", features = ["defmt"] } -embassy-sync = { version = "0.7.2", features = ["defmt"] } +embassy-embedded-hal = { version = "0.5.0" } +embassy-sync = { version = "0.7.2" } embassy-executor = { version = "0.9.0", features = [ "arch-cortex-m", "executor-thread", "executor-interrupt", - "defmt", -] } -embassy-time = { version = "0.5.0", features = [ - "defmt", - "defmt-timestamp-uptime", ] } +embassy-time = { version = "0.5.0" } embassy-rp = { version = "0.9.0", features = [ - "defmt", "unstable-pac", "time-driver", "critical-section-impl", @@ -61,7 +69,6 @@ embassy-rp = { version = "0.9.0", features = [ ] } embassy-futures = { version = "0.1.2" } embassy-net = { version = "0.7.1", features = [ - "defmt", "icmp", "udp", "tcp", @@ -75,8 +82,8 @@ embassy-net = { version = "0.7.1", features = [ "medium-ip", "medium-ethernet", ] } -cyw43 = { version = "0.6.0", features = ["defmt", "firmware-logs"] } -cyw43-pio = { version = "0.9.0", features = ["defmt"] } +cyw43 = { version = "0.6.0", features = ["firmware-logs"] } +cyw43-pio = { version = "0.9.0" } [build-dependencies] sachy-config = { git = "https://tangled.org/sachy.dev/sachy-embed-core", package = "sachy-config" } diff --git a/src/allocator.rs b/src/allocator.rs index 4a7551a..3e2ea7a 100644 --- a/src/allocator.rs +++ b/src/allocator.rs @@ -20,7 +20,8 @@ pub struct PicoHeap { block: ConstStaticCell<[u8; MEMORY_SIZE]>, } -#[derive(Debug, defmt::Format)] +#[derive(Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum HeapError { MemoryBlockTooSmall, BlockAlreadyTaken, diff --git a/src/main.rs b/src/main.rs index 98651f9..6190ee1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,6 +15,8 @@ mod state; mod updates; mod utils; mod wifi; +#[cfg(not(feature = "defmt"))] +mod panic_handler; use crate::{allocator::PicoHeap, rtc::GlobalRtc}; use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi}; @@ -33,6 +35,7 @@ use static_cell::{ConstStaticCell, StaticCell}; extern crate alloc; +#[cfg(feature = "defmt")] use {defmt_rtt as _, panic_probe as _}; mod constants { diff --git a/src/panic_handler.rs b/src/panic_handler.rs new file mode 100644 index 0000000..0b7cddf --- /dev/null +++ b/src/panic_handler.rs @@ -0,0 +1,15 @@ +use core::{ + panic::PanicInfo, + sync::atomic::{Ordering, compiler_fence}, +}; + +#[cfg(not(feature = "defmt"))] +#[inline(never)] +#[panic_handler] +fn panic_reset(_info: &PanicInfo) -> ! { + cortex_m::interrupt::disable(); + + // Halt execution and reset the chip. + compiler_fence(Ordering::SeqCst); + cortex_m::peripheral::SCB::sys_reset(); +} -- 2.51.2