diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e4c3a9..1c33373 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - PWM complementary output capability for TIM1 with new example to demonstrate - Implement interface for reading and writing to the internal flash memory and an example for demonstration. - PWM output on complementary channels only for single channel timers (TIM16 + TIM17) +- impl embedded_hal_1::i2c::I2c for I2c ### Fixed diff --git a/Cargo.toml b/Cargo.toml index 393a9c7..6c1135c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ bare-metal = { version = "1.0.0" } cast = "0.3" cortex-m = "0.7" embedded-hal = { version = "0.2", features = ["unproven"] } +embedded-hal-1 = { package = "embedded-hal", version = "1.0" } stm32f0 = "0.14" nb = "1" void = { version = "1.0", default-features = false } diff --git a/src/i2c.rs b/src/i2c.rs index 7d5fa80..3eeec03 100644 --- a/src/i2c.rs +++ b/src/i2c.rs @@ -313,15 +313,8 @@ where let value = self.i2c.rxdr.read().bits() as u8; Ok(value) } -} - -impl WriteRead for I2c -where - I2C: Deref, -{ - type Error = Error; - fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> { + fn write_read_impl(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> { // Set up current slave address for writing and disable autoending self.i2c.cr2.modify(|_, w| { w.sadd() @@ -386,15 +379,8 @@ where Ok(()) } -} -impl Read for I2c -where - I2C: Deref, -{ - type Error = Error; - - fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> { + fn read_impl(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> { // Set up current address for reading self.i2c.cr2.modify(|_, w| { w.sadd() @@ -421,15 +407,8 @@ where Ok(()) } -} - -impl Write for I2c -where - I2C: Deref, -{ - type Error = Error; - fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { + fn write_impl(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { // Set up current slave address for writing and enable autoending self.i2c.cr2.modify(|_, w| { w.sadd() @@ -456,3 +435,93 @@ where Ok(()) } } + +impl WriteRead for I2c +where + I2C: Deref, +{ + type Error = Error; + + fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Error> { + self.write_read_impl(addr, bytes, buffer) + } +} + +impl Read for I2c +where + I2C: Deref, +{ + type Error = Error; + + fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Error> { + self.read_impl(addr, buffer) + } +} + +impl Write for I2c +where + I2C: Deref, +{ + type Error = Error; + + fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Error> { + self.write_impl(addr, bytes) + } +} + +impl embedded_hal_1::i2c::I2c for I2c +where + I2C: Deref, +{ + fn read( + &mut self, + address: embedded_hal_1::i2c::SevenBitAddress, + read: &mut [u8], + ) -> Result<(), Self::Error> { + self.read_impl(address, read) + } + + fn write( + &mut self, + address: embedded_hal_1::i2c::SevenBitAddress, + write: &[u8], + ) -> Result<(), Self::Error> { + self.write_impl(address, write) + } + + fn write_read( + &mut self, + address: embedded_hal_1::i2c::SevenBitAddress, + write: &[u8], + read: &mut [u8], + ) -> Result<(), Self::Error> { + self.write_read_impl(address, write, read) + } + + fn transaction( + &mut self, + _address: embedded_hal_1::i2c::SevenBitAddress, + _operations: &mut [embedded_hal_1::i2c::Operation<'_>], + ) -> Result<(), Self::Error> { + todo!() + } +} + +impl embedded_hal_1::i2c::ErrorType for I2c +where + I2C: Deref, +{ + type Error = Error; +} + +impl embedded_hal_1::i2c::Error for Error { + fn kind(&self) -> embedded_hal_1::i2c::ErrorKind { + match self { + Error::OVERRUN => embedded_hal_1::i2c::ErrorKind::Overrun, + Error::NACK => embedded_hal_1::i2c::ErrorKind::NoAcknowledge( + embedded_hal_1::i2c::NoAcknowledgeSource::Unknown, + ), + Error::BUS => embedded_hal_1::i2c::ErrorKind::Bus, + } + } +}