Skip to content

Commit

Permalink
fix: transparent Serial-port
Browse files Browse the repository at this point in the history
Signed-off-by: Fritz Stracke <[email protected]>
  • Loading branch information
fstracke committed Dec 17, 2024
1 parent edfd286 commit eb34985
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/arch/aarch64/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand All @@ -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();
}

Expand Down
60 changes: 60 additions & 0 deletions src/arch/aarch64/drivers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use qemu_serial::QemuSerial;
use xlnx_serial::XlnxSerial;

pub mod qemu_serial;
pub mod xlnx_serial;

pub enum SerialSuccess<T> {
Success(T),
ERetry
}

pub enum SerialPort {
Qemu(QemuSerial),
Xlnx(XlnxSerial)
}

pub trait SerialDriver {
fn init(&mut self);
fn set_baud(&self, baud_rate: u32);
Expand All @@ -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<u8> {
match self {
SerialPort::Qemu(qemu_serial) => qemu_serial.putc(c),
SerialPort::Xlnx(xlnx_serial) => xlnx_serial.putc(c),
}
}

fn getc(&self) -> SerialSuccess<u8> {
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(),
}
}
}

0 comments on commit eb34985

Please sign in to comment.