summaryrefslogtreecommitdiff
path: root/src/serial/buffer.rs
blob: b52728a92fe5870f4a9363835efbbfe9f0ccfa46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use crate::sync::interface::Mutex;
use crate::sync::NullLock;
use uart_16550::SerialPort;

pub struct Serial {
    buffer: Option<SerialPort>,
}

impl Serial {
    pub const fn new() -> Self {
        Serial { buffer: None }
    }
}

impl crate::sync::interface::Initializable for Serial {
    fn init(&mut self) {
        let mut serial_port = unsafe { SerialPort::new(0x3F8) };
        serial_port.init();
        self.buffer = Some(serial_port);
    }
}

#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
    use core::fmt::Write;
    SERIAL1.lock(|serial| {
        if let Some(buffer) = &mut serial.buffer {
            buffer.write_fmt(args).expect("printing to serial failed!");
        }
    })
}

/// Prints to the host through the serial interface.
#[macro_export]
macro_rules! serial_print {
	($($arg:tt)*) => {
		$crate::serial::_print(format_args!($($arg)*));
	};
}

/// Prints to the host through the serial interface, appending a newline.
#[macro_export]
macro_rules! serial_println {
	() => ($crate::serial_print!("\n"));
	($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n")));
	($fmt:expr, $($arg:tt)*) => ($crate::serial_print!(
				concat!($fmt, "\n"), $($arg)*));
}

pub static SERIAL1: NullLock<Serial> = NullLock::new(Serial::new());