1use 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 pub spi: Spi,
13}
14
15impl<Spi> DeviceInterfaceAsync<Spi> {
16 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}