1use crate::spi::{vdm_header, AccessMode};
13use eh1::spi::ErrorType;
14
15#[derive(Debug)]
17#[cfg_attr(feature = "defmt", derive(defmt::Format))]
18pub struct W5500<SPI> {
19 spi: SPI,
21}
22
23impl<SPI, E> W5500<SPI>
24where
25 SPI: eh1::spi::SpiDevice<Error = E>,
26{
27 #[inline]
40 pub fn new(spi: SPI) -> Self {
41 W5500 { spi }
42 }
43
44 #[inline]
58 pub fn free(self) -> SPI {
59 self.spi
60 }
61}
62
63impl<SPI> crate::Registers for W5500<SPI>
64where
65 SPI: eh1::spi::SpiDevice,
66{
67 type Error = SPI::Error;
69
70 #[inline]
72 fn read(
73 &mut self,
74 address: u16,
75 block: u8,
76 data: &mut [u8],
77 ) -> Result<(), <SPI as ErrorType>::Error> {
78 let header = vdm_header(address, block, AccessMode::Read);
79 let mut ops = [
80 eh1::spi::Operation::Write(&header),
81 eh1::spi::Operation::Read(data),
82 ];
83 self.spi.transaction(&mut ops)
84 }
85
86 #[inline]
88 fn write(
89 &mut self,
90 address: u16,
91 block: u8,
92 data: &[u8],
93 ) -> Result<(), <SPI as ErrorType>::Error> {
94 let header = vdm_header(address, block, AccessMode::Write);
95 let mut ops = [
96 eh1::spi::Operation::Write(&header),
97 eh1::spi::Operation::Write(data),
98 ];
99 self.spi.transaction(&mut ops)
100 }
101}
102
103#[cfg(feature = "eha1")]
104impl<SPI> crate::aio::Registers for W5500<SPI>
105where
106 SPI: eha1::spi::SpiDevice,
107{
108 type Error = SPI::Error;
110
111 async fn read(
113 &mut self,
114 address: u16,
115 block: u8,
116 data: &mut [u8],
117 ) -> Result<(), <SPI as ErrorType>::Error> {
118 let header = vdm_header(address, block, AccessMode::Read);
119 let mut ops = [
120 eh1::spi::Operation::Write(&header),
121 eh1::spi::Operation::Read(data),
122 ];
123 self.spi.transaction(&mut ops).await
124 }
125
126 async fn write(
128 &mut self,
129 address: u16,
130 block: u8,
131 data: &[u8],
132 ) -> Result<(), <SPI as ErrorType>::Error> {
133 let header = vdm_header(address, block, AccessMode::Write);
134 let mut ops = [
135 eh1::spi::Operation::Write(&header),
136 eh1::spi::Operation::Write(data),
137 ];
138 self.spi.transaction(&mut ops).await
139 }
140}