use crate::spi::{vdm_header, AccessMode};
use eh1::spi::ErrorType;
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct W5500<SPI> {
spi: SPI,
}
impl<SPI, E> W5500<SPI>
where
SPI: eh1::spi::SpiDevice<Error = E>,
{
#[inline]
pub fn new(spi: SPI) -> Self {
W5500 { spi }
}
#[inline]
pub fn free(self) -> SPI {
self.spi
}
}
impl<SPI> crate::Registers for W5500<SPI>
where
SPI: eh1::spi::SpiDevice,
{
type Error = SPI::Error;
#[inline]
fn read(
&mut self,
address: u16,
block: u8,
data: &mut [u8],
) -> Result<(), <SPI as ErrorType>::Error> {
let header = vdm_header(address, block, AccessMode::Read);
let mut ops = [
eh1::spi::Operation::Write(&header),
eh1::spi::Operation::Read(data),
];
self.spi.transaction(&mut ops)
}
#[inline]
fn write(
&mut self,
address: u16,
block: u8,
data: &[u8],
) -> Result<(), <SPI as ErrorType>::Error> {
let header = vdm_header(address, block, AccessMode::Write);
let mut ops = [
eh1::spi::Operation::Write(&header),
eh1::spi::Operation::Write(data),
];
self.spi.transaction(&mut ops)
}
}
#[cfg(feature = "eha1")]
impl<SPI> crate::aio::Registers for W5500<SPI>
where
SPI: eha1::spi::SpiDevice,
{
type Error = SPI::Error;
async fn read(
&mut self,
address: u16,
block: u8,
data: &mut [u8],
) -> Result<(), <SPI as ErrorType>::Error> {
let header = vdm_header(address, block, AccessMode::Read);
let mut ops = [
eh1::spi::Operation::Write(&header),
eh1::spi::Operation::Read(data),
];
self.spi.transaction(&mut ops).await
}
async fn write(
&mut self,
address: u16,
block: u8,
data: &[u8],
) -> Result<(), <SPI as ErrorType>::Error> {
let header = vdm_header(address, block, AccessMode::Write);
let mut ops = [
eh1::spi::Operation::Write(&header),
eh1::spi::Operation::Write(data),
];
self.spi.transaction(&mut ops).await
}
}