pub struct LpI2c<'d> { /* private fields */ }Expand description
Low-power I2C driver
§Example
use esp_hal::i2c::lp_i2c::{Config, LpI2c};
let mut i2c = LpI2c::new(
peripherals.LP_I2C0,
Config::default(),
peripherals.GPIO1,
peripherals.GPIO2,
)?;
let mut data = [0u8; 22];
i2c.read(DEVICE_ADDR, 0xaa, &mut data)?;Implementations§
Source§impl<'d> LpI2c<'d>
impl<'d> LpI2c<'d>
Sourcepub fn new(
i2c: LP_I2C0<'d>,
config: Config,
sda: impl Sda + 'd,
scl: impl Scl + 'd,
) -> Result<Self, ConfigError>
pub fn new( i2c: LP_I2C0<'d>, config: Config, sda: impl Sda + 'd, scl: impl Scl + 'd, ) -> Result<Self, ConfigError>
Creates a new instance of the LpI2c peripheral.
§Errors
A crate::i2c::lp_i2c::ConfigError variant will be returned if the provided config is
invalid.
§Example
use esp_hal::i2c::lp_i2c::{Config, LpI2c};
let i2c = LpI2c::new(
peripherals.LP_I2C0,
Config::default(),
peripherals.GPIO1,
peripherals.GPIO2,
)?;Sourcepub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError>
pub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError>
Applies a new configuration.
§Errors
A crate::i2c::lp_i2c::ConfigError variant will be returned if the provided config is
invalid.
§Example
use esp_hal::i2c::lp_i2c::{Config, LpI2c};
let mut i2c = LpI2c::new(
peripherals.LP_I2C0,
Config::default(),
peripherals.GPIO1,
peripherals.GPIO2,
)?;
i2c.apply_config(&Config::default())?;Sourcepub fn write(
&mut self,
address: u8,
register: u8,
data: &[u8],
) -> Result<(), Error>
pub fn write( &mut self, address: u8, register: u8, data: &[u8], ) -> Result<(), Error>
Writes data to the register of the slave with the given address.
The transfer consists of a single write transaction that sends the device address, the
register address, then data.
§Errors
Error::TransactionSizeLimitExceeded is returned if data is longer than the driver
can transfer in a single transaction.
§Example
use esp_hal::i2c::lp_i2c::{Config, LpI2c};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = LpI2c::new(
peripherals.LP_I2C0,
Config::default(),
peripherals.GPIO1,
peripherals.GPIO2,
)?;
i2c.write(DEVICE_ADDR, 2, &[0xaa])?;Sourcepub fn read(
&mut self,
address: u8,
register: u8,
data: &mut [u8],
) -> Result<(), Error>
pub fn read( &mut self, address: u8, register: u8, data: &mut [u8], ) -> Result<(), Error>
Reads enough bytes from the register of the slave with the given address to fill
data.
The transfer writes the device address and the register address, then repeats the start
condition to read data back.
§Errors
Error::TransactionSizeLimitExceeded is returned if data is longer than the driver
can transfer in a single transaction.
§Example
use esp_hal::i2c::lp_i2c::{Config, LpI2c};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = LpI2c::new(
peripherals.LP_I2C0,
Config::default(),
peripherals.GPIO1,
peripherals.GPIO2,
)?;
let mut data = [0u8; 22];
i2c.read(DEVICE_ADDR, 7, &mut data)?;