From e8e28702e2fb577268da4f8c6bc4e66432e81edf Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Nov 2024 00:00:18 +0800 Subject: [PATCH 1/6] fix position of clear bit; add backpressure feature for NTO --- libs/cramium-hal/src/udma/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libs/cramium-hal/src/udma/mod.rs b/libs/cramium-hal/src/udma/mod.rs index 0e92bc727..7aaf94665 100644 --- a/libs/cramium-hal/src/udma/mod.rs +++ b/libs/cramium-hal/src/udma/mod.rs @@ -350,11 +350,17 @@ pub(crate) const CFG_SIZE_16: u32 = 0b00_0010; // 16-bit transfer #[allow(dead_code)] pub(crate) const CFG_SIZE_32: u32 = 0b00_0100; // 32-bit transfer #[allow(dead_code)] -pub(crate) const CFG_CLEAR: u32 = 0b10_0000; // stop and clear all pending transfers +/// NOTE NOTE NOTE: The position of this bit is different in the RTL from the documentation +/// Bit 6 is what is in the RTL, so we are using this instead of bit 5, which is the docus +pub(crate) const CFG_CLEAR: u32 = 0b100_0000; // stop and clear all pending transfers #[allow(dead_code)] -pub(crate) const CFG_PENDING: u32 = 0b10_0000; // on read, indicates a transfer pending +/// NOTE NOTE NOTE: the transfer pending bit *is* in the correct place +pub(crate) const CFG_PENDING: u32 = 0b10_0000; // indicates a transfer pending pub(crate) const CFG_SHADOW: u32 = 0b10_0000; // indicates a shadow transfer +#[allow(dead_code)] +pub(crate) const CFG_BACKPRESSURE: u32 = 0b1000_0000; // use RX backpressure to stall interface (found on SPIM in NTO) + #[repr(usize)] pub enum Bank { Rx = 0, From a40d78f3ead086a1879c03ad91e0f43809ecaa2b Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Nov 2024 00:00:50 +0800 Subject: [PATCH 2/6] add backpressure setting to SPIM; also re-attempt interface clearing ...now that we have the right bit... --- libs/cramium-hal/src/udma/spim.rs | 56 +++++++++++++++++++++++++++---- 1 file changed, 50 insertions(+), 6 deletions(-) diff --git a/libs/cramium-hal/src/udma/spim.rs b/libs/cramium-hal/src/udma/spim.rs index 0f480ac1b..2d7917a08 100644 --- a/libs/cramium-hal/src/udma/spim.rs +++ b/libs/cramium-hal/src/udma/spim.rs @@ -640,6 +640,19 @@ impl Spim { self.csr().base().add(Bank::Rx as usize).add(DmaReg::Cfg.into()).read_volatile() }, ); + unsafe { + self.csr() + .base() + .add(Bank::Rx as usize) + .add(DmaReg::Cfg.into()) + .write_volatile(CFG_CLEAR); + log::info!( + "Rx Cfg: {:x}", + self.csr().base().add(Bank::Rx as usize).add(DmaReg::Cfg.into()).read_volatile() + ); + self.csr().base().add(Bank::Rx as usize).add(DmaReg::Saddr.into()).write_volatile(0); + self.csr().base().add(Bank::Rx as usize).add(DmaReg::Cfg.into()).write_volatile(0); // clear bit is not self-clearing + }; } Ok(&self.rx_buf()[..pending]) } else { @@ -770,7 +783,13 @@ impl Spim { } // safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference unsafe { self.udma_enqueue(Bank::Tx, &self.tx_buf_phys::()[..tx_len], CFG_EN | cfg_size) }; - unsafe { self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::()[..tx_len], CFG_EN | cfg_size) } + unsafe { + self.udma_enqueue( + Bank::Rx, + &self.rx_buf_phys::()[..tx_len], + CFG_EN | cfg_size | CFG_BACKPRESSURE, + ) + } } else if let Some((start, _len)) = parts { // safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference // This will correctly panic if the size of the data to be sent is larger than the physical @@ -781,7 +800,11 @@ impl Spim { &self.tx_buf_phys::()[start..(start + tx_len)], CFG_EN | cfg_size, ); - self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::()[..tx_len], CFG_EN | cfg_size) + self.udma_enqueue( + Bank::Rx, + &self.rx_buf_phys::()[..tx_len], + CFG_EN | cfg_size | CFG_BACKPRESSURE, + ) } } Ok(()) @@ -838,7 +861,13 @@ impl Spim { // read back the ID result let cmd_list = [SpimCmd::RxData(self.mode, SpimWordsPerXfer::Words1, 8, SpimEndian::MsbFirst, 3)]; // safety: this is safe because rx_buf_phys() slice is only used as a base/bounds reference - unsafe { self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::()[..3], CFG_EN | CFG_SIZE_8) }; + unsafe { + self.udma_enqueue( + Bank::Rx, + &self.rx_buf_phys::()[..3], + CFG_EN | CFG_SIZE_8 | CFG_BACKPRESSURE, + ) + }; self.send_cmd_list(&cmd_list); while self.udma_busy(Bank::Rx) { #[cfg(feature = "std")] @@ -863,7 +892,13 @@ impl Spim { // 48 bits of unique ID -- we only retrieve the top 16 of that here. let cmd_list = [SpimCmd::RxData(self.mode, SpimWordsPerXfer::Words1, 8, SpimEndian::MsbFirst, 7)]; // safety: this is safe because rx_buf_phys() slice is only used as a base/bounds reference - unsafe { self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::()[..7], CFG_EN | CFG_SIZE_8) }; + unsafe { + self.udma_enqueue( + Bank::Rx, + &self.rx_buf_phys::()[..7], + CFG_EN | CFG_SIZE_8 | CFG_BACKPRESSURE, + ) + }; self.send_cmd_list(&cmd_list); while self.udma_busy(Bank::Rx) { #[cfg(feature = "std")] @@ -970,7 +1005,11 @@ impl Spim { } // safety: this is safe because rx_buf_phys() slice is only used as a base/bounds reference unsafe { - self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::()[..chunk.len()], CFG_EN | CFG_SIZE_8) + self.udma_enqueue( + Bank::Rx, + &self.rx_buf_phys::()[..chunk.len()], + CFG_EN | CFG_SIZE_8 | CFG_BACKPRESSURE, + ) }; self.send_cmd_list(&rd_cmd); while self.udma_busy(Bank::Rx) { @@ -982,7 +1021,6 @@ impl Spim { timeout += 1; if (self.mode == SpimMode::Quad) && (timeout > chunk.len() * 10_000) { success = false; - /* // unsuccessful attempt to clear the pending transfer manually // the root cause of this is when the UDMA RX FIFO fills up and // RX packets get dropped. The Rx counter becomes "desynced" from the @@ -995,7 +1033,13 @@ impl Spim { .add(DmaReg::Cfg.into()) .write_volatile(CFG_SIZE_8 | CFG_CLEAR); self.csr().base().add(Bank::Rx as usize).add(DmaReg::Saddr.into()).write_volatile(0); + self.csr() + .base() + .add(Bank::Rx as usize) + .add(DmaReg::Cfg.into()) + .write_volatile(CFG_SIZE_8); // clear bit is not self-clearing }; + /* // send an EOT let cmd_list = [SpimCmd::EndXfer(SpimEventGen::Disabled)]; self.send_cmd_list(&cmd_list); From 289d6879ca01364b3930fb23178f8234566ebde9 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Nov 2024 22:15:47 +0800 Subject: [PATCH 3/6] fix typo --- libs/cramium-hal/src/board/baosec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/cramium-hal/src/board/baosec.rs b/libs/cramium-hal/src/board/baosec.rs index 0b5c4d6de..4dceb4fa8 100644 --- a/libs/cramium-hal/src/board/baosec.rs +++ b/libs/cramium-hal/src/board/baosec.rs @@ -136,7 +136,7 @@ pub fn setup_memory_pins(iox: &dyn IoSetup) -> crate::udma::SpimChannel { Some(IoxEnable::Enable), Some(IoxDriveStrength::Drive2mA), ); - // SPIM_CSN0_A[1] + // SPIM_CSN1_A[1] iox.setup_pin( IoxPort::PC, 13, From 265805ac94f763dd2d9e1f424cfb10156ac9c782 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Nov 2024 22:17:07 +0800 Subject: [PATCH 4/6] cleanup warnings --- services/bao-video/src/main.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 53689008f..9c5c44b2a 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -90,7 +90,7 @@ fn wrapped_main() -> ! { let mut frames = 0; let mut frame = [0u8; IMAGE_WIDTH * IMAGE_HEIGHT]; let mut decode_success; - // while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High {} + while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High {} loop { #[cfg(not(feature = "decongest-udma"))] cam.capture_async(); @@ -325,11 +325,11 @@ fn wrapped_main() -> ! { // clear the front buffer sh1107.clear(); - let mut start = tt.elapsed_ms(); - let mut now = tt.elapsed_ms(); const TIMEOUT_MS: u64 = 100; #[cfg(feature = "decongest-udma")] { + let start = tt.elapsed_ms(); + let mut now = tt.elapsed_ms(); // don't parallelize the camera capture to avoid triggering a hardware bug // in the SPIM block. while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High && ((now - start) < TIMEOUT_MS) { @@ -342,8 +342,8 @@ fn wrapped_main() -> ! { } // wait for the transfer to finish - start = tt.elapsed_ms(); - now = tt.elapsed_ms(); + let start = tt.elapsed_ms(); + let mut now = tt.elapsed_ms(); while cam.udma_busy(cramium_hal::udma::Bank::Rx) && ((now - start) < TIMEOUT_MS) { now = tt.elapsed_ms(); // busy-wait to get better time resolution on when the frame ends From d78899ca74876aee185f85c9ac85f75cf9a9def8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Nov 2024 22:17:19 +0800 Subject: [PATCH 5/6] implement retry on SPIM failure somehow, the retry still *fails* - to be looked into in the future --- services/xous-swapper/Cargo.toml | 2 +- services/xous-swapper/src/main.rs | 47 ++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/services/xous-swapper/Cargo.toml b/services/xous-swapper/Cargo.toml index 3357ed91b..569f2bfac 100644 --- a/services/xous-swapper/Cargo.toml +++ b/services/xous-swapper/Cargo.toml @@ -42,4 +42,4 @@ oom-doom = [] debug-print = [] debug-verbose = [] -default = [] +default = ["debug-print"] diff --git a/services/xous-swapper/src/main.rs b/services/xous-swapper/src/main.rs index 6408628c3..9f56c2858 100644 --- a/services/xous-swapper/src/main.rs +++ b/services/xous-swapper/src/main.rs @@ -521,22 +521,37 @@ fn swap_handler( // safety: this is only safe because the pointer we're passed from the kernel is guaranteed to be // a valid u8-page in memory let buf = unsafe { core::slice::from_raw_parts_mut(vaddr_in_swap as *mut u8, PAGE_SIZE) }; - match ss.hal.decrypt_swap_from( - buf, - ss.sct.counts[paddr_in_swap / PAGE_SIZE], - paddr_in_swap, - vaddr_in_pid, - pid, - ) { - Ok(_) => {} - Err(e) => { - writeln!( - DebugUart {}, - "Decryption error: swap image corrupted, the tag does not match the data! {:?}", - e - ) - .ok(); - panic!("Decryption error: swap image corrupted, the tag does not match the data!"); + // this is in a retry loop because the SPIM interface can timeout during high bus congestion + // periods. + const TIMEOUT_RETRIES: usize = 3; + let mut retries = 0; + while retries < TIMEOUT_RETRIES { + match ss.hal.decrypt_swap_from( + buf, + ss.sct.counts[paddr_in_swap / PAGE_SIZE], + paddr_in_swap, + vaddr_in_pid, + pid, + ) { + Ok(_) => { + break; + } + Err(e) => { + retries += 1; + writeln!( + DebugUart {}, + "Decryption error: swap image corrupted, the tag does not match the data! {:?} (try {}/{})", + e, + retries, + TIMEOUT_RETRIES + ) + .ok(); + if retries >= TIMEOUT_RETRIES { + panic!( + "Decryption error: swap image corrupted, the tag does not match the data; retry count exceeded!" + ); + } + } } } // at this point, the `buf` has our desired data, we're done, modulo updating the count. From e42b7195b0dcf97bd8cf09de1ad4ccb571f85cbb Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 19 Nov 2024 22:21:48 +0800 Subject: [PATCH 6/6] add notes about performance improvement for the future --- services/bao-video/src/main.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/services/bao-video/src/main.rs b/services/bao-video/src/main.rs index 9c5c44b2a..e83ae2dfa 100644 --- a/services/bao-video/src/main.rs +++ b/services/bao-video/src/main.rs @@ -12,6 +12,15 @@ const IMAGE_WIDTH: usize = 256; const IMAGE_HEIGHT: usize = 240; const BW_THRESH: u8 = 128; +// Next steps for performance improvement: +// +// Improve qr::mapping -> point_from_hv_lines such that we're not just deriving the HV +// lines from the the edges of the finder regions, we're also using the very edge of +// the whole QR code itself to guide the line. This will improve the intersection point +// so that we can accurately hit the "fourth corner". At the moment it's sort of a +// luck of the draw if the interpolation hits exactly right, or if we're roughly a module +// off from ideal, which causes the data around that point to be interpreted incorrectly. + pub fn blit_to_display(sh1107: &mut Oled128x128, frame: &[u8], display_cleared: bool) { for (y, row) in frame.chunks(IMAGE_WIDTH).enumerate() { if y & 1 == 0 {