Skip to content

Commit badcb4c

Browse files
author
Henrik Snöman
committed
Add defmt
1 parent 864750d commit badcb4c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ runner = 'probe-rs run --connect-under-reset'
33
rustflags = [
44
# LLD (shipped with the Rust toolchain) is used as the default linker
55
"-C", "link-arg=-Tlink.x",
6+
"-C", "link-arg=-Tdefmt.x",
67
]
78

89
[build]

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ log = { version = "0.4.20", optional = true}
6868
[dev-dependencies]
6969
log = { version = "0.4.20"}
7070
cortex-m-rt = "0.7.3"
71+
defmt-rtt = { version = "0.4.0" }
7172
panic-halt = "0.2.0"
7273
panic-rtt-target = { version = "0.1.0", features = ["cortex-m"] }
7374
cfg-if = "1.0.0"
@@ -76,6 +77,7 @@ lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
7677
cortex-m-log = { version = "0.8.0", features = ["itm", "semihosting", "log-integration"] }
7778
cortex-m-semihosting = "0.5.0"
7879
panic-itm = { version = "~0.4.1" }
80+
panic-probe = "0.3.2"
7981
panic-semihosting = "0.6"
8082

8183
[profile.release]
@@ -86,7 +88,6 @@ opt-level = "s" # optimize for binary size
8688

8789
[[example]]
8890
name = "blinky"
89-
required-features = ["stm32h503"]
9091

9192
[[example]]
9293
name = "i2c"

examples/blinky.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use cortex_m_rt::entry;
88
use embedded_hal::delay::DelayNs;
99
use fugit::SecsDurationU32;
1010
use stm32h5xx_hal::{delay::Delay, pac, prelude::*};
11+
use utilities::logger::info;
1112

1213
#[entry]
1314
fn main() -> ! {
@@ -31,10 +32,10 @@ fn main() -> ! {
3132

3233
loop {
3334
led.set_low();
34-
log::info!("Off");
35+
info!("Off");
3536
delay.delay_ms(duration);
3637
led.set_high();
37-
log::info!("On");
38+
info!("On");
3839
delay.delay_ms(duration);
3940
}
4041
}

examples/utilities/logger.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
#![cfg_attr(feature = "log-itm", allow(unsafe_code))]
22

3+
cfg_if::cfg_if! {
4+
if #[cfg(feature = "defmt")] {
5+
#[allow(unused_imports)]
6+
pub use defmt::{info, trace, warn, debug, error};
7+
8+
} else {
9+
#[allow(unused_imports)]
10+
pub use log::{info, trace, warn, debug, error};
11+
}
12+
}
13+
314
cfg_if::cfg_if! {
415
if #[cfg(any(feature = "log-itm"))] {
516
use panic_itm as _;
@@ -32,6 +43,17 @@ cfg_if::cfg_if! {
3243
}
3344

3445
}
46+
else if #[cfg(feature = "defmt")] {
47+
use defmt_rtt as _; // global logger
48+
use panic_probe as _;
49+
#[allow(unused_imports)]
50+
pub use defmt::Logger;
51+
#[allow(unused_imports)]
52+
pub use defmt::println;
53+
54+
#[allow(dead_code)]
55+
pub fn init() {}
56+
}
3557
else if #[cfg(any(feature = "log-rtt"))] {
3658
use panic_rtt_target as _;
3759

0 commit comments

Comments
 (0)