net-w5500: integrate into main repo.
This commit is contained in:
@ -10,17 +10,7 @@ edition = "2021"
|
||||
[dependencies]
|
||||
embedded-hal = { version = "1.0.0-alpha.10" }
|
||||
embedded-hal-async = { version = "=0.2.0-alpha.1" }
|
||||
embassy-net-driver-channel = { version = "0.1.0" }
|
||||
embassy-net-driver-channel = { version = "0.1.0", path = "../embassy-net-driver-channel"}
|
||||
embassy-time = { version = "0.1.0" }
|
||||
embassy-futures = { version = "0.1.0" }
|
||||
defmt = { version = "0.3", optional = true }
|
||||
|
||||
[patch.crates-io]
|
||||
embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
embassy-net-driver = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
embassy-net-driver-channel = { git = "https://github.com/embassy-rs/embassy", rev = "e179e7cf85810f0aa7ef8027d8d48f6d21f64dac" }
|
||||
|
@ -1,6 +1,7 @@
|
||||
use embedded_hal_async::spi::SpiDevice;
|
||||
|
||||
use crate::socket;
|
||||
use crate::spi::SpiInterface;
|
||||
use embedded_hal_async::spi::SpiDevice;
|
||||
|
||||
pub const MODE: u16 = 0x00;
|
||||
pub const MAC: u16 = 0x09;
|
||||
@ -27,12 +28,10 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
||||
pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<W5500<SPI>, SPI::Error> {
|
||||
let mut bus = SpiInterface(spi);
|
||||
// Reset device
|
||||
bus.write_frame(RegisterBlock::Common, MODE, &[0x80])
|
||||
.await?;
|
||||
bus.write_frame(RegisterBlock::Common, MODE, &[0x80]).await?;
|
||||
|
||||
// Enable interrupt pin
|
||||
bus.write_frame(RegisterBlock::Common, SOCKET_INTR, &[0x01])
|
||||
.await?;
|
||||
bus.write_frame(RegisterBlock::Common, SOCKET_INTR, &[0x01]).await?;
|
||||
// Enable receive interrupt
|
||||
bus.write_frame(
|
||||
RegisterBlock::Socket0,
|
||||
@ -42,8 +41,7 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
||||
.await?;
|
||||
|
||||
// Set MAC address
|
||||
bus.write_frame(RegisterBlock::Common, MAC, &mac_addr)
|
||||
.await?;
|
||||
bus.write_frame(RegisterBlock::Common, MAC, &mac_addr).await?;
|
||||
|
||||
// Set the raw socket RX/TX buffer sizes to 16KB
|
||||
bus.write_frame(RegisterBlock::Socket0, socket::TXBUF_SIZE, &[16])
|
||||
@ -53,8 +51,7 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
||||
|
||||
// MACRAW mode with MAC filtering.
|
||||
let mode: u8 = (1 << 2) | (1 << 7);
|
||||
bus.write_frame(RegisterBlock::Socket0, socket::MODE, &[mode])
|
||||
.await?;
|
||||
bus.write_frame(RegisterBlock::Socket0, socket::MODE, &[mode]).await?;
|
||||
socket::command(&mut bus, socket::Command::Open).await?;
|
||||
|
||||
Ok(Self { bus })
|
||||
@ -70,17 +67,9 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
||||
&mut buffer[..rx_size - offset as usize]
|
||||
};
|
||||
|
||||
let read_ptr = socket::get_rx_read_ptr(&mut self.bus)
|
||||
.await?
|
||||
.wrapping_add(offset);
|
||||
self.bus
|
||||
.read_frame(RegisterBlock::RxBuf, read_ptr, read_buffer)
|
||||
.await?;
|
||||
socket::set_rx_read_ptr(
|
||||
&mut self.bus,
|
||||
read_ptr.wrapping_add(read_buffer.len() as u16),
|
||||
)
|
||||
.await?;
|
||||
let read_ptr = socket::get_rx_read_ptr(&mut self.bus).await?.wrapping_add(offset);
|
||||
self.bus.read_frame(RegisterBlock::RxBuf, read_ptr, read_buffer).await?;
|
||||
socket::set_rx_read_ptr(&mut self.bus, read_ptr.wrapping_add(read_buffer.len() as u16)).await?;
|
||||
|
||||
Ok(read_buffer.len())
|
||||
}
|
||||
@ -125,9 +114,7 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
||||
pub async fn write_frame(&mut self, frame: &[u8]) -> Result<usize, SPI::Error> {
|
||||
while socket::get_tx_free_size(&mut self.bus).await? < frame.len() as u16 {}
|
||||
let write_ptr = socket::get_tx_write_ptr(&mut self.bus).await?;
|
||||
self.bus
|
||||
.write_frame(RegisterBlock::TxBuf, write_ptr, frame)
|
||||
.await?;
|
||||
self.bus.write_frame(RegisterBlock::TxBuf, write_ptr, frame).await?;
|
||||
socket::set_tx_write_ptr(&mut self.bus, write_ptr.wrapping_add(frame.len() as u16)).await?;
|
||||
socket::command(&mut self.bus, socket::Command::Send).await?;
|
||||
Ok(frame.len())
|
||||
|
@ -4,7 +4,6 @@ mod device;
|
||||
mod socket;
|
||||
mod spi;
|
||||
|
||||
use crate::device::W5500;
|
||||
use embassy_futures::select::{select, Either};
|
||||
use embassy_net_driver_channel as ch;
|
||||
use embassy_net_driver_channel::driver::LinkState;
|
||||
@ -12,6 +11,8 @@ use embassy_time::{Duration, Timer};
|
||||
use embedded_hal::digital::OutputPin;
|
||||
use embedded_hal_async::digital::Wait;
|
||||
use embedded_hal_async::spi::SpiDevice;
|
||||
|
||||
use crate::device::W5500;
|
||||
const MTU: usize = 1514;
|
||||
|
||||
/// Type alias for the embassy-net driver for W5500
|
||||
@ -77,14 +78,7 @@ impl<'d, SPI: SpiDevice, INT: Wait, RST: OutputPin> Runner<'d, SPI, INT, RST> {
|
||||
}
|
||||
|
||||
/// Obtain a driver for using the W5500 with [`embassy-net`](crates.io/crates/embassy-net).
|
||||
pub async fn new<
|
||||
'a,
|
||||
const N_RX: usize,
|
||||
const N_TX: usize,
|
||||
SPI: SpiDevice,
|
||||
INT: Wait,
|
||||
RST: OutputPin,
|
||||
>(
|
||||
pub async fn new<'a, const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT: Wait, RST: OutputPin>(
|
||||
mac_addr: [u8; 6],
|
||||
state: &'a mut State<N_RX, N_TX>,
|
||||
spi_dev: SPI,
|
||||
|
@ -1,6 +1,7 @@
|
||||
use embedded_hal_async::spi::SpiDevice;
|
||||
|
||||
use crate::device::RegisterBlock;
|
||||
use crate::spi::SpiInterface;
|
||||
use embedded_hal_async::spi::SpiDevice;
|
||||
|
||||
pub const MODE: u16 = 0x00;
|
||||
pub const COMMAND: u16 = 0x01;
|
||||
@ -25,79 +26,55 @@ pub enum Interrupt {
|
||||
Receive = 0b00100_u8,
|
||||
}
|
||||
|
||||
pub async fn reset_interrupt<SPI: SpiDevice>(
|
||||
bus: &mut SpiInterface<SPI>,
|
||||
code: Interrupt,
|
||||
) -> Result<(), SPI::Error> {
|
||||
pub async fn reset_interrupt<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, code: Interrupt) -> Result<(), SPI::Error> {
|
||||
let data = [code as u8];
|
||||
bus.write_frame(RegisterBlock::Socket0, INTR, &data).await
|
||||
}
|
||||
|
||||
pub async fn get_tx_write_ptr<SPI: SpiDevice>(
|
||||
bus: &mut SpiInterface<SPI>,
|
||||
) -> Result<u16, SPI::Error> {
|
||||
pub async fn get_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||
let mut data = [0u8; 2];
|
||||
bus.read_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &mut data)
|
||||
.await?;
|
||||
Ok(u16::from_be_bytes(data))
|
||||
}
|
||||
|
||||
pub async fn set_tx_write_ptr<SPI: SpiDevice>(
|
||||
bus: &mut SpiInterface<SPI>,
|
||||
ptr: u16,
|
||||
) -> Result<(), SPI::Error> {
|
||||
pub async fn set_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> {
|
||||
let data = ptr.to_be_bytes();
|
||||
bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data)
|
||||
.await
|
||||
bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data).await
|
||||
}
|
||||
|
||||
pub async fn get_rx_read_ptr<SPI: SpiDevice>(
|
||||
bus: &mut SpiInterface<SPI>,
|
||||
) -> Result<u16, SPI::Error> {
|
||||
pub async fn get_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||
let mut data = [0u8; 2];
|
||||
bus.read_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &mut data)
|
||||
.await?;
|
||||
Ok(u16::from_be_bytes(data))
|
||||
}
|
||||
|
||||
pub async fn set_rx_read_ptr<SPI: SpiDevice>(
|
||||
bus: &mut SpiInterface<SPI>,
|
||||
ptr: u16,
|
||||
) -> Result<(), SPI::Error> {
|
||||
pub async fn set_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> {
|
||||
let data = ptr.to_be_bytes();
|
||||
bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data)
|
||||
.await
|
||||
bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data).await
|
||||
}
|
||||
|
||||
pub async fn command<SPI: SpiDevice>(
|
||||
bus: &mut SpiInterface<SPI>,
|
||||
command: Command,
|
||||
) -> Result<(), SPI::Error> {
|
||||
pub async fn command<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, command: Command) -> Result<(), SPI::Error> {
|
||||
let data = [command as u8];
|
||||
bus.write_frame(RegisterBlock::Socket0, COMMAND, &data)
|
||||
.await
|
||||
bus.write_frame(RegisterBlock::Socket0, COMMAND, &data).await
|
||||
}
|
||||
|
||||
pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||
loop {
|
||||
// Wait until two sequential reads are equal
|
||||
let mut res0 = [0u8; 2];
|
||||
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0)
|
||||
.await?;
|
||||
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0).await?;
|
||||
let mut res1 = [0u8; 2];
|
||||
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1)
|
||||
.await?;
|
||||
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1).await?;
|
||||
if res0 == res1 {
|
||||
break Ok(u16::from_be_bytes(res0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_tx_free_size<SPI: SpiDevice>(
|
||||
bus: &mut SpiInterface<SPI>,
|
||||
) -> Result<u16, SPI::Error> {
|
||||
pub async fn get_tx_free_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||
let mut data = [0; 2];
|
||||
bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data)
|
||||
.await?;
|
||||
bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data).await?;
|
||||
Ok(u16::from_be_bytes(data))
|
||||
}
|
||||
|
@ -1,17 +1,13 @@
|
||||
use crate::device::RegisterBlock;
|
||||
use embedded_hal_async::spi::{Operation, SpiDevice};
|
||||
|
||||
use crate::device::RegisterBlock;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
pub struct SpiInterface<SPI>(pub SPI);
|
||||
|
||||
impl<SPI: SpiDevice> SpiInterface<SPI> {
|
||||
pub async fn read_frame(
|
||||
&mut self,
|
||||
block: RegisterBlock,
|
||||
address: u16,
|
||||
data: &mut [u8],
|
||||
) -> Result<(), SPI::Error> {
|
||||
pub async fn read_frame(&mut self, block: RegisterBlock, address: u16, data: &mut [u8]) -> Result<(), SPI::Error> {
|
||||
let address_phase = address.to_be_bytes();
|
||||
let control_phase = [(block as u8) << 3];
|
||||
let operations = &mut [
|
||||
@ -22,12 +18,7 @@ impl<SPI: SpiDevice> SpiInterface<SPI> {
|
||||
self.0.transaction(operations).await
|
||||
}
|
||||
|
||||
pub async fn write_frame(
|
||||
&mut self,
|
||||
block: RegisterBlock,
|
||||
address: u16,
|
||||
data: &[u8],
|
||||
) -> Result<(), SPI::Error> {
|
||||
pub async fn write_frame(&mut self, block: RegisterBlock, address: u16, data: &[u8]) -> Result<(), SPI::Error> {
|
||||
let address_phase = address.to_be_bytes();
|
||||
let control_phase = [(block as u8) << 3 | 0b0000_0100];
|
||||
let data_phase = data;
|
||||
|
Reference in New Issue
Block a user