From 299d06dad9c4bfc8eef70271204a4f9f7379e5ce Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Mon, 8 Aug 2022 14:41:00 -0400 Subject: =?UTF-8?q?update=20log=20inteface,=20add=20(and=20use)=20?= =?UTF-8?q?=E2=80=98log!=E2=80=99=20macro.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/log.rs | 34 +++++++++++++++++++++++++++------- src/main.rs | 5 +++-- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/log.rs b/src/log.rs index 07525cc..b6204d6 100644 --- a/src/log.rs +++ b/src/log.rs @@ -1,16 +1,36 @@ -use core::fmt::Write; +use core::fmt::{write, Arguments}; use gd32vf103xx_hal::{ - pac::USART1, 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: Option> = None; +static mut TX: Mutex>> = Mutex::new(None); -pub unsafe fn new(tx: Tx) { - TX = Some(tx); +pub fn init(tx: Tx) { + interrupt::free(|_cs| { + unsafe { TX.get_mut().insert(tx) }; + }); } -pub unsafe fn log(str: &str) { - TX.as_mut().map(|tx| tx.write_str(str)); +pub fn log_args(args: Arguments) { + interrupt::free(|_cs| { + unsafe { TX.get_mut().as_mut().map(|tx| write(tx, args)) }; + }); } diff --git a/src/main.rs b/src/main.rs index 2b0bde2..7fd2ba3 100755 --- a/src/main.rs +++ b/src/main.rs @@ -54,13 +54,14 @@ fn main(_hartid: usize) -> ! { &mut rcu, ); let (tx, _rx) = serial.split(); - unsafe { log::new(tx) }; + log::init(tx); loop { let mut can_sleep = true; can_sleep &= blink.poll() == PollResult::WouldBlock; - unsafe { log::log("hi\r\n") }; + log!("yo!"); + logln!(" mtv raps"); if can_sleep { unsafe { wfi() }; -- cgit v1.2.3