mirror of
https://github.com/laanwj/k210-sdk-stuff.git
synced 2024-11-24 18:36:19 +04:00
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:
parent
67078f6242
commit
5af12f9582
40
rust/k210-shared/src/debug.rs
Normal file
40
rust/k210-shared/src/debug.rs
Normal 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();
|
||||||
|
})
|
||||||
|
}
|
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
pub mod board;
|
pub mod board;
|
||||||
#[cfg(not(test))]
|
#[cfg(not(test))]
|
||||||
|
pub mod debug;
|
||||||
pub mod panic;
|
pub mod panic;
|
||||||
pub mod soc;
|
pub mod soc;
|
||||||
pub mod timing;
|
pub mod timing;
|
||||||
|
Loading…
Reference in New Issue
Block a user