2023-08-15 17:50:56 +02:00
|
|
|
use core::marker::PhantomData;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
2023-08-15 17:50:56 +02:00
|
|
|
use embedded_hal_async::spi::SpiDevice;
|
2023-08-15 17:20:41 +02:00
|
|
|
|
2023-08-15 17:50:56 +02:00
|
|
|
use crate::chip::Chip;
|
2023-08-15 17:20:41 +02:00
|
|
|
|
|
|
|
#[repr(u8)]
|
2023-08-15 17:50:56 +02:00
|
|
|
enum Command {
|
2023-08-15 17:20:41 +02:00
|
|
|
Open = 0x01,
|
|
|
|
Send = 0x20,
|
|
|
|
Receive = 0x40,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[repr(u8)]
|
2023-08-15 17:50:56 +02:00
|
|
|
enum Interrupt {
|
2023-08-15 17:20:41 +02:00
|
|
|
Receive = 0b00100_u8,
|
|
|
|
}
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
/// W5500 in MACRAW mode
|
|
|
|
#[derive(Debug)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2023-08-15 17:50:56 +02:00
|
|
|
pub(crate) struct WiznetDevice<C, SPI> {
|
|
|
|
spi: SPI,
|
|
|
|
_phantom: PhantomData<C>,
|
2023-05-09 01:51:08 +02:00
|
|
|
}
|
|
|
|
|
2023-08-15 17:50:56 +02:00
|
|
|
impl<C: Chip, SPI: SpiDevice> WiznetDevice<C, SPI> {
|
2023-05-09 01:51:08 +02:00
|
|
|
/// Create and initialize the W5500 driver
|
2023-08-15 17:50:56 +02:00
|
|
|
pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<Self, SPI::Error> {
|
|
|
|
let mut this = Self {
|
|
|
|
spi,
|
|
|
|
_phantom: PhantomData,
|
|
|
|
};
|
|
|
|
|
2023-05-09 01:51:08 +02:00
|
|
|
// Reset device
|
2023-08-15 17:50:56 +02:00
|
|
|
this.bus_write(C::COMMON_MODE, &[0x80]).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
// Enable interrupt pin
|
2023-08-15 17:50:56 +02:00
|
|
|
this.bus_write(C::COMMON_SOCKET_INTR, &[0x01]).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
// Enable receive interrupt
|
2023-08-15 17:50:56 +02:00
|
|
|
this.bus_write(C::SOCKET_INTR_MASK, &[Interrupt::Receive as u8]).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
// Set MAC address
|
2023-08-15 17:50:56 +02:00
|
|
|
this.bus_write(C::COMMON_MAC, &mac_addr).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
// Set the raw socket RX/TX buffer sizes to 16KB
|
2023-08-15 17:50:56 +02:00
|
|
|
this.bus_write(C::SOCKET_TXBUF_SIZE, &[16]).await?;
|
|
|
|
this.bus_write(C::SOCKET_RXBUF_SIZE, &[16]).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
// MACRAW mode with MAC filtering.
|
|
|
|
let mode: u8 = (1 << 2) | (1 << 7);
|
2023-08-15 17:50:56 +02:00
|
|
|
this.bus_write(C::SOCKET_MODE, &[mode]).await?;
|
2023-08-15 17:20:41 +02:00
|
|
|
this.command(Command::Open).await?;
|
|
|
|
|
|
|
|
Ok(this)
|
|
|
|
}
|
|
|
|
|
2023-08-15 17:50:56 +02:00
|
|
|
async fn bus_read(&mut self, address: C::Address, data: &mut [u8]) -> Result<(), SPI::Error> {
|
|
|
|
C::bus_read(&mut self.spi, address, data).await
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn bus_write(&mut self, address: C::Address, data: &[u8]) -> Result<(), SPI::Error> {
|
|
|
|
C::bus_write(&mut self.spi, address, data).await
|
|
|
|
}
|
|
|
|
|
2023-08-15 17:20:41 +02:00
|
|
|
async fn reset_interrupt(&mut self, code: Interrupt) -> Result<(), SPI::Error> {
|
|
|
|
let data = [code as u8];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_write(C::SOCKET_INTR, &data).await
|
2023-08-15 17:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_tx_write_ptr(&mut self) -> Result<u16, SPI::Error> {
|
|
|
|
let mut data = [0u8; 2];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_read(C::SOCKET_TX_DATA_WRITE_PTR, &mut data).await?;
|
2023-08-15 17:20:41 +02:00
|
|
|
Ok(u16::from_be_bytes(data))
|
|
|
|
}
|
2023-05-09 01:51:08 +02:00
|
|
|
|
2023-08-15 17:20:41 +02:00
|
|
|
async fn set_tx_write_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> {
|
|
|
|
let data = ptr.to_be_bytes();
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_write(C::SOCKET_TX_DATA_WRITE_PTR, &data).await
|
2023-08-15 17:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_rx_read_ptr(&mut self) -> Result<u16, SPI::Error> {
|
|
|
|
let mut data = [0u8; 2];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_read(C::SOCKET_RX_DATA_READ_PTR, &mut data).await?;
|
2023-08-15 17:20:41 +02:00
|
|
|
Ok(u16::from_be_bytes(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn set_rx_read_ptr(&mut self, ptr: u16) -> Result<(), SPI::Error> {
|
|
|
|
let data = ptr.to_be_bytes();
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_write(C::SOCKET_RX_DATA_READ_PTR, &data).await
|
2023-08-15 17:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn command(&mut self, command: Command) -> Result<(), SPI::Error> {
|
|
|
|
let data = [command as u8];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_write(C::SOCKET_COMMAND, &data).await
|
2023-08-15 17:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_rx_size(&mut self) -> Result<u16, SPI::Error> {
|
|
|
|
loop {
|
|
|
|
// Wait until two sequential reads are equal
|
|
|
|
let mut res0 = [0u8; 2];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_read(C::SOCKET_RECVD_SIZE, &mut res0).await?;
|
2023-08-15 17:20:41 +02:00
|
|
|
let mut res1 = [0u8; 2];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_read(C::SOCKET_RECVD_SIZE, &mut res1).await?;
|
2023-08-15 17:20:41 +02:00
|
|
|
if res0 == res1 {
|
|
|
|
break Ok(u16::from_be_bytes(res0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_tx_free_size(&mut self) -> Result<u16, SPI::Error> {
|
|
|
|
let mut data = [0; 2];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_read(C::SOCKET_TX_FREE_SIZE, &mut data).await?;
|
2023-08-15 17:20:41 +02:00
|
|
|
Ok(u16::from_be_bytes(data))
|
2023-05-09 01:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read bytes from the RX buffer. Returns the number of bytes read.
|
2023-08-15 16:47:45 +02:00
|
|
|
async fn read_bytes(&mut self, read_ptr: &mut u16, buffer: &mut [u8]) -> Result<(), SPI::Error> {
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_read(C::rx_addr(*read_ptr), buffer).await?;
|
2023-08-15 16:47:45 +02:00
|
|
|
*read_ptr = (*read_ptr).wrapping_add(buffer.len() as u16);
|
2023-05-09 01:51:08 +02:00
|
|
|
|
2023-08-15 16:47:45 +02:00
|
|
|
Ok(())
|
2023-05-09 01:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Read an ethernet frame from the device. Returns the number of bytes read.
|
|
|
|
pub async fn read_frame(&mut self, frame: &mut [u8]) -> Result<usize, SPI::Error> {
|
2023-08-15 17:20:41 +02:00
|
|
|
let rx_size = self.get_rx_size().await? as usize;
|
2023-05-09 01:51:08 +02:00
|
|
|
if rx_size == 0 {
|
|
|
|
return Ok(0);
|
|
|
|
}
|
|
|
|
|
2023-08-15 17:20:41 +02:00
|
|
|
self.reset_interrupt(Interrupt::Receive).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
2023-08-15 17:20:41 +02:00
|
|
|
let mut read_ptr = self.get_rx_read_ptr().await?;
|
2023-08-15 16:47:45 +02:00
|
|
|
|
2023-05-09 01:51:08 +02:00
|
|
|
// First two bytes gives the size of the received ethernet frame
|
|
|
|
let expected_frame_size: usize = {
|
|
|
|
let mut frame_bytes = [0u8; 2];
|
2023-08-15 16:47:45 +02:00
|
|
|
self.read_bytes(&mut read_ptr, &mut frame_bytes).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
u16::from_be_bytes(frame_bytes) as usize - 2
|
|
|
|
};
|
|
|
|
|
|
|
|
// Read the ethernet frame
|
2023-08-15 16:47:45 +02:00
|
|
|
self.read_bytes(&mut read_ptr, &mut frame[..expected_frame_size])
|
|
|
|
.await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
// Register RX as completed
|
2023-08-15 17:20:41 +02:00
|
|
|
self.set_rx_read_ptr(read_ptr).await?;
|
|
|
|
self.command(Command::Receive).await?;
|
2023-05-09 01:51:08 +02:00
|
|
|
|
2023-08-15 16:47:45 +02:00
|
|
|
Ok(expected_frame_size)
|
2023-05-09 01:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Write an ethernet frame to the device. Returns number of bytes written
|
|
|
|
pub async fn write_frame(&mut self, frame: &[u8]) -> Result<usize, SPI::Error> {
|
2023-08-15 17:20:41 +02:00
|
|
|
while self.get_tx_free_size().await? < frame.len() as u16 {}
|
|
|
|
let write_ptr = self.get_tx_write_ptr().await?;
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_write(C::tx_addr(write_ptr), frame).await?;
|
2023-08-15 17:20:41 +02:00
|
|
|
self.set_tx_write_ptr(write_ptr.wrapping_add(frame.len() as u16))
|
|
|
|
.await?;
|
|
|
|
self.command(Command::Send).await?;
|
2023-05-13 06:34:03 +02:00
|
|
|
Ok(frame.len())
|
2023-05-09 01:51:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn is_link_up(&mut self) -> bool {
|
|
|
|
let mut link = [0];
|
2023-08-15 17:50:56 +02:00
|
|
|
self.bus_read(C::COMMON_PHY_CFG, &mut link).await.ok();
|
2023-05-09 01:51:08 +02:00
|
|
|
link[0] & 1 == 1
|
|
|
|
}
|
|
|
|
}
|