Skip to content

Commit

Permalink
feat(board): support SBI call #2 getchar
Browse files Browse the repository at this point in the history
Support SBI call #0 Console Getchar from UART0.

Signed-off-by: tfx2001 <tfx2001@outlook.com>
  • Loading branch information
tfx2001 committed Jul 24, 2024
1 parent 92655d5 commit b6ac53e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
22 changes: 18 additions & 4 deletions src/board/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ static UART: Mutex<MaybeUninit<Uart>> = Mutex::new(MaybeUninit::uninit());
#[macro_export]
macro_rules! print {
($fmt: literal $(, $($args: tt)+)?) => {
$crate::board::_print(format_args!($fmt $(, $($args)+)?));
$crate::board::putchar(format_args!($fmt $(, $($args)+)?));
};
}

#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => {{
$crate::board::_print(core::format_args!($($arg)*));
$crate::board::putchar(core::format_args!($($arg)*));
$crate::println!();
}}
}
Expand All @@ -47,7 +47,7 @@ pub fn board_init() {
pins.setup();

let uart = Uart::new(pac::UART0);
uart.setup(250_000, clock.get_clk_freq(clocks::URT0));
uart.setup(115_200, clock.get_clk_freq(clocks::URT0));
*UART.lock() = MaybeUninit::new(uart);

let cpu0_clock_freq = clock.get_cpu0_clk_freq();
Expand All @@ -68,12 +68,26 @@ pub fn board_init() {
}

#[inline]
pub fn _print(args: fmt::Arguments) {
pub fn putchar(args: fmt::Arguments) {
let mut guard = UART.lock();

unsafe { guard.assume_init_mut().write_fmt(args).unwrap() }
}

#[inline]
pub fn getchar() -> usize {
let mut guard = UART.lock();
let mut c: u8 = 0;

unsafe {
if guard.assume_init_mut().receive_byte(&mut c) {
c as _
} else {
usize::MAX
}
}
}

pub fn board_init_timer() -> MachineTimer {
MachineTimer::new(pac::MCHTMR)
}
15 changes: 15 additions & 0 deletions src/board/uart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,26 @@ impl Uart {
self.inner.lsr().read().thre()
}

#[inline]
fn is_data_ready(&self) -> bool {
self.inner.lsr().read().dr()
}

#[inline]
pub fn send_byte(&self, byte: u8) {
while !self.is_tx_fifo_empty() {}
self.inner.dll().write(|w| w.set_dll(byte));
}

#[inline]
pub fn receive_byte(&self, byte: &mut u8) -> bool {
if self.is_data_ready() {
*byte = self.inner.rbr().read().rbr();
true
} else {
false
}
}
}

impl Write for Uart {
Expand Down
6 changes: 5 additions & 1 deletion src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use riscv::register::{
};
use rustsbi::RustSBI;

use crate::board;
use crate::extension::SBI;
use crate::local_hsm;
use crate::print;
Expand Down Expand Up @@ -128,7 +129,10 @@ pub extern "C" fn fast_handler(
ret.error = 0;
ret.value = a1;
}
legacy::LEGACY_CONSOLE_GETCHAR => unimplemented!(),
legacy::LEGACY_CONSOLE_GETCHAR => {
ret.error = board::getchar();
ret.value = a1;
}
_ => unimplemented!(
"EID: {:#010x} FID: {:#010x} is not implemented!",
a7,
Expand Down

0 comments on commit b6ac53e

Please sign in to comment.