Skip to content

Commit

Permalink
Fixing unrelated async feature issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Apr 22, 2024
1 parent 6c0cbad commit cf6db34
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
26 changes: 19 additions & 7 deletions embedded-hal-bus/src/spi/exclusive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use embedded_hal::spi::{ErrorType, Operation, SpiBus, SpiDevice};
#[cfg(feature = "async")]
use embedded_hal_async::{
delay::DelayNs as AsyncDelayNs,
spi::{SpiBus as AsyncSpiBus, SpiDevice as AsyncSpiDevice},
spi::{
ErrorType as AsyncErrorType, Operation as AsyncOperation, SpiBus as AsyncSpiBus,
SpiDevice as AsyncSpiDevice,
},
};

use super::shared::transaction;
Expand Down Expand Up @@ -89,6 +92,15 @@ where
}
}

#[cfg(feature = "async")]
impl<BUS, CS, D> AsyncErrorType for ExclusiveDevice<BUS, CS, D>
where
BUS: AsyncErrorType,
CS: OutputPin,
{
type Error = DeviceError<BUS::Error, CS::Error>;
}

#[cfg(feature = "async")]
#[cfg_attr(docsrs, doc(cfg(feature = "async")))]
impl<Word: Copy + 'static, BUS, CS, D> AsyncSpiDevice<Word> for ExclusiveDevice<BUS, CS, D>
Expand All @@ -100,18 +112,18 @@ where
#[inline]
async fn transaction(
&mut self,
operations: &mut [Operation<'_, Word>],
operations: &mut [AsyncOperation<'_, Word>],
) -> Result<(), Self::Error> {
self.cs.set_low().map_err(DeviceError::Cs)?;

let op_res = 'ops: {
for op in operations {
let res = match op {
Operation::Read(buf) => self.bus.read(buf).await,
Operation::Write(buf) => self.bus.write(buf).await,
Operation::Transfer(read, write) => self.bus.transfer(read, write).await,
Operation::TransferInPlace(buf) => self.bus.transfer_in_place(buf).await,
Operation::DelayNs(ns) => match self.bus.flush().await {
AsyncOperation::Read(buf) => self.bus.read(buf).await,
AsyncOperation::Write(buf) => self.bus.write(buf).await,
AsyncOperation::Transfer(read, write) => self.bus.transfer(read, write).await,
AsyncOperation::TransferInPlace(buf) => self.bus.transfer_in_place(buf).await,
AsyncOperation::DelayNs(ns) => match self.bus.flush().await {
Err(e) => Err(e),
Ok(()) => {
self.delay.delay_ns(*ns).await;
Expand Down
18 changes: 18 additions & 0 deletions embedded-hal-bus/src/spi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
use core::fmt::{self, Debug, Display, Formatter};
use embedded_hal::spi::{Error, ErrorKind};

#[cfg(feature = "async")]
use embedded_hal_async::spi::{Error as AsyncError, ErrorKind as AsyncErrorKind};

mod exclusive;
pub use exclusive::*;
mod refcell;
Expand Down Expand Up @@ -57,6 +60,21 @@ where
}
}

#[cfg(feature = "async")]
impl<BUS, CS> AsyncError for DeviceError<BUS, CS>
where
BUS: AsyncError + Debug,
CS: Debug,
{
#[inline]
fn kind(&self) -> AsyncErrorKind {
match self {
Self::Spi(e) => e.kind(),
Self::Cs(_) => AsyncErrorKind::ChipSelectFault,
}
}
}

/// Dummy [`DelayNs`](embedded_hal::delay::DelayNs) implementation that panics on use.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
Expand Down

0 comments on commit cf6db34

Please sign in to comment.