Skip to main content

LpI2c

Struct LpI2c 

Source
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>

Source

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,
)?;
Source

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())?;
Source

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])?;
Source

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)?;

Trait Implementations§

Source§

impl Drop for LpI2c<'_>

Available on crate feature unstable only.
Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'d> Freeze for LpI2c<'d>

§

impl<'d> RefUnwindSafe for LpI2c<'d>

§

impl<'d> Send for LpI2c<'d>

§

impl<'d> Sync for LpI2c<'d>

§

impl<'d> Unpin for LpI2c<'d>

§

impl<'d> UnsafeUnpin for LpI2c<'d>

§

impl<'d> !UnwindSafe for LpI2c<'d>

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.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

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.