From e8fd388ae9d8a0d19a7111922b38c9e48e653dda Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 22 Dec 2024 00:34:16 +0800 Subject: [PATCH] add support for paged mode debugging we have to allocate the virtual page in our process space before we can use the debug UART. --- libs/cramium-hal/src/debug.rs | 52 +++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/libs/cramium-hal/src/debug.rs b/libs/cramium-hal/src/debug.rs index 1d68f956b..ce022249e 100644 --- a/libs/cramium-hal/src/debug.rs +++ b/libs/cramium-hal/src/debug.rs @@ -1,23 +1,71 @@ +#[allow(unused_imports)] use utralib::generated::*; pub struct Uart {} +#[allow(dead_code)] +pub static mut SWAP_APP_UART_VADDR: usize = 0; +#[allow(dead_code)] +pub static mut SWAP_APP_UART_IFRAM_VADDR: usize = 0; + impl Uart { + #[cfg(all(feature = "debug-print-usb", not(target_os = "xous")))] pub fn putc(&self, c: u8) { let base = utra::duart::HW_DUART_BASE as *mut u32; let mut uart = CSR::new(base); while uart.r(utra::duart::SFR_SR) != 0 {} uart.wo(utra::duart::SFR_TXD, c as u32); } + + #[cfg(all(feature = "debug-print-usb", target_os = "xous"))] + pub fn putc(&mut self, c: u8) { + if unsafe { SWAP_APP_UART_VADDR } == 0 { + let uart = xous::syscall::map_memory( + xous::MemoryAddress::new(utralib::utra::udma_uart_0::HW_UDMA_UART_0_BASE), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map debug serial port"); + unsafe { SWAP_APP_UART_VADDR = uart.as_mut_ptr() as usize }; + } + if unsafe { SWAP_APP_UART_IFRAM_VADDR } == 0 { + let mem = xous::syscall::map_memory( + xous::MemoryAddress::new(crate::board::APP_UART_IFRAM_ADDR), + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map debug serial port dma ram"); + unsafe { SWAP_APP_UART_IFRAM_VADDR = mem.as_mut_ptr() as usize }; + } + use crate::udma; + // 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 + // is handled by the loader and tossed to us, and exclusivity of access is something + // we just have to not bungle. + let mut uart = unsafe { + udma::Uart::get_handle( + SWAP_APP_UART_VADDR as usize, + crate::board::APP_UART_IFRAM_ADDR as usize, + SWAP_APP_UART_IFRAM_VADDR as usize, + ) + }; + // enqueue our character to send via DMA + uart.write(&[c]); + } + + #[cfg(not(feature = "debug-print-usb"))] + pub fn putc(&mut self, _c: u8) {} } use core::fmt::{Error, Write}; impl Write for Uart { fn write_str(&mut self, s: &str) -> Result<(), Error> { - #[cfg(not(feature = "std"))] + #[cfg(not(all(feature = "std", not(feature = "debug-print-usb"))))] for c in s.bytes() { self.putc(c); } - #[cfg(feature = "std")] + #[cfg(all(feature = "std", not(feature = "debug-print-usb")))] log::info!("{}", s); Ok(()) }