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,14 +665,42 @@ 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(
tt_conn,
xous::Message::new_blocking_scalar(
xous_api_ticktimer::api::Opcode::ElapsedMs.to_usize().unwrap(),
0,
0,
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!();
}
}
let mut tt_conn = TT_CONN.load(Ordering::SeqCst);
if tt_conn == 0 {
tt_conn = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap()).unwrap();
TT_CONN.store(tt_conn, Ordering::SeqCst);
}
// wrapped in "if" because if the packet is short, we might be done already; don't make
// the syscall and waste time
if self.udma_busy(Bank::Tx) || self.udma_busy(Bank::Rx) || self.udma_busy(Bank::Custom) {
let start = elapsed_ms(tt_conn);
let mut now = start; let mut now = start;
const TIMEOUT_MS: u64 = 500; const TIMEOUT_MS: u64 = 250;
while (self.udma_busy(Bank::Tx) || self.udma_busy(Bank::Rx) || self.udma_busy(Bank::Custom)) // this checks again after the syscall - could be done already!
while (self.udma_busy(Bank::Tx)
|| self.udma_busy(Bank::Rx)
|| self.udma_busy(Bank::Custom))
&& ((now - start) < TIMEOUT_MS) && ((now - start) < TIMEOUT_MS)
{ {
now = tt.elapsed_ms(); now = elapsed_ms(tt_conn);
} }
if now - start >= TIMEOUT_MS { if now - start >= TIMEOUT_MS {
let tx_timeout = self.udma_busy(Bank::Tx); let tx_timeout = self.udma_busy(Bank::Tx);
@ -676,10 +712,18 @@ impl Spim {
rx_timeout, rx_timeout,
custom_timeout, custom_timeout,
unsafe { unsafe {
self.csr().base().add(Bank::Rx as usize).add(DmaReg::Saddr.into()).read_volatile() self.csr()
.base()
.add(Bank::Rx as usize)
.add(DmaReg::Saddr.into())
.read_volatile()
}, },
unsafe { unsafe {
self.csr().base().add(Bank::Rx as usize).add(DmaReg::Cfg.into()).read_volatile() self.csr()
.base()
.add(Bank::Rx as usize)
.add(DmaReg::Cfg.into())
.read_volatile()
}, },
); );
if rx_timeout { if rx_timeout {
@ -765,6 +809,7 @@ impl Spim {
} }
return Err(xous::Error::Timeout); return Err(xous::Error::Timeout);
} }
}
Ok(&self.rx_buf()[..pending]) Ok(&self.rx_buf()[..pending])
} else { } else {
Err(xous::Error::UseBeforeInit) Err(xous::Error::UseBeforeInit)