pub struct I2c<'d, Dm: DriverMode> { /* private fields */ }Expand description
I2C driver
§Examples
use esp_hal::i2c::master::{Config, I2c};
let mut i2c = I2c::new(peripherals.I2C0, Config::default())?
.with_sda(peripherals.GPIO1)
.with_scl(peripherals.GPIO2);
let mut data = [0u8; 22];
i2c.write_read(DEVICE_ADDR, &[0xaa], &mut data)?;Implementations§
Source§impl<'d> I2c<'d, Blocking>
impl<'d> I2c<'d, Blocking>
Sourcepub fn new(i2c: impl Instance + 'd, config: Config) -> Result<Self, ConfigError>
pub fn new(i2c: impl Instance + 'd, config: Config) -> Result<Self, ConfigError>
Creates a new I2C instance.
§Examples
use esp_hal::i2c::master::{Config, I2c};
let i2c = I2c::new(peripherals.I2C0, Config::default())?
.with_sda(peripherals.GPIO1)
.with_scl(peripherals.GPIO2);§Errors
ConfigError when bus frequency or timeout passed in config is invalid.
Sourcepub fn into_async(self) -> I2c<'d, Async>
pub fn into_async(self) -> I2c<'d, Async>
Sourcepub fn set_interrupt_handler(&mut self, handler: InterruptHandler)
Available on crate feature unstable only.
pub fn set_interrupt_handler(&mut self, handler: InterruptHandler)
unstable only.Registers an interrupt handler for the peripheral on the current core.
Replaces any previously registered interrupt handlers.
The default/unhandled interrupt handler can be restored by passing DEFAULT_INTERRUPT_HANDLER.
§Panics
Panics if passed interrupt handler is invalid (e.g. has priority
None)
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn listen(&mut self, interrupts: impl Into<EnumSet<Event>>)
Available on crate feature unstable only.
pub fn listen(&mut self, interrupts: impl Into<EnumSet<Event>>)
unstable only.Listens for the given interrupts.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn unlisten(&mut self, interrupts: impl Into<EnumSet<Event>>)
Available on crate feature unstable only.
pub fn unlisten(&mut self, interrupts: impl Into<EnumSet<Event>>)
unstable only.Unlistens from the given interrupts.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn interrupts(&mut self) -> EnumSet<Event>
Available on crate feature unstable only.
pub fn interrupts(&mut self) -> EnumSet<Event>
unstable only.Returns the asserted interrupts.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn clear_interrupts(&mut self, interrupts: EnumSet<Event>)
Available on crate feature unstable only.
pub fn clear_interrupts(&mut self, interrupts: EnumSet<Event>)
unstable only.Resets asserted interrupts.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§impl<'d> I2c<'d, Async>
impl<'d> I2c<'d, Async>
Sourcepub fn into_blocking(self) -> I2c<'d, Blocking>
pub fn into_blocking(self) -> I2c<'d, Blocking>
Sourcepub async fn write_async<A: Into<I2cAddress>>(
&mut self,
address: A,
buffer: &[u8],
) -> Result<(), Error>
pub async fn write_async<A: Into<I2cAddress>>( &mut self, address: A, buffer: &[u8], ) -> Result<(), Error>
Writes bytes to slave with given address.
Dropping the returned Future aborts the transfer, but blocks while the driver finishes clearing and releasing the bus.
§Examples
use esp_hal::i2c::master::{Config, I2c};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = I2c::new(peripherals.I2C0, Config::default())?
.with_sda(peripherals.GPIO1)
.with_scl(peripherals.GPIO2)
.into_async();
i2c.write_async(DEVICE_ADDR, &[0xaa]).await?;Sourcepub async fn read_async<A: Into<I2cAddress>>(
&mut self,
address: A,
buffer: &mut [u8],
) -> Result<(), Error>
pub async fn read_async<A: Into<I2cAddress>>( &mut self, address: A, buffer: &mut [u8], ) -> Result<(), Error>
Reads enough bytes from slave with address to fill buffer.
Dropping the returned Future aborts the transfer, but blocks while the driver finishes clearing and releasing the bus.
§Examples
use esp_hal::i2c::master::{Config, I2c};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = I2c::new(peripherals.I2C0, Config::default())?
.with_sda(peripherals.GPIO1)
.with_scl(peripherals.GPIO2)
.into_async();
let mut data = [0u8; 22];
i2c.read_async(DEVICE_ADDR, &mut data).await?;§Errors
Error when the passed buffer has zero length.
Sourcepub async fn write_read_async<A: Into<I2cAddress>>(
&mut self,
address: A,
write_buffer: &[u8],
read_buffer: &mut [u8],
) -> Result<(), Error>
pub async fn write_read_async<A: Into<I2cAddress>>( &mut self, address: A, write_buffer: &[u8], read_buffer: &mut [u8], ) -> Result<(), Error>
Writes bytes to slave with given address and then reads enough
bytes to fill buffer in a single transaction.
Dropping the returned Future aborts the transfer, but blocks while the driver finishes clearing and releasing the bus.
§Examples
use esp_hal::i2c::master::{Config, I2c};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = I2c::new(peripherals.I2C0, Config::default())?
.with_sda(peripherals.GPIO1)
.with_scl(peripherals.GPIO2)
.into_async();
let mut data = [0u8; 22];
i2c.write_read_async(DEVICE_ADDR, &[0xaa], &mut data)
.await?;§Errors
Error when the passed buffer has zero length.
Sourcepub async fn transaction_async<'a, A: Into<I2cAddress>>(
&mut self,
address: A,
operations: impl IntoIterator<Item = &'a mut Operation<'a>>,
) -> Result<(), Error>
pub async fn transaction_async<'a, A: Into<I2cAddress>>( &mut self, address: A, operations: impl IntoIterator<Item = &'a mut Operation<'a>>, ) -> Result<(), Error>
Executes the provided operations on the I2C bus as a single transaction.
Dropping the returned Future aborts the transfer, but blocks while the driver finishes clearing and releasing the bus.
Transaction contract:
-
Before executing the first operation an ST is sent automatically. This is followed by SAD+R/W as appropriate.
-
Data from adjacent operations of the same type are sent after each other without an SP or SR.
-
Between adjacent operations of a different type an SR and SAD+R/W is sent.
-
After executing the last operation an SP is sent automatically.
-
If the last operation is a
Readthe master does not send an acknowledge for the last byte. -
ST= start condition -
SAD+R/W= slave address followed by bit 1 to indicate reading or 0 to indicate writing -
SR= repeated start condition -
SP= stop condition
§Examples
use esp_hal::i2c::master::{Config, I2c, Operation};
const DEVICE_ADDR: u8 = 0x77;
let mut i2c = I2c::new(peripherals.I2C0, Config::default())?
.with_sda(peripherals.GPIO1)
.with_scl(peripherals.GPIO2)
.into_async();
let mut data = [0u8; 22];
i2c.transaction_async(
DEVICE_ADDR,
&mut [Operation::Write(&[0xaa]), Operation::Read(&mut data)],
)
.await?;§Errors
Error when the buffer passed to an Operation has zero length.
Source§impl<'d, Dm> I2c<'d, Dm>where
Dm: DriverMode,
impl<'d, Dm> I2c<'d, Dm>where
Dm: DriverMode,
Sourcepub fn with_sda(
self,
sda: impl PeripheralInput<'d> + PeripheralOutput<'d>,
) -> Self
pub fn with_sda( self, sda: impl PeripheralInput<'d> + PeripheralOutput<'d>, ) -> Self
Connects a pin to the I2C SDA signal.
If called with a pin singleton (e.g. GPIO2), the pin is configured to use
the internal pull-up resistor. If that is undesired, call this method with a
fully configured Flex pin driver. With Flex, the I2C
driver does not change the pin configuration.
This will replace previous pin assignments for this signal.
§Examples
Basic usage
use esp_hal::i2c::master::{Config, I2c};
let i2c = I2c::new(peripherals.I2C0, Config::default())?.with_sda(peripherals.GPIO2);Using Flex to configure the pin
use esp_hal::{
gpio::{DriveMode, Flex, OutputConfig},
i2c::master::{Config, I2c},
};
let mut sda = Flex::new(peripherals.GPIO2);
// The default pullup setting is `Pull::None`.
sda.apply_output_config(&OutputConfig::default().with_drive_mode(DriveMode::OpenDrain));
sda.set_input_enable(true);
sda.set_output_enable(true);
// Initial pin state to avoid the pin to go low during peripheral configuration.
sda.set_high();
let i2c = I2c::new(peripherals.I2C0, Config::default())?.with_sda(sda);Sourcepub fn with_scl(
self,
scl: impl PeripheralInput<'d> + PeripheralOutput<'d>,
) -> Self
pub fn with_scl( self, scl: impl PeripheralInput<'d> + PeripheralOutput<'d>, ) -> Self
Connects a pin to the I2C SCL signal.
If called with a pin singleton (e.g. GPIO2), the pin is configured to use
the internal pull-up resistor. If that is undesired, call this method with a
fully configured Flex pin driver. With Flex, the I2C
driver does not change the pin configuration.
This will replace previous pin assignments for this signal.
§Examples
Basic usage
use esp_hal::i2c::master::{Config, I2c};
let i2c = I2c::new(peripherals.I2C0, Config::default())?.with_scl(peripherals.GPIO2);Using Flex to configure the pin
use esp_hal::{
gpio::{DriveMode, Flex, OutputConfig},
i2c::master::{Config, I2c},
};
let mut scl = Flex::new(peripherals.GPIO2);
// The default pullup setting is `Pull::None`.
scl.apply_output_config(&OutputConfig::default().with_drive_mode(DriveMode::OpenDrain));
scl.set_input_enable(true);
scl.set_output_enable(true);
// Initial pin state to avoid the pin to go low during peripheral configuration.
scl.set_high();
let i2c = I2c::new(peripherals.I2C0, Config::default())?.with_scl(scl);Sourcepub fn write<A: Into<I2cAddress>>(
&mut self,
address: A,
buffer: &[u8],
) -> Result<(), Error>
pub fn write<A: Into<I2cAddress>>( &mut self, address: A, buffer: &[u8], ) -> Result<(), Error>
Writes bytes to slave with given address.
§Examples
use esp_hal::i2c::master::{Config, I2c};
i2c.write(DEVICE_ADDR, &[0xaa])?;Sourcepub fn read<A: Into<I2cAddress>>(
&mut self,
address: A,
buffer: &mut [u8],
) -> Result<(), Error>
pub fn read<A: Into<I2cAddress>>( &mut self, address: A, buffer: &mut [u8], ) -> Result<(), Error>
Sourcepub fn write_read<A: Into<I2cAddress>>(
&mut self,
address: A,
write_buffer: &[u8],
read_buffer: &mut [u8],
) -> Result<(), Error>
pub fn write_read<A: Into<I2cAddress>>( &mut self, address: A, write_buffer: &[u8], read_buffer: &mut [u8], ) -> Result<(), Error>
Sourcepub fn transaction<'a, A: Into<I2cAddress>>(
&mut self,
address: A,
operations: impl IntoIterator<Item = &'a mut Operation<'a>>,
) -> Result<(), Error>
pub fn transaction<'a, A: Into<I2cAddress>>( &mut self, address: A, operations: impl IntoIterator<Item = &'a mut Operation<'a>>, ) -> Result<(), Error>
Executes the provided operations on the I2C bus.
Transaction contract:
-
Before executing the first operation an ST is sent automatically. This is followed by SAD+R/W as appropriate.
-
Data from adjacent operations of the same type are sent after each other without an SP or SR.
-
Between adjacent operations of a different type an SR and SAD+R/W is sent.
-
After executing the last operation an SP is sent automatically.
-
If the last operation is a
Readthe master does not send an acknowledge for the last byte. -
ST= start condition -
SAD+R/W= slave address followed by bit 1 to indicate reading or 0 to indicate writing -
SR= repeated start condition -
SP= stop condition
§Examples
use esp_hal::i2c::master::{Config, I2c, Operation};
let mut data = [0u8; 22];
i2c.transaction(
DEVICE_ADDR,
&mut [Operation::Write(&[0xaa]), Operation::Read(&mut data)],
)?;§Errors
Error when the buffer passed to an Operation has zero length.
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::master::{Config, I2c};
let mut i2c = I2c::new(peripherals.I2C0, Config::default())?;
i2c.apply_config(&Config::default().with_frequency(Rate::from_khz(400)))?;§Errors
ConfigError when bus frequency or timeout passed in config is invalid.
Sourcepub fn force_scl_low(&mut self, low: bool)
Available on crate feature unstable only.
pub fn force_scl_low(&mut self, low: bool)
unstable only.Drives SCL low (true) or releases it (false).
Forcing a line low interrupts normal peripheral operation — no transactions should be started while a line is held low.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Sourcepub fn force_sda_low(&mut self, low: bool)
Available on crate feature unstable only.
pub fn force_sda_low(&mut self, low: bool)
unstable only.Drives SDA low (true) or releases it (false).
Forcing a line low interrupts normal peripheral operation — no transactions should be started while a line is held low.
§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Trait Implementations§
Source§impl<'d, Dm: Debug + DriverMode> Debug for I2c<'d, Dm>
impl<'d, Dm: Debug + DriverMode> Debug for I2c<'d, Dm>
Source§impl I2c for I2c<'_, Async>
impl I2c for I2c<'_, Async>
Source§async fn transaction(
&mut self,
address: u8,
operations: &mut [EhalOperation<'_>],
) -> Result<(), Self::Error>
async fn transaction( &mut self, address: u8, operations: &mut [EhalOperation<'_>], ) -> Result<(), Self::Error>
Source§async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error>
async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error>
address. Read moreSource§async fn write_read(
&mut self,
address: A,
write: &[u8],
read: &mut [u8],
) -> Result<(), Self::Error>
async fn write_read( &mut self, address: A, write: &[u8], read: &mut [u8], ) -> Result<(), Self::Error>
address and then reads enough bytes to fill read in a
single transaction. Read moreSource§impl<Dm: DriverMode> I2c for I2c<'_, Dm>
impl<Dm: DriverMode> I2c for I2c<'_, Dm>
Source§fn transaction(
&mut self,
address: u8,
operations: &mut [Operation<'_>],
) -> Result<(), Self::Error>
fn transaction( &mut self, address: u8, operations: &mut [Operation<'_>], ) -> Result<(), Self::Error>
Source§fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error>
fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error>
address. Read moreSource§fn write_read(
&mut self,
address: A,
write: &[u8],
read: &mut [u8],
) -> Result<(), Self::Error>
fn write_read( &mut self, address: A, write: &[u8], read: &mut [u8], ) -> Result<(), Self::Error>
address and then reads enough bytes to fill read in a
single transaction. Read moreSource§impl InterruptConfigurable for I2c<'_, Blocking>
Available on crate feature unstable only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl InterruptConfigurable for I2c<'_, Blocking>
unstable only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§fn set_interrupt_handler(&mut self, handler: InterruptHandler)
fn set_interrupt_handler(&mut self, handler: InterruptHandler)
Source§impl<Dm: DriverMode> SetConfig for I2c<'_, Dm>
Available on crate feature unstable only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
impl<Dm: DriverMode> SetConfig for I2c<'_, Dm>
unstable only.§Stability
This API is marked as unstable and is only available when the unstable
crate feature is enabled. This comes with no stability guarantees, and could be changed
or removed at any time.
Source§type ConfigError = ConfigError
type ConfigError = ConfigError
set_config fails.