mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
refactor xous-log to use cramium HAL
code is much cleaner, closer to production quality
This commit is contained in:
parent
0e14761641
commit
2a389d2999
3 changed files with 126 additions and 178 deletions
|
|
@ -24,6 +24,8 @@ rkyv = { version = "0.4.3", default-features = false, features = [
|
|||
"const_generics",
|
||||
], optional = true }
|
||||
|
||||
cramium-hal = { path = "../../libs/cramium-hal", optional = true }
|
||||
|
||||
[target.'cfg(target_arch = "arm")'.dependencies]
|
||||
atsama5d27 = { git = "https://github.com/Foundation-Devices/atsama5d27.git", branch = "master", features = [
|
||||
"lcd-console",
|
||||
|
|
@ -33,7 +35,7 @@ xous = { version = "0.9.57", features = [
|
|||
] } # v2p feature is used when lcd-console feature is turned on
|
||||
|
||||
[features]
|
||||
cramium-soc = ["utralib/cramium-soc"]
|
||||
cramium-soc = ["utralib/cramium-soc", "cramium-hal"]
|
||||
cramium-fpga = ["utralib/cramium-fpga"]
|
||||
precursor = ["utralib/precursor"]
|
||||
hosted = ["utralib/hosted"]
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use core::fmt::{Error, Write};
|
||||
|
||||
use utralib::generated::*;
|
||||
use cramium_hal::udma;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! print
|
||||
|
|
@ -43,21 +43,32 @@ impl Uart {
|
|||
#[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 });
|
||||
// enqueue our character to send via DMA
|
||||
unsafe {
|
||||
if crate::implementation::UART_DMA_BUF as usize != 0 {
|
||||
crate::implementation::UART_DMA_BUF.write_volatile(c); // write to the virtual memory address
|
||||
}
|
||||
// check that we've been initialized before attempting to send any characters...
|
||||
if unsafe { DEFAULT_UART_ADDR } as usize == 0 {
|
||||
return;
|
||||
}
|
||||
if unsafe { crate::implementation::UART_DMA_TX_BUF_VIRT } as usize == 0 {
|
||||
return;
|
||||
}
|
||||
// safety: safe to call as long as DEFAULT_UART_ADDR is 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.
|
||||
let mut uart = unsafe { udma::Uart::get_handle(DEFAULT_UART_ADDR as usize) };
|
||||
|
||||
// enqueue our character to send via DMA
|
||||
let tx_buf_virt = unsafe {
|
||||
// safety: it's safe only because we are manually tracking the allocations in IFRAM0.
|
||||
core::slice::from_raw_parts_mut(crate::implementation::UART_DMA_TX_BUF_VIRT as *mut u8, 1)
|
||||
};
|
||||
let tx_buf_phys = unsafe {
|
||||
// safety: it's safe only because we are manually tracking the allocations in IFRAM0.
|
||||
core::slice::from_raw_parts_mut(crate::implementation::UART_DMA_TX_BUF_PHYS as *mut u8, 1)
|
||||
};
|
||||
tx_buf_virt[0] = c;
|
||||
// note that write takes *physical* addresses
|
||||
unsafe {
|
||||
uart.write_phys(tx_buf_phys);
|
||||
}
|
||||
// configure the DMA
|
||||
uart_csr.wo(utra::udma_uart_0::REG_TX_SADDR, utralib::HW_IFRAM0_MEM as u32); // source is the physical address
|
||||
uart_csr.wo(utra::udma_uart_0::REG_TX_SIZE, 1);
|
||||
// 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 {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,11 +1,15 @@
|
|||
use core::fmt::{Error, Write};
|
||||
|
||||
use cramium_hal::udma;
|
||||
use utralib::generated::*;
|
||||
|
||||
pub struct Output {}
|
||||
|
||||
#[cfg(feature = "cramium-soc")]
|
||||
pub static mut UART_DMA_BUF: *mut u8 = 0x0000_0000 as *mut u8;
|
||||
pub static mut UART_DMA_TX_BUF_VIRT: *mut u8 = 0x0000_0000 as *mut u8;
|
||||
|
||||
#[cfg(feature = "cramium-soc")]
|
||||
pub const UART_DMA_TX_BUF_PHYS: usize = utralib::HW_IFRAM0_MEM + utralib::HW_IFRAM0_MEM_LEN - 4096;
|
||||
|
||||
pub fn init() -> Output {
|
||||
#[cfg(feature = "cramium-fpga")]
|
||||
|
|
@ -27,100 +31,26 @@ pub fn init() -> Output {
|
|||
)
|
||||
.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 = "cramium-soc")]
|
||||
{
|
||||
// TODO: need to write an allocator for the UDMA memory region as well
|
||||
// Note: for the TX buf, we allocate a pre-reserved portion of IFRAM as our
|
||||
// DMA buffer. We do *not* use the IFRAM allocator in `cram-hal-service` because
|
||||
// we want to avoid a circular dependency between the logging crate and
|
||||
// the HAL service. Instead, we simply note that this region of memory is
|
||||
// always reserved by the logging crate in its allocator.
|
||||
let tx_buf_region = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utralib::HW_IFRAM0_MEM),
|
||||
// we take the last page of IFRAM0 for the Tx buffer.
|
||||
xous::MemoryAddress::new(UART_DMA_TX_BUF_PHYS),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map UDMA buffer");
|
||||
unsafe { UART_DMA_BUF = tx_buf_region.as_mut_ptr() as *mut u8 };
|
||||
|
||||
/*
|
||||
// TODO: migrate this to an allocator that can handle all IOs
|
||||
let iox_buf = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utra::iox::HW_IOX_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map iox buffer");
|
||||
|
||||
let udma_ctrl = xous::syscall::map_memory(
|
||||
xous::MemoryAddress::new(utra::udma_ctrl::HW_UDMA_CTRL_BASE),
|
||||
None,
|
||||
4096,
|
||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
||||
)
|
||||
.expect("couldn't map UDMA control");
|
||||
|
||||
let iox_csr = iox_buf.as_mut_ptr() as *mut u32;
|
||||
unsafe {
|
||||
iox_csr.add(0).write_volatile(0b00_00_00_01_01_00_00_00); // PAL AF1 on PA3/PA4
|
||||
iox_csr.add(0x1c / core::mem::size_of::<u32>()).write_volatile(0x1400); // PDH
|
||||
iox_csr.add(0x148 / core::mem::size_of::<u32>()).write_volatile(0x10); // PA4 output
|
||||
iox_csr.add(0x148 / core::mem::size_of::<u32>() + 3).write_volatile(0xffff); // PD
|
||||
iox_csr.add(0x160 / core::mem::size_of::<u32>()).write_volatile(0x8); // PA3 pullup
|
||||
}
|
||||
|
||||
let mut udma_ctrl = CSR::new(udma_ctrl.as_mut_ptr() as _);
|
||||
// ungate the clock for the UART. TODO: send this to a power management common server...
|
||||
// probably should be in the same server that allocates UDMA buffers.
|
||||
udma_ctrl.wo(utra::udma_ctrl::REG_CG, 1);
|
||||
|
||||
// setup the baud rate
|
||||
let mut uart_csr = CSR::new(uart.as_mut_ptr() as *mut u32);
|
||||
let baudrate: u32 = 115200;
|
||||
let freq: u32 = 100_000_000;
|
||||
let clk_counter: u32 = (freq + baudrate / 2) / baudrate;
|
||||
uart_csr.wo(utra::udma_uart_0::REG_UART_SETUP,
|
||||
0x0306 | (clk_counter << 16)
|
||||
); */
|
||||
|
||||
// rely on the bootloader to set up all the above params
|
||||
/* // for debug: send a test string to confirm everything is configured correctly
|
||||
let tx_buf = tx_buf_region.as_mut_ptr() as *mut u8;
|
||||
for i in 0..16 {
|
||||
unsafe { tx_buf.add(i).write_volatile('M' as u32 as u8 + i as u8) };
|
||||
}
|
||||
let mut udma_uart = CSR::new(uart.as_mut_ptr() as *mut u32);
|
||||
udma_uart.wo(utra::udma_uart_0::REG_TX_SADDR, tx_buf as u32);
|
||||
udma_uart.wo(utra::udma_uart_0::REG_TX_SIZE, 16);
|
||||
// send it
|
||||
udma_uart.wo(utra::udma_uart_0::REG_TX_CFG, 0x10); // EN
|
||||
// wait for it all to be done
|
||||
while udma_uart.rf(utra::udma_uart_0::REG_TX_CFG_R_TX_EN) != 0 { }
|
||||
while (udma_uart.r(utra::udma_uart_0::REG_STATUS) & 1) != 0 { }
|
||||
*/
|
||||
unsafe { UART_DMA_TX_BUF_VIRT = tx_buf_region.as_mut_ptr() as *mut u8 };
|
||||
}
|
||||
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 {}
|
||||
}
|
||||
|
||||
|
|
@ -134,40 +64,6 @@ impl Output {
|
|||
}
|
||||
}
|
||||
|
||||
#[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 {}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
@ -187,21 +83,29 @@ impl OutputWriter {
|
|||
}
|
||||
#[cfg(feature = "cramium-soc")]
|
||||
{
|
||||
let mut uart_csr = CSR::new(unsafe { crate::platform::debug::DEFAULT_UART_ADDR as *mut u32 });
|
||||
// safety: safe to call as long as DEFAULT_UART_ADDR is 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.
|
||||
let mut uart =
|
||||
unsafe { udma::Uart::get_handle(crate::platform::debug::DEFAULT_UART_ADDR as usize) };
|
||||
|
||||
// enqueue our character to send via DMA
|
||||
let tx_buf_virt = unsafe {
|
||||
// safety: it's safe only because we are manually tracking the allocations in IFRAM0.
|
||||
core::slice::from_raw_parts_mut(UART_DMA_TX_BUF_VIRT as *mut u8, 1)
|
||||
};
|
||||
let tx_buf_phys = unsafe {
|
||||
// safety: it's safe only because we know that the UART TX buffer is expressly reserved at
|
||||
// this location
|
||||
core::slice::from_raw_parts_mut(UART_DMA_TX_BUF_PHYS as *mut u8, 1)
|
||||
};
|
||||
tx_buf_virt[0] = c;
|
||||
// note that write_phys takes the *physical* address. We use `write_phys` because benchmarks
|
||||
// show significant performance improvements skipping the V2P step inherent `write` (an extra
|
||||
// 2 milliseconds for 15 print statements that are 90 chars in length).
|
||||
unsafe {
|
||||
if UART_DMA_BUF as usize != 0 {
|
||||
UART_DMA_BUF.write_volatile(c); // write to the virtual memory address
|
||||
}
|
||||
uart.write_phys(tx_buf_phys);
|
||||
}
|
||||
// configure the DMA
|
||||
uart_csr.wo(utra::udma_uart_0::REG_TX_SADDR, utralib::HW_IFRAM0_MEM as u32); // source is the physical address
|
||||
uart_csr.wo(utra::udma_uart_0::REG_TX_SIZE, 1);
|
||||
// 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 {}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -211,36 +115,31 @@ impl OutputWriter {
|
|||
pub fn write(&mut self, buf: &[u8]) -> usize {
|
||||
#[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).
|
||||
// safety: safe to call as long as DEFAULT_UART_ADDR is 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.
|
||||
let mut uart =
|
||||
unsafe { udma::Uart::get_handle(crate::platform::debug::DEFAULT_UART_ADDR as usize) };
|
||||
|
||||
let mut uart_csr = CSR::new(unsafe { crate::platform::debug::DEFAULT_UART_ADDR as *mut u32 });
|
||||
// enqueue our character to send via DMA
|
||||
unsafe {
|
||||
if UART_DMA_BUF as usize != 0 {
|
||||
// convert the raw pointer to a 4k buffer region. This is "by fiat", we don't
|
||||
// have a formal allocator for this region yet
|
||||
let dest_buf = core::slice::from_raw_parts_mut(UART_DMA_BUF as *mut u8, 4096);
|
||||
// copy the whole buf to the destination
|
||||
for (&s, d) in buf.iter().zip(dest_buf.iter_mut()) {
|
||||
*d = s;
|
||||
}
|
||||
let tx_buf_virt = unsafe {
|
||||
// safety: it's safe only because we are manually tracking the allocations in IFRAM0.
|
||||
core::slice::from_raw_parts_mut(UART_DMA_TX_BUF_VIRT as *mut u8, 4096)
|
||||
};
|
||||
// create a "fictional" physical slice at the physical address where our buffer is mapped.
|
||||
let tx_buf_phys = unsafe {
|
||||
// safety: it's safe only because we know that the UART TX buffer is expressly reserved at
|
||||
// this location
|
||||
core::slice::from_raw_parts_mut(UART_DMA_TX_BUF_PHYS as *mut u8, 4096)
|
||||
};
|
||||
let mut writelen = 0;
|
||||
for page in buf.chunks(4096) {
|
||||
tx_buf_virt[..page.len()].copy_from_slice(page);
|
||||
// note that the write_phys uses the *physical address* slice
|
||||
unsafe {
|
||||
uart.write_phys(&tx_buf_phys[..page.len()]);
|
||||
}
|
||||
}
|
||||
// configure the DMA
|
||||
uart_csr.wo(utra::udma_uart_0::REG_TX_SADDR, utralib::HW_IFRAM0_MEM as u32); // source is the physical address
|
||||
let writelen = buf.len().min(4096); // we will send the smaller of the buffer length or the maximum size of the DMA page
|
||||
uart_csr.wo(utra::udma_uart_0::REG_TX_SIZE, writelen as u32);
|
||||
// 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 {
|
||||
// this should complete quickly because we're just ensuring nothing is already in progress
|
||||
}
|
||||
while (uart_csr.r(utra::udma_uart_0::REG_STATUS) & 1) != 0 {
|
||||
// this takes a bit longer; yield the time because we expect the average
|
||||
// time to send to be around 0.25ms or so, so this is worth it.
|
||||
xous::yield_slice();
|
||||
writelen += page.len();
|
||||
}
|
||||
|
||||
writelen
|
||||
|
|
@ -258,11 +157,47 @@ impl OutputWriter {
|
|||
}
|
||||
|
||||
impl Write for OutputWriter {
|
||||
/// Optimizes performance for the typical case of strings < 210 chars long.
|
||||
/// We have to insert `\r` after every `\n`, leading to a potential 2x growth in
|
||||
/// string size for a reference string that is all `\n`. The downside of
|
||||
/// over-initializing stack space is has to have 0's written to it, which costs
|
||||
/// CPU time and blows out the cache. We're going to go out on a limb and guess
|
||||
/// that there are few cases where we'd pack a huge number of newlines with
|
||||
/// short strings. In the case that we do, we'll end up truncating the output
|
||||
/// because we ran out of space to insert the newlines.
|
||||
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);
|
||||
const OPTIMIZATION_BREAKPOINT: usize = 210;
|
||||
const STACKBUF_LEN: usize = 256;
|
||||
if s.len() < OPTIMIZATION_BREAKPOINT {
|
||||
let mut crlf_buf = [0u8; STACKBUF_LEN];
|
||||
let mut ptr = 0;
|
||||
for c in s.bytes() {
|
||||
crlf_buf[ptr] = c;
|
||||
ptr += 1;
|
||||
if ptr >= STACKBUF_LEN {
|
||||
break;
|
||||
}
|
||||
if c == '\n' as u8 {
|
||||
crlf_buf[ptr] = '\r' as u8;
|
||||
ptr += 1;
|
||||
if ptr >= STACKBUF_LEN {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
// now emit the buffer in one efficient, DMA-powered write operation
|
||||
self.write(&crlf_buf[..ptr]);
|
||||
} else {
|
||||
// in case of long strings, the print will be less efficient because
|
||||
// we go character-by-character to convert CRLF; the trade-off is the storage
|
||||
// could be unbounded. Could also fall back to using a `Vec`, but alloc's
|
||||
// are actually extremely expensive and we'd like to avoid them in the logging
|
||||
// crate.
|
||||
for c in s.bytes() {
|
||||
self.putc(c);
|
||||
if c == '\n' as u8 {
|
||||
self.putc('\r' as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Reference in a new issue