optimize timeout handling in spim interface

while debugging the disconnect issue, discovered that the timeout
routine in `std` was extremely wastefully blasting the name server
to connect to the ticktimer every time it was called. yowch!

this patch does the following:

- cache the ticktimer connection in an AtomicU32
- check if the transaction is done on the "Fast path" before
doing any waiting
- check again after fetching time - at this point, most transacitons
are actually likely done
- only then, do the wait loop

this greatly speeds up screen drawing, it seems.
This commit is contained in:
bunnie 2026-02-02 01:22:26 +08:00
parent be5e817c7c
commit 7f6b045681

View file

@ -1,4 +1,9 @@
#[cfg(feature = "std")]
use std::sync::atomic::{AtomicU32, Ordering};
use bao1x_api::*; use bao1x_api::*;
#[cfg(feature = "std")]
use num_traits::*;
use utralib::*; use utralib::*;
use crate::ifram::IframRange; use crate::ifram::IframRange;
@ -8,6 +13,9 @@ pub const FLASH_PAGE_LEN: usize = crate::board::SPINOR_PAGE_LEN as usize;
pub const FLASH_SECTOR_LEN: usize = crate::board::SPINOR_ERASE_SIZE as usize; pub const FLASH_SECTOR_LEN: usize = crate::board::SPINOR_ERASE_SIZE as usize;
pub const BLOCK_ERASE_LEN: usize = crate::board::SPINOR_BULK_ERASE_SIZE as usize; pub const BLOCK_ERASE_LEN: usize = crate::board::SPINOR_BULK_ERASE_SIZE as usize;
#[cfg(feature = "std")]
static TT_CONN: AtomicU32 = AtomicU32::new(0);
// ----------------------------------- SPIM ------------------------------------ // ----------------------------------- SPIM ------------------------------------
/// The SPIM implementation for UDMA does reg-ception, in that they bury /// The SPIM implementation for UDMA does reg-ception, in that they bury
@ -657,113 +665,150 @@ impl Spim {
#[cfg(feature = "std")] #[cfg(feature = "std")]
{ {
if let Some(pending) = self.pending_txrx.take() { if let Some(pending) = self.pending_txrx.take() {
let tt = xous_api_ticktimer::Ticktimer::new().unwrap(); fn elapsed_ms(tt_conn: u32) -> u64 {
let start = tt.elapsed_ms(); let response = xous::send_message(
let mut now = start; tt_conn,
const TIMEOUT_MS: u64 = 500; xous::Message::new_blocking_scalar(
while (self.udma_busy(Bank::Tx) || self.udma_busy(Bank::Rx) || self.udma_busy(Bank::Custom)) xous_api_ticktimer::api::Opcode::ElapsedMs.to_usize().unwrap(),
&& ((now - start) < TIMEOUT_MS) 0,
{ 0,
now = tt.elapsed_ms(); 0,
0,
),
)
.expect("Ticktimer: failure to send message to Ticktimer");
if let xous::Result::Scalar2(upper, lower) = response {
upper as u64 | ((lower as u64) << 32)
} else {
unimplemented!();
}
} }
if now - start >= TIMEOUT_MS { let mut tt_conn = TT_CONN.load(Ordering::SeqCst);
let tx_timeout = self.udma_busy(Bank::Tx); if tt_conn == 0 {
let rx_timeout = self.udma_busy(Bank::Rx); tt_conn = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap()).unwrap();
let custom_timeout = self.udma_busy(Bank::Custom); TT_CONN.store(tt_conn, Ordering::SeqCst);
log::warn!( }
"Timeout in txrx_await(): Tx {:?} Rx {:?} Custom {:?}, Rx SA: {:x}, Rx Cfg: {:x}", // wrapped in "if" because if the packet is short, we might be done already; don't make
tx_timeout, // the syscall and waste time
rx_timeout, if self.udma_busy(Bank::Tx) || self.udma_busy(Bank::Rx) || self.udma_busy(Bank::Custom) {
custom_timeout, let start = elapsed_ms(tt_conn);
unsafe { let mut now = start;
self.csr().base().add(Bank::Rx as usize).add(DmaReg::Saddr.into()).read_volatile() const TIMEOUT_MS: u64 = 250;
}, // this checks again after the syscall - could be done already!
unsafe { while (self.udma_busy(Bank::Tx)
self.csr().base().add(Bank::Rx as usize).add(DmaReg::Cfg.into()).read_volatile() || self.udma_busy(Bank::Rx)
}, || self.udma_busy(Bank::Custom))
); && ((now - start) < TIMEOUT_MS)
if rx_timeout { {
unsafe { now = elapsed_ms(tt_conn);
self.csr() }
.base() if now - start >= TIMEOUT_MS {
.add(Bank::Rx as usize) let tx_timeout = self.udma_busy(Bank::Tx);
.add(DmaReg::Cfg.into()) let rx_timeout = self.udma_busy(Bank::Rx);
.write_volatile(CFG_CLEAR); let custom_timeout = self.udma_busy(Bank::Custom);
log::info!( log::warn!(
"Rx Cfg: {:x}", "Timeout in txrx_await(): Tx {:?} Rx {:?} Custom {:?}, Rx SA: {:x}, Rx Cfg: {:x}",
tx_timeout,
rx_timeout,
custom_timeout,
unsafe {
self.csr()
.base()
.add(Bank::Rx as usize)
.add(DmaReg::Saddr.into())
.read_volatile()
},
unsafe {
self.csr() self.csr()
.base() .base()
.add(Bank::Rx as usize) .add(Bank::Rx as usize)
.add(DmaReg::Cfg.into()) .add(DmaReg::Cfg.into())
.read_volatile() .read_volatile()
); },
self.csr() );
.base() if rx_timeout {
.add(Bank::Rx as usize) unsafe {
.add(DmaReg::Saddr.into()) self.csr()
.write_volatile(0); .base()
self.csr() .add(Bank::Rx as usize)
.base() .add(DmaReg::Cfg.into())
.add(Bank::Rx as usize) .write_volatile(CFG_CLEAR);
.add(DmaReg::Cfg.into()) log::info!(
.write_volatile(0); // clear bit is not self-clearing "Rx Cfg: {:x}",
}; self.csr()
} .base()
if tx_timeout { .add(Bank::Rx as usize)
unsafe { .add(DmaReg::Cfg.into())
self.csr() .read_volatile()
.base() );
.add(Bank::Tx as usize) self.csr()
.add(DmaReg::Cfg.into()) .base()
.write_volatile(CFG_CLEAR); .add(Bank::Rx as usize)
log::info!( .add(DmaReg::Saddr.into())
"Tx Cfg: {:x}", .write_volatile(0);
self.csr()
.base()
.add(Bank::Rx as usize)
.add(DmaReg::Cfg.into())
.write_volatile(0); // clear bit is not self-clearing
};
}
if tx_timeout {
unsafe {
self.csr() self.csr()
.base() .base()
.add(Bank::Tx as usize) .add(Bank::Tx as usize)
.add(DmaReg::Cfg.into()) .add(DmaReg::Cfg.into())
.read_volatile() .write_volatile(CFG_CLEAR);
); log::info!(
self.csr() "Tx Cfg: {:x}",
.base() self.csr()
.add(Bank::Tx as usize) .base()
.add(DmaReg::Saddr.into()) .add(Bank::Tx as usize)
.write_volatile(0); .add(DmaReg::Cfg.into())
self.csr() .read_volatile()
.base() );
.add(Bank::Tx as usize) self.csr()
.add(DmaReg::Cfg.into()) .base()
.write_volatile(0); // clear bit is not self-clearing .add(Bank::Tx as usize)
}; .add(DmaReg::Saddr.into())
} .write_volatile(0);
if custom_timeout { self.csr()
unsafe { .base()
self.csr() .add(Bank::Tx as usize)
.base() .add(DmaReg::Cfg.into())
.add(Bank::Custom as usize) .write_volatile(0); // clear bit is not self-clearing
.add(DmaReg::Cfg.into()) };
.write_volatile(CFG_CLEAR); }
log::info!( if custom_timeout {
"Custom Cfg: {:x}", unsafe {
self.csr() self.csr()
.base() .base()
.add(Bank::Custom as usize) .add(Bank::Custom as usize)
.add(DmaReg::Cfg.into()) .add(DmaReg::Cfg.into())
.read_volatile() .write_volatile(CFG_CLEAR);
); log::info!(
self.csr() "Custom Cfg: {:x}",
.base() self.csr()
.add(Bank::Custom as usize) .base()
.add(DmaReg::Saddr.into()) .add(Bank::Custom as usize)
.write_volatile(0); .add(DmaReg::Cfg.into())
self.csr() .read_volatile()
.base() );
.add(Bank::Custom as usize) self.csr()
.add(DmaReg::Cfg.into()) .base()
.write_volatile(0); // clear bit is not self-clearing .add(Bank::Custom as usize)
}; .add(DmaReg::Saddr.into())
.write_volatile(0);
self.csr()
.base()
.add(Bank::Custom as usize)
.add(DmaReg::Cfg.into())
.write_volatile(0); // clear bit is not self-clearing
};
}
return Err(xous::Error::Timeout);
} }
return Err(xous::Error::Timeout);
} }
Ok(&self.rx_buf()[..pending]) Ok(&self.rx_buf()[..pending])
} else { } else {