cleanup the i2c API

condense async/sync methods into a single one and decide which
is which based upon the presence of an Option<callback>
This commit is contained in:
bunnie 2021-05-06 14:03:06 +08:00
parent b50aa60591
commit adc7f8e98d
3 changed files with 85 additions and 124 deletions

View file

@ -181,10 +181,10 @@ impl Codec {
}
fn w(&mut self, adr: u8, data: &[u8]) -> bool {
log::trace!("writing to 0x{:x}, {:x?}", adr, data);
match self.llio.i2c_write_sync(TLV320AIC3100_I2C_ADR, adr, data) {
//log::trace!("writing to 0x{:x}, {:x?}", adr, data);
match self.llio.i2c_write(TLV320AIC3100_I2C_ADR, adr, data, None) {
Ok(status) => {
log::trace!("write returned with status {:?}", status);
//log::trace!("write returned with status {:?}", status);
match status {
I2cStatus::ResponseWriteOk => true,
I2cStatus::ResponseBusy => false,
@ -195,7 +195,7 @@ impl Codec {
}
}
fn r(&mut self, adr: u8, data: &mut[u8]) -> bool {
match self.llio.i2c_read_sync(TLV320AIC3100_I2C_ADR, adr, data) {
match self.llio.i2c_read(TLV320AIC3100_I2C_ADR, adr, data, None) {
Ok(status) => {
match status {
I2cStatus::ResponseReadOk => true,

View file

@ -76,25 +76,41 @@ impl Llio {
pub fn i2c_set_timeout(&mut self, timeout: u32) {
self.i2c_timeout_ms = timeout;
}
pub fn i2c_write_async(&mut self, dev: u8, adr: u8, data: &[u8], maybe_cb: Option<fn(I2cTransaction)>) -> Result<I2cStatus, xous::Error> {
/// initiate an i2c write. if async_cb is `None`, one will be provided and the routine will synchronously block until write is complete.
/// if you want to "fire and forget" the write and don't care when or how it finishes, simply provide a dummy callback.
pub fn i2c_write(&mut self, dev: u8, adr: u8, data: &[u8], async_cb: Option<fn(I2cTransaction)>) -> Result<I2cStatus, xous::Error> {
if data.len() > I2C_MAX_LEN - 1 {
return Err(xous::Error::OutOfMemory)
}
let synchronous: bool;
let mut transaction = I2cTransaction::new();
if let Some(cb) = maybe_cb {
self.check_cb_init();
unsafe {
if let Some(old_cb) = I2C_CB {
if old_cb != cb {
log::warn!("Multiple outstanding write transactions, with different callbacks. You are probably making an error!");
}
let cb = if let Some(callback) = async_cb {
synchronous = false;
callback
} else {
synchronous = true;
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
log::error!("entering a synchronous write routine, but somehow a synchronous operation was already in progress!");
return Err(xous::Error::InternalError);
}
sync_i2c_cb
};
self.check_cb_init();
unsafe {
if let Some(old_cb) = I2C_CB {
if old_cb != cb {
log::warn!("Multiple outstanding write transactions, with different callbacks. You are probably making an error!");
}
I2C_CB = Some(cb);
}
match self.i2c_sid {
Some(sid) => transaction.listener = Some(sid.to_u32()),
None => log::error!("We requested a local listener, but somehow it's not there!"),
}
I2C_CB = Some(cb);
}
if synchronous {
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
}
match self.i2c_sid {
Some(sid) => transaction.listener = Some(sid.to_u32()),
None => log::error!("We requested a local listener, but somehow it's not there!"),
}
let mut txbuf = [0; I2C_MAX_LEN];
txbuf[0] = adr;
@ -110,12 +126,40 @@ impl Llio {
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
let result = buf.to_original::<I2cStatus, _>().unwrap();
Ok(result)
if synchronous {
if result != I2cStatus::ResponseInProgress {
return Err(xous::Error::OutOfMemory);
}
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
xous::yield_slice();
}
Ok(I2cStatus::ResponseWriteOk)
} else {
Ok(result)
}
}
pub fn i2c_read_async(&mut self, dev: u8, adr: u8, data: &mut[u8], read_cb: fn(I2cTransaction)) -> Result<I2cStatus, xous::Error> {
/// 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.
pub fn i2c_read(&mut self, dev: u8, adr: u8, data: &mut[u8], asyncread_cb: Option<fn(I2cTransaction)>) -> Result<I2cStatus, xous::Error> {
if data.len() > I2C_MAX_LEN - 1 {
return Err(xous::Error::OutOfMemory)
}
let synchronous: bool;
let read_cb = if let Some(cb) = asyncread_cb {
// user is supplying an async callback
synchronous = false;
cb
} else {
// user hasn't provided a callback, we'll provide one and return synchronously
synchronous = true;
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
log::error!("trying a synchronous read, but somehow a synchronous operation was already in progress!");
return Err(xous::Error::InternalError);
}
sync_i2c_cb // supply a default callback
};
let mut transaction = I2cTransaction::new();
self.check_cb_init();
unsafe {
@ -126,9 +170,8 @@ impl Llio {
}
I2C_CB = Some(read_cb);
}
match self.i2c_sid {
Some(sid) => transaction.listener = Some(sid.to_u32()),
None => log::error!("We requested a local listener, but somehow it's not there!"),
if synchronous {
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
}
match self.i2c_sid {
Some(sid) => transaction.listener = Some(sid.to_u32()),
@ -148,110 +191,28 @@ impl Llio {
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
let result = buf.to_original::<I2cStatus, _>().unwrap();
Ok(result)
if synchronous {
if result != I2cStatus::ResponseInProgress {
return Err(xous::Error::OutOfMemory);
}
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
xous::yield_slice();
}
unsafe {
for (&src, dst) in I2C_RX_HANDOFF.iter().zip(data.iter_mut()) {
*dst = src;
}
}
Ok(I2cStatus::ResponseReadOk)
} else {
Ok(result)
}
}
// used by async callback handlers to indicate their completion, allowing e.g. later synchronous operations
pub fn i2c_async_done(&self) {
unsafe{I2C_CB = None};
}
pub fn i2c_write_sync(&mut self, dev: u8, adr: u8, data: &[u8]) -> Result<I2cStatus, xous::Error> {
if data.len() > I2C_MAX_LEN - 1 {
return Err(xous::Error::OutOfMemory)
}
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
log::error!("entering a synchronous routine, but somehow something was already in progress!");
return Err(xous::Error::InternalError);
}
let mut transaction = I2cTransaction::new();
self.check_cb_init();
unsafe {
if let Some(old_cb) = I2C_CB {
if old_cb != sync_i2c_cb {
log::error!("Mix-and-match of sync and async callbacks is not allowed. Finish your async callbacks first, and call i2c_async_done!");
return Err(xous::Error::ShareViolation);
}
}
I2C_CB = Some(sync_i2c_cb);
}
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
match self.i2c_sid {
Some(sid) => transaction.listener = Some(sid.to_u32()),
None => log::error!("We requested a local listener, but somehow it's not there!"),
}
let mut txbuf = [0; I2C_MAX_LEN];
txbuf[0] = adr;
for i in 0..data.len() {
txbuf[i+1] = data[i];
}
transaction.bus_addr = dev;
transaction.txbuf = Some(txbuf);
transaction.txlen = (data.len() + 1) as u32;
transaction.status = I2cStatus::RequestIncoming;
transaction.timeout_ms = self.i2c_timeout_ms;
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
let result = buf.to_original::<I2cStatus, _>().unwrap();
if result != I2cStatus::ResponseInProgress {
return Err(xous::Error::OutOfMemory);
}
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
xous::yield_slice();
}
Ok(I2cStatus::ResponseWriteOk)
}
pub fn i2c_read_sync(&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)
}
if I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
log::error!("entering a synchronous routine, but somehow something was already in progress!");
return Err(xous::Error::InternalError);
}
let mut transaction = I2cTransaction::new();
self.check_cb_init();
unsafe {
if let Some(old_cb) = I2C_CB {
if old_cb != sync_i2c_cb {
log::error!("Mix-and-match of sync and async callbacks is not allowed. Finish your async callbacks first, and call i2c_async_done!");
return Err(xous::Error::ShareViolation);
}
}
I2C_CB = Some(sync_i2c_cb);
}
I2C_IN_PROGRESS_MUTEX.store(true, Ordering::Relaxed);
match self.i2c_sid {
Some(sid) => transaction.listener = Some(sid.to_u32()),
None => log::error!("We requested a local listener, but somehow it's not there!"),
}
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.status = I2cStatus::RequestIncoming;
transaction.timeout_ms = self.i2c_timeout_ms;
let mut buf = Buffer::into_buf(transaction).or(Err(xous::Error::InternalError))?;
buf.lend_mut(self.i2c_conn, I2cOpcode::I2cTxRx.to_u32().unwrap()).or(Err(xous::Error::InternalError))?;
let result = buf.to_original::<I2cStatus, _>().unwrap();
if result != I2cStatus::ResponseInProgress {
return Err(xous::Error::OutOfMemory);
}
while I2C_IN_PROGRESS_MUTEX.load(Ordering::Relaxed) {
xous::yield_slice();
}
unsafe {
for (&src, dst) in I2C_RX_HANDOFF.iter().zip(data.iter_mut()) {
*dst = src;
}
}
Ok(I2cStatus::ResponseReadOk)
}
///////////////////////// I2C done ///////////////
///////////////////////// end I2C ///////////////

View file

@ -295,7 +295,7 @@ mod implementation {
txbuf[6] = to_bcd(months);
txbuf[7] = to_bcd(years);
match self.llio.i2c_write_sync(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &txbuf) {
match self.llio.i2c_write(ABRTCMC_I2C_ADR, ABRTCMC_CONTROL3, &txbuf, None) {
Ok(status) => {
match status {
I2cStatus::ResponseWriteOk => Ok(true),
@ -309,7 +309,7 @@ mod implementation {
pub fn rtc_get(&mut self) -> Result<(), xous::Error> {
let mut rxbuf = [0; 7];
match self.llio.i2c_read_async(ABRTCMC_I2C_ADR, ABRTCMC_SECONDS, &mut rxbuf, i2c_callback) {
match self.llio.i2c_read(ABRTCMC_I2C_ADR, ABRTCMC_SECONDS, &mut rxbuf, Some(i2c_callback)) {
Ok(status) => {
match status {
I2cStatus::ResponseInProgress => Ok(()),
@ -329,7 +329,7 @@ mod implementation {
// blocking_i2c_write2(adr: u8, data: u8) -> bool
// but need to make sure we don't bork any of the constants later on in this code :P
fn blocking_i2c_write2(&mut self, adr: u8, data: u8) -> bool {
match self.llio.i2c_write_sync(ABRTCMC_I2C_ADR, adr, &[data]) {
match self.llio.i2c_write(ABRTCMC_I2C_ADR, adr, &[data], None) {
Ok(status) => {
match status {
I2cStatus::ResponseWriteOk => true,