diff --git a/esp-hal-common/Cargo.toml b/esp-hal-common/Cargo.toml index 1387cde513e..6f0e9549695 100644 --- a/esp-hal-common/Cargo.toml +++ b/esp-hal-common/Cargo.toml @@ -18,8 +18,8 @@ critical-section = "1.1.1" embedded-can = { version = "0.4.1", optional = true } embedded-dma = "0.2.0" embedded-hal = { version = "0.2.7", features = ["unproven"] } -embedded-hal-1 = { version = "=1.0.0-alpha.9", optional = true, package = "embedded-hal" } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal-1 = { version = "=1.0.0-alpha.10", optional = true, package = "embedded-hal" } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } esp-synopsys-usb-otg = { version = "0.3.1", optional = true, features = ["fs", "esp32sx"] } fugit = "0.3.6" lock_api = { version = "0.4.9", optional = true } diff --git a/esp-hal-common/src/delay.rs b/esp-hal-common/src/delay.rs index b8fea440571..c5f0bd287d7 100644 --- a/esp-hal-common/src/delay.rs +++ b/esp-hal-common/src/delay.rs @@ -28,12 +28,8 @@ where #[cfg(feature = "eh1")] impl embedded_hal_1::delay::DelayUs for Delay { - type Error = core::convert::Infallible; - - fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> { + fn delay_us(&mut self, us: u32) { self.delay(us); - - Ok(()) } } diff --git a/esp-hal-common/src/i2c.rs b/esp-hal-common/src/i2c.rs index 76b55271eb2..283542453da 100644 --- a/esp-hal-common/src/i2c.rs +++ b/esp-hal-common/src/i2c.rs @@ -212,13 +212,6 @@ where self.peripheral.master_write(address, bytes) } - fn write_iter(&mut self, _address: u8, _bytes: B) -> Result<(), Self::Error> - where - B: IntoIterator, - { - todo!() - } - fn write_read( &mut self, address: u8, @@ -228,18 +221,6 @@ where self.peripheral.master_write_read(address, bytes, buffer) } - fn write_iter_read( - &mut self, - _address: u8, - _bytes: B, - _buffer: &mut [u8], - ) -> Result<(), Self::Error> - where - B: IntoIterator, - { - todo!() - } - fn transaction<'a>( &mut self, _address: u8, @@ -247,13 +228,6 @@ where ) -> Result<(), Self::Error> { todo!() } - - fn transaction_iter<'a, O>(&mut self, _address: u8, _operations: O) -> Result<(), Self::Error> - where - O: IntoIterator>, - { - todo!() - } } impl<'d, T> I2C<'d, T> diff --git a/esp-hal-common/src/spi.rs b/esp-hal-common/src/spi.rs index a6c01ca7523..c894c62f2db 100644 --- a/esp-hal-common/src/spi.rs +++ b/esp-hal-common/src/spi.rs @@ -1488,11 +1488,14 @@ mod ehal1 { use embedded_hal_1::spi::{ self, ErrorType, + Operation, SpiBus, SpiBusFlush, SpiBusRead, SpiBusWrite, SpiDevice, + SpiDeviceRead, + SpiDeviceWrite, }; use embedded_hal_nb::spi::FullDuplex; @@ -1688,34 +1691,86 @@ mod ehal1 { type Error = spi::ErrorKind; } - impl<'a, 'd, I, CS, M> SpiDevice for SpiBusDevice<'a, 'd, I, CS, M> + impl<'a, 'd, I, CS, M> SpiDeviceRead for SpiBusDevice<'a, 'd, I, CS, M> where I: Instance, - CS: OutputPin + crate::gpio::OutputPin, + CS: OutputPin, M: IsFullDuplex, { - type Bus = Spi<'d, I, M>; - - fn transaction( - &mut self, - f: impl FnOnce(&mut Self::Bus) -> Result::Error>, - ) -> Result { + fn read_transaction(&mut self, operations: &mut [&mut [u8]]) -> Result<(), Self::Error> { critical_section::with(|cs| { let mut bus = self.bus.lock.borrow_ref_mut(cs); + self.cs.connect_peripheral_to_output(bus.spi.cs_signal()); + + let op_res = operations + .iter_mut() + .try_for_each(|buf| SpiBusRead::read(&mut (*bus), buf)); + + // On failure, it's important to still flush and de-assert CS. + let flush_res = bus.flush(); + self.cs.disconnect_peripheral_from_output(); + + op_res.map_err(|_| spi::ErrorKind::Other)?; + flush_res.map_err(|_| spi::ErrorKind::Other)?; + + Ok(()) + }) + } + } + impl<'a, 'd, I, CS, M> SpiDeviceWrite for SpiBusDevice<'a, 'd, I, CS, M> + where + I: Instance, + CS: OutputPin, + M: IsFullDuplex, + { + fn write_transaction(&mut self, operations: &[&[u8]]) -> Result<(), Self::Error> { + critical_section::with(|cs| { + let mut bus = self.bus.lock.borrow_ref_mut(cs); self.cs.connect_peripheral_to_output(bus.spi.cs_signal()); - // We postpone handling these errors until AFTER we raised CS again, so the bus - // is free (Or we die trying if CS errors). - let f_res = f(&mut bus); + let op_res = operations + .iter() + .try_for_each(|buf| SpiBusWrite::write(&mut (*bus), buf)); + + // On failure, it's important to still flush and de-assert CS. let flush_res = bus.flush(); + self.cs.disconnect_peripheral_from_output(); + + op_res.map_err(|_| spi::ErrorKind::Other)?; + flush_res.map_err(|_| spi::ErrorKind::Other)?; + Ok(()) + }) + } + } + + impl<'a, 'd, I, CS, M> SpiDevice for SpiBusDevice<'a, 'd, I, CS, M> + where + I: Instance, + CS: OutputPin, + M: IsFullDuplex, + { + fn transaction(&mut self, operations: &mut [Operation<'_, u8>]) -> Result<(), Self::Error> { + critical_section::with(|cs| { + let mut bus = self.bus.lock.borrow_ref_mut(cs); + self.cs.connect_peripheral_to_output(bus.spi.cs_signal()); + + let op_res = operations.iter_mut().try_for_each(|op| match op { + Operation::Read(buf) => SpiBusRead::read(&mut (*bus), buf), + Operation::Write(buf) => SpiBusWrite::write(&mut (*bus), buf), + Operation::Transfer(read, write) => bus.transfer(read, write), + Operation::TransferInPlace(buf) => bus.transfer_in_place(buf), + }); + + // On failure, it's important to still flush and de-assert CS. + let flush_res = bus.flush(); self.cs.disconnect_peripheral_from_output(); - let f_res = f_res.map_err(|_| spi::ErrorKind::Other)?; + op_res.map_err(|_| spi::ErrorKind::Other)?; flush_res.map_err(|_| spi::ErrorKind::Other)?; - Ok(f_res) + Ok(()) }) } } diff --git a/esp32-hal/Cargo.toml b/esp32-hal/Cargo.toml index 3c717fcf0a3..d6a8cc66587 100644 --- a/esp32-hal/Cargo.toml +++ b/esp32-hal/Cargo.toml @@ -27,9 +27,9 @@ categories = [ [dependencies] embassy-time = { version = "0.1.0", features = ["nightly"], optional = true } embedded-hal = { version = "0.2.7", features = ["unproven"] } -embedded-hal-1 = { version = "=1.0.0-alpha.9", optional = true, package = "embedded-hal" } +embedded-hal-1 = { version = "=1.0.0-alpha.10", optional = true, package = "embedded-hal" } embedded-hal-async = { version = "0.2.0-alpha.0", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } esp-hal-common = { version = "0.8.0", features = ["esp32"], path = "../esp-hal-common" } [dev-dependencies] diff --git a/esp32c2-hal/Cargo.toml b/esp32c2-hal/Cargo.toml index 617f39be43f..74b81d94286 100644 --- a/esp32c2-hal/Cargo.toml +++ b/esp32c2-hal/Cargo.toml @@ -27,9 +27,9 @@ categories = [ [dependencies] embassy-time = { version = "0.1.0", features = ["nightly"], optional = true } embedded-hal = { version = "0.2.7", features = ["unproven"] } -embedded-hal-1 = { version = "=1.0.0-alpha.9", optional = true, package = "embedded-hal" } +embedded-hal-1 = { version = "=1.0.0-alpha.10", optional = true, package = "embedded-hal" } embedded-hal-async = { version = "0.2.0-alpha.0", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } esp-hal-common = { version = "0.8.0", features = ["esp32c2"], path = "../esp-hal-common" } [dev-dependencies] diff --git a/esp32c3-hal/Cargo.toml b/esp32c3-hal/Cargo.toml index 6ef107aca1f..33b9d1f8a7e 100644 --- a/esp32c3-hal/Cargo.toml +++ b/esp32c3-hal/Cargo.toml @@ -28,9 +28,9 @@ categories = [ cfg-if = "1.0.0" embassy-time = { version = "0.1.0", features = ["nightly"], optional = true } embedded-hal = { version = "0.2.7", features = ["unproven"] } -embedded-hal-1 = { version = "=1.0.0-alpha.9", optional = true, package = "embedded-hal" } +embedded-hal-1 = { version = "=1.0.0-alpha.10", optional = true, package = "embedded-hal" } embedded-hal-async = { version = "0.2.0-alpha.0", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } embedded-can = { version = "0.4.1", optional = true } esp-hal-common = { version = "0.8.0", features = ["esp32c3"], path = "../esp-hal-common" } diff --git a/esp32c6-hal/Cargo.toml b/esp32c6-hal/Cargo.toml index 78319d02412..fc661cec04e 100644 --- a/esp32c6-hal/Cargo.toml +++ b/esp32c6-hal/Cargo.toml @@ -29,9 +29,9 @@ categories = [ cfg-if = "1.0.0" embassy-time = { version = "0.1.0", features = ["nightly"], optional = true } embedded-hal = { version = "0.2.7", features = ["unproven"] } -embedded-hal-1 = { version = "=1.0.0-alpha.9", optional = true, package = "embedded-hal" } +embedded-hal-1 = { version = "=1.0.0-alpha.10", optional = true, package = "embedded-hal" } embedded-hal-async = { version = "0.2.0-alpha.0", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } embedded-can = { version = "0.4.1", optional = true } esp-hal-common = { version = "0.8.0", features = ["esp32c6"], path = "../esp-hal-common" } diff --git a/esp32s2-hal/Cargo.toml b/esp32s2-hal/Cargo.toml index d6cabad0503..3507714eafd 100644 --- a/esp32s2-hal/Cargo.toml +++ b/esp32s2-hal/Cargo.toml @@ -27,9 +27,9 @@ categories = [ [dependencies] embassy-time = { version = "0.1.0", features = ["nightly"], optional = true } embedded-hal = { version = "0.2.7", features = ["unproven"] } -embedded-hal-1 = { version = "=1.0.0-alpha.9", optional = true, package = "embedded-hal" } +embedded-hal-1 = { version = "=1.0.0-alpha.10", optional = true, package = "embedded-hal" } embedded-hal-async = { version = "0.2.0-alpha.0", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } esp-hal-common = { version = "0.8.0", features = ["esp32s2"], path = "../esp-hal-common" } xtensa-atomic-emulation-trap = { version = "0.4.0" } diff --git a/esp32s3-hal/Cargo.toml b/esp32s3-hal/Cargo.toml index acacf53dc75..6fa02187f01 100644 --- a/esp32s3-hal/Cargo.toml +++ b/esp32s3-hal/Cargo.toml @@ -28,9 +28,9 @@ categories = [ bare-metal = "1.0.0" embassy-time = { version = "0.1.0", features = ["nightly"], optional = true } embedded-hal = { version = "0.2.7", features = ["unproven"] } -embedded-hal-1 = { version = "=1.0.0-alpha.9", optional = true, package = "embedded-hal" } +embedded-hal-1 = { version = "=1.0.0-alpha.10", optional = true, package = "embedded-hal" } embedded-hal-async = { version = "0.2.0-alpha.0", optional = true } -embedded-hal-nb = { version = "=1.0.0-alpha.1", optional = true } +embedded-hal-nb = { version = "=1.0.0-alpha.2", optional = true } embedded-can = { version = "0.4.1", optional = true } esp-hal-common = { version = "0.8.0", features = ["esp32s3"], path = "../esp-hal-common" } r0 = { version = "1.0.0", optional = true }