Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 4 additions & 27 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ features = [
[target.'cfg(target_arch = "x86_64")'.dependencies]
free-list = { version = "0.3", features = ["x86_64"] }
raw-cpuid = "11"
uart_16550 = "0.4"
uart_16550 = { git = "https://github.com/phip1611/uart_16550.git", branch = "rewrite" }
x86_64 = "0.15"
memory_addresses = { version = "0.2.3", default-features = false, features = [
"x86_64",
Expand Down
25 changes: 16 additions & 9 deletions src/arch/x86_64/kernel/serial.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use alloc::collections::VecDeque;
use core::hint;

use embedded_io::{ErrorType, Read, ReadReady, Write};
use hermit_sync::{InterruptTicketMutex, Lazy};
use uart_16550::backend::PioBackend;
use uart_16550::{Config, Uart16550};

#[cfg(feature = "pci")]
use crate::arch::x86_64::kernel::interrupts;
Expand All @@ -16,7 +19,7 @@ static UART_DEVICE: Lazy<InterruptTicketMutex<UartDevice>> =
Lazy::new(|| unsafe { InterruptTicketMutex::new(UartDevice::new()) });

struct UartDevice {
pub uart: uart_16550::SerialPort,
pub uart: Uart16550<PioBackend>,
pub buffer: VecDeque<u8>,
}

Expand All @@ -27,8 +30,10 @@ impl UartDevice {
.serial_port_base
.unwrap()
.get();
let mut uart = unsafe { uart_16550::SerialPort::new(base) };
uart.init();
let mut uart = unsafe { Uart16550::new_port(base).unwrap() };
uart.init(Config::default()).unwrap();
uart.test_loopback().unwrap();
uart.check_remote_ready_to_receive().unwrap();

Self {
uart,
Expand Down Expand Up @@ -65,11 +70,12 @@ impl Write for SerialDevice {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let mut guard = UART_DEVICE.lock();

for &data in buf {
guard.uart.send(data);
loop {
match guard.uart.try_send_bytes(buf) {
Ok(n) => return Ok(n),
Err(_) => hint::spin_loop(),
}
}

Ok(buf.len())
}

fn flush(&mut self) -> Result<(), Self::Error> {
Expand All @@ -81,8 +87,9 @@ impl Write for SerialDevice {
pub(crate) fn get_serial_handler() -> (InterruptLine, fn()) {
fn serial_handler() {
let mut guard = UART_DEVICE.lock();
if let Ok(c) = guard.uart.try_receive() {
guard.buffer.push_back(c);

while let Ok(byte) = guard.uart.try_receive_byte() {
guard.buffer.push_back(byte);
}

drop(guard);
Expand Down