From c50c9358c5081edc4247c9555cc4edd8a9638ab8 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 1 Feb 2023 03:15:53 +0800 Subject: [PATCH] add option for disabling repeated start condition A very tiny bit of fine print in the RTC datasheet indicates that the RTC hardware unit is derpy and it does not support repeated starts. In this case, a stop condition must be issued between a write that sets an address, and the subsequent read transaction. --- services/llio/src/api/i2c_api.rs | 3 ++- services/llio/src/i2c/hardware.rs | 2 +- services/llio/src/i2c_lib.rs | 40 ++++++++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/services/llio/src/api/i2c_api.rs b/services/llio/src/api/i2c_api.rs index 8cdf201a2..8a973bb10 100644 --- a/services/llio/src/api/i2c_api.rs +++ b/services/llio/src/api/i2c_api.rs @@ -45,10 +45,11 @@ pub struct I2cTransaction { pub rxbuf: Option<[u8; I2C_MAX_LEN]>, pub rxlen: u32, pub timeout_ms: u32, + pub use_repeated_start: bool, } impl I2cTransaction { pub fn new() -> Self { - I2cTransaction{ bus_addr: 0, txbuf: None, txlen: 0, rxbuf: None, rxlen: 0, timeout_ms: 500 } + I2cTransaction{ bus_addr: 0, txbuf: None, txlen: 0, rxbuf: None, rxlen: 0, timeout_ms: 500, use_repeated_start: true } } } #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] diff --git a/services/llio/src/i2c/hardware.rs b/services/llio/src/i2c/hardware.rs index 2fd398d4d..e39db6f67 100644 --- a/services/llio/src/i2c/hardware.rs +++ b/services/llio/src/i2c/hardware.rs @@ -320,7 +320,7 @@ impl I2cStateMachine { // send next byte if there is one if self.index < transaction.txlen { self.i2c_csr.wfo(utra::i2c::TXR_TXR, txbuf[self.index as usize] as u32); - if self.index == (transaction.txlen - 1) && transaction.rxbuf.is_none() { + if self.index == (transaction.txlen - 1) && (transaction.rxbuf.is_none() || !transaction.use_repeated_start) { // send a stop bit if this is the very last in the series self.i2c_csr.wo(utra::i2c::COMMAND, self.i2c_csr.ms(utra::i2c::COMMAND_WR, 1) | diff --git a/services/llio/src/i2c_lib.rs b/services/llio/src/i2c_lib.rs index 68403f4ef..ee3e3d7c1 100644 --- a/services/llio/src/i2c_lib.rs +++ b/services/llio/src/i2c_lib.rs @@ -79,9 +79,8 @@ impl I2c { } } - /// initiate an i2c read. if asyncread_cb is `None`, one will be provided and the routine will synchronously block until read is complete. - /// synchronous reads will return the data in &mut `data`. Asynchronous reads will provide the result in the `rxbuf` field of the `I2cTransaction` - /// returned via the callback. Note that the callback API may be revised to return a smaller, more targeted structure in the future. + /// initiate an i2c read. always blocks until done. uses a "repeated start" to switch between + /// addressing and reading the device. pub fn i2c_read(&mut self, dev: u8, adr: u8, data: &mut [u8]) -> Result { if data.len() > I2C_MAX_LEN - 1 { return Err(xous::Error::OutOfMemory) @@ -113,6 +112,41 @@ impl I2c { } } } + + /// initiate an i2c read, but for devices that don't support repeated starts, such as the AB-RTCMC-32.768 + pub fn i2c_read_no_repeated_start(&mut self, dev: u8, adr: u8, data: &mut [u8]) -> Result { + if data.len() > I2C_MAX_LEN - 1 { + return Err(xous::Error::OutOfMemory) + } + let mut transaction = I2cTransaction::new(); + let mut txbuf = [0; I2C_MAX_LEN]; + txbuf[0] = adr; + let rxbuf = [0; I2C_MAX_LEN]; + transaction.bus_addr = dev; + transaction.txbuf = Some(txbuf); + transaction.txlen = 1; + transaction.rxbuf = Some(rxbuf); + transaction.rxlen = data.len() as u32; + transaction.timeout_ms = self.timeout_ms; + transaction.use_repeated_start = false; + + let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + let result = buf.to_original::().unwrap(); + match result.status { + I2cStatus::ResponseReadOk => { + for (&src, dst) in result.rxbuf[..result.rxlen as usize].iter().zip(data.iter_mut()) { + *dst = src; + } + Ok(I2cStatus::ResponseReadOk) + } + _ => { + log::error!("I2C error: {:?}", result); + Err(xous::Error::InternalError) + } + } + } + } impl Drop for I2c {