Merge pull request #594 from betrusted-io/udma-debug

Udma debug
This commit is contained in:
bunnie 2024-11-19 22:33:08 +08:00 committed by GitHub
commit 5279e50ace
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 105 additions and 31 deletions

View file

@ -136,7 +136,7 @@ pub fn setup_memory_pins(iox: &dyn IoSetup) -> crate::udma::SpimChannel {
Some(IoxEnable::Enable), Some(IoxEnable::Enable),
Some(IoxDriveStrength::Drive2mA), Some(IoxDriveStrength::Drive2mA),
); );
// SPIM_CSN0_A[1] // SPIM_CSN1_A[1]
iox.setup_pin( iox.setup_pin(
IoxPort::PC, IoxPort::PC,
13, 13,

View file

@ -350,11 +350,17 @@ pub(crate) const CFG_SIZE_16: u32 = 0b00_0010; // 16-bit transfer
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) const CFG_SIZE_32: u32 = 0b00_0100; // 32-bit transfer pub(crate) const CFG_SIZE_32: u32 = 0b00_0100; // 32-bit transfer
#[allow(dead_code)] #[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)] #[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 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)] #[repr(usize)]
pub enum Bank { pub enum Bank {
Rx = 0, Rx = 0,

View file

@ -640,6 +640,19 @@ impl Spim {
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()
}, },
); );
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]) Ok(&self.rx_buf()[..pending])
} else { } else {
@ -770,7 +783,13 @@ impl Spim {
} }
// safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference // 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::<T>()[..tx_len], CFG_EN | cfg_size) }; unsafe { self.udma_enqueue(Bank::Tx, &self.tx_buf_phys::<T>()[..tx_len], CFG_EN | cfg_size) };
unsafe { self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::<T>()[..tx_len], CFG_EN | cfg_size) } unsafe {
self.udma_enqueue(
Bank::Rx,
&self.rx_buf_phys::<T>()[..tx_len],
CFG_EN | cfg_size | CFG_BACKPRESSURE,
)
}
} else if let Some((start, _len)) = parts { } else if let Some((start, _len)) = parts {
// safety: this is safe because tx_buf_phys() slice is only used as a base/bounds reference // 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 // 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::<T>()[start..(start + tx_len)], &self.tx_buf_phys::<T>()[start..(start + tx_len)],
CFG_EN | cfg_size, CFG_EN | cfg_size,
); );
self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::<T>()[..tx_len], CFG_EN | cfg_size) self.udma_enqueue(
Bank::Rx,
&self.rx_buf_phys::<T>()[..tx_len],
CFG_EN | cfg_size | CFG_BACKPRESSURE,
)
} }
} }
Ok(()) Ok(())
@ -838,7 +861,13 @@ impl Spim {
// read back the ID result // read back the ID result
let cmd_list = [SpimCmd::RxData(self.mode, SpimWordsPerXfer::Words1, 8, SpimEndian::MsbFirst, 3)]; 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 // 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::<u8>()[..3], CFG_EN | CFG_SIZE_8) }; unsafe {
self.udma_enqueue(
Bank::Rx,
&self.rx_buf_phys::<u8>()[..3],
CFG_EN | CFG_SIZE_8 | CFG_BACKPRESSURE,
)
};
self.send_cmd_list(&cmd_list); self.send_cmd_list(&cmd_list);
while self.udma_busy(Bank::Rx) { while self.udma_busy(Bank::Rx) {
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -863,7 +892,13 @@ impl Spim {
// 48 bits of unique ID -- we only retrieve the top 16 of that here. // 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)]; 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 // 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::<u8>()[..7], CFG_EN | CFG_SIZE_8) }; unsafe {
self.udma_enqueue(
Bank::Rx,
&self.rx_buf_phys::<u8>()[..7],
CFG_EN | CFG_SIZE_8 | CFG_BACKPRESSURE,
)
};
self.send_cmd_list(&cmd_list); self.send_cmd_list(&cmd_list);
while self.udma_busy(Bank::Rx) { while self.udma_busy(Bank::Rx) {
#[cfg(feature = "std")] #[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 // safety: this is safe because rx_buf_phys() slice is only used as a base/bounds reference
unsafe { unsafe {
self.udma_enqueue(Bank::Rx, &self.rx_buf_phys::<u8>()[..chunk.len()], CFG_EN | CFG_SIZE_8) self.udma_enqueue(
Bank::Rx,
&self.rx_buf_phys::<u8>()[..chunk.len()],
CFG_EN | CFG_SIZE_8 | CFG_BACKPRESSURE,
)
}; };
self.send_cmd_list(&rd_cmd); self.send_cmd_list(&rd_cmd);
while self.udma_busy(Bank::Rx) { while self.udma_busy(Bank::Rx) {
@ -982,7 +1021,6 @@ impl Spim {
timeout += 1; timeout += 1;
if (self.mode == SpimMode::Quad) && (timeout > chunk.len() * 10_000) { if (self.mode == SpimMode::Quad) && (timeout > chunk.len() * 10_000) {
success = false; success = false;
/*
// unsuccessful attempt to clear the pending transfer manually // unsuccessful attempt to clear the pending transfer manually
// the root cause of this is when the UDMA RX FIFO fills up and // 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 // RX packets get dropped. The Rx counter becomes "desynced" from the
@ -995,7 +1033,13 @@ impl Spim {
.add(DmaReg::Cfg.into()) .add(DmaReg::Cfg.into())
.write_volatile(CFG_SIZE_8 | CFG_CLEAR); .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::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 // send an EOT
let cmd_list = [SpimCmd::EndXfer(SpimEventGen::Disabled)]; let cmd_list = [SpimCmd::EndXfer(SpimEventGen::Disabled)];
self.send_cmd_list(&cmd_list); self.send_cmd_list(&cmd_list);

View file

@ -12,6 +12,15 @@ const IMAGE_WIDTH: usize = 256;
const IMAGE_HEIGHT: usize = 240; const IMAGE_HEIGHT: usize = 240;
const BW_THRESH: u8 = 128; 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) { pub fn blit_to_display(sh1107: &mut Oled128x128, frame: &[u8], display_cleared: bool) {
for (y, row) in frame.chunks(IMAGE_WIDTH).enumerate() { for (y, row) in frame.chunks(IMAGE_WIDTH).enumerate() {
if y & 1 == 0 { if y & 1 == 0 {
@ -90,7 +99,7 @@ fn wrapped_main() -> ! {
let mut frames = 0; let mut frames = 0;
let mut frame = [0u8; IMAGE_WIDTH * IMAGE_HEIGHT]; let mut frame = [0u8; IMAGE_WIDTH * IMAGE_HEIGHT];
let mut decode_success; 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 { loop {
#[cfg(not(feature = "decongest-udma"))] #[cfg(not(feature = "decongest-udma"))]
cam.capture_async(); cam.capture_async();
@ -325,11 +334,11 @@ fn wrapped_main() -> ! {
// clear the front buffer // clear the front buffer
sh1107.clear(); sh1107.clear();
let mut start = tt.elapsed_ms();
let mut now = tt.elapsed_ms();
const TIMEOUT_MS: u64 = 100; const TIMEOUT_MS: u64 = 100;
#[cfg(feature = "decongest-udma")] #[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 // don't parallelize the camera capture to avoid triggering a hardware bug
// in the SPIM block. // in the SPIM block.
while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High && ((now - start) < TIMEOUT_MS) { while iox.get_gpio_pin_value(IoxPort::PB, 9) == IoxValue::High && ((now - start) < TIMEOUT_MS) {
@ -342,8 +351,8 @@ fn wrapped_main() -> ! {
} }
// wait for the transfer to finish // wait for the transfer to finish
start = tt.elapsed_ms(); let start = tt.elapsed_ms();
now = tt.elapsed_ms(); let mut now = tt.elapsed_ms();
while cam.udma_busy(cramium_hal::udma::Bank::Rx) && ((now - start) < TIMEOUT_MS) { while cam.udma_busy(cramium_hal::udma::Bank::Rx) && ((now - start) < TIMEOUT_MS) {
now = tt.elapsed_ms(); now = tt.elapsed_ms();
// busy-wait to get better time resolution on when the frame ends // busy-wait to get better time resolution on when the frame ends

View file

@ -42,4 +42,4 @@ oom-doom = []
debug-print = [] debug-print = []
debug-verbose = [] debug-verbose = []
default = [] default = ["debug-print"]

View file

@ -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 // safety: this is only safe because the pointer we're passed from the kernel is guaranteed to be
// a valid u8-page in memory // a valid u8-page in memory
let buf = unsafe { core::slice::from_raw_parts_mut(vaddr_in_swap as *mut u8, PAGE_SIZE) }; let buf = unsafe { core::slice::from_raw_parts_mut(vaddr_in_swap as *mut u8, PAGE_SIZE) };
match ss.hal.decrypt_swap_from( // this is in a retry loop because the SPIM interface can timeout during high bus congestion
buf, // periods.
ss.sct.counts[paddr_in_swap / PAGE_SIZE], const TIMEOUT_RETRIES: usize = 3;
paddr_in_swap, let mut retries = 0;
vaddr_in_pid, while retries < TIMEOUT_RETRIES {
pid, match ss.hal.decrypt_swap_from(
) { buf,
Ok(_) => {} ss.sct.counts[paddr_in_swap / PAGE_SIZE],
Err(e) => { paddr_in_swap,
writeln!( vaddr_in_pid,
DebugUart {}, pid,
"Decryption error: swap image corrupted, the tag does not match the data! {:?}", ) {
e Ok(_) => {
) break;
.ok(); }
panic!("Decryption error: swap image corrupted, the tag does not match the data!"); 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. // at this point, the `buf` has our desired data, we're done, modulo updating the count.