From e1f740caba56b47b685bc8844c5b331f7dcb2b5a Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Thu, 8 Apr 2021 14:16:26 +0200 Subject: [PATCH] wip: move beginner to defmt --- beginner/apps/.cargo/config.toml | 1 + beginner/apps/Cargo.toml | 3 +-- beginner/apps/src/bin/blinky.rs | 5 +++-- beginner/apps/src/bin/hello.rs | 4 ++-- beginner/apps/src/bin/led.rs | 3 ++- beginner/apps/src/bin/panic.rs | 6 ++++-- beginner/apps/src/bin/radio-puzzle-1.rs | 11 ++++++----- beginner/apps/src/bin/radio-puzzle-2.rs | 7 ++++--- beginner/apps/src/lib.rs | 17 +++++++++++++++++ 9 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 beginner/apps/src/lib.rs diff --git a/beginner/apps/.cargo/config.toml b/beginner/apps/.cargo/config.toml index be9aeec..b697f78 100644 --- a/beginner/apps/.cargo/config.toml +++ b/beginner/apps/.cargo/config.toml @@ -2,6 +2,7 @@ # (..) rustflags = [ "-C", "linker=flip-link", # adds stack overflow protection + "-C", "link-arg=-Tdefmt.x", # defmt support # (..) ] diff --git a/beginner/apps/Cargo.toml b/beginner/apps/Cargo.toml index f457b4f..83d9d28 100644 --- a/beginner/apps/Cargo.toml +++ b/beginner/apps/Cargo.toml @@ -10,7 +10,6 @@ cortex-m = "0.6.4" cortex-m-rt = "0.6.13" dk = { path = "../../boards/dk", features = ["beginner"] } heapless = "0.5.5" -panic-log = { path = "../../common/panic-log" } panic-probe = { version = "0.2.0", features = ["print-defmt"] } defmt = "0.2.0" defmt-rtt = "0.2.0" @@ -18,7 +17,7 @@ defmt-rtt = "0.2.0" # optimize code in both profiles [profile.dev] codegen-units = 1 -debug = 1 +debug = 2 debug-assertions = true # ! incremental = false lto = "fat" diff --git a/beginner/apps/src/bin/blinky.rs b/beginner/apps/src/bin/blinky.rs index 1548525..5ba6da6 100644 --- a/beginner/apps/src/bin/blinky.rs +++ b/beginner/apps/src/bin/blinky.rs @@ -4,7 +4,8 @@ use core::time::Duration; use cortex_m_rt::entry; -use panic_log as _; // panic handler +// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior +use apps as _; #[entry] fn main() -> ! { @@ -19,7 +20,7 @@ fn main() -> ! { for _ in 0..10 { led.toggle(); timer.wait(Duration::from_secs(1)); - log::info!("LED toggled at {:?}", dk::uptime()); + defmt::debug!("LED toggled at {:?}", dk::uptime()); } dk::exit() diff --git a/beginner/apps/src/bin/hello.rs b/beginner/apps/src/bin/hello.rs index e1ec04c..fbb5eba 100644 --- a/beginner/apps/src/bin/hello.rs +++ b/beginner/apps/src/bin/hello.rs @@ -3,8 +3,8 @@ use cortex_m::asm; use cortex_m_rt::entry; -use defmt_rtt as _; // global logger -use panic_probe as _; // the panicking behavior +// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior +use apps as _; #[entry] fn main() -> ! { diff --git a/beginner/apps/src/bin/led.rs b/beginner/apps/src/bin/led.rs index f037c3f..8b3aa61 100644 --- a/beginner/apps/src/bin/led.rs +++ b/beginner/apps/src/bin/led.rs @@ -3,7 +3,8 @@ use cortex_m::asm; use cortex_m_rt::entry; -use panic_log as _; // the panicking behavior +// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior +use apps as _; #[entry] fn main() -> ! { diff --git a/beginner/apps/src/bin/panic.rs b/beginner/apps/src/bin/panic.rs index eb86bc7..b3c139d 100644 --- a/beginner/apps/src/bin/panic.rs +++ b/beginner/apps/src/bin/panic.rs @@ -3,7 +3,9 @@ use cortex_m::asm; use cortex_m_rt::entry; -use panic_log as _; // the panicking behavior +// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior +use apps as _; + #[entry] fn main() -> ! { @@ -28,7 +30,7 @@ fn bar() { let array = [0, 1, 2]; let x = array[i]; // out of bounds access - log::info!("{}", x); + defmt::info!("{}", x); } fn index() -> usize { diff --git a/beginner/apps/src/bin/radio-puzzle-1.rs b/beginner/apps/src/bin/radio-puzzle-1.rs index 3c9ad08..5c798b3 100644 --- a/beginner/apps/src/bin/radio-puzzle-1.rs +++ b/beginner/apps/src/bin/radio-puzzle-1.rs @@ -4,7 +4,8 @@ use cortex_m_rt::entry; use dk::ieee802154::{Channel, Packet}; -use panic_log as _; // the panicking behavior +// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior +use apps as _; const TEN_MS: u32 = 10_000; @@ -36,15 +37,15 @@ fn main() -> ! { if packet.len() == 1 { let destination = packet[0]; - log::info!("{} -> {}", source, destination); + defmt::info!("{} -> {}", source, destination); // or cast to `char` for a more readable output - log::info!("{:?} -> {:?}", source as char, destination as char); + defmt::info!("{:?} -> {:?}", source as char, destination as char); } else { - log::error!("response packet was not a single byte"); + defmt::error!("response packet was not a single byte"); dk::exit() } } else { - log::error!("no response or response packet was corrupted"); + defmt::error!("no response or response packet was corrupted"); dk::exit() } diff --git a/beginner/apps/src/bin/radio-puzzle-2.rs b/beginner/apps/src/bin/radio-puzzle-2.rs index cfb8d53..083ed35 100644 --- a/beginner/apps/src/bin/radio-puzzle-2.rs +++ b/beginner/apps/src/bin/radio-puzzle-2.rs @@ -7,7 +7,8 @@ use cortex_m_rt::entry; // lookup performance when the dictionary contains a large number of items but performance is // not important for this exercise use heapless::{consts, LinearMap}; -use panic_log as _; // the panicking behavior +// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior +use apps as _; #[entry] fn main() -> ! { @@ -25,9 +26,9 @@ fn main() -> ! { let key = b'A'; let value = dict[&key]; // the key needs to be passed by reference - log::info!("{} -> {}", key, value); + defmt::info!("{} -> {}", key, value); // more readable - log::info!("{:?} -> {:?}", key as char, value as char); + defmt::info!("{:?} -> {:?}", key as char, value as char); // TODO try another insertion // TODO try looking up a key not contained in the dictionary diff --git a/beginner/apps/src/lib.rs b/beginner/apps/src/lib.rs new file mode 100644 index 0000000..7480423 --- /dev/null +++ b/beginner/apps/src/lib.rs @@ -0,0 +1,17 @@ +#![no_std] + +use panic_probe as _; + +// 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() +} + +/// Terminates the application and makes `probe-run` exit with exit-code = 0 +pub fn exit() -> ! { + loop { + cortex_m::asm::bkpt(); + } +}