summaryrefslogtreecommitdiff
path: root/kernel/src/io/serial.rs
blob: 2a3bae87fe16a98a70c78a1b0f5641bf2dbb83fe (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
use spin::Mutex;
use uart_16550::SerialPort;

/*lazy_static! {
    pub static ref SERIAL1: Mutex<SerialPort> = {
        let mut serial_port = unsafe { SerialPort::new(0x3F8) };
        serial_port.init();
        Mutex::new(serial_port)
    };
}*/
const SERIAL_IO_PORT: u16 = 0x3F8;

static CONNECTION: Mutex<Serial> = Mutex::new(Serial {
    initialized: false,
    port: unsafe { SerialPort::new(SERIAL_IO_PORT) },
});

struct Serial {
    pub initialized: bool,
    pub port: SerialPort,
}

pub struct SerialStream {}

impl SerialStream {
    pub fn new() -> Self {
        SerialStream {}
    }
}

impl core::fmt::Write for SerialStream {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        let mut guard = CONNECTION.lock();
        if !guard.initialized {
            unsafe {
                guard.port.init();
            }
            guard.initialized = true;
        }
        guard.port.write_str(s)
    }
}