From 5195d46d7be2b5407a2082aa5383aef90803af48 Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 19 Dec 2025 03:30:20 +0800 Subject: [PATCH] add println_d! macro to bao1x-hal this allows forcing prints to go to the duart this is useful for debugging early boot issues --- libs/bao1x-hal/src/debug.rs | 53 +++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/libs/bao1x-hal/src/debug.rs b/libs/bao1x-hal/src/debug.rs index 7aeb27918..827ac1b40 100644 --- a/libs/bao1x-hal/src/debug.rs +++ b/libs/bao1x-hal/src/debug.rs @@ -139,3 +139,56 @@ macro_rules! println $crate::print!(concat!($fmt, "\r\n"), $($args)+) }); } + +// ==== DUART-only debug print ==== +// Used for forcing the use of DUART in debugging +pub struct Duart {} + +impl Duart { + /// Print a character + pub fn putc(&self, c: u8) { + let base = utra::duart::HW_DUART_BASE as *mut u32; + let mut uart = CSR::new(base); + if uart.rf(utra::duart::SFR_CR_SFR_CR) == 0 { + uart.wfo(utra::duart::SFR_CR_SFR_CR, 1); + } + while uart.r(utra::duart::SFR_SR) != 0 {} + uart.wo(utra::duart::SFR_TXD, c as u32); + } +} + +impl Write for Duart { + fn write_str(&mut self, s: &str) -> Result<(), Error> { + for c in s.bytes() { + self.putc(c); + } + Ok(()) + } +} + +#[macro_use] +/// Hardware debug print module +pub mod debug_print_duart { + #[macro_export] + macro_rules! print_d + { + ($($args:tt)+) => ({ + use core::fmt::Write; + let _ = write!(crate::debug::Duart {}, $($args)+); + }); + } +} + +#[macro_export] +macro_rules! println_d +{ + () => ({ + $crate::print_d!("\r\n") + }); + ($fmt:expr) => ({ + $crate::print_d!(concat!($fmt, "\r\n")) + }); + ($fmt:expr, $($args:tt)+) => ({ + $crate::print_d!(concat!($fmt, "\r\n"), $($args)+) + }); +}