Struct I2c

Source
pub struct I2c<'d> { /* private fields */ }
Available on crate feature unstable only.
Expand description

I2C (RTC) driver

§Example

use esp_hal::i2c::rtc::{Config, I2c};
let mut i2c = I2c::new(
    peripherals.RTC_I2C,
    Config::default(),
    peripherals.GPIO1,
    peripherals.GPIO2,
)?;

let mut data = [0u8; 22];
i2c.read(DEVICE_ADDR, 0xaa, &mut data)?;

Implementations§

Source§

impl<'d> I2c<'d>

Source

pub fn new( i2c: RTC_I2C<'d>, config: Config, sda: impl Sda + 'd, scl: impl Scl + 'd, ) -> Result<Self, ConfigError>

Create a new I2C (RTC) instance.

§Errors

A crate::i2c::rtc::ConfigError variant will be returned if bus frequency or timeout passed in config is invalid.

§Example
use esp_hal::i2c::rtc::{Config, I2c};
let i2c = I2c::new(
    peripherals.RTC_I2C,
    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::rtc::ConfigError variant will be returned if bus frequency or timeout passed in config is invalid.

§Example
use core::time::Duration;

use esp_hal::i2c::rtc::{Config, I2c};
let mut i2c = I2c::new(
    peripherals.RTC_I2C,
    Config::default(),
    peripherals.GPIO1,
    peripherals.GPIO2,
)?;

i2c.apply_config(&Config::default().with_timeout(Duration::from_micros(100)))?;
Source

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

Writes bytes to slave with given address and register.

§Example
use esp_hal::i2c::rtc::{Config, I2c};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = I2c::new(
    peripherals.RTC_I2C,
    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>

Writes bytes to slave with given address and register.

§Example
use esp_hal::i2c::rtc::{Config, I2c};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = I2c::new(
    peripherals.RTC_I2C,
    Config::default(),
    peripherals.GPIO1,
    peripherals.GPIO2,
)?;

let mut data = [0u8; 22];
i2c.read(DEVICE_ADDR, 7, &mut data)?;

Trait Implementations§

Source§

impl Drop for I2c<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'d> Freeze for I2c<'d>

§

impl<'d> RefUnwindSafe for I2c<'d>

§

impl<'d> Send for I2c<'d>

§

impl<'d> Sync for I2c<'d>

§

impl<'d> Unpin for I2c<'d>

§

impl<'d> !UnwindSafe for I2c<'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.