for hwsim target make xous-log output to the duart

this removes the kernel log but you have the verilator sim trace
to debug things
This commit is contained in:
bunnie 2025-09-20 22:12:19 +08:00
parent ee36a25acd
commit 0b1f2796f7
3 changed files with 57 additions and 14 deletions

View file

@ -44,6 +44,7 @@ board-baosec = ["bao1x-hal/board-baosec"]
board-baosor = ["bao1x-hal/board-baosor"]
board-dabao = ["bao1x-hal/board-dabao"]
hosted-baosec = []
hwsim = []
precursor = ["utralib/precursor"]
hosted = ["utralib/hosted"]

View file

@ -1,6 +1,6 @@
use core::fmt::{Error, Write};
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use bao1x_hal::udma;
#[macro_export]
@ -30,7 +30,7 @@ pub struct Uart {}
// this is a hack to bypass an explicit initialization/allocation step for the debug structure
pub static mut DEFAULT_UART_ADDR: *mut usize = 0x0000_0000 as *mut usize;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
impl Uart {
pub fn putc(&self, c: u8) {
// check that we've been initialized before attempting to send any characters...
@ -56,6 +56,18 @@ impl Uart {
uart.write(&[c]);
}
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
impl Uart {
pub fn putc(&self, c: u8) {
// check that we've been initialized before attempting to send any characters...
if unsafe { DEFAULT_UART_ADDR } as usize == 0 {
return;
}
let mut uart_csr = unsafe { utralib::CSR::new(DEFAULT_UART_ADDR) };
while uart_csr.r(utralib::utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utralib::utra::duart::SFR_TXD, c as usize);
}
}
impl Write for Uart {
fn write_str(&mut self, s: &str) -> Result<(), Error> {

View file

@ -1,28 +1,28 @@
use core::fmt::{Error, Write};
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use std::pin::Pin;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use bao1x_hal::board::UART_DMA_TX_BUF_PHYS;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
use bao1x_hal::udma;
use utralib::generated::*;
pub struct Output {}
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
pub static mut UART_DMA_TX_BUF_VIRT: *mut u8 = 0x0000_0000 as *mut u8;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
pub static mut UART_IRQ: Option<Pin<Box<bao1x_hal::udma::UartIrq>>> = None;
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
pub static mut KBD_CONN: u32 = 0;
pub fn init() -> Output {
// TODO: migrate this to a "proper" UART that is available on SoC hardware, but for now all we have access
// to is the DUART.
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::udma_uart_2::HW_UDMA_UART_2_BASE),
None,
@ -30,8 +30,11 @@ pub fn init() -> Output {
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map serial port");
unsafe { crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ };
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
unsafe {
crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _
};
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
{
// Note: for the TX buf, we allocate a pre-reserved portion of IFRAM as our
// DMA buffer. We do *not* use the IFRAM allocator in `bao1x-hal-service` because
@ -65,13 +68,25 @@ pub fn init() -> Output {
};
udma_uart.setup_async_read();
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::duart::HW_DUART_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map UDMA buffer");
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
unsafe {
crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _
};
println!("Mapped UART @ {:08x}", uart.as_ptr() as usize);
println!("Process: map success!");
Output {}
}
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
fn uart_handler(_irq_no: usize, _arg: *mut usize) {
let mut uart = unsafe {
udma::Uart::get_handle(
@ -117,7 +132,7 @@ pub struct OutputWriter {}
#[allow(dead_code)]
impl OutputWriter {
pub fn putc(&self, c: u8) {
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
{
// safety: safe to call as long as the raw parts are initialized and we exclusively
// own it; and the UART has been initialized. For this peripheral, initialization
@ -134,13 +149,19 @@ impl OutputWriter {
// enqueue our character to send via DMA
uart.write(&[c]);
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
{
let mut uart_csr = unsafe { CSR::new(crate::platform::debug::DEFAULT_UART_ADDR) };
while uart_csr.r(utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utra::duart::SFR_TXD, c as usize);
}
}
/// Write a buffer to the output and return the number of
/// bytes written. This is mostly compatible with `std::io::Write`,
/// except it is infallible.
pub fn write(&mut self, buf: &[u8]) -> usize {
#[cfg(feature = "bao1x")]
#[cfg(all(feature = "bao1x", not(feature = "hwsim")))]
{
// safety: safe to call as long as the raw parts are initialized and we exclusively
// own it; and the UART has been initialized. For this peripheral, initialization
@ -155,6 +176,15 @@ impl OutputWriter {
};
uart.write(buf)
}
#[cfg(all(feature = "bao1x", feature = "hwsim"))]
{
let mut uart_csr = unsafe { CSR::new(crate::platform::debug::DEFAULT_UART_ADDR) };
for &c in buf.iter() {
while uart_csr.r(utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utra::duart::SFR_TXD, c as usize);
}
buf.len()
}
}
pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> { Ok(self.write(buf)) }