pub struct I2cScript { /* private fields */ }Expand description
An I2C bus that plays a script of transfers and replies.
Each transaction the driver issues is matched against the next step. A register
read (a write followed by a read in one transaction) consumes one
I2cStep::WriteRead; a plain write or read consumes a I2cStep::Write or
I2cStep::Read; a longer transaction consumes steps in order. A transfer that
does not match fails with ScriptError::Mismatch, which names the step and what
arrived, and a test ends by checking done so an unfinished
script is a failure too.
§Examples
use pamoja_hal::i2c::I2c;
use pamoja_hal::script::{I2cScript, I2cStep};
// A TMP117, from its datasheet: the configuration register takes continuous conversion,
// its factory setting, and the result register counts 7.8125 millidegrees a step.
const TMP117: u8 = 0x48;
const RESULT: u8 = 0x00;
const CONFIGURATION: u8 = 0x01;
let [high, low] = 0x0220u16.to_be_bytes();
let reading = (25.0 / 0.0078125) as i16;
// The part's side: the configuration the driver must write, then 25 degrees.
let mut bus = I2cScript::new([
I2cStep::write(TMP117, [CONFIGURATION, high, low]),
I2cStep::write_read(TMP117, [RESULT], reading.to_be_bytes()),
]);
// The driver's side.
bus.write(TMP117, &[CONFIGURATION, high, low])?;
let mut result = [0u8; 2];
bus.write_read(TMP117, &[RESULT], &mut result)?;
assert_eq!(f64::from(i16::from_be_bytes(result)) * 0.0078125, 25.0);
assert!(bus.done(), "every transfer the part expected has happened");Implementations§
Trait Implementations§
Source§impl I2c for I2cScript
impl I2c for I2cScript
Source§fn transaction(
&mut self,
address: SevenBitAddress,
operations: &mut [Operation<'_>],
) -> Result<(), ScriptError>
fn transaction( &mut self, address: SevenBitAddress, operations: &mut [Operation<'_>], ) -> Result<(), ScriptError>
Execute the provided operations on the I2C bus. Read more
Auto Trait Implementations§
impl Freeze for I2cScript
impl RefUnwindSafe for I2cScript
impl Send for I2cScript
impl Sync for I2cScript
impl Unpin for I2cScript
impl UnsafeUnpin for I2cScript
impl UnwindSafe for I2cScript
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more