pub struct I2c<'d, Dm: DriverMode> { /* private fields */ }
Expand description
I2C driver
§I2C initialization and communication with the device
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, Dm> I2c<'d, Dm>where
Dm: DriverMode,
impl<'d, Dm> I2c<'d, Dm>where
Dm: DriverMode,
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 ConfigError
variant will be returned if bus frequency or timeout
passed in config is invalid.
Sourcepub fn with_sda(
self,
sda: impl Peripheral<P = impl PeripheralOutput> + 'd,
) -> Self
pub fn with_sda( self, sda: impl Peripheral<P = impl PeripheralOutput> + 'd, ) -> Self
Connect a pin to the I2C SDA signal.
This will replace previous pin assignments for this signal.
Sourcepub fn with_scl(
self,
scl: impl Peripheral<P = impl PeripheralOutput> + 'd,
) -> Self
pub fn with_scl( self, scl: impl Peripheral<P = impl PeripheralOutput> + 'd, ) -> Self
Connect a pin to the I2C SCL signal.
This will replace previous pin assignments for this signal.
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 address address
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>
Execute 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
Read
the 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
let mut data = [0u8; 22];
i2c.transaction(
DEVICE_ADDR,
&mut [Operation::Write(&[0xaa]), Operation::Read(&mut data)]
)?;
§Errors
The corresponding error variant from Error
will be returned if the buffer passed to an Operation
has zero length.
Source§impl<'d> I2c<'d, Blocking>
impl<'d> I2c<'d, Blocking>
Sourcepub fn new(
i2c: impl Peripheral<P = impl Instance> + 'd,
config: Config,
) -> Result<Self, ConfigError>
pub fn new( i2c: impl Peripheral<P = impl Instance> + 'd, config: Config, ) -> Result<Self, ConfigError>
Create a new I2C instance.
§Errors
A ConfigError
variant will be returned if bus frequency or timeout
passed in config is invalid.
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.
Note that this will replace any previously registered interrupt handlers.
You can restore the default/unhandled interrupt handler by using [crate::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.Listen 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.Unlisten 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.Gets 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.
Sourcepub fn into_async(self) -> I2c<'d, Async>
pub fn into_async(self) -> I2c<'d, Async>
Configures the I2C peripheral to operate in asynchronous mode.
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>
Configure the I2C peripheral to operate in blocking mode.
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 address address
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>
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>
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>
Execute the provided operations on the I2C bus as a single transaction.
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
Read
the 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
§Errors
The corresponding error variant from Error
will be returned if the
buffer passed to an Operation
has zero length.
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<'d, Dm: DriverMode> Format for I2c<'d, Dm>
impl<'d, Dm: DriverMode> Format 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.