From af4b9e3b769386c89a758fc7d0677a1781165e9e Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Thu, 8 Apr 2021 13:59:01 +0200 Subject: [PATCH] k: move to defmt --- boards/dk/Cargo.toml | 20 ++++++++++++-- boards/dk/src/lib.rs | 62 ++++++++++---------------------------------- 2 files changed, 31 insertions(+), 51 deletions(-) diff --git a/boards/dk/Cargo.toml b/boards/dk/Cargo.toml index e844710..4008bce 100644 --- a/boards/dk/Cargo.toml +++ b/boards/dk/Cargo.toml @@ -11,8 +11,24 @@ cortex-m-rt = "0.6.13" embedded-hal = "0.2.3" hal = { package = "nrf52840-hal", version = "0.12.1" } log = "0.4.8" -rtt-target = { version = "0.2.0", features = ["cortex-m"] } +defmt = "0.2.1" +defmt-rtt = "0.2.0" +panic-probe = { version = "0.2.0", features = ["print-defmt" ] } [features] beginner = [] -advanced = [] \ No newline at end of file +advanced = [] + +# defmt logging levels +default = [ + "defmt-debug", + # "dependency-a/defmt-trace", +] + +# do NOT modify these features +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] diff --git a/boards/dk/src/lib.rs b/boards/dk/src/lib.rs index aaa1005..62b410b 100644 --- a/boards/dk/src/lib.rs +++ b/boards/dk/src/lib.rs @@ -21,10 +21,11 @@ use hal::{ rtc::{Rtc, RtcInterrupt}, timer::OneShot, }; -use log::{LevelFilter, Log}; -use rtt_target::rprintln; + +use defmt; #[cfg(any(feature = "beginner", feature = "advanced"))] -use rtt_target::rtt_init_print; +use defmt_rtt as _; // global logger + #[cfg(feature = "advanced")] use crate::{ @@ -79,7 +80,7 @@ pub struct Led { impl Led { /// Turns on the LED pub fn on(&mut self) { - log::trace!( + defmt::trace!( "setting P{}.{} low (LED on)", if self.inner.port() == Port::Port1 { '1' @@ -137,7 +138,7 @@ pub struct Timer { impl Timer { /// Blocks program execution for at least the specified `duration` pub fn wait(&mut self, duration: Duration) { - log::trace!("blocking for {:?} ...", duration); + defmt::trace!("blocking for {:?} ...", duration); // 1 cycle = 1 microsecond const NANOS_IN_ONE_MICRO: u32 = 1_000; @@ -164,7 +165,7 @@ impl Timer { self.inner.delay(cycles) } - log::trace!("... DONE"); + defmt::trace!("... DONE"); } } @@ -195,21 +196,7 @@ pub fn init() -> Result { Clocks, > = None; - // NOTE this must be executed as early as possible or the tool will timeout - // NOTE the unsafety of this macro is incorrect; it must be run at most once - #[cfg(feature = "beginner")] - rtt_init_print!(BlockIfFull, 16384); - #[cfg(feature = "advanced")] - rtt_init_print!(NoBlockSkip, 16384); - - log::set_logger(&Logger).unwrap(); - - // if not configured in the application we default to the `Info` level - if log::max_level() == LevelFilter::Off { - log::set_max_level(LevelFilter::Info) - } - - log::debug!("Initializing the board"); + defmt::debug!("Initializing the board"); let clocks = Clocks::new(periph.CLOCK); let clocks = clocks.enable_ext_hfosc(); @@ -220,7 +207,7 @@ pub fn init() -> Result { #[cfg(feature = "beginner")] let clocks = unsafe { CLOCKS.get_or_insert(_clocks) }; - log::debug!("Clocks configured"); + defmt::debug!("Clocks configured"); let mut rtc = Rtc::new(periph.RTC0, 0).unwrap(); rtc.enable_interrupt(RtcInterrupt::Overflow, None); @@ -239,7 +226,7 @@ pub fn init() -> Result { // mechanism) unsafe { NVIC::unmask(Interrupt::RTC0) }; - log::debug!("RTC started"); + defmt::debug!("RTC started"); let pins = p0::Parts::new(periph.P0); @@ -249,7 +236,7 @@ pub fn init() -> Result { let _3 = pins.p0_15.degrade().into_push_pull_output(Level::High); let _4 = pins.p0_16.degrade().into_push_pull_output(Level::High); - log::debug!("I/O pins have been configured for digital output"); + defmt::debug!("I/O pins have been configured for digital output"); let timer = hal::Timer::new(periph.TIMER0); @@ -259,7 +246,7 @@ pub fn init() -> Result { // set TX power to its maximum value radio.set_txpower(ieee802154::TxPower::Pos8dBm); - log::debug!("Radio initialized and configured with TX power set to the maximum value"); + defmt::debug!("Radio initialized and configured with TX power set to the maximum value"); radio }; @@ -285,29 +272,6 @@ pub fn init() -> Result { } } -struct Logger; - -impl Log for Logger { - fn enabled(&self, metadata: &log::Metadata) -> bool { - metadata.level() <= log::STATIC_MAX_LEVEL - } - - fn log(&self, record: &log::Record) { - if !self.enabled(record.metadata()) { - return; - } - - rprintln!( - "{}:{} -- {}", - record.level(), - record.target(), - record.args() - ); - } - - fn flush(&self) {} -} - // Counter of OVERFLOW events -- an OVERFLOW occurs every (1<<24) ticks static OVERFLOWS: AtomicU32 = AtomicU32::new(0); @@ -324,7 +288,7 @@ fn RTC0() { /// Exits the application and prints a backtrace when the program is executed through the `probe-run` /// Cargo runner pub fn exit() -> ! { - log::info!("`dk::exit() called; exiting ...`"); + defmt::info!("`dk::exit() called; exiting ...`"); // force any pending memory operation to complete before the BKPT instruction that follows atomic::compiler_fence(Ordering::SeqCst); loop {