format services/xous-log

This commit is contained in:
bunnie 2024-01-14 22:56:54 +08:00
parent 4f6f595b2f
commit 931309590f
9 changed files with 83 additions and 130 deletions

View file

@ -7,8 +7,8 @@ use xous_api_log::api;
mod platform;
use core::fmt::Write;
use num_traits::FromPrimitive;
use num_traits::FromPrimitive;
use platform::implementation;
/// A page-aligned stack allocation for connection requests (used by USB resolver)
@ -21,13 +21,7 @@ struct ConnectRequest {
}
#[cfg(feature = "usb")]
impl Default for ConnectRequest {
fn default() -> Self {
ConnectRequest {
name: [0u8; 64],
len: 0,
_padding: [0u8; 4096 - 4 - 64],
}
}
fn default() -> Self { ConnectRequest { name: [0u8; 64], len: 0, _padding: [0u8; 4096 - 4 - 64] } }
}
#[cfg(feature = "usb")]
@ -39,10 +33,7 @@ pub struct UsbString {
#[cfg(feature = "usb")]
fn usb_send_str(conn: xous::CID, s: &str) {
let serializer = UsbString {
s: xous_ipc::String::<4000>::from_str(s),
sent: None,
};
let serializer = UsbString { s: xous_ipc::String::<4000>::from_str(s), sent: None };
let buf = xous_ipc::Buffer::into_buf(serializer).expect("usb error");
// failures to send are silent & ignored; also, this API doesn't block.
buf.send(conn, 8192 /* LogString */).expect("usb error");
@ -125,10 +116,12 @@ fn reader_thread(arg: usize) {
#[cfg(feature = "usb")]
if let Some(conn) = usb_serial {
if usb_str.len() > 0 {
// test length so we aren't constantly copying 4096 bytes of 0's clearing an already cleared structure.
// test length so we aren't constantly copying 4096 bytes of 0's clearing an
// already cleared structure.
usb_str.clear();
}
// duplicate the above code because doing repeated calls to the USB stack is inefficient
// duplicate the above code because doing repeated calls to the USB stack is
// inefficient
write!(usb_str, "{}:", level).ok();
for c in module_slice {
usb_str.push_byte(*c).ok();
@ -181,8 +174,8 @@ fn reader_thread(arg: usize) {
#[cfg(feature = "usb")]
if let Some(conn) = usb_serial {
// safety: this routine will just blow up if you try to pass non utf-8 to it,
// so it's not very safe. On the other hand, it's fast and shame on you for sending
// non-utf8 to this API.
// so it's not very safe. On the other hand, it's fast and shame on you for
// sending non-utf8 to this API.
usb_send_str(conn, unsafe { std::str::from_utf8_unchecked(buffer) });
}
}
@ -343,11 +336,8 @@ fn main() -> ! {
let mut writer = output.get_writer();
println!("LOG: my PID is {}", xous::process::id());
println!("LOG: Creating the reader thread");
xous::create_thread_1(
reader_thread,
&mut writer as *mut implementation::OutputWriter as usize,
)
.expect("create reader thread");
xous::create_thread_1(reader_thread, &mut writer as *mut implementation::OutputWriter as usize)
.expect("create reader thread");
println!("LOG: Running the output");
output.run();
panic!("LOG: Exited");

View file

@ -1,17 +1,14 @@
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-License-Identifier: Apache-2.0
use atsama5d27::{
uart::{Uart as UartHw, Uart1, UART_BASE_ADDRESS},
};
use core::fmt::{Error, Write};
use atsama5d27::uart::{Uart as UartHw, Uart1, UART_BASE_ADDRESS};
#[cfg(feature = "lcd-console")]
use atsama5d27::{lcdc::Lcdc, console::DisplayAndUartConsole, display::FramebufDisplay};
use atsama5d27::{console::DisplayAndUartConsole, display::FramebufDisplay, lcdc::Lcdc};
#[cfg(feature = "lcd-console")]
use utralib::HW_LCDC_BASE;
use core::fmt::{Error, Write};
const UART_NUMBER: usize = 1;
pub type UartType = UartHw<Uart1>; // Make sure this matches the UART_NUMBER above
pub const HW_UART_BASE: u32 = UART_BASE_ADDRESS[UART_NUMBER];
@ -52,21 +49,16 @@ impl Console<UartType> {
// 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");
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");
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(
@ -75,11 +67,13 @@ impl Console<UartType> {
FB_SIZE_BYTES,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map lcd framebuffer");
.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); }
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 {
@ -98,7 +92,7 @@ impl Console<UartType> {
4096 * 4, // In ATSAMA5D2 peripherals occupy 16K
xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::DEV,
)
.expect("couldn't map LCDC peripheral");
.expect("couldn't map LCDC peripheral");
let lcdc_addr = addr.as_mut_ptr() as _;
let mut lcdc = Lcdc::new_vma(
lcdc_addr,

View file

@ -1,4 +1,5 @@
use core::fmt::{Error, Write};
use crate::platform::console::ConsoleSingleton;
pub struct Output {}
@ -17,9 +18,7 @@ pub fn init() -> Output {
}
impl Output {
pub fn get_writer(&self) -> OutputWriter {
OutputWriter {}
}
pub fn get_writer(&self) -> OutputWriter { OutputWriter {} }
pub fn run(&mut self) {
loop {
@ -32,8 +31,8 @@ impl Output {
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:
//let mut uart_csr = crate::platform::debug::UartType::with_alt_base_addr(unsafe {
// crate::platform::debug::DEFAULT_UART_ADDR as u32 }); TODO:
}
}
@ -56,14 +55,9 @@ impl OutputWriter {
buf.len()
}
pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> {
Ok(self.write(buf))
}
pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> { Ok(self.write(buf)) }
}
impl Write for OutputWriter {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
ConsoleSingleton {}.write_str(s)
}
fn write_str(&mut self, s: &str) -> Result<(), Error> { ConsoleSingleton {}.write_str(s) }
}

View file

@ -29,7 +29,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="cramium-fpga")]
#[cfg(feature = "cramium-fpga")]
impl Uart {
pub fn putc(&self, c: u8) {
assert!(unsafe { DEFAULT_UART_ADDR } as usize != 0);
@ -40,7 +40,7 @@ impl Uart {
}
}
#[cfg(feature="cramium-soc")]
#[cfg(feature = "cramium-soc")]
impl Uart {
pub fn putc(&self, c: u8) {
let mut uart_csr = CSR::new(unsafe { crate::platform::debug::DEFAULT_UART_ADDR as *mut u32 });
@ -56,8 +56,8 @@ impl Uart {
// send it
uart_csr.wo(utra::udma_uart_0::REG_TX_CFG, 0x10); // EN
// wait for it all to be done
while uart_csr.rf(utra::udma_uart_0::REG_TX_CFG_R_TX_EN) != 0 { }
while (uart_csr.r(utra::udma_uart_0::REG_STATUS) & 1) != 0 { }
while uart_csr.rf(utra::udma_uart_0::REG_TX_CFG_R_TX_EN) != 0 {}
while (uart_csr.r(utra::udma_uart_0::REG_STATUS) & 1) != 0 {}
}
}

View file

@ -1,13 +1,14 @@
use core::fmt::{Error, Write};
use utralib::generated::*;
pub struct Output {}
#[cfg(feature="cramium-soc")]
#[cfg(feature = "cramium-soc")]
pub static mut UART_DMA_BUF: *mut u8 = 0x0000_0000 as *mut u8;
pub fn init() -> Output {
#[cfg(feature="cramium-fpga")]
#[cfg(feature = "cramium-fpga")]
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::duart::HW_DUART_BASE),
None,
@ -15,8 +16,9 @@ pub fn init() -> Output {
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map serial port");
// 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="cramium-soc")]
// 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 = "cramium-soc")]
let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::udma_uart_0::HW_UDMA_UART_0_BASE),
None,
@ -28,7 +30,7 @@ pub fn init() -> Output {
println!("Mapped UART @ {:08x}", uart.as_ptr() as usize);
println!("Process: map success!");
#[cfg(feature="cramium-soc")]
#[cfg(feature = "cramium-soc")]
{
// TODO: need to write an allocator for the UDMA memory region as well
let tx_buf_region = xous::syscall::map_memory(
@ -98,7 +100,7 @@ pub fn init() -> Output {
*/
}
#[cfg(feature="inject")]
#[cfg(feature = "inject")]
{
let inject_mem = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::keyinject::HW_KEYINJECT_BASE),
@ -106,7 +108,7 @@ pub fn init() -> Output {
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map keyinjection CSR range");
.expect("couldn't map keyinjection CSR range");
println!("Note: character injection via console UART is enabled.");
println!("Allocating IRQ...");
@ -115,7 +117,7 @@ pub fn init() -> Output {
handle_console_irq,
inject_mem.as_mut_ptr() as *mut usize,
)
.expect("couldn't claim interrupt");
.expect("couldn't claim interrupt");
println!("Claimed IRQ {}", utra::console::CONSOLE_IRQ);
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
}
@ -123,9 +125,7 @@ pub fn init() -> Output {
}
impl Output {
pub fn get_writer(&self) -> OutputWriter {
OutputWriter {}
}
pub fn get_writer(&self) -> OutputWriter { OutputWriter {} }
pub fn run(&mut self) {
loop {
@ -134,7 +134,7 @@ impl Output {
}
}
#[cfg(feature="inject")]
#[cfg(feature = "inject")]
fn handle_console_irq(_irq_no: usize, arg: *mut usize) {
if cfg!(feature = "logging") {
let mut inject_csr = CSR::new(arg as *mut u32);
@ -142,10 +142,7 @@ fn handle_console_irq(_irq_no: usize, arg: *mut usize) {
// println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY));
while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 {
// I really rather think this is more readable, than the "Rusty" version below.
inject_csr.wfo(
utra::keyinject::UART_CHAR_CHAR,
uart_csr.rf(utra::uart::RXTX_RXTX),
);
inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, uart_csr.rf(utra::uart::RXTX_RXTX));
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
// I guess this is how you would do it if you were "really doing Rust"
@ -175,19 +172,19 @@ pub struct OutputWriter {}
impl OutputWriter {
pub fn putc(&self, c: u8) {
#[cfg(feature="cramium-fpga")]
#[cfg(feature = "cramium-fpga")]
{
let mut uart_csr = CSR::new(unsafe { crate::platform::debug::DEFAULT_UART_ADDR as *mut u32 });
while uart_csr.r(utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utra::duart::SFR_TXD, c as u32);
// there's a race condition in the handler, if a new character comes in while handling the interrupt,
// the pending bit never clears. If the console seems to freeze, uncomment this line.
// This kind of works around that, at the expense of maybe losing some Rx characters.
// uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
// there's a race condition in the handler, if a new character comes in while handling the
// interrupt, the pending bit never clears. If the console seems to freeze, uncomment
// this line. This kind of works around that, at the expense of maybe losing some Rx
// characters. uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
}
#[cfg(feature="cramium-soc")]
#[cfg(feature = "cramium-soc")]
{
let mut uart_csr = CSR::new(unsafe { crate::platform::debug::DEFAULT_UART_ADDR as *mut u32 });
// enqueue our character to send via DMA
@ -202,8 +199,8 @@ impl OutputWriter {
// send it
uart_csr.wo(utra::udma_uart_0::REG_TX_CFG, 0x10); // EN
// wait for it all to be done
while uart_csr.rf(utra::udma_uart_0::REG_TX_CFG_R_TX_EN) != 0 { }
while (uart_csr.r(utra::udma_uart_0::REG_STATUS) & 1) != 0 { }
while uart_csr.rf(utra::udma_uart_0::REG_TX_CFG_R_TX_EN) != 0 {}
while (uart_csr.r(utra::udma_uart_0::REG_STATUS) & 1) != 0 {}
}
}
@ -211,7 +208,7 @@ impl OutputWriter {
/// bytes written. This is mostly compatible with `std::io::Write`,
/// except it is infallible.
pub fn write(&mut self, buf: &[u8]) -> usize {
#[cfg(feature="cramium-soc")]
#[cfg(feature = "cramium-soc")]
{
// write the whole buffer via DMA, and then idle with yield_slice() for better
// concurrency (as opposed to character-by-character polling).
@ -247,7 +244,7 @@ impl OutputWriter {
writelen
}
#[cfg(feature="cramium-fpga")]
#[cfg(feature = "cramium-fpga")]
{
for c in buf {
self.putc(*c)
@ -256,9 +253,7 @@ impl OutputWriter {
}
}
pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> {
Ok(self.write(buf))
}
pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> { Ok(self.write(buf)) }
}
impl Write for OutputWriter {

View file

@ -16,11 +16,7 @@ pub struct Output {
pub fn init() -> Output {
let (tx, rx) = channel();
Output {
tx,
rx,
stdout: std::io::stdout(),
}
Output { tx, rx, stdout: std::io::stdout() }
}
impl Output {
@ -42,17 +38,11 @@ impl Output {
}
}
pub fn get_writer(&self) -> OutputWriter {
OutputWriter {
tx: self.tx.clone(),
}
}
pub fn get_writer(&self) -> OutputWriter { OutputWriter { tx: self.tx.clone() } }
}
impl Drop for Output {
fn drop(&mut self) {
self.tx.send(ControlMessage::Exit).unwrap();
}
fn drop(&mut self) { self.tx.send(ControlMessage::Exit).unwrap(); }
}
impl Write for Output {
@ -68,9 +58,7 @@ pub struct OutputWriter {
}
impl OutputWriter {
pub fn putc(&self, c: u8) {
self.tx.send(ControlMessage::Byte(c)).unwrap();
}
pub fn putc(&self, c: u8) { self.tx.send(ControlMessage::Byte(c)).unwrap(); }
}
impl Write for OutputWriter {

View file

@ -1,11 +1,10 @@
#[cfg(any(feature="precursor", feature="renode"))]
#[cfg(any(feature = "precursor", feature = "renode"))]
#[macro_use]
pub mod precursor;
#[cfg(any(feature="precursor", feature="renode"))]
#[cfg(any(feature = "precursor", feature = "renode"))]
pub use precursor::debug;
#[cfg(any(feature="precursor", feature="renode"))]
#[cfg(any(feature = "precursor", feature = "renode"))]
pub use precursor::implementation;
#[cfg(not(target_os = "xous"))]
@ -15,25 +14,22 @@ pub mod hosted;
#[cfg(not(target_os = "xous"))]
pub use hosted::implementation;
#[cfg(any(feature="atsama5d27"))]
#[cfg(any(feature = "atsama5d27"))]
#[macro_use]
pub mod atsama5d2;
#[cfg(feature="atsama5d27")]
pub use atsama5d2::debug;
#[cfg(feature="atsama5d27")]
#[cfg(feature = "atsama5d27")]
pub use atsama5d2::console;
#[cfg(feature="atsama5d27")]
#[cfg(feature = "atsama5d27")]
pub use atsama5d2::debug;
#[cfg(feature = "atsama5d27")]
pub use atsama5d2::implementation;
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
#[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))]
#[macro_use]
pub mod cramium;
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
#[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))]
pub use cramium::debug;
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
#[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))]
pub use cramium::implementation;

View file

@ -60,13 +60,13 @@ impl Uart {
while uart_csr.r(utra::uart::TXFULL) != 0 {}
uart_csr.wo(utra::uart::RXTX, c as u32);
}
/// This is an abstraction violation, but this call is actually relied upon by the
/// non-debug path to re-enable UART RX during a resume. This should be cleaned up.
pub fn enable_rx(&self) {
let mut uart_csr = CSR::new(unsafe { DEFAULT_UART_ADDR as *mut u32 });
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
}
}
impl Write for Uart {

View file

@ -1,4 +1,5 @@
use core::fmt::{Error, Write};
use utralib::generated::*;
pub struct Output {}
@ -11,7 +12,7 @@ pub fn init() -> Output {
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map serial port");
.expect("couldn't map serial port");
unsafe { crate::platform::debug::DEFAULT_UART_ADDR = uart.as_mut_ptr() as _ };
println!("Mapped UART @ {:08x}", uart.as_ptr() as usize);
let mut uart_csr = CSR::new(uart.as_mut_ptr() as *mut u32);
@ -24,7 +25,7 @@ pub fn init() -> Output {
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map keyinjection CSR range");
.expect("couldn't map keyinjection CSR range");
println!("Note: character injection via console UART is enabled.");
println!("Allocating IRQ...");
@ -33,7 +34,7 @@ pub fn init() -> Output {
handle_console_irq,
inject_mem.as_mut_ptr() as *mut usize,
)
.expect("couldn't claim interrupt");
.expect("couldn't claim interrupt");
println!("Claimed IRQ {}", utra::console::CONSOLE_IRQ);
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
}
@ -41,9 +42,7 @@ pub fn init() -> Output {
}
impl Output {
pub fn get_writer(&self) -> OutputWriter {
OutputWriter {}
}
pub fn get_writer(&self) -> OutputWriter { OutputWriter {} }
pub fn run(&mut self) {
loop {
@ -59,10 +58,7 @@ fn handle_console_irq(_irq_no: usize, arg: *mut usize) {
// println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY));
while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 {
// I really rather think this is more readable, than the "Rusty" version below.
inject_csr.wfo(
utra::keyinject::UART_CHAR_CHAR,
uart_csr.rf(utra::uart::RXTX_RXTX),
);
inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, uart_csr.rf(utra::uart::RXTX_RXTX));
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
// I guess this is how you would do it if you were "really doing Rust"
@ -99,10 +95,10 @@ impl OutputWriter {
while uart_csr.r(utra::uart::TXFULL) != 0 {}
uart_csr.wo(utra::uart::RXTX, c as u32);
// there's a race condition in the handler, if a new character comes in while handling the interrupt,
// the pending bit never clears. If the console seems to freeze, uncomment this line.
// This kind of works around that, at the expense of maybe losing some Rx characters.
// uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
// there's a race condition in the handler, if a new character comes in while handling the
// interrupt, the pending bit never clears. If the console seems to freeze, uncomment
// this line. This kind of works around that, at the expense of maybe losing some Rx
// characters. uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
}
}
}