From c27a6c149b918a50516876fcfdb103ee30c5137c Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Fri, 1 Nov 2019 20:34:03 -0600 Subject: [PATCH] Support SerialPort> --- src/devices/uart_16550.rs | 53 +++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 24 deletions(-) diff --git a/src/devices/uart_16550.rs b/src/devices/uart_16550.rs index c888c8a..d482504 100644 --- a/src/devices/uart_16550.rs +++ b/src/devices/uart_16550.rs @@ -1,3 +1,5 @@ +use core::convert::TryInto; + use crate::syscall::io::{Io, Pio, Mmio, ReadOnly}; bitflags! { @@ -22,7 +24,8 @@ bitflags! { } #[allow(dead_code)] -pub struct SerialPort> { +#[repr(packed)] +pub struct SerialPort { /// Data register, read to receive, write to send data: T, /// Interrupt enable @@ -53,40 +56,38 @@ impl SerialPort> { } } -impl SerialPort> { - pub fn new(_base: usize) -> SerialPort> { - SerialPort { - data: Mmio::new(), - int_en: Mmio::new(), - fifo_ctrl: Mmio::new(), - line_ctrl: Mmio::new(), - modem_ctrl: Mmio::new(), - line_sts: ReadOnly::new(Mmio::new()), - modem_sts: ReadOnly::new(Mmio::new()) - } +impl SerialPort> { + pub unsafe fn new(base: usize) -> &'static mut SerialPort> { + &mut *(base as *mut Self) } } -impl> SerialPort { +impl SerialPort + where T::Value: From + TryInto +{ pub fn init(&mut self) { //TODO: Cleanup - self.int_en.write(0x00); - self.line_ctrl.write(0x80); - self.data.write(0x01); - self.int_en.write(0x00); - self.line_ctrl.write(0x03); - self.fifo_ctrl.write(0xC7); - self.modem_ctrl.write(0x0B); - self.int_en.write(0x01); + self.int_en.write(0x00.into()); + self.line_ctrl.write(0x80.into()); + self.data.write(0x01.into()); + self.int_en.write(0x00.into()); + self.line_ctrl.write(0x03.into()); + self.fifo_ctrl.write(0xC7.into()); + self.modem_ctrl.write(0x0B.into()); + self.int_en.write(0x01.into()); } fn line_sts(&self) -> LineStsFlags { - LineStsFlags::from_bits_truncate(self.line_sts.read()) + LineStsFlags::from_bits_truncate( + (self.line_sts.read() & 0xFF.into()).try_into().unwrap_or(0) + ) } pub fn receive(&mut self) -> Option { if self.line_sts().contains(LineStsFlags::INPUT_FULL) { - Some(self.data.read()) + Some( + (self.data.read() & 0xFF.into()).try_into().unwrap_or(0) + ) } else { None } @@ -94,7 +95,7 @@ impl> SerialPort { pub fn send(&mut self, data: u8) { while ! self.line_sts().contains(LineStsFlags::OUTPUT_EMPTY) {} - self.data.write(data); + self.data.write(data.into()); } pub fn write(&mut self, buf: &[u8]) { @@ -105,6 +106,10 @@ impl> SerialPort { self.send(b' '); self.send(8); }, + b'\n' => { + self.send(b'\r'); + self.send(b'\n'); + }, _ => { self.send(b); }