refactor UDMA UART to use "poll" mode to receive data

The prior implementation tried to use the UDMA streaming mode
to get Rx data out of the UART.

The problem is that with streaming mode, it's hard to tell how much
data is actually available. This is because the thing that sets up
the stream sets "some number of bytes" as the fetch depth of the
DMA Rx, but all you can read is "number of bytes remaining" out
of the DMA interface.

So when 0 bytes are remaining, you can fetch some data, but you don't
know *how much* to fetch. And if someone else fetched partial data
before you, you don't know how much partial data was fetched. This
could be resolved with some gnarly, highly concurrent state that
gets incremented on every fetch.

Or, we could use poll mode. There's a reason i think it's in the
block, because the Rx DMA mode is more or less not usable. Poll
mode you read a config register, see if there's valid data, and if
so, when you read the data register, it's pulled out of a FIFO.
The FIFO is in hardware and it's 4-deep. As long as you don't
need more than a depth of 4, you're rock solid.

This is a lot easier than the UDMA Rx mode, so I've just gone ahead
and deprecated the Rx mode. I could only ever get the Rx mode
to work to receive a single character *anyways*, so despite its
potential to receive thousands of bytes without CPU intervention,
it was only ever used as a 1-deep buffer because of the inability
to know exactly how much was in the buffer. Thus, the poll mode
is actually an improvement over the DMA mode, in practice.

More importantly, it means the Rx link is much more stable in
GDB kernel mode, if the baud rate is cranked down to 115200 (at
1M baud, the kernel still can't respond quite fast enough to
reliably drain it).
This commit is contained in:
bunnie 2026-01-14 03:21:06 +08:00
parent 24105e7b22
commit 29d25c13a1

View file

@ -5,9 +5,13 @@ use crate::udma::*;
// ----------------------------------- UART ------------------------------------
#[repr(usize)]
enum UartReg {
pub enum UartReg {
Status = 0,
Setup = 1,
Error = 2,
IrqEn = 3,
Valid = 4,
Data = 5,
}
impl Into<usize> for UartReg {
fn into(self) -> usize { self as usize }
@ -92,10 +96,13 @@ impl Uart {
let clk_counter: u32 = (clk_freq + baud / 2) / baud;
// setup baud, bits, parity, etc.
// bit 0x10 sets poll mode - data is read out through the command interface not the streaming Rx fifo
// the command interface is more reliable in terms of determining exactly how many bytes are left to
// read.
csr.base()
.add(Bank::Custom.into())
.add(UartReg::Setup.into())
.write_volatile(0x0306 | (clk_counter << 16));
.write_volatile(0x0316 | (clk_counter << 16));
Uart { csr, ifram: IframRange::request(UART_RX_BUF_SIZE + UART_TX_BUF_SIZE, None).unwrap() }
}
@ -125,12 +132,15 @@ impl Uart {
}
// setup baud, bits, parity, etc.
// safety: this is safe to call as long as the base address points at a valid UART.
// bit 0x10 sets poll mode - data is read out through the command interface not the streaming Rx fifo
// the command interface is more reliable in terms of determining exactly how many bytes are left to
// read.
unsafe {
self.csr
.base()
.add(Bank::Custom.into())
.add(UartReg::Setup.into())
.write_volatile(0x0306 | (clk_counter << 16));
.write_volatile(0x0316 | (clk_counter << 16));
}
}
@ -232,11 +242,11 @@ impl Uart {
);
}
self.wait_rx_done();
#[cfg(feature = "std")]
#[cfg(any(feature = "std", feature = "kernel"))]
chunk.copy_from_slice(
&self.ifram.as_slice::<u8>()[UART_RX_BUF_START..UART_RX_BUF_START + chunk.len()],
);
#[cfg(not(feature = "std"))]
#[cfg(not(any(feature = "std", feature = "kernel")))]
unsafe {
chunk.copy_from_slice(
&self.ifram.as_phys_slice::<u8>()[UART_RX_BUF_START..UART_RX_BUF_START + chunk.len()],
@ -251,44 +261,25 @@ impl Uart {
///
/// Returns actual number of bytes read (0 or 1).
pub fn read_async(&mut self, c: &mut u8) -> usize {
let bank_addr = unsafe { self.csr().base().add(Bank::Rx as usize) };
// retrieve total bytes available
let pending = unsafe { bank_addr.add(DmaReg::Size.into()).read_volatile() } as usize;
// retrieve valid
let valid =
unsafe { self.csr().base().add(Bank::Custom.into()).add(UartReg::Valid.into()).read_volatile() }
as usize;
// recover the pending byte. Hard-coded for case of RX_BUF_DEPTH == 1
assert!(RX_BUF_DEPTH == 1, "Need to refactor buf recovery code if RX_BUF_DEPTH > 1");
#[cfg(feature = "std")]
{
*c = self.ifram.as_slice::<u8>()[UART_RX_BUF_START];
}
#[cfg(not(feature = "std"))]
unsafe {
*c = self.ifram.as_phys_slice::<u8>()[UART_RX_BUF_START];
}
if valid != 0 {
*c = unsafe {
self.csr().base().add(Bank::Custom.into()).add(UartReg::Data.into()).read_volatile()
} as u8;
// queue the next round
#[cfg(feature = "std")]
unsafe {
self.udma_enqueue(
Bank::Rx,
&self.ifram.as_phys_slice::<u8>()[UART_RX_BUF_START..UART_RX_BUF_START + RX_BUF_DEPTH],
CFG_EN | CFG_CONT,
);
1
} else {
0
}
#[cfg(not(feature = "std"))]
unsafe {
self.udma_enqueue(
Bank::Rx,
&self.ifram.as_phys_slice::<u8>()[UART_RX_BUF_START..UART_RX_BUF_START + RX_BUF_DEPTH],
CFG_EN | CFG_CONT,
);
}
pending
}
/// Call this to prime the system for async reads. This must be called at least once if any characters
/// are ever to be received.
/// are ever to be received, if the system is in FIFO mode (note that by default the system is now in POLL
/// mode)
pub fn setup_async_read(&mut self) {
#[cfg(feature = "std")]
unsafe {