feat: implement Reader on RawConsole

This commit is contained in:
Paul Pan 2023-09-15 16:49:55 +08:00
parent 59fbc75552
commit 2252edf228
6 changed files with 41 additions and 5 deletions

View File

@ -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(())

View File

@ -1,3 +1,5 @@
pub const UART0_BASE: usize = 0x1000_0000;
pub const UART0_LSR: usize = 0x1000_0005;
pub mod printer;
pub mod reader;

View File

@ -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) {

View 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() }
}
}

View File

@ -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;
}
}
}

View File

@ -3,6 +3,7 @@
#![feature(naked_functions)]
#![feature(panic_info_message)]
#![feature(fmt_internals)]
#![feature(stmt_expr_attributes)]
// arch
pub mod arch;