Skip to main content

I2cBus

Trait I2cBus 

Source
pub trait I2cBus {
    // Required methods
    fn write(&mut self, address: u8, data: &[u8]) -> Result<(), I2cError>;
    fn read(&mut self, address: u8, data: &mut [u8]) -> Result<(), I2cError>;
    fn write_read(
        &mut self,
        address: u8,
        data: &[u8],
        buffer: &mut [u8],
    ) -> Result<(), I2cError>;

    // Provided method
    fn set_delay(&mut self, microseconds: u16) { ... }
}
Expand description

I2C master the adapter drives.

Implement it around the bus driver at hand — see the usb_i2c_tiny example for one wrapping a hardware I2C peripheral. The class translates the host’s i2c_msg flags: a lone message becomes a write or read, a write followed by a read becomes a write_read with a repeated START between the phases.

Required Methods§

Source

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

Write data to the 7-bit address, START to STOP.

An empty slice only probes the address, which is how bus scanning works.

Source

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

Read from the 7-bit address, START to STOP.

Source

fn write_read( &mut self, address: u8, data: &[u8], buffer: &mut [u8], ) -> Result<(), I2cError>

Write data, then read buffer from the same address with a repeated START between the phases.

Provided Methods§

Source

fn set_delay(&mut self, microseconds: u16)

Bit delay requested by Set Delay.

100 kHz corresponds to 10 µs, so a peripheral with a configurable baud rate can use 1_000_000 / microseconds. Bit-banged buses use it as the half-period; hardware that cannot change speed may ignore it.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§