Skip to main content

I2cScript

Struct I2cScript 

Source
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 part at 0x48 whose 16-bit result register 0x00 reads 0x0C80.
let mut bus = I2cScript::new([
    I2cStep::write(0x48, [0x01, 0x60, 0x20]),
    I2cStep::write_read(0x48, [0x00], [0x0C, 0x80]),
]);

bus.write(0x48, &[0x01, 0x60, 0x20])?;
let mut result = [0u8; 2];
bus.write_read(0x48, &[0x00], &mut result)?;
assert_eq!(u16::from_be_bytes(result), 0x0C80);
assert!(bus.done());

Implementations§

Source§

impl I2cScript

Source

pub fn new(steps: impl IntoIterator<Item = I2cStep>) -> I2cScript

Creates a bus that expects steps in order.

§Arguments
  • steps - the transfers the driver is expected to make, and their replies.
§Returns

The scripted bus.

Source

pub fn done(&self) -> bool

Reports whether every step has been consumed.

§Returns

true once the driver has made every transfer the script expected.

Source

pub fn remaining(&self) -> usize

Reports how many steps remain.

§Returns

The number of steps the driver has not yet reached.

Source

pub fn consumed(&self) -> usize

Reports how many steps the driver has consumed.

§Returns

The number of steps matched or faulted so far.

Trait Implementations§

Source§

impl Clone for I2cScript

Source§

fn clone(&self) -> I2cScript

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for I2cScript

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for I2cScript

Source§

fn default() -> I2cScript

Returns the “default value” for a type. Read more
Source§

impl ErrorType for I2cScript

Source§

type Error = ScriptError

Error type
Source§

impl I2c for I2cScript

Source§

fn transaction( &mut self, address: SevenBitAddress, operations: &mut [Operation<'_>], ) -> Result<(), ScriptError>

Execute the provided operations on the I2C bus. Read more
§

fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error>

Reads enough bytes from slave with address to fill read. Read more
§

fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error>

Writes bytes to slave with address address. Read more
§

fn write_read( &mut self, address: A, write: &[u8], read: &mut [u8], ) -> Result<(), Self::Error>

Writes bytes to slave with address address and then reads enough bytes to fill read in a single transaction. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.