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

View file

@ -1,17 +1,14 @@
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc. <hello@foundationdevices.com> // SPDX-FileCopyrightText: 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
use atsama5d27::{ use core::fmt::{Error, Write};
uart::{Uart as UartHw, Uart1, UART_BASE_ADDRESS},
};
use atsama5d27::uart::{Uart as UartHw, Uart1, UART_BASE_ADDRESS};
#[cfg(feature = "lcd-console")] #[cfg(feature = "lcd-console")]
use atsama5d27::{lcdc::Lcdc, console::DisplayAndUartConsole, display::FramebufDisplay}; use atsama5d27::{console::DisplayAndUartConsole, display::FramebufDisplay, lcdc::Lcdc};
#[cfg(feature = "lcd-console")] #[cfg(feature = "lcd-console")]
use utralib::HW_LCDC_BASE; use utralib::HW_LCDC_BASE;
use core::fmt::{Error, Write};
const UART_NUMBER: usize = 1; const UART_NUMBER: usize = 1;
pub type UartType = UartHw<Uart1>; // Make sure this matches the UART_NUMBER above pub type UartType = UartHw<Uart1>; // Make sure this matches the UART_NUMBER above
pub const HW_UART_BASE: u32 = UART_BASE_ADDRESS[UART_NUMBER]; pub const HW_UART_BASE: u32 = UART_BASE_ADDRESS[UART_NUMBER];
@ -52,12 +49,8 @@ impl Console<UartType> {
// Allocate a page for DMA descriptor // Allocate a page for DMA descriptor
#[cfg(feature = "lcd-console")] #[cfg(feature = "lcd-console")]
let inner = { let inner = {
let dma_desc_addr = xous::syscall::map_memory( let dma_desc_addr =
None, xous::syscall::map_memory(None, None, 4096, xous::MemoryFlags::R | xous::MemoryFlags::W)
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map LCDC DMA descriptor"); .expect("couldn't map LCDC DMA descriptor");
// Access the address of the page to get it physically allocated // Access the address of the page to get it physically allocated
@ -65,8 +58,7 @@ impl Console<UartType> {
dma_desc_addr.as_mut_ptr().write_volatile(0); dma_desc_addr.as_mut_ptr().write_volatile(0);
} }
let dma_desc_addr = dma_desc_addr.as_ptr() as _; let dma_desc_addr = dma_desc_addr.as_ptr() as _;
let dma_desc_addr_phys = let dma_desc_addr_phys = xous::syscall::virt_to_phys(dma_desc_addr).expect("can't convert v2p");
xous::syscall::virt_to_phys(dma_desc_addr).expect("can't convert v2p");
// Allocate framebuffer pages // Allocate framebuffer pages
let fb_addr = xous::syscall::map_memory( let fb_addr = xous::syscall::map_memory(
@ -79,7 +71,9 @@ impl Console<UartType> {
// Access the addresses of the framebuffer pages to give them // Access the addresses of the framebuffer pages to give them
// a continuous physical backing // 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 _; let fb_addr = fb_addr.as_ptr() as _;
for page in (0..FB_SIZE_BYTES).into_iter().step_by(4096) { for page in (0..FB_SIZE_BYTES).into_iter().step_by(4096) {
unsafe { unsafe {

View file

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

View file

@ -1,4 +1,5 @@
use core::fmt::{Error, Write}; use core::fmt::{Error, Write};
use utralib::generated::*; use utralib::generated::*;
pub struct Output {} pub struct Output {}
@ -15,7 +16,8 @@ pub fn init() -> Output {
xous::MemoryFlags::R | xous::MemoryFlags::W, xous::MemoryFlags::R | xous::MemoryFlags::W,
) )
.expect("couldn't map serial port"); .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. // 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")] #[cfg(feature = "cramium-soc")]
let uart = xous::syscall::map_memory( let uart = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::udma_uart_0::HW_UDMA_UART_0_BASE), xous::MemoryAddress::new(utra::udma_uart_0::HW_UDMA_UART_0_BASE),
@ -123,9 +125,7 @@ pub fn init() -> Output {
} }
impl Output { impl Output {
pub fn get_writer(&self) -> OutputWriter { pub fn get_writer(&self) -> OutputWriter { OutputWriter {} }
OutputWriter {}
}
pub fn run(&mut self) { pub fn run(&mut self) {
loop { loop {
@ -142,10 +142,7 @@ fn handle_console_irq(_irq_no: usize, arg: *mut usize) {
// println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY)); // println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY));
while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 { while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 {
// I really rather think this is more readable, than the "Rusty" version below. // I really rather think this is more readable, than the "Rusty" version below.
inject_csr.wfo( inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, uart_csr.rf(utra::uart::RXTX_RXTX));
utra::keyinject::UART_CHAR_CHAR,
uart_csr.rf(utra::uart::RXTX_RXTX),
);
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1); uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
// I guess this is how you would do it if you were "really doing Rust" // I guess this is how you would do it if you were "really doing Rust"
@ -182,10 +179,10 @@ impl OutputWriter {
while uart_csr.r(utra::duart::SFR_SR) != 0 {} while uart_csr.r(utra::duart::SFR_SR) != 0 {}
uart_csr.wo(utra::duart::SFR_TXD, c as u32); 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, // there's a race condition in the handler, if a new character comes in while handling the
// the pending bit never clears. If the console seems to freeze, uncomment this line. // interrupt, the pending bit never clears. If the console seems to freeze, uncomment
// This kind of works around that, at the expense of maybe losing some Rx characters. // this line. This kind of works around that, at the expense of maybe losing some Rx
// uart_csr.wfo(utra::uart::EV_PENDING_RX, 1); // characters. uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
} }
#[cfg(feature = "cramium-soc")] #[cfg(feature = "cramium-soc")]
{ {
@ -256,9 +253,7 @@ impl OutputWriter {
} }
} }
pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> { pub fn write_all(&mut self, buf: &[u8]) -> core::result::Result<usize, ()> { Ok(self.write(buf)) }
Ok(self.write(buf))
}
} }
impl Write for OutputWriter { impl Write for OutputWriter {

View file

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

View file

@ -4,7 +4,6 @@ pub mod precursor;
#[cfg(any(feature = "precursor", feature = "renode"))] #[cfg(any(feature = "precursor", feature = "renode"))]
pub use precursor::debug; pub use precursor::debug;
#[cfg(any(feature = "precursor", feature = "renode"))] #[cfg(any(feature = "precursor", feature = "renode"))]
pub use precursor::implementation; pub use precursor::implementation;
@ -19,12 +18,10 @@ pub use hosted::implementation;
#[macro_use] #[macro_use]
pub mod atsama5d2; pub mod atsama5d2;
#[cfg(feature="atsama5d27")]
pub use atsama5d2::debug;
#[cfg(feature = "atsama5d27")] #[cfg(feature = "atsama5d27")]
pub use atsama5d2::console; pub use atsama5d2::console;
#[cfg(feature = "atsama5d27")]
pub use atsama5d2::debug;
#[cfg(feature = "atsama5d27")] #[cfg(feature = "atsama5d27")]
pub use atsama5d2::implementation; pub use atsama5d2::implementation;
@ -34,6 +31,5 @@ pub mod cramium;
#[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))] #[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))]
pub use cramium::debug; pub use cramium::debug;
#[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))] #[cfg(any(feature = "cramium-soc", feature = "cramium-fpga"))]
pub use cramium::implementation; pub use cramium::implementation;

View file

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

View file

@ -1,4 +1,5 @@
use core::fmt::{Error, Write}; use core::fmt::{Error, Write};
use utralib::generated::*; use utralib::generated::*;
pub struct Output {} pub struct Output {}
@ -41,9 +42,7 @@ pub fn init() -> Output {
} }
impl Output { impl Output {
pub fn get_writer(&self) -> OutputWriter { pub fn get_writer(&self) -> OutputWriter { OutputWriter {} }
OutputWriter {}
}
pub fn run(&mut self) { pub fn run(&mut self) {
loop { loop {
@ -59,10 +58,7 @@ fn handle_console_irq(_irq_no: usize, arg: *mut usize) {
// println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY)); // println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY));
while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 { while uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY) == 0 {
// I really rather think this is more readable, than the "Rusty" version below. // I really rather think this is more readable, than the "Rusty" version below.
inject_csr.wfo( inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, uart_csr.rf(utra::uart::RXTX_RXTX));
utra::keyinject::UART_CHAR_CHAR,
uart_csr.rf(utra::uart::RXTX_RXTX),
);
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1); uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
// I guess this is how you would do it if you were "really doing Rust" // 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 {} while uart_csr.r(utra::uart::TXFULL) != 0 {}
uart_csr.wo(utra::uart::RXTX, c as u32); 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, // there's a race condition in the handler, if a new character comes in while handling the
// the pending bit never clears. If the console seems to freeze, uncomment this line. // interrupt, the pending bit never clears. If the console seems to freeze, uncomment
// This kind of works around that, at the expense of maybe losing some Rx characters. // this line. This kind of works around that, at the expense of maybe losing some Rx
// uart_csr.wfo(utra::uart::EV_PENDING_RX, 1); // characters. uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
} }
} }
} }