From 5cd21f43300f4f2d273164b50912575dda18915c Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 10 Dec 2024 14:25:14 +0800 Subject: [PATCH] add an RTL-only i2c test mode The test is abridged because i2c is a pretty slow protocol, so we do the bare minimum to exercise a write and a read. --- libs/cramium-hal/Cargo.toml | 3 ++- libs/cramium-hal/src/udma/i2c.rs | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/libs/cramium-hal/Cargo.toml b/libs/cramium-hal/Cargo.toml index 155759cfd..0fe0fb902 100644 --- a/libs/cramium-hal/Cargo.toml +++ b/libs/cramium-hal/Cargo.toml @@ -40,6 +40,7 @@ board-baosor = ["camera-ov2640", "axp2101"] # Precursor form factor board-dabao = [] # Dev board form factor verilator-only = [] mpw = [] +hdl-test = [] udma-stress-test = [ ] # For RTL simulation testing - aggravates a key corner case @@ -48,4 +49,4 @@ compress-entropy = [] magic-manual = [] std = ["log", "xous-api-names", "usb-device", "xous-api-ticktimer"] derive-rkyv = ["rkyv"] -default = [] +default = ["hdl-test"] diff --git a/libs/cramium-hal/src/udma/i2c.rs b/libs/cramium-hal/src/udma/i2c.rs index 5074f2304..fd8bd1c8e 100644 --- a/libs/cramium-hal/src/udma/i2c.rs +++ b/libs/cramium-hal/src/udma/i2c.rs @@ -305,6 +305,29 @@ impl<'a> I2c<'a> { || self.udma_busy(Bank::Rx) } + /// This is a custom I2C routine that implements a protocol used for the HDL test suite. + #[cfg(feature = "hdl-test")] + pub fn i2c_hdl_test(&mut self, byte: u8) -> Result { + self.new_tranaction(); + self.push_cmd(I2cCmd::Config(self.divider)); + self.push_cmd(I2cCmd::Start); + self.push_cmd(I2cCmd::WriteByte(byte << 1)); + self.push_cmd(I2cCmd::RdNack); + self.push_cmd(I2cCmd::Stop); + + // safety: this is safe because the cmd_buf_phys() slice is passed to a function that only + // uses it as a base/bounds reference and it will not actually access the data. + unsafe { + self.udma_enqueue(Bank::Rx, &self.rx_buf_phys[..1], CFG_EN); + self.udma_enqueue(Bank::Custom, &self.cmd_buf_phys[..self.seq_len], CFG_EN); + } + self.pending = I2cPending::Read(1); + + let mut rx = [0u8]; + self.i2c_await(Some(&mut rx), false)?; + Ok(rx[0]) + } + pub fn i2c_write_async(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result { // The implementation of this is gross because we have to stuff the command list assert!(data.len() < 256); // is is a conservative bound, the limit is due to the cmd buf length limit