Skip to content

Commit b22c88c

Browse files
committed
Add transactional I2C trait
1 parent 036cfa4 commit b22c88c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/blocking/i2c.rs

+36
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,39 @@ pub trait WriteIterRead {
125125
where
126126
B: IntoIterator<Item = u8>;
127127
}
128+
129+
/// Transactional I2C operation.
130+
///
131+
/// Several operations can be combined as part of a transaction.
132+
#[derive(Debug, PartialEq)]
133+
pub enum Operation<'a> {
134+
/// Read data into the provided buffer
135+
Read(&'a mut [u8]),
136+
/// Write data from the provided buffer
137+
Write(&'a [u8]),
138+
}
139+
140+
/// Transactional I2C interface.
141+
///
142+
/// This allows combining operation within an I2C transaction.
143+
pub trait Transactional {
144+
/// Error type
145+
type Error;
146+
147+
/// Execute the provided operations on the I2C bus.
148+
///
149+
/// Transaction contract:
150+
/// - Before executing the first operation an ST is sent automatically. This is followed by SAD+R/W as appropriate.
151+
/// - Data from adjacent operations of the same type are sent after each other without an SP or SR.
152+
/// - Between adjacent operations of a different type an SR and SAD+R/W is sent.
153+
/// - After executing the last operation an SP is sent automatically.
154+
/// - If the last operation is a `Read` the master does not send an acknowledge for the last byte.
155+
///
156+
/// - `ST` = start condition
157+
/// - `SAD+R/W` = slave address followed by bit 1 to indicate reading or 0 to indicate writing
158+
/// - `SR` = repeated start condition
159+
/// - `SP` = stop condition
160+
fn try_exec<'a, O>(&mut self, address: u8, operations: O) -> Result<(), Self::Error>
161+
where
162+
O: AsMut<[Operation<'a>]>;
163+
}

0 commit comments

Comments
 (0)