Unify IO in arch

master
WangRunji 6 years ago
parent f1407dff7b
commit 60c2a77ac1

@ -0,0 +1,21 @@
use super::bbl::sbi;
use core::fmt::{Write, Result, Arguments};
struct SerialPort;
impl Write for SerialPort {
fn write_str(&mut self, s: &str) -> Result {
for c in s.bytes() {
sbi::console_putchar(c as usize);
}
Ok(())
}
}
pub fn getchar() -> char {
sbi::console_getchar() as u8 as char
}
pub fn putfmt(fmt: Arguments) {
SerialPort.write_fmt(fmt).unwrap();
}

@ -1,7 +1,7 @@
extern crate bbl;
extern crate riscv;
pub mod serial;
pub mod io;
pub mod interrupt;
pub mod timer;
pub mod paging;

@ -1,13 +0,0 @@
use super::bbl::sbi;
use core::fmt;
pub struct SerialPort;
impl fmt::Write for SerialPort {
fn write_str(&mut self, s: &str) -> fmt::Result {
for c in s.bytes() {
sbi::console_putchar(c as usize);
}
Ok(())
}
}

@ -20,20 +20,17 @@ pub fn init() {
}
pub trait SerialRead {
fn receive(&mut self);
fn receive(&mut self) -> u8;
}
impl SerialRead for SerialPort {
fn receive(&mut self) {
fn receive(&mut self) -> u8 {
unsafe {
let ports = self as *mut _ as *mut [Pio<u8>; 6];
let line_sts = &(*ports)[5];
let data = &(*ports)[0];
while line_sts.read() & 1 == 1 {
let data = data.read();
writeln!(self, "serial receive {}", data).unwrap();
// TODO handle received data
}
while line_sts.read() & 1 != 1 {}
data.read()
}
}
}

@ -0,0 +1,12 @@
use super::driver::serial::*;
use core::fmt::{Arguments, Write};
pub fn getchar() -> char {
unsafe { COM1.force_unlock(); }
COM1.lock().receive() as char
}
pub fn putfmt(fmt: Arguments) {
unsafe { COM1.force_unlock(); }
COM1.lock().write_fmt(fmt).unwrap()
}

@ -10,6 +10,7 @@ pub mod gdt;
pub mod idt;
pub mod smp;
pub mod memory;
pub mod io;
pub fn init() {
idt::init();

@ -1,6 +1,5 @@
use core::fmt;
use log;
use log::{Level, LevelFilter, Log, Metadata, Record};
use log::{self, Level, LevelFilter, Log, Metadata, Record};
pub fn init() {
static LOGGER: SimpleLogger = SimpleLogger;
@ -34,42 +33,14 @@ macro_rules! with_color {
}};
}
#[cfg(target_arch = "x86_64")]
fn print_in_color(args: fmt::Arguments, color: Color) {
use core::fmt::Write;
use arch::driver::serial::COM1;
// use arch::driver::vga::*;
// {
// let mut writer = vga_writer::VGA_WRITER.lock();
// writer.set_color(color);
// writer.write_fmt(args).unwrap();
// }
// TODO: 解决死锁问题
// 若进程在持有锁时被中断,中断处理程序请求输出,就会死锁
unsafe{ COM1.force_unlock(); }
COM1.lock().write_fmt(with_color!(args, color)).unwrap();
use arch::io;
io::putfmt(with_color!(args, color));
}
#[cfg(target_arch = "riscv")]
fn print_in_color(args: fmt::Arguments, color: Color) {
use arch::serial::SerialPort;
use core::fmt::Write;
SerialPort.write_fmt(with_color!(args, color)).unwrap();
}
#[cfg(target_arch = "x86_64")]
pub fn print(args: fmt::Arguments) {
use core::fmt::Write;
use arch::driver::serial::COM1;
unsafe { COM1.force_unlock(); }
COM1.lock().write_fmt(args).unwrap();
}
#[cfg(target_arch = "riscv")]
pub fn print(args: fmt::Arguments) {
use arch::serial::SerialPort;
use core::fmt::Write;
SerialPort.write_fmt(args).unwrap();
use arch::io;
io::putfmt(args);
}
struct SimpleLogger;

Loading…
Cancel
Save