zc706/src/stdio.rs

54 lines
1.2 KiB
Rust
Raw Normal View History

2019-06-20 06:30:18 +08:00
use crate::uart::Uart;
2019-08-30 15:57:42 +08:00
use crate::mutex::Mutex;
2019-06-20 06:30:18 +08:00
const UART_RATE: u32 = 115_200;
static mut UART: Option<Uart> = None;
2019-08-30 15:57:42 +08:00
static mut UART_MUTEX: Mutex = Mutex::new_unlocked();
2019-06-20 06:30:18 +08:00
#[doc(hidden)]
2019-08-30 15:57:42 +08:00
fn get_uart() -> &'static mut Uart {
2019-06-20 06:30:18 +08:00
unsafe {
match &mut UART {
None => {
2019-08-11 06:55:27 +08:00
let uart = Uart::serial(UART_RATE);
2019-06-20 06:30:18 +08:00
UART = Some(uart);
UART.as_mut().unwrap()
}
Some(uart) => uart,
}
}
}
2019-08-30 15:57:42 +08:00
// call f(UART) with UART locked via UART_MUTEX
pub fn with_uart<F>(f: F) where F: Fn(&mut Uart) -> () {
unsafe {
UART_MUTEX.acquire();
}
f(get_uart());
unsafe {
UART_MUTEX.release();
}
}
2019-06-21 07:18:24 +08:00
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ({
2019-08-30 15:57:42 +08:00
crate::stdio::with_uart(|uart| {
use core::fmt::Write;
let _ = write!(uart, $($arg)*);
});
2019-06-21 07:18:24 +08:00
})
}
2019-06-20 06:30:18 +08:00
#[macro_export]
macro_rules! println {
($($arg:tt)*) => ({
2019-08-30 15:57:42 +08:00
crate::stdio::with_uart(|uart| {
use core::fmt::Write;
let _ = write!(uart, $($arg)*);
let _ = write!(uart, "\r\n");
while !uart.tx_fifo_empty() {}
});
2019-06-20 06:30:18 +08:00
})
}