From 1747fea888119a3a9f1286c11ef6510e74740a3d Mon Sep 17 00:00:00 2001 From: Gabriel Dobato Date: Thu, 16 May 2024 07:51:59 +0200 Subject: [PATCH] Use defmt --- .cargo/config.toml | 11 +++++++++-- Cargo.toml | 4 +++- Embed.toml | 4 +++- examples/blinky.rs | 15 ++++----------- examples/usb_echo.rs | 13 ++++++------- src/board.rs | 5 ----- src/lib.rs | 6 +++--- src/panic.rs | 13 ------------- 8 files changed, 28 insertions(+), 43 deletions(-) delete mode 100644 src/panic.rs diff --git a/.cargo/config.toml b/.cargo/config.toml index a58fd5c..c9e03b8 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -15,6 +15,13 @@ usb_echo-bin = "oe usb_echo --release -- -O binary target/thumbv7em-none-eabihf/ target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU) rustflags = [ - # Use LLD as the default linker - "-C", "link-arg=-Tlink.x", + "-C", + "link-arg=-Tlink.x", + "-C", + "link-arg=--nmagic", + "-C", + "link-arg=-Tdefmt.x", ] + +[env] +DEFMT_LOG = "debug" diff --git a/Cargo.toml b/Cargo.toml index df65cc3..f075d0b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,9 @@ stm32h7xx-hal = { version = "0.16.0", features = [ "rt", "usb_hs", ] } -rtt-target = { version = "0.4.0" } +defmt = "=0.3.7" +defmt-rtt = "0.4.1" +panic-probe = { version = "0.3", features = ["print-defmt"] } [dev-dependencies] rtic = { version = "2.1.1", features = ["thumbv7-backend"] } diff --git a/Embed.toml b/Embed.toml index 994e5bb..b750015 100644 --- a/Embed.toml +++ b/Embed.toml @@ -15,4 +15,6 @@ enabled = true halt_afterwards = false [default.rtt] -enabled = true \ No newline at end of file +enabled = true + +channels = [{ up = 0, down = 0, format = "Defmt" }] diff --git a/examples/blinky.rs b/examples/blinky.rs index e57638f..08261c5 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -6,10 +6,10 @@ #![no_std] #![no_main] -#[allow(unused_imports)] +use defmt::info; use portenta_h7::{ board::{self, Board}, - led, log, log_init, + led, }; use rtic::app; use rtic_monotonics::systick::*; @@ -30,8 +30,7 @@ mod app { #[init] fn init(cx: init::Context) -> (Shared, Local) { - log_init!(); - + info!("Init"); let systick_mono_token = rtic_monotonics::create_systick_token!(); Systick::start( cx.core.SYST, @@ -48,7 +47,7 @@ mod app { } = Board::take(); #[cfg(debug_assertions)] - log!("spawning tasks"); + info!("spawning tasks"); let _ = blink_led_red::spawn(); let _ = blink_led_green::spawn(); let _ = blink_led_blue::spawn(); @@ -66,8 +65,6 @@ mod app { #[task(local = [led_red])] async fn blink_led_red(cx: blink_led_red::Context) { loop { - #[cfg(debug_assertions)] - log!("toggling {:?}", cx.local.led_red); cx.local.led_red.toggle(); Systick::delay(500.millis()).await; } @@ -76,8 +73,6 @@ mod app { #[task(local = [led_green])] async fn blink_led_green(cx: blink_led_green::Context) { loop { - #[cfg(debug_assertions)] - log!("toggling {:?}", cx.local.led_green); cx.local.led_green.toggle(); Systick::delay(1000.millis()).await; } @@ -86,8 +81,6 @@ mod app { #[task(local = [led_blue])] async fn blink_led_blue(cx: blink_led_blue::Context) { loop { - #[cfg(debug_assertions)] - log!("toggling {:?}", cx.local.led_blue); cx.local.led_blue.toggle(); Systick::delay(1000.millis()).await; } diff --git a/examples/usb_echo.rs b/examples/usb_echo.rs index e81fcf4..a88903e 100644 --- a/examples/usb_echo.rs +++ b/examples/usb_echo.rs @@ -6,10 +6,10 @@ #![no_std] #![no_main] +use defmt::{error, info}; use portenta_h7::{ board::{self, Board, UsbBus, USB}, led::{self, Led}, - log, log_init, }; use rtic::app; use rtic_monotonics::systick::*; @@ -39,7 +39,7 @@ mod app { #[init] fn init(cx: init::Context) -> (Shared, Local) { - log_init!(); + info!("Init"); let systick_mono_token = rtic_monotonics::create_systick_token!(); @@ -92,8 +92,7 @@ mod app { // Read from reception fifo match usb_serial_port.read_packet(&mut app_buff) { Ok(cnt) if cnt > 0 => { - #[cfg(debug_assertions)] - log!( + info!( "Received {} bytes: {}", cnt, core::str::from_utf8(&app_buff[..cnt]).unwrap_or("not valid") @@ -102,7 +101,7 @@ mod app { match usb_serial_port.write_packet(&app_buff[..cnt]) { Ok(_) => (), Err(err) => { - log!("Error in transmission: {:?}", err) + error!("Error in transmission: {:?}", err as u8) } } } @@ -114,14 +113,14 @@ mod app { match usb_dev.state() { // Transition to enumeration UsbDeviceState::Configured if previous_state == UsbDeviceState::Addressed => { - log!("Enumeration completed"); + info!("Enumeration completed"); cx.local.led_blue.on(); } // Already enumerated UsbDeviceState::Configured => {} // Enumeration lost _ if previous_state == UsbDeviceState::Configured => { - log!("Enumeration lost"); + info!("Enumeration lost"); cx.local.led_blue.off(); } _ => (), diff --git a/src/board.rs b/src/board.rs index 54afa25..84caefd 100644 --- a/src/board.rs +++ b/src/board.rs @@ -1,7 +1,5 @@ //! board -#[allow(unused)] -use crate::log; use crate::{hal, led, sys}; use core::sync::atomic::{AtomicBool, Ordering}; pub use hal::usb_hs::{UsbBus, USB1_ULPI as USB}; @@ -24,9 +22,6 @@ impl Board { } fn setup() -> Self { - #[cfg(debug_assertions)] - log!("Board init"); - // Reset previous configuration and enable external oscillator as HSE source (25 MHz) sys::Clk::new().reset().enable_ext_clock(); let dp = pac::Peripherals::take().unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 4a9e0f4..a3d775f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,9 +3,9 @@ pub mod board; pub mod interrupt; pub mod led; -pub mod panic; mod sys; pub use cortex_m_rt::entry; -#[allow(unused_imports)] -pub use rtt_target::{rprintln as log, rtt_init_print as log_init}; +#[allow(unused)] +use defmt_rtt as _; +use panic_probe as _; pub use stm32h7xx_hal as hal; diff --git a/src/panic.rs b/src/panic.rs deleted file mode 100644 index d1c34f5..0000000 --- a/src/panic.rs +++ /dev/null @@ -1,13 +0,0 @@ -//! panic - -use core::panic::PanicInfo; -#[cfg(debug_assertions)] -use rtt_target::rprintln as log; - -#[panic_handler] -#[allow(unused_variables)] -fn panic(info: &PanicInfo) -> ! { - #[cfg(debug_assertions)] - log!("{}", info); - loop {} -}