Skip to main content

esp_hal/i2c/master/
eh.rs

1use embedded_hal::i2c::Operation as EhalOperation;
2
3use super::{Async, DriverMode, Error, I2c, I2cAddress, Operation};
4use crate::i2c::master::I2cClockGuard;
5
6impl embedded_hal::i2c::Error for Error {
7    fn kind(&self) -> embedded_hal::i2c::ErrorKind {
8        use embedded_hal::i2c::ErrorKind;
9
10        match self {
11            Self::FifoExceeded => ErrorKind::Overrun,
12            Self::ArbitrationLost => ErrorKind::ArbitrationLoss,
13            Self::AcknowledgeCheckFailed(reason) => ErrorKind::NoAcknowledge(reason.into()),
14            _ => ErrorKind::Other,
15        }
16    }
17}
18
19#[instability::unstable]
20impl<Dm: DriverMode> embassy_embedded_hal::SetConfig for I2c<'_, Dm> {
21    type Config = super::Config;
22    type ConfigError = super::ConfigError;
23
24    fn set_config(&mut self, config: &Self::Config) -> Result<(), Self::ConfigError> {
25        self.apply_config(config)
26    }
27}
28
29impl<Dm: DriverMode> embedded_hal::i2c::ErrorType for I2c<'_, Dm> {
30    type Error = Error;
31}
32
33impl<Dm: DriverMode> embedded_hal::i2c::I2c for I2c<'_, Dm> {
34    fn transaction(
35        &mut self,
36        address: u8,
37        operations: &mut [embedded_hal::i2c::Operation<'_>],
38    ) -> Result<(), Self::Error> {
39        let _clock_guard = I2cClockGuard::new(self.i2c.reborrow());
40        self.driver()
41            .transaction_impl(
42                I2cAddress::SevenBit(address),
43                operations.iter_mut().map(Operation::from),
44            )
45            .inspect_err(|error| self.internal_recover(error))
46    }
47}
48
49impl embedded_hal_async::i2c::I2c for I2c<'_, Async> {
50    async fn transaction(
51        &mut self,
52        address: u8,
53        operations: &mut [EhalOperation<'_>],
54    ) -> Result<(), Self::Error> {
55        let _clock_guard = I2cClockGuard::new(self.i2c.reborrow());
56        self.driver()
57            .transaction_impl_async(address.into(), operations.iter_mut().map(Operation::from))
58            .await
59            .inspect_err(|error| self.internal_recover(error))
60    }
61}