use crate::spi::{vdm_header, AccessMode};
use core::convert::Infallible;
use eh0::digital::v2::OutputPin;
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct W5500<SPI, CS> {
spi: SPI,
cs: CS,
}
impl<SPI, CS> W5500<SPI, CS>
where
SPI: eh0::blocking::spi::Transfer<u8, Error = Infallible>
+ eh0::blocking::spi::Write<u8, Error = Infallible>,
CS: OutputPin<Error = Infallible>,
{
#[inline]
#[allow(clippy::unnecessary_safety_doc)]
pub fn new(spi: SPI, cs: CS) -> Self {
W5500 { spi, cs }
}
#[inline]
pub fn free(self) -> (SPI, CS) {
(self.spi, self.cs)
}
}
impl<SPI, CS> crate::Registers for W5500<SPI, CS>
where
SPI: eh0::blocking::spi::Transfer<u8, Error = Infallible>
+ eh0::blocking::spi::Write<u8, Error = Infallible>,
CS: OutputPin<Error = Infallible>,
{
type Error = Infallible;
#[inline]
fn read(&mut self, address: u16, block: u8, data: &mut [u8]) -> Result<(), Self::Error> {
let header = vdm_header(address, block, AccessMode::Read);
self.cs.set_low().unwrap();
self.spi.write(&header).unwrap();
self.spi.transfer(data).unwrap();
self.cs.set_high().unwrap();
self.cs.set_high()
}
#[inline]
fn write(&mut self, address: u16, block: u8, data: &[u8]) -> Result<(), Self::Error> {
let header = vdm_header(address, block, AccessMode::Write);
self.cs.set_low().unwrap();
self.spi.write(&header).unwrap();
self.spi.write(data).unwrap();
self.cs.set_high().unwrap();
self.cs.set_high()
}
}