mirror of
https://github.com/panpaul/tiny_os
synced 2024-09-20 09:45:19 +08:00
feat: implement Reader on RawConsole
This commit is contained in:
parent
59fbc75552
commit
2252edf228
@ -1,8 +1,6 @@
|
||||
use core::fmt::Write;
|
||||
|
||||
pub struct RawConsole;
|
||||
|
||||
pub trait Printer: Write {
|
||||
pub trait Printer: core::fmt::Write {
|
||||
fn put_char(c: char);
|
||||
|
||||
#[inline]
|
||||
@ -64,7 +62,11 @@ pub trait Printer: Write {
|
||||
}
|
||||
}
|
||||
|
||||
impl Write for RawConsole {
|
||||
pub trait Reader {
|
||||
fn get_char() -> char;
|
||||
}
|
||||
|
||||
impl core::fmt::Write for RawConsole {
|
||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||
Self::put_str(s);
|
||||
Ok(())
|
||||
|
@ -1,3 +1,5 @@
|
||||
pub const UART0_BASE: usize = 0x1000_0000;
|
||||
pub const UART0_LSR: usize = 0x1000_0005;
|
||||
|
||||
pub mod printer;
|
||||
pub mod reader;
|
||||
|
@ -1,5 +1,11 @@
|
||||
use crate::arch::io::{Printer, RawConsole};
|
||||
|
||||
/*
|
||||
Theoretically, we should wait until
|
||||
"THR Empty" bit (1<<5 in LSR) is set
|
||||
before writing to UART.
|
||||
*/
|
||||
|
||||
impl Printer for RawConsole {
|
||||
#[inline]
|
||||
fn put_char(c: char) {
|
||||
|
11
src/arch/riscv/board/virt/reader.rs
Normal file
11
src/arch/riscv/board/virt/reader.rs
Normal file
@ -0,0 +1,11 @@
|
||||
use crate::arch::io::{RawConsole, Reader};
|
||||
|
||||
impl Reader for RawConsole {
|
||||
#[inline]
|
||||
fn get_char() -> char {
|
||||
let uart = super::UART0_BASE as *mut char;
|
||||
let line_sts = super::UART0_LSR as *mut u8;
|
||||
while unsafe { line_sts.read_volatile() } & 0x01 == 0 {}
|
||||
unsafe { uart.read_volatile() }
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
#[allow(unused_imports)]
|
||||
use crate::arch::io::{Printer, RawConsole};
|
||||
use crate::arch::io::{Printer, RawConsole, Reader};
|
||||
|
||||
#[cfg(feature = "board_default")]
|
||||
impl Printer for RawConsole {
|
||||
@ -8,3 +8,17 @@ impl Printer for RawConsole {
|
||||
sbi_rt::legacy::console_putchar(c as usize);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "board_default")]
|
||||
impl Reader for RawConsole {
|
||||
fn get_char() -> char {
|
||||
loop {
|
||||
#[allow(deprecated)]
|
||||
let ch = sbi_rt::legacy::console_getchar();
|
||||
if ch == usize::MAX {
|
||||
continue;
|
||||
}
|
||||
return ch as u8 as char;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#![feature(naked_functions)]
|
||||
#![feature(panic_info_message)]
|
||||
#![feature(fmt_internals)]
|
||||
#![feature(stmt_expr_attributes)]
|
||||
|
||||
// arch
|
||||
pub mod arch;
|
||||
|
Loading…
Reference in New Issue
Block a user