From eb349850f39dbefc89ef3746d411e8c23290562c Mon Sep 17 00:00:00 2001 From: Fritz Stracke Date: Tue, 17 Dec 2024 14:37:50 +0100 Subject: [PATCH] fix: transparent Serial-port Signed-off-by: Fritz Stracke --- src/arch/aarch64/console.rs | 14 +++++--- src/arch/aarch64/drivers/mod.rs | 60 +++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/arch/aarch64/console.rs b/src/arch/aarch64/console.rs index b29ecff7..7afca256 100644 --- a/src/arch/aarch64/console.rs +++ b/src/arch/aarch64/console.rs @@ -3,14 +3,15 @@ use hermit_dtb::Dtb; use crate::arch::drivers::qemu_serial::QemuSerial; use crate::arch::drivers::xlnx_serial::XlnxSerial; -use crate::arch::drivers::SerialDriver; +use crate::arch::drivers::SerialPort; +use crate::arch::aarch64::drivers::SerialDriver; pub struct Console { - stdout: QemuSerial, + stdout: SerialPort, } ///TODO: Rewrite to create serial driver available on target hardware (read from dtb) -pub fn stdout() -> QemuSerial { +pub fn stdout() -> SerialPort { /// Physical address of UART0 at Qemu's virt emulation const SERIAL_PORT_ADDRESS: u32 = 0x09000000; @@ -49,7 +50,7 @@ pub fn stdout() -> QemuSerial { }; let mut serial = QemuSerial::from_addr(NonZeroU32::new(uart_address).unwrap()); serial.init(); - serial + SerialPort::Qemu(serial) } impl Console { @@ -62,7 +63,10 @@ impl Console { } pub(crate) fn set_stdout(&mut self, stdout: u32) { - self.stdout = QemuSerial::from_addr(NonZeroU32::new(stdout).unwrap()); + match self.stdout { + SerialPort::Qemu(_) => self.stdout = SerialPort::Qemu(QemuSerial::from_addr(NonZeroU32::new(stdout).unwrap())), + SerialPort::Xlnx(_) => self.stdout = SerialPort::Xlnx(XlnxSerial::from_addr(NonZeroU32::new(stdout).unwrap())), + } self.stdout.init(); } diff --git a/src/arch/aarch64/drivers/mod.rs b/src/arch/aarch64/drivers/mod.rs index 841d89bc..dbfc6955 100644 --- a/src/arch/aarch64/drivers/mod.rs +++ b/src/arch/aarch64/drivers/mod.rs @@ -1,3 +1,6 @@ +use qemu_serial::QemuSerial; +use xlnx_serial::XlnxSerial; + pub mod qemu_serial; pub mod xlnx_serial; @@ -5,6 +8,12 @@ pub enum SerialSuccess { Success(T), ERetry } + +pub enum SerialPort { + Qemu(QemuSerial), + Xlnx(XlnxSerial) +} + pub trait SerialDriver { fn init(&mut self); fn set_baud(&self, baud_rate: u32); @@ -13,4 +22,55 @@ pub trait SerialDriver { fn putstr(&mut self, s: &[u8]); fn get_addr(&self) -> u32; fn wait_empty(&mut self); +} + +impl SerialDriver for SerialPort { + fn init(&mut self) { + match self { + SerialPort::Qemu(qemu_serial) => qemu_serial.init(), + SerialPort::Xlnx(xlnx_serial) => xlnx_serial.init(), + } + } + + fn set_baud(&self, baud_rate: u32) { + match self { + SerialPort::Qemu(qemu_serial) => qemu_serial.set_baud(baud_rate), + SerialPort::Xlnx(xlnx_serial) => xlnx_serial.set_baud(baud_rate), + } + } + + fn putc(&mut self, c: u8) -> SerialSuccess { + match self { + SerialPort::Qemu(qemu_serial) => qemu_serial.putc(c), + SerialPort::Xlnx(xlnx_serial) => xlnx_serial.putc(c), + } + } + + fn getc(&self) -> SerialSuccess { + match self { + SerialPort::Qemu(qemu_serial) => qemu_serial.getc(), + SerialPort::Xlnx(xlnx_serial) => xlnx_serial.getc(), + } + } + + fn putstr(&mut self, s: &[u8]) { + match self { + SerialPort::Qemu(qemu_serial) => qemu_serial.putstr(s), + SerialPort::Xlnx(xlnx_serial) => xlnx_serial.putstr(s), + } + } + + fn get_addr(&self) -> u32 { + match self { + SerialPort::Qemu(qemu_serial) => qemu_serial.get_addr(), + SerialPort::Xlnx(xlnx_serial) => xlnx_serial.get_addr(), + } + } + + fn wait_empty(&mut self) { + match self { + SerialPort::Qemu(qemu_serial) => qemu_serial.wait_empty(), + SerialPort::Xlnx(xlnx_serial) => xlnx_serial.wait_empty(), + } + } } \ No newline at end of file