Skip to content

Commit

Permalink
Merge pull request #73 from usbalbin/fix_defmt_examples
Browse files Browse the repository at this point in the history
Fix defmt examples
  • Loading branch information
no111u3 authored Nov 28, 2023
2 parents 7acb0b2 + 65d7b32 commit 8b57030
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ lazy_static = { version = "1.4", features = ["spin_no_std"] }
log = "0.4.11"
cortex-m-log = { version = "0.7", features = ["log-integration"] }
cfg-if = "0.1.10"
rtt-target = { version = "0.3.0", features = ["cortex-m"] }
panic-rtt-target = { version = "0.1.1", features = ["cortex-m"] }
mpu6050 = "0.1.4"
bme680 = "0.6.0"
embedded-sdmmc = "0.3.0"
Expand Down
8 changes: 5 additions & 3 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ use hal::stm32;
use stm32g4xx_hal as hal;

use cortex_m_rt::entry;
use log::info;

#[macro_use]
mod utils;

use utils::logger::info;

#[entry]
fn main() -> ! {
utils::logger::init();
Expand All @@ -27,11 +28,12 @@ fn main() -> ! {

loop {
info!("Set Led low");
for _ in 0..100_000 {
for _ in 0..10_000_000 {
led.set_low().unwrap();
}

info!("Set Led High");
for _ in 0..100_000 {
for _ in 0..10_000_000 {
led.set_high().unwrap();
}
}
Expand Down
31 changes: 15 additions & 16 deletions examples/button.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#![no_main]
#![no_std]

use panic_rtt_target as _;

use stm32g4xx_hal::{
//delay::{DelayExt, SYSTDelayExt},
gpio::{gpioc, ExtiPin, GpioExt, Input, PullDown, SignalEdge},
Expand All @@ -18,24 +16,27 @@ use cortex_m::{asm::wfi, interrupt::Mutex};
use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin;

use rtt_target::{rprintln, rtt_init_print};

type ButtonPin = gpioc::PC13<Input<PullDown>>;

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

#[macro_use]
mod utils;

use utils::logger::println;

// Define an interupt handler, i.e. function to call when interrupt occurs.
// This specific interrupt will "trip" when the button is pressed
#[interrupt]
fn EXTI15_10() {
static mut BUTTON: Option<ButtonPin> = None;

rprintln!("Got IRQ!");
println!("Got IRQ!");

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

#[entry]
fn main() -> ! {
rtt_init_print!();

let mut dp = stm32::Peripherals::take().expect("cannot take peripherals");
let mut rcc = dp.RCC.constrain();
let mut syscfg = dp.SYSCFG.constrain();

rprintln!("Led Init");
println!("Led Init");
// Configure PA5 pin to blink LED
let gpioa = dp.GPIOA.split(&mut rcc);
let mut led = gpioa.pa5.into_push_pull_output();

rprintln!("Button Init");
println!("Button Init");
let gpioc = dp.GPIOC.split(&mut rcc);
let mut button = gpioc.pc13.into_pull_down_input();
button.make_interrupt_source(&mut syscfg);
button.trigger_on_edge(&mut dp.EXTI, SignalEdge::Rising);
button.enable_interrupt(&mut dp.EXTI);

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

rprintln!("Enable EXTI Interrupt");
println!("Enable EXTI Interrupt");
unsafe {
cortex_m::peripheral::NVIC::unmask(Interrupt::EXTI15_10);
}

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

rprintln!("Start Loop");
println!("Start Loop");
loop {
wfi();
rprintln!("Check");
println!("Check");

if G_LED_ON.load(Ordering::Relaxed) {
rprintln!("Turn Led On!");
println!("Turn Led On!");
led.set_high().unwrap();
} else {
rprintln!("Turn Led Off!");
println!("Turn Led Off!");
led.set_low().unwrap();
}
}
Expand Down
9 changes: 6 additions & 3 deletions examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@

extern crate cortex_m;
extern crate cortex_m_rt as rt;
extern crate panic_semihosting;
extern crate stm32g4xx_hal as hal;

use cortex_m_semihosting::hprintln;
use rt::entry;

#[macro_use]
mod utils;

use utils::logger::println;

#[entry]
fn main() -> ! {
hprintln!("Hello, STM32G4!").unwrap();
let _ = println!("Hello, STM32G4!");

#[allow(clippy::empty_loop)]
loop {}
Expand Down
13 changes: 6 additions & 7 deletions examples/panic.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#![no_main]
#![no_std]

use defmt_rtt as _;

use panic_probe as _;

use stm32g4 as _;

use defmt::Format;
#[macro_use]
mod utils;

use utils::logger;

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

#[cortex_m_rt::entry]
fn main() -> ! {
defmt::info!("main");
logger::info!("main");

panic!("Something bad");
// defmt::panic!()
}
53 changes: 23 additions & 30 deletions examples/utils/logger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
#![allow(unsafe_code)]
cfg_if::cfg_if! {
if #[cfg(all(feature = "log-rtt", feature = "defmt"))] {
pub use defmt::{info, trace, warn, debug, error};
pub use defmt::println as println;
} else {
pub use log::{info, trace, warn, debug, error};
pub use cortex_m_semihosting::hprintln as println;
}
}

cfg_if::cfg_if! {
if #[cfg(any(feature = "log-itm"))] {
use panic_itm as _;
Expand Down Expand Up @@ -28,42 +38,22 @@ cfg_if::cfg_if! {
};
}

#[allow(dead_code)]
pub fn init() {
cortex_m_log::log::init(&LOGGER).unwrap();
}

}
else if #[cfg(any(feature = "log-rtt"))] {
use panic_rtt_target as _;

use log::{Level, Metadata, Record, LevelFilter};
use rtt_target::{rprintln, rtt_init_print};
else if #[cfg(all(feature = "log-rtt", feature = "defmt"))] {
use defmt_rtt as _; // global logger
use panic_probe as _;
pub use defmt::Logger;

pub struct Logger {
level: Level,
}

static LOGGER: Logger = Logger {
level: Level::Info,
};

pub fn init() {
rtt_init_print!();
log::set_logger(&LOGGER).map(|()| log::set_max_level(LevelFilter::Info)).unwrap();
}

impl log::Log for Logger {
fn enabled(&self, metadata: &Metadata) -> bool {
metadata.level() <= self.level

}

fn log(&self, record: &Record) {
rprintln!("{} - {}", record.level(), record.args());
}

fn flush(&self) {}
}
#[allow(dead_code)]
pub fn init() {}
}
else if #[cfg(all(feature = "log-rtt", not(feature = "defmt")))] {
// TODO
}
else if #[cfg(any(feature = "log-semihost"))] {
use panic_semihosting as _;
Expand All @@ -84,12 +74,15 @@ cfg_if::cfg_if! {
};
}

#[allow(dead_code)]
pub fn init() {
cortex_m_log::log::init(&LOGGER).unwrap();
}
}
else {
use panic_halt as _;

#[allow(dead_code)]
pub fn init() {}
}
}

0 comments on commit 8b57030

Please sign in to comment.