rust: Add simple debug macros

Add simple debug!() and debugln!() macros that can be used for everywhere
to print stuff to UARTHS.
This commit is contained in:
Wladimir J. van der Laan 2020-05-31 18:55:14 +00:00
parent 67078f6242
commit 5af12f9582
2 changed files with 41 additions and 0 deletions

View File

@ -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();
})
}

View File

@ -5,6 +5,7 @@
pub mod board;
#[cfg(not(test))]
pub mod debug;
pub mod panic;
pub mod soc;
pub mod timing;