macro_rules! spi_transaction {
    ($device:expr, move |$bus:ident| async move $closure_body:expr) => { ... };
    ($device:expr, move |$bus:ident| async $closure_body:expr) => { ... };
    ($device:expr, |$bus:ident| async move $closure_body:expr) => { ... };
    ($device:expr, |$bus:ident| async $closure_body:expr) => { ... };
}
Expand description

Do an SPI transaction on a bus. This is a safe wrapper for SpiDevice::transaction, which handles dereferencing the raw pointer for you.

Examples

use embedded_hal_async::spi::{transaction, SpiBus, SpiBusRead, SpiBusWrite, SpiDevice};

pub async fn transaction_example<SPI>(mut device: SPI) -> Result<u32, SPI::Error>
where
    SPI: SpiDevice,
    SPI::Bus: SpiBus,
{
    transaction!(&mut device, move |bus| async move {
        // Unlike `SpiDevice::transaction`, we don't need to
        // manually dereference a pointer in order to use the bus.
        bus.write(&[42]).await?;
        let mut data = [0; 4];
        bus.read(&mut data).await?;
        Ok(u32::from_be_bytes(data))
    })
    .await
}

Note that the compiler will prevent you from moving the bus reference outside of the closure

    let mut bus_smuggler: Option<&mut SPI::Bus> = None;
    transaction!(&mut device, move |bus| async move {
        bus_smuggler = Some(bus);
        Ok(())
    })
    .await