pub struct LpI2c<'d> { /* private fields */ }Expand description
Low-power I2C driver
§Examples
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.
§Examples
use esp_hal::i2c::lp_i2c::{Config, LpI2c};
let i2c = LpI2c::new(
peripherals.LP_I2C0,
Config::default(),
peripherals.GPIO1,
peripherals.GPIO2,
)?;§Errors
crate::i2c::lp_i2c::ConfigError when the provided config is invalid.
Sourcepub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError>
pub fn apply_config(&mut self, config: &Config) -> Result<(), ConfigError>
Applies a new configuration.
§Examples
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())?;§Errors
crate::i2c::lp_i2c::ConfigError when the provided config is invalid.
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.
§Examples
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])?;§Errors
Error::TransactionSizeLimitExceeded when data is longer than the driver
can transfer in a single transaction.
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.
§Examples
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)?;§Errors
Error::TransactionSizeLimitExceeded when data is longer than the driver
can transfer in a single transaction.