diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index f836d9f76..ce5fac3f0 100644 --- a/embassy-embedded-hal/src/lib.rs +++ b/embassy-embedded-hal/src/lib.rs @@ -1,5 +1,5 @@ #![cfg_attr(not(feature = "std"), no_std)] -#![cfg_attr(feature = "nightly", feature(async_fn_in_trait, impl_trait_projections, try_blocks))] +#![cfg_attr(feature = "nightly", feature(async_fn_in_trait, impl_trait_projections))] #![cfg_attr(feature = "nightly", allow(stable_features, unknown_lints, async_fn_in_trait))] #![warn(missing_docs)] diff --git a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs index 5d3cf658a..17d5f3676 100644 --- a/embassy-embedded-hal/src/shared_bus/asynch/spi.rs +++ b/embassy-embedded-hal/src/shared_bus/asynch/spi.rs @@ -66,19 +66,29 @@ where let mut bus = self.bus.lock().await; self.cs.set_low().map_err(SpiDeviceError::Cs)?; - let op_res: Result<(), BUS::Error> = try { + let op_res = 'ops: { for op in operations { - match op { - Operation::Read(buf) => bus.read(buf).await?, - Operation::Write(buf) => bus.write(buf).await?, - Operation::Transfer(read, write) => bus.transfer(read, write).await?, - Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await?, + let res = match op { + Operation::Read(buf) => bus.read(buf).await, + Operation::Write(buf) => bus.write(buf).await, + Operation::Transfer(read, write) => bus.transfer(read, write).await, + Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await, #[cfg(not(feature = "time"))] - Operation::DelayUs(_) => return Err(SpiDeviceError::DelayUsNotSupported), + Operation::DelayUs(us) => return Err(SpiDeviceError::DelayUsNotSupported), #[cfg(feature = "time")] - Operation::DelayUs(us) => embassy_time::Timer::after_micros(*us as _).await, + Operation::DelayUs(us) => match bus.flush().await { + Err(e) => Err(e), + Ok(()) => { + embassy_time::Timer::after_micros(*us as _).await; + Ok(()) + } + }, + }; + if let Err(e) = res { + break 'ops Err(e); } } + Ok(()) }; // On failure, it's important to still flush and deassert CS. @@ -131,19 +141,29 @@ where bus.set_config(&self.config).map_err(|_| SpiDeviceError::Config)?; self.cs.set_low().map_err(SpiDeviceError::Cs)?; - let op_res: Result<(), BUS::Error> = try { + let op_res = 'ops: { for op in operations { - match op { - Operation::Read(buf) => bus.read(buf).await?, - Operation::Write(buf) => bus.write(buf).await?, - Operation::Transfer(read, write) => bus.transfer(read, write).await?, - Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await?, + let res = match op { + Operation::Read(buf) => bus.read(buf).await, + Operation::Write(buf) => bus.write(buf).await, + Operation::Transfer(read, write) => bus.transfer(read, write).await, + Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await, #[cfg(not(feature = "time"))] - Operation::DelayUs(_) => return Err(SpiDeviceError::DelayUsNotSupported), + Operation::DelayUs(us) => return Err(SpiDeviceError::DelayUsNotSupported), #[cfg(feature = "time")] - Operation::DelayUs(us) => embassy_time::Timer::after_micros(*us as _).await, + Operation::DelayUs(us) => match bus.flush().await { + Err(e) => Err(e), + Ok(()) => { + embassy_time::Timer::after_micros(*us as _).await; + Ok(()) + } + }, + }; + if let Err(e) = res { + break 'ops Err(e); } } + Ok(()) }; // On failure, it's important to still flush and deassert CS.