From 1f6227c59fbd02a3cace5a568708e6d8b9048696 Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Wed, 7 Feb 2024 23:55:25 +0100 Subject: [PATCH] Add eh1 impl for LPI2C --- src/common/lpi2c.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/src/common/lpi2c.rs b/src/common/lpi2c.rs index 98aa0615..e4fd9bdc 100644 --- a/src/common/lpi2c.rs +++ b/src/common/lpi2c.rs @@ -747,6 +747,50 @@ impl blocking::WriteRead for Lpi2c { } } +impl eh1::i2c::Error for ControllerStatus { + fn kind(&self) -> eh1::i2c::ErrorKind { + use eh1::i2c::{ErrorKind, NoAcknowledgeSource}; + + if self.contains(ControllerStatus::BUS_BUSY) { + return ErrorKind::Bus; + } + + if self.contains(ControllerStatus::NACK_DETECTED) { + return ErrorKind::NoAcknowledge(NoAcknowledgeSource::Unknown); + } + + if self.contains(ControllerStatus::ARBITRATION_LOST) { + return ErrorKind::ArbitrationLoss; + } + + ErrorKind::Other + } +} + +impl eh1::i2c::ErrorType for Lpi2c { + type Error = ControllerStatus; +} + +impl eh1::i2c::I2c for Lpi2c { + fn transaction( + &mut self, + address: u8, + operations: &mut [eh1::i2c::Operation<'_>], + ) -> Result<(), Self::Error> { + let mut runner = transaction::Runner::new(self)?; + for operation in operations { + // Convert from eh1 Operation to eh02 Operation + let mut operation = match operation { + eh1::i2c::Operation::Read(buffer) => blocking::Operation::Read(buffer), + eh1::i2c::Operation::Write(buffer) => blocking::Operation::Write(buffer), + }; + runner.next_operation(address, &mut operation)?; + } + runner.stop()?; + Ok(()) + } +} + /// Details for the embedded-hal Transaction /// implementation. mod transaction {