add cramium target to xous-log

This commit is contained in:
bunnie 2023-07-17 18:25:09 +08:00
parent f680b630cb
commit 6920860427
5 changed files with 233 additions and 0 deletions

View file

@ -27,6 +27,8 @@ atsama5d27 = { git = "https://github.com/Foundation-Devices/atsama5d27.git", bra
xous = { version = "0.9.47", features = ["v2p"] } # v2p feature is used when lcd-console feature is turned on
[features]
cramium-soc = ["utralib/cramium-soc"]
cramium-fpga = ["utralib/cramium-fpga"]
precursor = ["utralib/precursor"]
hosted = ["utralib/hosted"]
renode = ["utralib/renode"]

View file

@ -0,0 +1,61 @@
use core::fmt::{Error, Write};
use utralib::generated::*;
#[macro_export]
macro_rules! print
{
($($args:tt)+) => ({
use core::fmt::Write;
let _ = write!(crate::platform::debug::Uart {}, $($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)+)
});
}
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")]
impl Uart {
pub fn putc(&self, c: u8) {
assert!(unsafe { DEFAULT_UART_ADDR } as usize != 0);
let mut uart_csr = CSR::new(unsafe { 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);
}
}
#[cfg(feature="cramium-soc")]
impl Uart {
pub fn putc(&self, c: u8) {
assert!(unsafe { DEFAULT_UART_ADDR } as usize != 0);
let mut uart_csr = CSR::new(unsafe { 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);
}
}
impl Write for Uart {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
for c in s.bytes() {
self.putc(c);
}
Ok(())
}
}

View file

@ -0,0 +1,156 @@
use core::fmt::{Error, Write};
use utralib::generated::*;
pub struct Output {}
pub fn init() -> Output {
#[cfg(feature="cramium-fpga")]
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 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")]
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 serial port");
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!");
#[cfg(feature="inject")]
{
let inject_mem = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::keyinject::HW_KEYINJECT_BASE),
None,
4096,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.expect("couldn't map keyinjection CSR range");
println!("Note: character injection via console UART is enabled.");
println!("Allocating IRQ...");
xous::syscall::claim_interrupt(
utra::console::CONSOLE_IRQ,
handle_console_irq,
inject_mem.as_mut_ptr() as *mut usize,
)
.expect("couldn't claim interrupt");
println!("Claimed IRQ {}", utra::console::CONSOLE_IRQ);
uart_csr.rmwf(utra::uart::EV_ENABLE_RX, 1);
}
Output {}
}
impl Output {
pub fn get_writer(&self) -> OutputWriter {
OutputWriter {}
}
pub fn run(&mut self) {
loop {
xous::wait_event();
}
}
}
#[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);
let mut uart_csr = CSR::new(unsafe { crate::platform::debug::DEFAULT_UART_ADDR as *mut u32 });
// 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),
);
uart_csr.wfo(utra::uart::EV_PENDING_RX, 1);
// I guess this is how you would do it if you were "really doing Rust"
// (except this is checking pending not fifo status for loop termination)
// (which was really hard to figure out just looking at this loop)
/*
let maybe_c = match uart_csr.rf(utra::uart::EV_PENDING_RX) {
0 => None,
ack => {
let c = Some(uart_csr.rf(utra::uart::RXTX_RXTX) as u8);
uart_csr.wfo(utra::uart::EV_PENDING_RX, ack);
c
}
};
if let Some(c) = maybe_c {
inject_csr.wfo(utra::keyinject::UART_CHAR_CHAR, (c & 0xff) as u32);
} else {
break;
}*/
}
// println!("rxe {}", uart_csr.rf(utra::uart::RXEMPTY_RXEMPTY));
// println!("pnd {}", uart_csr.rf(utra::uart::EV_PENDING_RX));
}
}
pub struct OutputWriter {}
impl OutputWriter {
pub fn putc(&self, c: u8) {
#[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);
}
#[cfg(feature="cramium-soc")]
{
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);
}
}
/// 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<usize, ()> {
Ok(self.write(buf))
}
}
impl Write for OutputWriter {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
for c in s.bytes() {
self.putc(c);
if c == '\n' as u8 {
self.putc('\r' as u8);
}
}
Ok(())
}
}

View file

@ -0,0 +1,4 @@
#[macro_use]
pub mod debug;
pub mod implementation;

View file

@ -27,3 +27,13 @@ pub use atsama5d2::console;
#[cfg(feature="atsama5d27")]
pub use atsama5d2::implementation;
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
#[macro_use]
pub mod cramium;
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
pub use cramium::debug;
#[cfg(any(feature="cramium-soc", feature="cramium-fpga"))]
pub use cramium::implementation;