From b9557bdc5ec6f8f3cc98fb9ed03dc44eb0636d86 Mon Sep 17 00:00:00 2001 From: Julien CLEMENT Date: Fri, 3 Dec 2021 00:30:08 +0100 Subject: [PATCH] feat(serial): implement own serial driver Signed-off-by: Julien CLEMENT --- Cargo.lock | 11 ----------- Cargo.toml | 1 - src/serial.rs | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8a032a8..6605e31 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,7 +20,6 @@ version = "0.1.0" dependencies = [ "lazy_static", "spin", - "uart_16550", "volatile 0.2.7", "x86_64", ] @@ -40,16 +39,6 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" -[[package]] -name = "uart_16550" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "65ad019480ef5ff8ffe66d6f6a259cd87cf317649481394981db1739d844f374" -dependencies = [ - "bitflags", - "x86_64", -] - [[package]] name = "volatile" version = "0.2.7" diff --git a/Cargo.toml b/Cargo.toml index fbf9c29..1e66036 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,6 @@ crate-type = ["staticlib"] volatile = "0.2.6" spin = "0.5.2" x86_64 = "0.14.2" -uart_16550 = "0.2.0" [dependencies.lazy_static] version = "1.0" diff --git a/src/serial.rs b/src/serial.rs index e8073d9..cd9d7c3 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -1,10 +1,19 @@ -use uart_16550::SerialPort; +use core::fmt; use spin::Mutex; use lazy_static::lazy_static; +use x86_64::instructions::port::Port; + +const COM1: u16 = 0x3f8; +#[allow(dead_code)] +const COM2: u16 = 0x2f8; +#[allow(dead_code)] +const COM3: u16 = 0x3e8; +#[allow(dead_code)] +const COM4: u16 = 0x2e8; lazy_static! { pub static ref SERIAL1: Mutex = { - let mut serial_port = unsafe { SerialPort::new(0x3F8) }; + let mut serial_port = SerialPort::new(COM1); serial_port.init(); Mutex::new(serial_port) }; @@ -32,3 +41,37 @@ macro_rules! serial_println { ($fmt:expr, $($arg:tt)*) => ($crate::serial_print!( concat!($fmt, "\n"), $($arg)*)); } + +pub struct SerialPort { + base: Port, +} + +impl SerialPort { + pub fn new(port: u16) -> SerialPort { + SerialPort { + base: Port::new(port) + } + } + + fn write_byte(&mut self, byte: u8) { + unsafe { + self.base.write(byte); + } + } + + fn write_string(&mut self, s: &str) { + for byte in s.bytes() { + self.write_byte(byte); + } + } + + fn init(&mut self) { + } +} + +impl fmt::Write for SerialPort { + fn write_str(&mut self, s: &str) -> fmt::Result { + self.write_string(s); + Ok(()) + } +}