Skip to content

Add transactional traits for SPI and I2C #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
23 changes: 23 additions & 0 deletions src/blocking/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,26 @@ pub trait WriteIterRead {
where
B: IntoIterator<Item = u8>;
}

/// Operation for transactional I2C trait
///
/// This allows composition of I2C operations into a single bus transaction
#[cfg(feature = "unproven")]
#[derive(Debug, PartialEq)]
pub enum Operation<'a> {
/// Read data into the provided buffer
Read(&'a mut [u8]),
/// Write data from the provided buffer
Write(&'a [u8]),
}

/// Transactional trait allows multiple actions to be executed
/// as part of a single I2C transaction
#[cfg(feature = "unproven")]
pub trait Transactional {
/// Associated error type
type Error;

/// Execute the provided actions against the provided I2C address
fn exec<'a>(&mut self, address: u8, operations: &mut[Operation<'a>]) -> Result<(), Self::Error>;
}
23 changes: 23 additions & 0 deletions src/blocking/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,26 @@ pub mod write_iter {
}
}
}

/// Operation for transactional SPI trait
///
/// This allows composition of SPI operations into a single bus transaction
#[cfg(feature = "unproven")]
#[derive(Debug, PartialEq)]
pub enum Operation<'a, W: 'static> {
/// Write data from the provided buffer, discarding read data
Write(&'a [W]),
/// Write data out while reading data into the provided buffer
WriteRead(&'a mut [W]),
}

/// Transactional trait allows multiple actions to be executed
/// as part of a single SPI transaction
#[cfg(feature = "unproven")]
pub trait Transactional<W: 'static> {
/// Associated error type
type Error;

/// Execute the provided transactions
fn exec<'a>(&mut self, operations: &mut [Operation<'a, W>]) -> Result<(), Self::Error>;
}