Skip to main content

I2c

Struct I2c 

Source
pub struct I2c<'d, T: Instance, M: Mode, O: OperatingMode = Master> { /* private fields */ }
Expand description

I2C driver.

Without the embassy feature, operations have no timeout and may wait indefinitely if the bus or peripheral does not make progress.

Implementations§

Source§

impl<'d, T: Instance, M: Mode> I2c<'d, T, M, Master>

Source

pub fn into_slave( self, slave_config: SlaveConfig, ) -> Result<I2c<'d, T, M, Slave>, Error>

Switch this driver to slave mode.

The bus timing and timeout configured by the constructor are kept; only the own address and the slave control bits are reprogrammed.

Source§

impl<'d, T: Instance> I2c<'d, T, Blocking, Slave>

Source

pub fn new_slave_blocking<const REMAP: u8>( peri: Peri<'d, T>, scl: Peri<'d, impl SclPin<T, REMAP>>, sda: Peri<'d, impl SdaPin<T, REMAP>>, freq: Hertz, config: Config, slave_config: SlaveConfig, ) -> Result<Self, Error>

Create a new blocking I2C slave.

config.timeout bounds every blocking_* call.

Source

pub fn blocking_listen(&mut self) -> Result<SlaveCommand, Error>

Block until the master addresses this slave.

Source

pub fn blocking_respond_to_write( &mut self, buf: &mut [u8], ) -> Result<usize, Error>

Receive bytes from a write transaction.

Returns the number of bytes stored in buf. Bytes beyond buf.len() are acknowledged and discarded, so the bus is never held low waiting for space.

Source

pub fn blocking_respond_to_read( &mut self, data: &[u8], ) -> Result<SendStatus, Error>

Transmit bytes for a read transaction.

Data beyond data is padded with 0x00 until the master ends the transaction, matching the reference manual’s slave transmitter flow.

Source§

impl<'d, T: Instance> I2c<'d, T, Async, Slave>

Source

pub fn new_slave<const REMAP: u8>( peri: Peri<'d, T>, scl: Peri<'d, impl SclPin<T, REMAP>>, sda: Peri<'d, impl SdaPin<T, REMAP>>, _irq: impl Binding<T::EventInterrupt, EventInterruptHandler<T>> + Binding<T::ErrorInterrupt, ErrorInterruptHandler<T>> + 'd, freq: Hertz, config: Config, slave_config: SlaveConfig, ) -> Result<Self, Error>

Create a new interrupt-driven I2C slave.

config is accepted for symmetry with the master constructors. Its timeout is stored but not applied: asynchronous operations wait for the master indefinitely.

Source

pub fn new_slave_with_dma<const REMAP: u8>( peri: Peri<'d, T>, scl: Peri<'d, impl SclPin<T, REMAP>>, sda: Peri<'d, impl SdaPin<T, REMAP>>, _irq: impl Binding<T::EventInterrupt, EventInterruptHandler<T>> + Binding<T::ErrorInterrupt, ErrorInterruptHandler<T>> + 'd, tx_dma: Peri<'d, impl TxDma<T>>, rx_dma: Peri<'d, impl RxDma<T>>, freq: Hertz, config: Config, slave_config: SlaveConfig, ) -> Result<Self, Error>

Create a new I2C slave that uses DMA for the data phase.

As with new_slave, config is accepted for symmetry with the master constructors; no asynchronous operation uses the timeout.

Source

pub async fn listen(&mut self) -> Result<SlaveCommand, Error>

Wait until the master addresses this slave.

Source

pub async fn respond_to_write(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Receive bytes from a write transaction.

Source

pub async fn respond_to_read( &mut self, data: &[u8], ) -> Result<SendStatus, Error>

Transmit bytes for a read transaction.

Source§

impl<'d, T: Instance> I2c<'d, T, Async, Master>

Source

pub fn new<const REMAP: u8>( peri: Peri<'d, T>, scl: Peri<'d, impl SclPin<T, REMAP>>, sda: Peri<'d, impl SdaPin<T, REMAP>>, _irq: impl Binding<T::EventInterrupt, EventInterruptHandler<T>> + Binding<T::ErrorInterrupt, ErrorInterruptHandler<T>> + 'd, tx_dma: Peri<'d, impl TxDma<T>>, rx_dma: Peri<'d, impl RxDma<T>>, freq: Hertz, config: Config, ) -> Self

Create a new I2C driver.

Source§

impl<'d, T: Instance> I2c<'d, T, Blocking, Master>

Source

pub fn new_blocking<const REMAP: u8>( peri: Peri<'d, T>, scl: Peri<'d, impl SclPin<T, REMAP>>, sda: Peri<'d, impl SdaPin<T, REMAP>>, freq: Hertz, config: Config, ) -> Self

Create a new blocking I2C driver.

Source§

impl<'d, T: Instance, M: Mode> I2c<'d, T, M, Master>

Source

pub fn blocking_read(&mut self, addr: u8, read: &mut [u8]) -> Result<(), Error>

Blocking read.

Source

pub fn blocking_write(&mut self, addr: u8, write: &[u8]) -> Result<(), Error>

Blocking write.

Source

pub fn blocking_write_read( &mut self, addr: u8, write: &[u8], read: &mut [u8], ) -> Result<(), Error>

Blocking write, restart, read.

Source

pub fn blocking_transaction( &mut self, addr: u8, operations: &mut [Operation<'_>], ) -> Result<(), Error>

Blocking transaction with operations.

Consecutive operations of same type are merged. See transaction contract for details.

Source§

impl<'d, T: Instance> I2c<'d, T, Async, Master>

Source

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

Write.

Source

pub async fn read( &mut self, address: u8, buffer: &mut [u8], ) -> Result<(), Error>

Read.

Source

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

Write, restart, read.

Source

pub async fn transaction( &mut self, addr: u8, operations: &mut [Operation<'_>], ) -> Result<(), Error>

Transaction with operations.

Consecutive operations of same type are merged. See transaction contract for details.

Trait Implementations§

Source§

impl<'d, T: Instance, M: Mode> ErrorType for I2c<'d, T, M, Master>

Source§

type Error = Error

Error type
Source§

impl<'d, T: Instance, M: Mode> I2c for I2c<'d, T, M, Master>

Source§

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

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

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

Writes bytes to slave with address address. Read more
Source§

fn write_read( &mut self, address: u8, 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
Source§

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

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

impl<'d, T: Instance> I2c for I2c<'d, T, Async, Master>

Source§

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

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

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

Writes bytes to slave with address address. Read more
Source§

async fn write_read( &mut self, address: u8, 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
Source§

async fn transaction( &mut self, address: u8, operations: &mut [Operation<'_>], ) -> Result<(), Self::Error>

Execute the provided operations on the I2C bus as a single transaction. Read more

Auto Trait Implementations§

§

impl<'d, T, M, O = Master> !UnwindSafe for I2c<'d, T, M, O>

§

impl<'d, T, M, O> Freeze for I2c<'d, T, M, O>

§

impl<'d, T, M, O> RefUnwindSafe for I2c<'d, T, M, O>
where M: RefUnwindSafe, O: RefUnwindSafe, T: RefUnwindSafe,

§

impl<'d, T, M, O> Send for I2c<'d, T, M, O>
where M: Send, O: Send, T: Send,

§

impl<'d, T, M, O> Sync for I2c<'d, T, M, O>
where M: Sync, O: Sync, T: Sync,

§

impl<'d, T, M, O> Unpin for I2c<'d, T, M, O>
where M: Unpin, O: Unpin,

§

impl<'d, T, M, O> UnsafeUnpin for I2c<'d, T, M, O>

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.