From 1abf0430054f0eaf29fa440c529f3881f379092f Mon Sep 17 00:00:00 2001 From: bunnie Date: Sat, 7 Jun 2025 22:54:52 +0800 Subject: [PATCH] check NACK status in i2c_await routine; update bare metal API NACK bit is now checked after running the transaction, and if it is set an appropriate error code is returned. The trait implementation for i2c baremetal calls is also updated in this commit --- libs/cramium-hal/src/udma/i2c.rs | 68 +++++++++++++++++++++++++++++--- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/libs/cramium-hal/src/udma/i2c.rs b/libs/cramium-hal/src/udma/i2c.rs index 264c3f3a5..1c03aab4b 100644 --- a/libs/cramium-hal/src/udma/i2c.rs +++ b/libs/cramium-hal/src/udma/i2c.rs @@ -263,6 +263,50 @@ impl<'a> I2c<'a> { xous::yield_slice(); } } + let nack_bits = self.udma_global.irq_status_bits(IrqBank::I2cErr); + match self.channel { + I2cChannel::Channel0 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C0_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C0_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + I2cChannel::Channel1 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C1_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C1_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + I2cChannel::Channel2 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C2_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C2_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + I2cChannel::Channel3 => { + if (nack_bits + & (utralib::utra::irqarray12::EV_STATUS_I2C3_NACK.mask() + << utralib::utra::irqarray12::EV_STATUS_I2C3_NACK.offset()) + as u32) + != 0 + { + return Err(xous::Error::Timeout); + } + } + } + let ret = match self.pending.take() { I2cPending::Read(len) => { if let Some(buf) = rx_buf { @@ -458,13 +502,25 @@ impl I2cApi for I2c<'_> { adr: u8, buf: &mut [u8], repeated_start: bool, - ) -> Result { - self.i2c_read_async(dev, adr, buf.len(), repeated_start)?; - self.i2c_await(Some(buf), true) + ) -> Result { + match self.i2c_read_async(dev, adr, buf.len(), repeated_start) { + Err(_) => return Err(xous::Error::InternalError), + _ => (), + }; + match self.i2c_await(Some(buf), true) { + Ok(b) => Ok(I2cResult::Ack(b)), + _ => Ok(I2cResult::Nack), + } } - fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result { - self.i2c_write_async(dev, adr, data)?; - self.i2c_await(None, true) + fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result { + match self.i2c_write_async(dev, adr, data) { + Err(_) => return Err(xous::Error::InternalError), + _ => (), + }; + match self.i2c_await(None, true) { + Ok(b) => Ok(I2cResult::Ack(b)), + _ => Ok(I2cResult::Nack), + } } }