Skip to main content

ic_md/dd/
dd_async.rs

1//! The async interface.
2
3use embedded_hal::spi::Operation;
4use embedded_hal_async::spi::SpiDevice;
5
6use crate::dd::DeviceError;
7
8#[derive(Debug)]
9
10pub struct DeviceInterfaceAsync<Spi> {
11    /// The SPI device used to communicate with the iC-MD device.
12    pub spi: Spi,
13}
14
15impl<Spi> DeviceInterfaceAsync<Spi> {
16    /// Construct a new instance of the device.
17    ///
18    /// Spi mode 0, max 10 MHz according to the datasheet.
19    pub const fn new(spi: Spi) -> Self {
20        Self { spi }
21    }
22}
23
24impl<Spi: SpiDevice> device_driver::RegisterInterfaceBase for DeviceInterfaceAsync<Spi> {
25    type Error = DeviceError<Spi::Error>;
26    type AddressType = u8;
27}
28
29impl<Spi: SpiDevice> device_driver::AsyncRegisterInterface for DeviceInterfaceAsync<Spi> {
30    async fn write_register(
31        &mut self,
32        address: Self::AddressType,
33        data: &mut [u8],
34        _metadata: &device_driver::FieldsetMetadata,
35    ) -> Result<(), Self::Error> {
36        Ok(SpiDevice::transaction(
37            &mut self.spi,
38            &mut [Operation::Write(&[address]), Operation::Write(data)],
39        )
40        .await?)
41    }
42
43    async fn read_register(
44        &mut self,
45        address: Self::AddressType,
46        data: &mut [u8],
47        _metadata: &device_driver::FieldsetMetadata,
48    ) -> Result<(), Self::Error> {
49        SpiDevice::transaction(
50            &mut self.spi,
51            &mut [Operation::Write(&[0x80 | address]), Operation::Read(data)],
52        )
53        .await?;
54
55        Ok(())
56    }
57}