diff options
Diffstat (limited to 'src-riscv/log.rs')
-rw-r--r-- | src-riscv/log.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src-riscv/log.rs b/src-riscv/log.rs new file mode 100644 index 0000000..ddb6792 --- /dev/null +++ b/src-riscv/log.rs @@ -0,0 +1,36 @@ +use core::fmt::{write, Arguments}; + +use gd32vf103xx_hal::{ + serial::Tx, + pac::USART1, +}; +use riscv::interrupt::{self, Mutex}; + +#[macro_export] +macro_rules! log { + ($($args:tt)+) => { + $crate::log::log_args(core::format_args!($($args)+)) + } +} + +#[macro_export] +macro_rules! logln { + () => ({ kprint!("\r\n") }); + ($fmt: literal $(, $($arg: tt)+)?) => { + log!(concat!($fmt, "\n") $(, $($arg)+)?) + } +} + +static mut TX: Mutex<Option<Tx<USART1>>> = Mutex::new(None); + +pub fn init(tx: Tx<USART1>) { + interrupt::free(|_cs| { + unsafe { TX.get_mut().insert(tx) }; + }); +} + +pub fn log_args(args: Arguments) { + interrupt::free(|_cs| { + unsafe { TX.get_mut().as_mut().map(|tx| write(tx, args)) }; + }); +} |