diff options
Diffstat (limited to 'embassy-net-wiznet/src/device.rs')
| -rw-r--r-- | embassy-net-wiznet/src/device.rs | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/embassy-net-wiznet/src/device.rs b/embassy-net-wiznet/src/device.rs new file mode 100644 index 000000000..43f9512a3 --- /dev/null +++ b/embassy-net-wiznet/src/device.rs | |||
| @@ -0,0 +1,195 @@ | |||
| 1 | use core::marker::PhantomData; | ||
| 2 | |||
| 3 | use embedded_hal_async::spi::SpiDevice; | ||
| 4 | |||
| 5 | use crate::chip::Chip; | ||
| 6 | |||
| 7 | #[repr(u8)] | ||
| 8 | enum Command { | ||
| 9 | Open = 0x01, | ||
| 10 | Send = 0x20, | ||
| 11 | Receive = 0x40, | ||
| 12 | } | ||
| 13 | |||
| 14 | #[repr(u8)] | ||
| 15 | enum Interrupt { | ||
| 16 | Receive = 0b00100_u8, | ||
| 17 | } | ||
| 18 | |||
| 19 | /// Wiznet chip in MACRAW mode | ||
| 20 | #[derive(Debug)] | ||
| 21 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 22 | pub(crate) struct WiznetDevice<C, SPI> { | ||
| 23 | spi: SPI, | ||
| 24 | _phantom: PhantomData<C>, | ||
| 25 | } | ||
| 26 | |||
| 27 | impl<C: Chip, SPI: SpiDevice> WiznetDevice<C, SPI> { | ||
| 28 | /// Create and initialize the driver | ||
| 29 | pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<Self, SPI::Error> { | ||
| 30 | let mut this = Self { | ||
| 31 | spi, | ||
| 32 | _phantom: PhantomData, | ||
| 33 | }; | ||
| 34 | |||
| 35 | // Reset device | ||
| 36 | this.bus_write(C::COMMON_MODE, &[0x80]).await?; | ||
| 37 | |||
| 38 | // Enable interrupt pin | ||
| 39 | this.bus_write(C::COMMON_SOCKET_INTR, &[0x01]).await?; | ||
| 40 | // Enable receive interrupt | ||
| 41 | this.bus_write(C::SOCKET_INTR_MASK, &[Interrupt::Receive as u8]).await?; | ||
| 42 | |||
| 43 | // Set MAC address | ||
| 44 | this.bus_write(C::COMMON_MAC, &mac_addr).await?; | ||
| 45 | |||
| 46 | // Set the raw socket RX/TX buffer sizes. | ||
| 47 | let buf_kbs = (C::BUF_SIZE / 1024) as u8; | ||
| 48 | this.bus_write(C::SOCKET_TXBUF_SIZE, &[buf_kbs]).await?; | ||
| 49 | this.bus_write(C::SOCKET_RXBUF_SIZE, &[buf_kbs]).await?; | ||
| 50 | |||
| 51 | // MACRAW mode with MAC filtering. | ||
| 52 | this.bus_write(C::SOCKET_MODE, &[C::SOCKET_MODE_VALUE]).await?; | ||
| 53 | this.command(Command::Open).await?; | ||
| 54 | |||
| 55 | Ok(this) | ||
| 56 | } | ||
| 57 | |||
| 58 | async fn bus_read(&mut self, address: C::Address, data: &mut [u8]) -> Result<(), SPI::Error> { | ||
| 59 | C::bus_read(&mut self.spi, address, data).await | ||
| 60 | } | ||
| 61 | |||
| 62 | async fn bus_write(&mut self, address: C::Address, data: &[u8]) -> Result<(), SPI::Error> { | ||
| 63 | C::bus_write(&mut self.spi, address, data).await | ||
| 64 | } | ||
| 65 | |||
| 66 | async fn reset_interrupt(&mut self, code: Interrupt) -> Result<(), SPI::Error> { | ||
| 67 | let data = [code as u8]; | ||
| 68 | self.bus_write(C::SOCKET_INTR, &data).await | ||
| 69 | } | ||
| 70 | |||
| 71 | async fn get_tx_write_ptr(&mut self) -> Result<u16, SPI::Error> { | ||
| 72 | let mut data = [0u8; 2]; | ||
| 73 | self.bus_read(C::SOCKET_TX_DATA_WRITE_PTR, &mut data).await?; | ||
| 74 | Ok(u16::from_be_bytes(data)) | ||
| 75 | } | ||
| 76 | |||
| 77 | async fn set_tx_write_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { | ||
| 78 | let data = ptr.to_be_bytes(); | ||
| 79 | self.bus_write(C::SOCKET_TX_DATA_WRITE_PTR, &data).await | ||
| 80 | } | ||
| 81 | |||
| 82 | async fn get_rx_read_ptr(&mut self) -> Result<u16, SPI::Error> { | ||
| 83 | let mut data = [0u8; 2]; | ||
| 84 | self.bus_read(C::SOCKET_RX_DATA_READ_PTR, &mut data).await?; | ||
| 85 | Ok(u16::from_be_bytes(data)) | ||
| 86 | } | ||
| 87 | |||
| 88 | async fn set_rx_read_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> { | ||
| 89 | let data = ptr.to_be_bytes(); | ||
| 90 | self.bus_write(C::SOCKET_RX_DATA_READ_PTR, &data).await | ||
| 91 | } | ||
| 92 | |||
| 93 | async fn command(&mut self, command: Command) -> Result<(), SPI::Error> { | ||
| 94 | let data = [command as u8]; | ||
| 95 | self.bus_write(C::SOCKET_COMMAND, &data).await | ||
| 96 | } | ||
| 97 | |||
| 98 | async fn get_rx_size(&mut self) -> Result<u16, SPI::Error> { | ||
| 99 | loop { | ||
| 100 | // Wait until two sequential reads are equal | ||
| 101 | let mut res0 = [0u8; 2]; | ||
| 102 | self.bus_read(C::SOCKET_RECVD_SIZE, &mut res0).await?; | ||
| 103 | let mut res1 = [0u8; 2]; | ||
| 104 | self.bus_read(C::SOCKET_RECVD_SIZE, &mut res1).await?; | ||
| 105 | if res0 == res1 { | ||
| 106 | break Ok(u16::from_be_bytes(res0)); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | async fn get_tx_free_size(&mut self) -> Result<u16, SPI::Error> { | ||
| 112 | let mut data = [0; 2]; | ||
| 113 | self.bus_read(C::SOCKET_TX_FREE_SIZE, &mut data).await?; | ||
| 114 | Ok(u16::from_be_bytes(data)) | ||
| 115 | } | ||
| 116 | |||
| 117 | /// Read bytes from the RX buffer. | ||
| 118 | async fn read_bytes(&mut self, read_ptr: &mut u16, buffer: &mut [u8]) -> Result<(), SPI::Error> { | ||
| 119 | if C::AUTO_WRAP { | ||
| 120 | self.bus_read(C::rx_addr(*read_ptr), buffer).await?; | ||
| 121 | } else { | ||
| 122 | let addr = *read_ptr % C::BUF_SIZE; | ||
| 123 | if addr as usize + buffer.len() <= C::BUF_SIZE as usize { | ||
| 124 | self.bus_read(C::rx_addr(addr), buffer).await?; | ||
| 125 | } else { | ||
| 126 | let n = C::BUF_SIZE - addr; | ||
| 127 | self.bus_read(C::rx_addr(addr), &mut buffer[..n as usize]).await?; | ||
| 128 | self.bus_read(C::rx_addr(0), &mut buffer[n as usize..]).await?; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | *read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16); | ||
| 133 | |||
| 134 | Ok(()) | ||
| 135 | } | ||
| 136 | |||
| 137 | /// Read an ethernet frame from the device. Returns the number of bytes read. | ||
| 138 | pub async fn read_frame(&mut self, frame: &mut [u8]) -> Result<usize, SPI::Error> { | ||
| 139 | let rx_size = self.get_rx_size().await? as usize; | ||
| 140 | if rx_size == 0 { | ||
| 141 | return Ok(0); | ||
| 142 | } | ||
| 143 | |||
| 144 | self.reset_interrupt(Interrupt::Receive).await?; | ||
| 145 | |||
| 146 | let mut read_ptr = self.get_rx_read_ptr().await?; | ||
| 147 | |||
| 148 | // First two bytes gives the size of the received ethernet frame | ||
| 149 | let expected_frame_size: usize = { | ||
| 150 | let mut frame_bytes = [0u8; 2]; | ||
| 151 | self.read_bytes(&mut read_ptr, &mut frame_bytes).await?; | ||
| 152 | u16::from_be_bytes(frame_bytes) as usize - 2 | ||
| 153 | }; | ||
| 154 | |||
| 155 | // Read the ethernet frame | ||
| 156 | self.read_bytes(&mut read_ptr, &mut frame[..expected_frame_size]) | ||
| 157 | .await?; | ||
| 158 | |||
| 159 | // Register RX as completed | ||
| 160 | self.set_rx_read_ptr(read_ptr).await?; | ||
| 161 | self.command(Command::Receive).await?; | ||
| 162 | |||
| 163 | Ok(expected_frame_size) | ||
| 164 | } | ||
| 165 | |||
| 166 | /// Write an ethernet frame to the device. Returns number of bytes written | ||
| 167 | pub async fn write_frame(&mut self, frame: &[u8]) -> Result<usize, SPI::Error> { | ||
| 168 | while self.get_tx_free_size().await? < frame.len() as u16 {} | ||
| 169 | let write_ptr = self.get_tx_write_ptr().await?; | ||
| 170 | |||
| 171 | if C::AUTO_WRAP { | ||
| 172 | self.bus_write(C::tx_addr(write_ptr), frame).await?; | ||
| 173 | } else { | ||
| 174 | let addr = write_ptr % C::BUF_SIZE; | ||
| 175 | if addr as usize + frame.len() <= C::BUF_SIZE as usize { | ||
| 176 | self.bus_write(C::tx_addr(addr), frame).await?; | ||
| 177 | } else { | ||
| 178 | let n = C::BUF_SIZE - addr; | ||
| 179 | self.bus_write(C::tx_addr(addr), &frame[..n as usize]).await?; | ||
| 180 | self.bus_write(C::tx_addr(0), &frame[n as usize..]).await?; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | self.set_tx_write_ptr(write_ptr.wrapping_add(frame.len() as u16)) | ||
| 185 | .await?; | ||
| 186 | self.command(Command::Send).await?; | ||
| 187 | Ok(frame.len()) | ||
| 188 | } | ||
| 189 | |||
| 190 | pub async fn is_link_up(&mut self) -> bool { | ||
| 191 | let mut link = [0]; | ||
| 192 | self.bus_read(C::COMMON_PHY_CFG, &mut link).await.ok(); | ||
| 193 | link[0] & 1 == 1 | ||
| 194 | } | ||
| 195 | } | ||
