1use crate::spi::{self, AccessMode};
15
16#[derive(Debug)]
21#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22pub struct W5500<SPI> {
23 spi: SPI,
25}
26
27impl<SPI, SpiError> W5500<SPI>
28where
29 SPI: eh0::blocking::spi::Transfer<u8, Error = SpiError>
30 + eh0::blocking::spi::Write<u8, Error = SpiError>,
31{
32 #[inline]
45 pub fn new(spi: SPI) -> Self {
46 W5500 { spi }
47 }
48
49 #[inline]
63 pub fn free(self) -> SPI {
64 self.spi
65 }
66}
67
68impl<SPI, SpiError> crate::Registers for W5500<SPI>
69where
70 SPI: eh0::blocking::spi::Transfer<u8, Error = SpiError>
71 + eh0::blocking::spi::Write<u8, Error = SpiError>,
72{
73 type Error = SpiError;
75
76 #[allow(clippy::while_let_on_iterator)]
78 fn read(&mut self, mut address: u16, block: u8, data: &mut [u8]) -> Result<(), Self::Error> {
79 let mut chunks = data.chunks_exact_mut(4);
80 while let Some(chunk) = chunks.next() {
81 let header = spi::fdm_header_4b(address, block, AccessMode::Read);
82 self.spi.write(&header)?;
83 self.spi.transfer(chunk)?;
84 address = address.wrapping_add(4);
85 }
86 let mut chunks = chunks.into_remainder().chunks_exact_mut(2);
87 while let Some(chunk) = chunks.next() {
88 let header = spi::fdm_header_2b(address, block, AccessMode::Read);
89 self.spi.write(&header)?;
90 self.spi.transfer(chunk)?;
91 address = address.wrapping_add(2);
92 }
93 let mut chunks = chunks.into_remainder().chunks_exact_mut(1);
94 while let Some(chunk) = chunks.next() {
95 let header = spi::fdm_header_1b(address, block, AccessMode::Read);
96 self.spi.write(&header)?;
97 self.spi.transfer(chunk)?;
98 address = address.wrapping_add(1);
99 }
100
101 Ok(())
102 }
103
104 #[allow(clippy::while_let_on_iterator)]
106 fn write(&mut self, mut address: u16, block: u8, data: &[u8]) -> Result<(), Self::Error> {
107 let mut chunks = data.chunks_exact(4);
108 while let Some(chunk) = chunks.next() {
109 let header = spi::fdm_header_4b(address, block, AccessMode::Write);
110 self.spi.write(&header)?;
111 self.spi.write(chunk)?;
112 address = address.wrapping_add(4);
113 }
114 let mut chunks = chunks.remainder().chunks_exact(2);
115 while let Some(chunk) = chunks.next() {
116 let header = spi::fdm_header_2b(address, block, AccessMode::Write);
117 self.spi.write(&header)?;
118 self.spi.write(chunk)?;
119 address = address.wrapping_add(2);
120 }
121 let mut chunks = chunks.remainder().chunks_exact(1);
122 while let Some(chunk) = chunks.next() {
123 let header = spi::fdm_header_1b(address, block, AccessMode::Write);
124 self.spi.write(&header)?;
125 self.spi.write(chunk)?;
126 address = address.wrapping_add(1);
127 }
128
129 Ok(())
130 }
131}