From b5f4e771867805374c0c099b4afccdd996412c47 Mon Sep 17 00:00:00 2001 From: cscherr Date: Mon, 28 Apr 2025 11:43:43 +0200 Subject: [PATCH] feat: logging with defmt --- .cargo/config.toml | 7 ++- Cargo.lock | 119 ++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 5 +- examples/logging.rs | 54 ++++++++++++++++++++ 4 files changed, 183 insertions(+), 2 deletions(-) create mode 100755 examples/logging.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index ef1fea6..a210e52 100755 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -3,7 +3,12 @@ target = "thumbv6m-none-eabi" [target.thumbv6m-none-eabi] runner = 'probe-rs run --chip STM32L053R8' -rustflags = ["-Clink-args=-Tlink.x"] + +rustflags = ["-C", "link-arg=-Tlink.x", "-C", "link-arg=-Tdefmt.x"] + [alias] cflash = "flash --chip STM32L053R8" + +[env] +DEFMT_LOG = "trace" # sets the log level to trace diff --git a/Cargo.lock b/Cargo.lock index e434c51..e3cea80 100755 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,12 @@ version = "0.13.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "byteorder" version = "1.5.0" @@ -67,6 +73,7 @@ checksum = "8ec610d8f49840a5b376c69663b6369e71f4b34484b9b2eb29fb918d92516cb9" dependencies = [ "bare-metal 0.2.5", "bitfield", + "critical-section", "embedded-hal", "volatile-register", ] @@ -91,6 +98,63 @@ dependencies = [ "syn", ] +[[package]] +name = "critical-section" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "790eea4361631c5e7d22598ecd5723ff611904e3344ce8720784c93e3d83d40b" + +[[package]] +name = "defmt" +version = "0.3.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0963443817029b2024136fc4dd07a5107eb8f977eaf18fcd1fdeb11306b64ad" +dependencies = [ + "defmt 1.0.1", +] + +[[package]] +name = "defmt" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "548d977b6da32fa1d1fda2876453da1e7df63ad0304c8b3dae4dbe7b96f39b78" +dependencies = [ + "bitflags", + "defmt-macros", +] + +[[package]] +name = "defmt-macros" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3d4fc12a85bcf441cfe44344c4b72d58493178ce635338a3f3b78943aceb258e" +dependencies = [ + "defmt-parser", + "proc-macro-error2", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "defmt-parser" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10d60334b3b2e7c9d91ef8150abfb6fa4c1c39ebbcf4a81c2e346aad939fee3e" +dependencies = [ + "thiserror", +] + +[[package]] +name = "defmt-rtt" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2cac3b8a5644a9e02b75085ebad3b6deafdbdbdec04bb25086523828aa4dfd1" +dependencies = [ + "critical-section", + "defmt 1.0.1", +] + [[package]] name = "embedded-hal" version = "0.2.7" @@ -159,9 +223,12 @@ version = "0.1.0" dependencies = [ "cortex-m", "cortex-m-rt", + "defmt 1.0.1", + "defmt-rtt", "hd44780-driver", "heapless", "panic-halt", + "panic-probe", "stm32l0xx-hal", ] @@ -233,6 +300,38 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a513e167849a384b7f9b746e517604398518590a9142f4846a32e3c2a4de7b11" +[[package]] +name = "panic-probe" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4047d9235d1423d66cc97da7d07eddb54d4f154d6c13805c6d0793956f4f25b0" +dependencies = [ + "cortex-m", + "defmt 0.3.100", +] + +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.95" @@ -331,6 +430,26 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "unicode-ident" version = "1.0.18" diff --git a/Cargo.toml b/Cargo.toml index 8d4708e..38a3552 100755 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,11 @@ hal = { package = "stm32l0xx-hal", version = "0.10.0", features = [ "mcu-STM32L053R8Tx", # specifically STM32L053R8T6 "rt", ] } +cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] } cortex-m-rt = "0.7.5" panic-halt = "1.0.0" -cortex-m = "0.7.7" hd44780-driver = "0.4.0" heapless = "0.8.0" +defmt = "1.0.1" +defmt-rtt = "1.0.0" +panic-probe = { version = "0.3", features = ["print-defmt"] } diff --git a/examples/logging.rs b/examples/logging.rs new file mode 100755 index 0000000..de4ede1 --- /dev/null +++ b/examples/logging.rs @@ -0,0 +1,54 @@ +#![no_main] +#![no_std] + +use defmt::{debug, error, info, println, trace, warn}; +use panic_probe as _; + +use defmt_rtt as _; // global logger + +use cortex_m_rt::entry; +use hal::{pac, prelude::*, rcc::Config}; + +// same panicking *behavior* as `panic-probe` but doesn't print a panic message +// this prevents the panic message being printed *twice* when `defmt::panic` is invoked +#[defmt::panic_handler] +fn panic() -> ! { + cortex_m::asm::udf() +} + +#[entry] +fn main() -> ! { + let dp = pac::Peripherals::take().unwrap(); + let cp = cortex_m::Peripherals::take().unwrap(); + + // Configure the clock. + let mut rcc = dp.RCC.freeze(Config::hsi16()); + + // Acquire the GPIOA peripheral. This also enables the clock for GPIOA in + // the RCC register. + let gpioa = dp.GPIOA.split(&mut rcc); + + // Configure PA5 as output. + let mut led = gpioa.pa5.into_push_pull_output(); + + // Get the delay provider. + let mut delay = cp.SYST.delay(rcc.clocks); + + #[allow(clippy::never_loop)] + loop { + println!("Hello World!"); + trace!("trace log"); + debug!("debug log"); + info!("info log"); + warn!("warn log"); + error!("error log"); + + led.set_high().unwrap(); + delay.delay_ms(500_u16); + + led.set_low().unwrap(); + delay.delay_ms(500_u16); + + panic!("This will use the defmt panic handler too"); + } +}