Skip to content

Commit 8b57030

Browse files
authored
Merge pull request #73 from usbalbin/fix_defmt_examples
Fix defmt examples
2 parents 7acb0b2 + 65d7b32 commit 8b57030

File tree

6 files changed

+55
-61
lines changed

6 files changed

+55
-61
lines changed

Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ lazy_static = { version = "1.4", features = ["spin_no_std"] }
6868
log = "0.4.11"
6969
cortex-m-log = { version = "0.7", features = ["log-integration"] }
7070
cfg-if = "0.1.10"
71-
rtt-target = { version = "0.3.0", features = ["cortex-m"] }
72-
panic-rtt-target = { version = "0.1.1", features = ["cortex-m"] }
7371
mpu6050 = "0.1.4"
7472
bme680 = "0.6.0"
7573
embedded-sdmmc = "0.3.0"

examples/blinky.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ use hal::stm32;
88
use stm32g4xx_hal as hal;
99

1010
use cortex_m_rt::entry;
11-
use log::info;
1211

1312
#[macro_use]
1413
mod utils;
1514

15+
use utils::logger::info;
16+
1617
#[entry]
1718
fn main() -> ! {
1819
utils::logger::init();
@@ -27,11 +28,12 @@ fn main() -> ! {
2728

2829
loop {
2930
info!("Set Led low");
30-
for _ in 0..100_000 {
31+
for _ in 0..10_000_000 {
3132
led.set_low().unwrap();
3233
}
34+
3335
info!("Set Led High");
34-
for _ in 0..100_000 {
36+
for _ in 0..10_000_000 {
3537
led.set_high().unwrap();
3638
}
3739
}

examples/button.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#![no_main]
22
#![no_std]
33

4-
use panic_rtt_target as _;
5-
64
use stm32g4xx_hal::{
75
//delay::{DelayExt, SYSTDelayExt},
86
gpio::{gpioc, ExtiPin, GpioExt, Input, PullDown, SignalEdge},
@@ -18,24 +16,27 @@ use cortex_m::{asm::wfi, interrupt::Mutex};
1816
use cortex_m_rt::entry;
1917
use embedded_hal::digital::v2::OutputPin;
2018

21-
use rtt_target::{rprintln, rtt_init_print};
22-
2319
type ButtonPin = gpioc::PC13<Input<PullDown>>;
2420

2521
// Make LED pin globally available
2622
static G_BUTTON: Mutex<RefCell<Option<ButtonPin>>> = Mutex::new(RefCell::new(None));
2723
static G_LED_ON: AtomicBool = AtomicBool::new(true);
2824

25+
#[macro_use]
26+
mod utils;
27+
28+
use utils::logger::println;
29+
2930
// Define an interupt handler, i.e. function to call when interrupt occurs.
3031
// This specific interrupt will "trip" when the button is pressed
3132
#[interrupt]
3233
fn EXTI15_10() {
3334
static mut BUTTON: Option<ButtonPin> = None;
3435

35-
rprintln!("Got IRQ!");
36+
println!("Got IRQ!");
3637

3738
let button = BUTTON.get_or_insert_with(|| {
38-
rprintln!("Transfer Button into EXTI interrupt");
39+
println!("Transfer Button into EXTI interrupt");
3940
cortex_m::interrupt::free(|cs| {
4041
// Move LED pin here, leaving a None in its place
4142
G_BUTTON.borrow(cs).replace(None).unwrap()
@@ -49,45 +50,43 @@ fn EXTI15_10() {
4950

5051
#[entry]
5152
fn main() -> ! {
52-
rtt_init_print!();
53-
5453
let mut dp = stm32::Peripherals::take().expect("cannot take peripherals");
5554
let mut rcc = dp.RCC.constrain();
5655
let mut syscfg = dp.SYSCFG.constrain();
5756

58-
rprintln!("Led Init");
57+
println!("Led Init");
5958
// Configure PA5 pin to blink LED
6059
let gpioa = dp.GPIOA.split(&mut rcc);
6160
let mut led = gpioa.pa5.into_push_pull_output();
6261

63-
rprintln!("Button Init");
62+
println!("Button Init");
6463
let gpioc = dp.GPIOC.split(&mut rcc);
6564
let mut button = gpioc.pc13.into_pull_down_input();
6665
button.make_interrupt_source(&mut syscfg);
6766
button.trigger_on_edge(&mut dp.EXTI, SignalEdge::Rising);
6867
button.enable_interrupt(&mut dp.EXTI);
6968

70-
rprintln!("Set Button into Global Mutex");
69+
println!("Set Button into Global Mutex");
7170
// Move the pin into our global storage
7271
cortex_m::interrupt::free(|cs| *G_BUTTON.borrow(cs).borrow_mut() = Some(button));
7372

74-
rprintln!("Enable EXTI Interrupt");
73+
println!("Enable EXTI Interrupt");
7574
unsafe {
7675
cortex_m::peripheral::NVIC::unmask(Interrupt::EXTI15_10);
7776
}
7877

7978
//let mut delay = cp.SYST.delay(&rcc.clocks);
8079

81-
rprintln!("Start Loop");
80+
println!("Start Loop");
8281
loop {
8382
wfi();
84-
rprintln!("Check");
83+
println!("Check");
8584

8685
if G_LED_ON.load(Ordering::Relaxed) {
87-
rprintln!("Turn Led On!");
86+
println!("Turn Led On!");
8887
led.set_high().unwrap();
8988
} else {
90-
rprintln!("Turn Led Off!");
89+
println!("Turn Led Off!");
9190
led.set_low().unwrap();
9291
}
9392
}

examples/hello.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@
55

66
extern crate cortex_m;
77
extern crate cortex_m_rt as rt;
8-
extern crate panic_semihosting;
98
extern crate stm32g4xx_hal as hal;
109

11-
use cortex_m_semihosting::hprintln;
1210
use rt::entry;
1311

12+
#[macro_use]
13+
mod utils;
14+
15+
use utils::logger::println;
16+
1417
#[entry]
1518
fn main() -> ! {
16-
hprintln!("Hello, STM32G4!").unwrap();
19+
let _ = println!("Hello, STM32G4!");
1720

1821
#[allow(clippy::empty_loop)]
1922
loop {}

examples/panic.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#![no_main]
22
#![no_std]
33

4-
use defmt_rtt as _;
5-
6-
use panic_probe as _;
7-
84
use stm32g4 as _;
95

10-
use defmt::Format;
6+
#[macro_use]
7+
mod utils;
8+
9+
use utils::logger;
1110

11+
#[cfg(feature = "defmt")]
1212
#[defmt::panic_handler]
1313
fn panic() -> ! {
1414
cortex_m::asm::udf()
@@ -22,8 +22,7 @@ pub fn exit() -> ! {
2222

2323
#[cortex_m_rt::entry]
2424
fn main() -> ! {
25-
defmt::info!("main");
25+
logger::info!("main");
2626

2727
panic!("Something bad");
28-
// defmt::panic!()
2928
}

examples/utils/logger.rs

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
#![allow(unsafe_code)]
2+
cfg_if::cfg_if! {
3+
if #[cfg(all(feature = "log-rtt", feature = "defmt"))] {
4+
pub use defmt::{info, trace, warn, debug, error};
5+
pub use defmt::println as println;
6+
} else {
7+
pub use log::{info, trace, warn, debug, error};
8+
pub use cortex_m_semihosting::hprintln as println;
9+
}
10+
}
11+
212
cfg_if::cfg_if! {
313
if #[cfg(any(feature = "log-itm"))] {
414
use panic_itm as _;
@@ -28,42 +38,22 @@ cfg_if::cfg_if! {
2838
};
2939
}
3040

41+
#[allow(dead_code)]
3142
pub fn init() {
3243
cortex_m_log::log::init(&LOGGER).unwrap();
3344
}
3445

3546
}
36-
else if #[cfg(any(feature = "log-rtt"))] {
37-
use panic_rtt_target as _;
38-
39-
use log::{Level, Metadata, Record, LevelFilter};
40-
use rtt_target::{rprintln, rtt_init_print};
47+
else if #[cfg(all(feature = "log-rtt", feature = "defmt"))] {
48+
use defmt_rtt as _; // global logger
49+
use panic_probe as _;
50+
pub use defmt::Logger;
4151

42-
pub struct Logger {
43-
level: Level,
44-
}
45-
46-
static LOGGER: Logger = Logger {
47-
level: Level::Info,
48-
};
49-
50-
pub fn init() {
51-
rtt_init_print!();
52-
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Info)).unwrap();
53-
}
54-
55-
impl log::Log for Logger {
56-
fn enabled(&self, metadata: &Metadata) -> bool {
57-
metadata.level() <= self.level
58-
59-
}
60-
61-
fn log(&self, record: &Record) {
62-
rprintln!("{} - {}", record.level(), record.args());
63-
}
64-
65-
fn flush(&self) {}
66-
}
52+
#[allow(dead_code)]
53+
pub fn init() {}
54+
}
55+
else if #[cfg(all(feature = "log-rtt", not(feature = "defmt")))] {
56+
// TODO
6757
}
6858
else if #[cfg(any(feature = "log-semihost"))] {
6959
use panic_semihosting as _;
@@ -84,12 +74,15 @@ cfg_if::cfg_if! {
8474
};
8575
}
8676

77+
#[allow(dead_code)]
8778
pub fn init() {
8879
cortex_m_log::log::init(&LOGGER).unwrap();
8980
}
9081
}
9182
else {
9283
use panic_halt as _;
84+
85+
#[allow(dead_code)]
9386
pub fn init() {}
9487
}
9588
}

0 commit comments

Comments
 (0)