use crate::target::UART0;
pub struct DebugLog {}
pub enum Error {}
impl DebugLog {
pub fn count(&mut self) -> u8 {
unsafe { (*UART0::ptr()).status.read().txfifo_cnt().bits() }
}
pub fn is_idle(&mut self) -> bool {
unsafe { (*UART0::ptr()).status.read().st_utx_out().is_tx_idle() }
}
pub fn write(&mut self, byte: u8) -> nb::Result<(), Error> {
if self.count() < 128 {
unsafe { (*UART0::ptr()).tx_fifo.write_with_zero(|w| w.bits(byte)) }
Ok(())
} else {
Err(nb::Error::WouldBlock)
}
}
}
impl core::fmt::Write for DebugLog {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
s.as_bytes()
.iter()
.try_for_each(|c| nb::block!(self.write(*c)))
.map_err(|_| core::fmt::Error)
}
}
pub static mut DEBUG_LOG: DebugLog = DebugLog {};
#[macro_export]
macro_rules! dprint {
($s:expr) => {
#[allow(unused_unsafe)]
unsafe {
use core::fmt::Write;
$crate::dprint::DEBUG_LOG.write_str($s).unwrap();
}
};
($($arg:tt)*) => {
#[allow(unused_unsafe)]
unsafe {
use core::fmt::Write;
$crate::dprint::DEBUG_LOG.write_fmt(format_args!($($arg)*)).unwrap();
}
};
}
#[macro_export]
macro_rules! dprintln {
() => {
#[allow(unused_unsafe)]
unsafe {
use core::fmt::Write;
$crate::dprint::DEBUG_LOG.write_str("\n").unwrap();
}
};
($fmt:expr) => {
#[allow(unused_unsafe)]
unsafe {
use core::fmt::Write;
$crate::dprint::DEBUG_LOG.write_str(concat!($fmt, "\n")).unwrap();
}
};
($fmt:expr, $($arg:tt)*) => {
#[allow(unused_unsafe)]
unsafe {
use core::fmt::Write;
$crate::dprint::DEBUG_LOG.write_fmt(format_args!(concat!($fmt, "\n"), $($arg)*)).unwrap();
}
};
}
#[macro_export]
macro_rules! dflush {
() => {
#[allow(unused_unsafe)]
unsafe {
while !$crate::dprint::DEBUG_LOG.is_idle() {}
}
};
}