diff --git a/services/xous-log/src/main.rs b/services/xous-log/src/main.rs index 0d9c73f25..13b919c54 100644 --- a/services/xous-log/src/main.rs +++ b/services/xous-log/src/main.rs @@ -3,7 +3,7 @@ use xous_api_log::api; -#[cfg(any(feature="precursor", feature="renode"))] +#[cfg(any(feature="precursor", feature="renode", feature = "atsama5d27"))] #[macro_use] mod platform; diff --git a/services/xous-log/src/platform/atsama5d2/console.rs b/services/xous-log/src/platform/atsama5d2/console.rs new file mode 100644 index 000000000..eec4b6f59 --- /dev/null +++ b/services/xous-log/src/platform/atsama5d2/console.rs @@ -0,0 +1,167 @@ +// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc. +// SPDX-License-Identifier: Apache-2.0 + +use atsama5d27::{ + uart::{Uart as UartHw, Uart1, UART_BASE_ADDRESS}, +}; + +#[cfg(feature = "lcd-console")] +use atsama5d27::{lcdc::Lcdc, console::DisplayAndUartConsole, display::FramebufDisplay}; +#[cfg(feature = "lcd-console")] +use utralib::HW_LCDC_BASE; + +use core::fmt::{Error, Write}; + +const UART_NUMBER: usize = 1; +pub type UartType = UartHw; // Make sure this matches the UART_NUMBER above +pub const HW_UART_BASE: u32 = UART_BASE_ADDRESS[UART_NUMBER]; + +#[cfg(feature = "lcd-console")] +const WIDTH: usize = 800; +#[cfg(feature = "lcd-console")] +const HEIGHT: usize = 480; +#[cfg(feature = "lcd-console")] +const FB_SIZE_BYTES: usize = WIDTH * HEIGHT * 4; // RGBA888 is 4 bytes per pixel + +pub static mut CONSOLE: Option> = None; + +/// UART and display console. +pub struct Console { + #[cfg(feature = "lcd-console")] + inner: DisplayAndUartConsole, + + #[cfg(not(feature = "lcd-console"))] + inner: U, +} + +impl Console { + pub fn new() -> Self { + // Map the UART peripheral. + let addr = xous::syscall::map_memory( + xous::MemoryAddress::new(HW_UART_BASE as usize), + None, + 4096 * 4, // In ATSAMA5D2 peripherals occupy 16K + xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::DEV, + ) + .expect("couldn't map debug UART"); + + let addr = addr.as_mut_ptr() as _; + let mut uart = UartType::with_alt_base_addr(addr); + writeln!(uart, "[xous-log] Allocated UART peripheral at {:08x}", addr).ok(); + + // Allocate a page for DMA descriptor + #[cfg(feature = "lcd-console")] + let inner = { + let dma_desc_addr = xous::syscall::map_memory( + None, + None, + 4096, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map LCDC DMA descriptor"); + + // Access the address of the page to get it physically allocated + unsafe { + dma_desc_addr.as_mut_ptr().write_volatile(0); + } + let dma_desc_addr = dma_desc_addr.as_ptr() as _; + let dma_desc_addr_phys = + xous::syscall::virt_to_phys(dma_desc_addr).expect("can't convert v2p"); + + // Allocate framebuffer pages + let fb_addr = xous::syscall::map_memory( + None, + None, + FB_SIZE_BYTES, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("couldn't map lcd framebuffer"); + + // Access the addresses of the framebuffer pages to give them + // a continuous physical backing + unsafe { fb_addr.as_mut_ptr().write_volatile(0x00); } + let fb_addr = fb_addr.as_ptr() as _; + for page in (0..FB_SIZE_BYTES).into_iter().step_by(4096) { + unsafe { + let page_addr = fb_addr + page; + let page_ptr = page_addr as *mut u32; + page_ptr.write_volatile(0); + } + } + + let fb_addr_phys = xous::syscall::virt_to_phys(fb_addr).expect("can't convert v2p"); + + // Map the LCDC peripheral. + let addr = xous::syscall::map_memory( + xous::MemoryAddress::new(HW_LCDC_BASE as usize), + None, + 4096 * 4, // In ATSAMA5D2 peripherals occupy 16K + xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::DEV, + ) + .expect("couldn't map LCDC peripheral"); + let lcdc_addr = addr.as_mut_ptr() as _; + let mut lcdc = Lcdc::new_vma( + lcdc_addr, + fb_addr_phys, + WIDTH as u16, + HEIGHT as u16, + dma_desc_addr, + dma_desc_addr_phys, + ); + lcdc.init(); + + writeln!(uart, "[xous-log] LCDC initialized").ok(); + + let fb: &'static mut [u32; WIDTH * HEIGHT] = unsafe { + let ptr: *const [u32; WIDTH * HEIGHT] = fb_addr as _; + core::mem::transmute(ptr) + }; + + let display = FramebufDisplay::new(fb, WIDTH, HEIGHT); + let mut inner = DisplayAndUartConsole::new(display, uart); + writeln!(inner, "").ok(); + inner + }; + #[cfg(not(feature = "lcd-console"))] + let inner = uart; + + // Claim UART interrupt. + // TODO: + /* + println!("Claiming IRQ {} via syscall...", utra::uart::UART_IRQ); + xous_kernel::claim_interrupt( + utra::uart::UART_IRQ, + Uart::irq, + (UART.as_mut().unwrap() as *mut Uart) as *mut usize, + ).expect("Couldn't claim debug interrupt"); + */ + + Self { inner } + } + + fn write_str(s: &str) { + unsafe { + if CONSOLE.is_none() { + CONSOLE.replace(Console::new()); + } + } + + if let Some(console) = unsafe { &mut CONSOLE } { + #[cfg(feature = "lcd-console")] + console.inner.write_str(s).ok(); + + #[cfg(not(feature = "lcd-console"))] + console.inner.write_str(s); + } + } +} + +pub struct ConsoleSingleton {} + +impl Write for ConsoleSingleton { + fn write_str(&mut self, s: &str) -> Result<(), Error> { + Console::write_str(s); + + Ok(()) + } +} diff --git a/services/xous-log/src/platform/atsama5d2/debug.rs b/services/xous-log/src/platform/atsama5d2/debug.rs new file mode 100644 index 000000000..f267dd23e --- /dev/null +++ b/services/xous-log/src/platform/atsama5d2/debug.rs @@ -0,0 +1,24 @@ +// SPDX-FileCopyrightText: 2022 Foundation Devices, Inc. +// SPDX-License-Identifier: Apache-2.0 + +#[macro_export] +macro_rules! print +{ + ($($args:tt)+) => ({ + use core::fmt::Write; + let _ = write!(crate::platform::console::ConsoleSingleton {}, $($args)+); + }); +} +#[macro_export] +macro_rules! println +{ + () => ({ + print!("\r\n") + }); + ($fmt:expr) => ({ + print!(concat!($fmt, "\r\n")) + }); + ($fmt:expr, $($args:tt)+) => ({ + print!(concat!($fmt, "\r\n"), $($args)+) + }); +} diff --git a/services/xous-log/src/platform/atsama5d2/implementation.rs b/services/xous-log/src/platform/atsama5d2/implementation.rs new file mode 100644 index 000000000..111938bdb --- /dev/null +++ b/services/xous-log/src/platform/atsama5d2/implementation.rs @@ -0,0 +1,69 @@ +use core::fmt::{Error, Write}; +use crate::platform::console::ConsoleSingleton; + +pub struct Output {} + +pub fn init() -> Output { + if cfg!(feature = "logging") { + // println!("[xous-log::implementation] Installing custom panic hook..."); + // std::panic::set_hook(Box::new(|info| { + // println!("[!] [xous-log panic] {}", info); + // unsafe { core::arch::asm!("bkpt"); } + // })); + // println!("[xous-log::implementation] Panic hook installed"); + } + + Output {} +} + +impl Output { + pub fn get_writer(&self) -> OutputWriter { + OutputWriter {} + } + + pub fn run(&mut self) { + loop { + xous::wait_event(); + } + } +} + +#[allow(dead_code)] +fn handle_console_irq(_irq_no: usize, _arg: *mut usize) { + if cfg!(feature = "logging") { + // let mut inject_csr = CSR::new(arg as *mut u32); + //let mut uart_csr = crate::platform::debug::UartType::with_alt_base_addr(unsafe { crate::platform::debug::DEFAULT_UART_ADDR as u32 }); + // TODO: + } +} + +pub struct OutputWriter {} + +impl OutputWriter { + pub fn putc(&self, c: u8) { + if cfg!(feature = "logging") { + write!(ConsoleSingleton {}, "{}", c as char).ok(); + } + } + + /// 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 { + for c in buf { + self.putc(*c); + } + buf.len() + } + + pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result { + Ok(self.write(buf)) + } +} + +impl Write for OutputWriter { + fn write_str(&mut self, s: &str) -> Result<(), Error> { + ConsoleSingleton {}.write_str(s) + } +} + diff --git a/services/xous-log/src/platform/atsama5d2/mod.rs b/services/xous-log/src/platform/atsama5d2/mod.rs new file mode 100644 index 000000000..1b9279e34 --- /dev/null +++ b/services/xous-log/src/platform/atsama5d2/mod.rs @@ -0,0 +1,6 @@ +#[macro_use] +pub mod debug; + +pub mod implementation; + +pub mod console; diff --git a/services/xous-log/src/platform/mod.rs b/services/xous-log/src/platform/mod.rs index b3ee8de7b..df1c36ca7 100644 --- a/services/xous-log/src/platform/mod.rs +++ b/services/xous-log/src/platform/mod.rs @@ -11,3 +11,16 @@ pub mod hosted; #[cfg(not(target_os = "xous"))] pub use hosted::implementation; + +#[cfg(any(feature="atsama5d27"))] +#[macro_use] +pub mod atsama5d2; + +#[cfg(feature="atsama5d27")] +pub use atsama5d2::debug; + +#[cfg(feature="atsama5d27")] +pub use atsama5d2::console; + +#[cfg(feature="atsama5d27")] +pub use atsama5d2::implementation;