|
|
|
|
@ -1,6 +1,28 @@
|
|
|
|
|
use core::{fmt, hint};
|
|
|
|
|
|
|
|
|
|
use crate::ioport;
|
|
|
|
|
use crate::{ioport, spin::Spinlock};
|
|
|
|
|
|
|
|
|
|
const COM1_PORT: u16 = 0x3f8;
|
|
|
|
|
|
|
|
|
|
pub static COM1: Spinlock<Option<Com>> = Spinlock::new(None);
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! com1_print {
|
|
|
|
|
($($args:tt)*) => ({
|
|
|
|
|
crate::com::with_com1_if_available(|com1| {
|
|
|
|
|
::core::write!(com1, $($args)*).ok();
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
|
macro_rules! com1_println {
|
|
|
|
|
($($args:tt)*) => ({
|
|
|
|
|
crate::com::with_com1_if_available(|com1| {
|
|
|
|
|
::core::writeln!(com1, $($args)*).ok();
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct Com {
|
|
|
|
|
port: u16,
|
|
|
|
|
@ -8,6 +30,27 @@ pub struct Com {
|
|
|
|
|
|
|
|
|
|
pub struct ComError;
|
|
|
|
|
|
|
|
|
|
pub fn with_com1_if_available<F>(f: F)
|
|
|
|
|
where
|
|
|
|
|
F: FnOnce(&mut Com),
|
|
|
|
|
{
|
|
|
|
|
let mut guard = COM1.lock();
|
|
|
|
|
if let Some(com1) = guard.as_mut() {
|
|
|
|
|
f(com1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe fn try_com1_init() -> bool {
|
|
|
|
|
match unsafe { Com::init(COM1_PORT) } {
|
|
|
|
|
Ok(com1) => {
|
|
|
|
|
let mut guard = COM1.lock();
|
|
|
|
|
*guard = Some(com1);
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
_ => false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Com {
|
|
|
|
|
pub unsafe fn init(port: u16) -> Result<Self, ComError> {
|
|
|
|
|
const ECHO_BYTE: u8 = 0x5a;
|
|
|
|
|
@ -43,9 +86,7 @@ impl Com {
|
|
|
|
|
ioport::outb(port + 4, 0x0f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
|
port,
|
|
|
|
|
})
|
|
|
|
|
Ok(Self { port })
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn poll_has_data(&self) -> bool {
|
|
|
|
|
@ -80,13 +121,4 @@ impl fmt::Write for Com {
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn write_char(&mut self, c: char) -> fmt::Result {
|
|
|
|
|
let mut buf = [0u8; 4];
|
|
|
|
|
let s = c.encode_utf8(&mut buf);
|
|
|
|
|
for b in s.bytes() {
|
|
|
|
|
self.write_poll(b);
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|