From bebd7c3371d896013085495d93d472fd49732b39 Mon Sep 17 00:00:00 2001 From: Justin Beaurivage Date: Wed, 31 Jan 2024 15:52:26 -0500 Subject: [PATCH] Implement i2c --- hal/src/sercom/i2c/impl_ehal.rs | 72 ++++++++++++++++++++++++++++++--- 1 file changed, 67 insertions(+), 5 deletions(-) diff --git a/hal/src/sercom/i2c/impl_ehal.rs b/hal/src/sercom/i2c/impl_ehal.rs index 64a70c0b06de..6b4c19297ae7 100644 --- a/hal/src/sercom/i2c/impl_ehal.rs +++ b/hal/src/sercom/i2c/impl_ehal.rs @@ -1,9 +1,71 @@ -//! `embedded-hal` trait implementations for [`I2c`]s +//! [`embedded-hal`] trait implementations for [`I2c`]s use super::{config::AnyConfig, flags::Error, I2c}; -use crate::ehal_02::blocking::i2c::{Read, Write, WriteRead}; +use crate::ehal::i2c::{self, ErrorKind, ErrorType, NoAcknowledgeSource}; -impl Write for I2c { +impl i2c::Error for Error { + fn kind(&self) -> ErrorKind { + match self { + Error::BusError => ErrorKind::Bus, + Error::ArbitrationLost => ErrorKind::ArbitrationLoss, + Error::LengthError => ErrorKind::Other, + Error::Nack => ErrorKind::NoAcknowledge(NoAcknowledgeSource::Unknown), + Error::Timeout => ErrorKind::Other, + } + } +} + +impl ErrorType for I2c { + type Error = Error; +} + +impl i2c::I2c for I2c { + fn transaction( + &mut self, + address: u8, + operations: &mut [i2c::Operation<'_>], + ) -> Result<(), Self::Error> { + for op in operations { + match op { + i2c::Operation::Read(buf) => { + self.do_read(address, buf)?; + self.cmd_stop(); + } + i2c::Operation::Write(bytes) => { + self.do_write(address, bytes)?; + self.cmd_stop(); + } + } + } + + Ok(()) + } + + fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { + self.do_write(address, bytes)?; + self.cmd_stop(); + Ok(()) + } + + fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { + self.do_read(address, buffer)?; + self.cmd_stop(); + Ok(()) + } + + fn write_read( + &mut self, + address: u8, + bytes: &[u8], + buffer: &mut [u8], + ) -> Result<(), Self::Error> { + self.do_write_read(address, bytes, buffer)?; + self.cmd_stop(); + Ok(()) + } +} + +impl crate::ehal_02::blocking::i2c::Write for I2c { type Error = Error; fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { @@ -13,7 +75,7 @@ impl Write for I2c { } } -impl Read for I2c { +impl crate::ehal_02::blocking::i2c::Read for I2c { type Error = Error; fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { @@ -23,7 +85,7 @@ impl Read for I2c { } } -impl WriteRead for I2c { +impl crate::ehal_02::blocking::i2c::WriteRead for I2c { type Error = Error; fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {