Rearrange serial I/O code and make 16550 driver generic over T: Io
This commit is contained in:
@@ -1 +1,5 @@
|
||||
pub mod x86_64;
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
#[macro_use]
|
||||
pub mod x86_64;
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
pub use self::x86_64::*;
|
||||
@@ -1,115 +1,11 @@
|
||||
use core::fmt::{self, Write};
|
||||
use devices::uart_16550::SerialPort;
|
||||
use syscall::io::Pio;
|
||||
use spin::Mutex;
|
||||
|
||||
use scheme::debug::debug_input;
|
||||
use syscall::io::{Io, Pio, ReadOnly};
|
||||
|
||||
pub static COM1: Mutex<SerialPort> = Mutex::new(SerialPort::new(0x3F8));
|
||||
pub static COM2: Mutex<SerialPort> = Mutex::new(SerialPort::new(0x2F8));
|
||||
pub static COM1: Mutex<SerialPort<Pio<u8>>> = Mutex::new(SerialPort::<Pio<u8>>::new(0x3F8));
|
||||
pub static COM2: Mutex<SerialPort<Pio<u8>>> = Mutex::new(SerialPort::<Pio<u8>>::new(0x2F8));
|
||||
|
||||
pub unsafe fn init() {
|
||||
COM1.lock().init();
|
||||
COM2.lock().init();
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// Interrupt enable flags
|
||||
flags IntEnFlags: u8 {
|
||||
const RECEIVED = 1,
|
||||
const SENT = 1 << 1,
|
||||
const ERRORED = 1 << 2,
|
||||
const STATUS_CHANGE = 1 << 3,
|
||||
// 4 to 7 are unused
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// Line status flags
|
||||
flags LineStsFlags: u8 {
|
||||
const INPUT_FULL = 1,
|
||||
// 1 to 4 unknown
|
||||
const OUTPUT_EMPTY = 1 << 5,
|
||||
// 6 and 7 unknown
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct SerialPort {
|
||||
/// Data register, read to receive, write to send
|
||||
data: Pio<u8>,
|
||||
/// Interrupt enable
|
||||
int_en: Pio<u8>,
|
||||
/// FIFO control
|
||||
fifo_ctrl: Pio<u8>,
|
||||
/// Line control
|
||||
line_ctrl: Pio<u8>,
|
||||
/// Modem control
|
||||
modem_ctrl: Pio<u8>,
|
||||
/// Line status
|
||||
line_sts: ReadOnly<Pio<u8>>,
|
||||
/// Modem status
|
||||
modem_sts: ReadOnly<Pio<u8>>,
|
||||
}
|
||||
|
||||
impl SerialPort {
|
||||
const fn new(base: u16) -> SerialPort {
|
||||
SerialPort {
|
||||
data: Pio::new(base),
|
||||
int_en: Pio::new(base + 1),
|
||||
fifo_ctrl: Pio::new(base + 2),
|
||||
line_ctrl: Pio::new(base + 3),
|
||||
modem_ctrl: Pio::new(base + 4),
|
||||
line_sts: ReadOnly::new(Pio::new(base + 5)),
|
||||
modem_sts: ReadOnly::new(Pio::new(base + 6))
|
||||
}
|
||||
}
|
||||
|
||||
fn init(&mut self) {
|
||||
//TODO: Cleanup
|
||||
self.int_en.write(0x00);
|
||||
self.line_ctrl.write(0x80);
|
||||
self.data.write(0x03);
|
||||
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);
|
||||
}
|
||||
|
||||
fn line_sts(&self) -> LineStsFlags {
|
||||
LineStsFlags::from_bits_truncate(self.line_sts.read())
|
||||
}
|
||||
|
||||
pub fn receive(&mut self) {
|
||||
while self.line_sts().contains(INPUT_FULL) {
|
||||
debug_input(self.data.read());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(&mut self, data: u8) {
|
||||
match data {
|
||||
8 | 0x7F => {
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(8);
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(b' ');
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(8);
|
||||
},
|
||||
_ => {
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for SerialPort {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
|
||||
for byte in s.bytes() {
|
||||
self.send(byte);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,20 @@
|
||||
/// Print to console
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => ({
|
||||
use core::fmt::Write;
|
||||
let _ = write!($crate::arch::device::serial::COM1.lock(), $($arg)*);
|
||||
});
|
||||
}
|
||||
|
||||
/// Print with new line to console
|
||||
#[macro_export]
|
||||
macro_rules! println {
|
||||
() => (print!("\n"));
|
||||
($fmt:expr) => (print!(concat!($fmt, "\n")));
|
||||
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
|
||||
}
|
||||
|
||||
/// Create an interrupt function that can safely run rust code
|
||||
#[macro_export]
|
||||
macro_rules! interrupt {
|
||||
|
||||
1
src/devices/mod.rs
Normal file
1
src/devices/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod uart_16550;
|
||||
122
src/devices/uart_16550.rs
Normal file
122
src/devices/uart_16550.rs
Normal file
@@ -0,0 +1,122 @@
|
||||
use core::fmt::{self, Write};
|
||||
|
||||
use scheme::debug::debug_input;
|
||||
use syscall::io::{Io, Pio, Mmio, ReadOnly};
|
||||
|
||||
bitflags! {
|
||||
/// Interrupt enable flags
|
||||
flags IntEnFlags: u8 {
|
||||
const RECEIVED = 1,
|
||||
const SENT = 1 << 1,
|
||||
const ERRORED = 1 << 2,
|
||||
const STATUS_CHANGE = 1 << 3,
|
||||
// 4 to 7 are unused
|
||||
}
|
||||
}
|
||||
|
||||
bitflags! {
|
||||
/// Line status flags
|
||||
flags LineStsFlags: u8 {
|
||||
const INPUT_FULL = 1,
|
||||
// 1 to 4 unknown
|
||||
const OUTPUT_EMPTY = 1 << 5,
|
||||
// 6 and 7 unknown
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub struct SerialPort<T: Io<Value = u8>> {
|
||||
/// Data register, read to receive, write to send
|
||||
data: T,
|
||||
/// Interrupt enable
|
||||
int_en: T,
|
||||
/// FIFO control
|
||||
fifo_ctrl: T,
|
||||
/// Line control
|
||||
line_ctrl: T,
|
||||
/// Modem control
|
||||
modem_ctrl: T,
|
||||
/// Line status
|
||||
line_sts: ReadOnly<T>,
|
||||
/// Modem status
|
||||
modem_sts: ReadOnly<T>,
|
||||
}
|
||||
|
||||
impl SerialPort<Pio<u8>> {
|
||||
pub const fn new(base: u16) -> SerialPort<Pio<u8>> {
|
||||
SerialPort {
|
||||
data: Pio::new(base),
|
||||
int_en: Pio::new(base + 1),
|
||||
fifo_ctrl: Pio::new(base + 2),
|
||||
line_ctrl: Pio::new(base + 3),
|
||||
modem_ctrl: Pio::new(base + 4),
|
||||
line_sts: ReadOnly::new(Pio::new(base + 5)),
|
||||
modem_sts: ReadOnly::new(Pio::new(base + 6))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl SerialPort<Mmio<u8>> {
|
||||
pub fn new(base: usize) -> SerialPort<Mmio<u8>> {
|
||||
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<T: Io<Value = u8>> SerialPort<T> {
|
||||
pub fn init(&mut self) {
|
||||
//TODO: Cleanup
|
||||
self.int_en.write(0x00);
|
||||
self.line_ctrl.write(0x80);
|
||||
self.data.write(0x03);
|
||||
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);
|
||||
}
|
||||
|
||||
fn line_sts(&self) -> LineStsFlags {
|
||||
LineStsFlags::from_bits_truncate(self.line_sts.read())
|
||||
}
|
||||
|
||||
pub fn receive(&mut self) {
|
||||
while self.line_sts().contains(INPUT_FULL) {
|
||||
debug_input(self.data.read());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn send(&mut self, data: u8) {
|
||||
match data {
|
||||
8 | 0x7F => {
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(8);
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(b' ');
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(8);
|
||||
},
|
||||
_ => {
|
||||
while ! self.line_sts().contains(OUTPUT_EMPTY) {}
|
||||
self.data.write(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Io<Value = u8>> Write for SerialPort<T> {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
|
||||
for byte in s.bytes() {
|
||||
self.send(byte);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
13
src/lib.rs
13
src/lib.rs
@@ -42,9 +42,10 @@ pub use consts::*;
|
||||
/// Shared data structures
|
||||
pub mod common;
|
||||
|
||||
/// Macros like print, println, and interrupt
|
||||
/// Architecture-dependent stuff
|
||||
#[macro_use]
|
||||
pub mod macros;
|
||||
pub mod arch;
|
||||
pub use arch::*;
|
||||
|
||||
/// Constants like memory locations
|
||||
pub mod consts;
|
||||
@@ -52,14 +53,12 @@ pub mod consts;
|
||||
/// ACPI table parsing
|
||||
mod acpi;
|
||||
|
||||
/// Architecture-dependent stuff
|
||||
mod arch;
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
pub use arch::x86_64::*;
|
||||
|
||||
/// Context management
|
||||
pub mod context;
|
||||
|
||||
/// Architecture-independent devices
|
||||
pub mod devices;
|
||||
|
||||
/// ELF file parsing
|
||||
pub mod elf;
|
||||
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
/// Print to console
|
||||
#[macro_export]
|
||||
macro_rules! print {
|
||||
($($arg:tt)*) => ({
|
||||
use core::fmt::Write;
|
||||
let _ = write!($crate::device::serial::COM1.lock(), $($arg)*);
|
||||
});
|
||||
}
|
||||
|
||||
/// Print with new line to console
|
||||
#[macro_export]
|
||||
macro_rules! println {
|
||||
() => (print!("\n"));
|
||||
($fmt:expr) => (print!(concat!($fmt, "\n")));
|
||||
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
|
||||
}
|
||||
Reference in New Issue
Block a user