From 5528c336495ffc5868d91af2867f6f8e22be8283 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Fri, 24 Nov 2023 18:44:55 +0100 Subject: [PATCH] embassy-embedded-hal: don't use feature(try_blocks). --- embassy-embedded-hal/src/lib.rs | 2 +- .../src/shared_bus/asynch/spi.rs | 52 +++++++++++++------ 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/embassy-embedded-hal/src/lib.rs b/embassy-embedded-hal/src/lib.rs index f836d9f7..ce5fac3f 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 5d3cf658..17d5f367 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.