embassy-embedded-hal: don't use feature(try_blocks).

This commit is contained in:
Dario Nieuwenhuis 2023-11-24 18:44:55 +01:00
parent e8ff5a2baf
commit 5528c33649
2 changed files with 37 additions and 17 deletions

View File

@ -1,5 +1,5 @@
#![cfg_attr(not(feature = "std"), no_std)] #![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))] #![cfg_attr(feature = "nightly", allow(stable_features, unknown_lints, async_fn_in_trait))]
#![warn(missing_docs)] #![warn(missing_docs)]

View File

@ -66,19 +66,29 @@ where
let mut bus = self.bus.lock().await; let mut bus = self.bus.lock().await;
self.cs.set_low().map_err(SpiDeviceError::Cs)?; self.cs.set_low().map_err(SpiDeviceError::Cs)?;
let op_res: Result<(), BUS::Error> = try { let op_res = 'ops: {
for op in operations { for op in operations {
match op { let res = match op {
Operation::Read(buf) => bus.read(buf).await?, Operation::Read(buf) => bus.read(buf).await,
Operation::Write(buf) => bus.write(buf).await?, Operation::Write(buf) => bus.write(buf).await,
Operation::Transfer(read, write) => bus.transfer(read, write).await?, Operation::Transfer(read, write) => bus.transfer(read, write).await,
Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await?, Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await,
#[cfg(not(feature = "time"))] #[cfg(not(feature = "time"))]
Operation::DelayUs(_) => return Err(SpiDeviceError::DelayUsNotSupported), Operation::DelayUs(us) => return Err(SpiDeviceError::DelayUsNotSupported),
#[cfg(feature = "time")] #[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. // 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)?; bus.set_config(&self.config).map_err(|_| SpiDeviceError::Config)?;
self.cs.set_low().map_err(SpiDeviceError::Cs)?; self.cs.set_low().map_err(SpiDeviceError::Cs)?;
let op_res: Result<(), BUS::Error> = try { let op_res = 'ops: {
for op in operations { for op in operations {
match op { let res = match op {
Operation::Read(buf) => bus.read(buf).await?, Operation::Read(buf) => bus.read(buf).await,
Operation::Write(buf) => bus.write(buf).await?, Operation::Write(buf) => bus.write(buf).await,
Operation::Transfer(read, write) => bus.transfer(read, write).await?, Operation::Transfer(read, write) => bus.transfer(read, write).await,
Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await?, Operation::TransferInPlace(buf) => bus.transfer_in_place(buf).await,
#[cfg(not(feature = "time"))] #[cfg(not(feature = "time"))]
Operation::DelayUs(_) => return Err(SpiDeviceError::DelayUsNotSupported), Operation::DelayUs(us) => return Err(SpiDeviceError::DelayUsNotSupported),
#[cfg(feature = "time")] #[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. // On failure, it's important to still flush and deassert CS.