From 2119b8e1ca34c4771fae88cf6684713e66d70ba4 Mon Sep 17 00:00:00 2001 From: Lixou <82600264+DasLixou@users.noreply.github.com> Date: Fri, 28 Apr 2023 21:23:32 +0200 Subject: [PATCH 1/2] Add `Transactional` trait to rp's i2c impl --- embassy-rp/src/i2c.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/embassy-rp/src/i2c.rs b/embassy-rp/src/i2c.rs index 40e85c66..d1df671e 100644 --- a/embassy-rp/src/i2c.rs +++ b/embassy-rp/src/i2c.rs @@ -617,6 +617,22 @@ mod eh02 { self.blocking_write_read(address, bytes, buffer) } } + + impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Transactional for I2c<'d, T, M> { + type Error = Error; + + fn exec(&mut self, address: u8, operations: &mut [embedded_hal_02::blocking::i2c::Operation<'_>]) -> Result<(), Self::Error> { + Self::setup(address.into())?; + for i in 0..operations.len() { + let last = i == operations.len() - 1; + match &mut operations[i] { + embedded_hal_02::blocking::i2c::Operation::Read(buf) => self.read_blocking_internal(buf, false, last)?, + embedded_hal_02::blocking::i2c::Operation::Write(buf) => self.write_blocking_internal(buf, last)?, + } + } + Ok(()) + } + } } #[cfg(feature = "unstable-traits")] From ba886b45b8a78d4fd3f2442607d515fc024eff1b Mon Sep 17 00:00:00 2001 From: xoviat <49173759+xoviat@users.noreply.github.com> Date: Fri, 28 Apr 2023 16:46:32 -0500 Subject: [PATCH 2/2] rustfmt --- embassy-rp/src/i2c.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/embassy-rp/src/i2c.rs b/embassy-rp/src/i2c.rs index d1df671e..cd536439 100644 --- a/embassy-rp/src/i2c.rs +++ b/embassy-rp/src/i2c.rs @@ -617,16 +617,22 @@ mod eh02 { self.blocking_write_read(address, bytes, buffer) } } - + impl<'d, T: Instance, M: Mode> embedded_hal_02::blocking::i2c::Transactional for I2c<'d, T, M> { type Error = Error; - - fn exec(&mut self, address: u8, operations: &mut [embedded_hal_02::blocking::i2c::Operation<'_>]) -> Result<(), Self::Error> { + + fn exec( + &mut self, + address: u8, + operations: &mut [embedded_hal_02::blocking::i2c::Operation<'_>], + ) -> Result<(), Self::Error> { Self::setup(address.into())?; for i in 0..operations.len() { let last = i == operations.len() - 1; match &mut operations[i] { - embedded_hal_02::blocking::i2c::Operation::Read(buf) => self.read_blocking_internal(buf, false, last)?, + embedded_hal_02::blocking::i2c::Operation::Read(buf) => { + self.read_blocking_internal(buf, false, last)? + } embedded_hal_02::blocking::i2c::Operation::Write(buf) => self.write_blocking_internal(buf, last)?, } }