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.
This commit is contained in:
bunnie 2023-02-01 03:15:53 +08:00
parent 8c08cf8f91
commit c50c9358c5
3 changed files with 40 additions and 5 deletions

View file

@ -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)]

View file

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

View file

@ -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<I2cStatus, xous::Error> {
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<I2cStatus, xous::Error> {
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::<I2cResult, _>().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 {