From 5af12f9582d011b6089782add437ceb4affe824b Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Sun, 31 May 2020 18:55:14 +0000 Subject: [PATCH] rust: Add simple debug macros Add simple debug!() and debugln!() macros that can be used for everywhere to print stuff to UARTHS. --- rust/k210-shared/src/debug.rs | 40 +++++++++++++++++++++++++++++++++++ rust/k210-shared/src/lib.rs | 1 + 2 files changed, 41 insertions(+) create mode 100644 rust/k210-shared/src/debug.rs diff --git a/rust/k210-shared/src/debug.rs b/rust/k210-shared/src/debug.rs new file mode 100644 index 0000000..ecbdd73 --- /dev/null +++ b/rust/k210-shared/src/debug.rs @@ -0,0 +1,40 @@ +/** Simple logging functionality that prints to UARTHS at the current settings. + * The advantage of this is that it can be used from anywhere without having to pass + * stdout around. The disadvantage is that this prevents rust's safety rules from preventing + * concurrent access. It also assumes someone else set up the UART already. + */ +use k210_hal::pac; +pub use core::fmt::Write; + +pub struct DebugLogger; + +impl Write for DebugLogger { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + let uart = unsafe {&*pac::UARTHS::ptr() }; + for &byte in s.as_bytes() { + while uart.txdata.read().full().bit_is_set() { + continue; + } + unsafe { + uart.txdata.write(|w| w.data().bits(byte)); + } + } + Ok(()) + } +} + +#[macro_export] +macro_rules! debugln { + ($($arg:tt)+) => ({ + let mut stdout = $crate::debug::DebugLogger {}; + writeln!(stdout, $($arg)+).unwrap(); + }) +} + +#[macro_export] +macro_rules! debug { + ($($arg:tt)+) => ({ + let mut stdout = $crate::debug::DebugLogger {}; + write!(stdout, $($arg)+).unwrap(); + }) +} diff --git a/rust/k210-shared/src/lib.rs b/rust/k210-shared/src/lib.rs index f39e0e6..430a30a 100644 --- a/rust/k210-shared/src/lib.rs +++ b/rust/k210-shared/src/lib.rs @@ -5,6 +5,7 @@ pub mod board; #[cfg(not(test))] +pub mod debug; pub mod panic; pub mod soc; pub mod timing;