From e562b822dc8bdb8b16f582037cf81c126d499977 Mon Sep 17 00:00:00 2001 From: topquark12 Date: Sat, 22 Oct 2022 21:00:17 +0800 Subject: [PATCH] use RTT for PanicInfo --- src/device/boot.rs | 5 +---- src/device/log_setup.rs | 2 -- src/main.rs | 27 +++++++++++++++++++++++++-- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/device/boot.rs b/src/device/boot.rs index 9d4ca5d..1a15821 100644 --- a/src/device/boot.rs +++ b/src/device/boot.rs @@ -1,4 +1,4 @@ -use super::{gpio, log_setup, sys_timer, usb}; +use super::{gpio, sys_timer, usb}; use crate::laser_diode::current_sources::*; use fugit::ExtU32; use log::info; @@ -15,9 +15,6 @@ const WATCHDOG_PERIOD: u32 = 1000; const WATCHDOG_PERIOD: u32 = 30000; pub fn bootup(mut core_perif: CorePeripherals, perif: Peripherals) -> IndependentWatchdog { - log_setup::init_log(); - info!("Kirdy init"); - core_perif.SCB.enable_icache(); core_perif.SCB.enable_dcache(&mut core_perif.CPUID); diff --git a/src/device/log_setup.rs b/src/device/log_setup.rs index 54a896c..59d3c51 100644 --- a/src/device/log_setup.rs +++ b/src/device/log_setup.rs @@ -9,9 +9,7 @@ pub fn init_log() { #[cfg(feature = "RTT")] pub fn init_log() { use super::rtt_logger; - use rtt_target::rtt_init_print; static RTT_LOGGER: rtt_logger::Logger = rtt_logger::Logger; - rtt_init_print!(); let _ = log::set_logger(&RTT_LOGGER); log::set_max_level(log::LevelFilter::Debug); } diff --git a/src/main.rs b/src/main.rs index f81ea30..6bf2920 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,15 +3,38 @@ use cortex_m_rt::entry; use log::info; -use panic_halt as _; use stm32f4xx_hal::pac::{CorePeripherals, Peripherals}; mod device; mod laser_diode; -use device::{boot::bootup, sys_timer}; +use device::{boot::bootup, log_setup, sys_timer}; + +// If RTT is used, print panic info through RTT +#[cfg(feature = "RTT")] +use { + core::panic::PanicInfo, + rtt_target::{rprintln, rtt_init_print}, +}; +#[cfg(feature = "RTT")] +#[panic_handler] +fn panic(info: &PanicInfo) -> ! { + rprintln!("{}", info); + loop {} +} +// Otherwise use panic halt +#[cfg(not(feature = "RTT"))] +use panic_halt as _; #[entry] fn main() -> ! { + + // RTT does not rely on any peripherals, can be set up immidiately + #[cfg(feature = "RTT")] + rtt_init_print!(); + + log_setup::init_log(); + info!("Kirdy init"); + let core_perif = CorePeripherals::take().unwrap(); let perif = Peripherals::take().unwrap();