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(IoxDriveStrength::Drive2mA),
);
// SPIM_CSN0_A[1]
// SPIM_CSN1_A[1]
iox.setup_pin(
IoxPort::PC,
13,

View file

@ -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,

View file

@ -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::<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 {
// 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::<T>()[start..(start + tx_len)],
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(())
@ -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::<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);
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::<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);
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::<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);
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);

View file

@ -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 {
@ -90,7 +99,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 +334,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 +351,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

View file

@ -42,4 +42,4 @@ oom-doom = []
debug-print = []
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
// 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.