net-w5500: integrate into main repo.
This commit is contained in:
parent
82d765689a
commit
d70994e4a8
@ -10,17 +10,7 @@ edition = "2021"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
embedded-hal = { version = "1.0.0-alpha.10" }
|
embedded-hal = { version = "1.0.0-alpha.10" }
|
||||||
embedded-hal-async = { version = "=0.2.0-alpha.1" }
|
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-time = { version = "0.1.0" }
|
||||||
embassy-futures = { version = "0.1.0" }
|
embassy-futures = { version = "0.1.0" }
|
||||||
defmt = { version = "0.3", optional = true }
|
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::socket;
|
||||||
use crate::spi::SpiInterface;
|
use crate::spi::SpiInterface;
|
||||||
use embedded_hal_async::spi::SpiDevice;
|
|
||||||
|
|
||||||
pub const MODE: u16 = 0x00;
|
pub const MODE: u16 = 0x00;
|
||||||
pub const MAC: u16 = 0x09;
|
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> {
|
pub async fn new(spi: SPI, mac_addr: [u8; 6]) -> Result<W5500<SPI>, SPI::Error> {
|
||||||
let mut bus = SpiInterface(spi);
|
let mut bus = SpiInterface(spi);
|
||||||
// Reset device
|
// Reset device
|
||||||
bus.write_frame(RegisterBlock::Common, MODE, &[0x80])
|
bus.write_frame(RegisterBlock::Common, MODE, &[0x80]).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Enable interrupt pin
|
// Enable interrupt pin
|
||||||
bus.write_frame(RegisterBlock::Common, SOCKET_INTR, &[0x01])
|
bus.write_frame(RegisterBlock::Common, SOCKET_INTR, &[0x01]).await?;
|
||||||
.await?;
|
|
||||||
// Enable receive interrupt
|
// Enable receive interrupt
|
||||||
bus.write_frame(
|
bus.write_frame(
|
||||||
RegisterBlock::Socket0,
|
RegisterBlock::Socket0,
|
||||||
@ -42,8 +41,7 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
// Set MAC address
|
// Set MAC address
|
||||||
bus.write_frame(RegisterBlock::Common, MAC, &mac_addr)
|
bus.write_frame(RegisterBlock::Common, MAC, &mac_addr).await?;
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Set the raw socket RX/TX buffer sizes to 16KB
|
// Set the raw socket RX/TX buffer sizes to 16KB
|
||||||
bus.write_frame(RegisterBlock::Socket0, socket::TXBUF_SIZE, &[16])
|
bus.write_frame(RegisterBlock::Socket0, socket::TXBUF_SIZE, &[16])
|
||||||
@ -53,8 +51,7 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
|||||||
|
|
||||||
// MACRAW mode with MAC filtering.
|
// MACRAW mode with MAC filtering.
|
||||||
let mode: u8 = (1 << 2) | (1 << 7);
|
let mode: u8 = (1 << 2) | (1 << 7);
|
||||||
bus.write_frame(RegisterBlock::Socket0, socket::MODE, &[mode])
|
bus.write_frame(RegisterBlock::Socket0, socket::MODE, &[mode]).await?;
|
||||||
.await?;
|
|
||||||
socket::command(&mut bus, socket::Command::Open).await?;
|
socket::command(&mut bus, socket::Command::Open).await?;
|
||||||
|
|
||||||
Ok(Self { bus })
|
Ok(Self { bus })
|
||||||
@ -70,17 +67,9 @@ impl<SPI: SpiDevice> W5500<SPI> {
|
|||||||
&mut buffer[..rx_size - offset as usize]
|
&mut buffer[..rx_size - offset as usize]
|
||||||
};
|
};
|
||||||
|
|
||||||
let read_ptr = socket::get_rx_read_ptr(&mut self.bus)
|
let read_ptr = socket::get_rx_read_ptr(&mut self.bus).await?.wrapping_add(offset);
|
||||||
.await?
|
self.bus.read_frame(RegisterBlock::RxBuf, read_ptr, read_buffer).await?;
|
||||||
.wrapping_add(offset);
|
socket::set_rx_read_ptr(&mut self.bus, read_ptr.wrapping_add(read_buffer.len() as u16)).await?;
|
||||||
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())
|
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> {
|
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 {}
|
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?;
|
let write_ptr = socket::get_tx_write_ptr(&mut self.bus).await?;
|
||||||
self.bus
|
self.bus.write_frame(RegisterBlock::TxBuf, write_ptr, frame).await?;
|
||||||
.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::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?;
|
socket::command(&mut self.bus, socket::Command::Send).await?;
|
||||||
Ok(frame.len())
|
Ok(frame.len())
|
||||||
|
@ -4,7 +4,6 @@ mod device;
|
|||||||
mod socket;
|
mod socket;
|
||||||
mod spi;
|
mod spi;
|
||||||
|
|
||||||
use crate::device::W5500;
|
|
||||||
use embassy_futures::select::{select, Either};
|
use embassy_futures::select::{select, Either};
|
||||||
use embassy_net_driver_channel as ch;
|
use embassy_net_driver_channel as ch;
|
||||||
use embassy_net_driver_channel::driver::LinkState;
|
use embassy_net_driver_channel::driver::LinkState;
|
||||||
@ -12,6 +11,8 @@ use embassy_time::{Duration, Timer};
|
|||||||
use embedded_hal::digital::OutputPin;
|
use embedded_hal::digital::OutputPin;
|
||||||
use embedded_hal_async::digital::Wait;
|
use embedded_hal_async::digital::Wait;
|
||||||
use embedded_hal_async::spi::SpiDevice;
|
use embedded_hal_async::spi::SpiDevice;
|
||||||
|
|
||||||
|
use crate::device::W5500;
|
||||||
const MTU: usize = 1514;
|
const MTU: usize = 1514;
|
||||||
|
|
||||||
/// Type alias for the embassy-net driver for W5500
|
/// 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).
|
/// Obtain a driver for using the W5500 with [`embassy-net`](crates.io/crates/embassy-net).
|
||||||
pub async fn new<
|
pub async fn new<'a, const N_RX: usize, const N_TX: usize, SPI: SpiDevice, INT: Wait, RST: OutputPin>(
|
||||||
'a,
|
|
||||||
const N_RX: usize,
|
|
||||||
const N_TX: usize,
|
|
||||||
SPI: SpiDevice,
|
|
||||||
INT: Wait,
|
|
||||||
RST: OutputPin,
|
|
||||||
>(
|
|
||||||
mac_addr: [u8; 6],
|
mac_addr: [u8; 6],
|
||||||
state: &'a mut State<N_RX, N_TX>,
|
state: &'a mut State<N_RX, N_TX>,
|
||||||
spi_dev: SPI,
|
spi_dev: SPI,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
|
use embedded_hal_async::spi::SpiDevice;
|
||||||
|
|
||||||
use crate::device::RegisterBlock;
|
use crate::device::RegisterBlock;
|
||||||
use crate::spi::SpiInterface;
|
use crate::spi::SpiInterface;
|
||||||
use embedded_hal_async::spi::SpiDevice;
|
|
||||||
|
|
||||||
pub const MODE: u16 = 0x00;
|
pub const MODE: u16 = 0x00;
|
||||||
pub const COMMAND: u16 = 0x01;
|
pub const COMMAND: u16 = 0x01;
|
||||||
@ -25,79 +26,55 @@ pub enum Interrupt {
|
|||||||
Receive = 0b00100_u8,
|
Receive = 0b00100_u8,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn reset_interrupt<SPI: SpiDevice>(
|
pub async fn reset_interrupt<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, code: Interrupt) -> Result<(), SPI::Error> {
|
||||||
bus: &mut SpiInterface<SPI>,
|
|
||||||
code: Interrupt,
|
|
||||||
) -> Result<(), SPI::Error> {
|
|
||||||
let data = [code as u8];
|
let data = [code as u8];
|
||||||
bus.write_frame(RegisterBlock::Socket0, INTR, &data).await
|
bus.write_frame(RegisterBlock::Socket0, INTR, &data).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_tx_write_ptr<SPI: SpiDevice>(
|
pub async fn get_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||||
bus: &mut SpiInterface<SPI>,
|
|
||||||
) -> Result<u16, SPI::Error> {
|
|
||||||
let mut data = [0u8; 2];
|
let mut data = [0u8; 2];
|
||||||
bus.read_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &mut data)
|
bus.read_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &mut data)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(u16::from_be_bytes(data))
|
Ok(u16::from_be_bytes(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_tx_write_ptr<SPI: SpiDevice>(
|
pub async fn set_tx_write_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> {
|
||||||
bus: &mut SpiInterface<SPI>,
|
|
||||||
ptr: u16,
|
|
||||||
) -> Result<(), SPI::Error> {
|
|
||||||
let data = ptr.to_be_bytes();
|
let data = ptr.to_be_bytes();
|
||||||
bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data)
|
bus.write_frame(RegisterBlock::Socket0, TX_DATA_WRITE_PTR, &data).await
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_rx_read_ptr<SPI: SpiDevice>(
|
pub async fn get_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||||
bus: &mut SpiInterface<SPI>,
|
|
||||||
) -> Result<u16, SPI::Error> {
|
|
||||||
let mut data = [0u8; 2];
|
let mut data = [0u8; 2];
|
||||||
bus.read_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &mut data)
|
bus.read_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &mut data)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(u16::from_be_bytes(data))
|
Ok(u16::from_be_bytes(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn set_rx_read_ptr<SPI: SpiDevice>(
|
pub async fn set_rx_read_ptr<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, ptr: u16) -> Result<(), SPI::Error> {
|
||||||
bus: &mut SpiInterface<SPI>,
|
|
||||||
ptr: u16,
|
|
||||||
) -> Result<(), SPI::Error> {
|
|
||||||
let data = ptr.to_be_bytes();
|
let data = ptr.to_be_bytes();
|
||||||
bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data)
|
bus.write_frame(RegisterBlock::Socket0, RX_DATA_READ_PTR, &data).await
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn command<SPI: SpiDevice>(
|
pub async fn command<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>, command: Command) -> Result<(), SPI::Error> {
|
||||||
bus: &mut SpiInterface<SPI>,
|
|
||||||
command: Command,
|
|
||||||
) -> Result<(), SPI::Error> {
|
|
||||||
let data = [command as u8];
|
let data = [command as u8];
|
||||||
bus.write_frame(RegisterBlock::Socket0, COMMAND, &data)
|
bus.write_frame(RegisterBlock::Socket0, COMMAND, &data).await
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
pub async fn get_rx_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||||
loop {
|
loop {
|
||||||
// Wait until two sequential reads are equal
|
// Wait until two sequential reads are equal
|
||||||
let mut res0 = [0u8; 2];
|
let mut res0 = [0u8; 2];
|
||||||
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0)
|
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res0).await?;
|
||||||
.await?;
|
|
||||||
let mut res1 = [0u8; 2];
|
let mut res1 = [0u8; 2];
|
||||||
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1)
|
bus.read_frame(RegisterBlock::Socket0, RECVD_SIZE, &mut res1).await?;
|
||||||
.await?;
|
|
||||||
if res0 == res1 {
|
if res0 == res1 {
|
||||||
break Ok(u16::from_be_bytes(res0));
|
break Ok(u16::from_be_bytes(res0));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_tx_free_size<SPI: SpiDevice>(
|
pub async fn get_tx_free_size<SPI: SpiDevice>(bus: &mut SpiInterface<SPI>) -> Result<u16, SPI::Error> {
|
||||||
bus: &mut SpiInterface<SPI>,
|
|
||||||
) -> Result<u16, SPI::Error> {
|
|
||||||
let mut data = [0; 2];
|
let mut data = [0; 2];
|
||||||
bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data)
|
bus.read_frame(RegisterBlock::Socket0, TX_FREE_SIZE, &mut data).await?;
|
||||||
.await?;
|
|
||||||
Ok(u16::from_be_bytes(data))
|
Ok(u16::from_be_bytes(data))
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,13 @@
|
|||||||
use crate::device::RegisterBlock;
|
|
||||||
use embedded_hal_async::spi::{Operation, SpiDevice};
|
use embedded_hal_async::spi::{Operation, SpiDevice};
|
||||||
|
|
||||||
|
use crate::device::RegisterBlock;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||||
pub struct SpiInterface<SPI>(pub SPI);
|
pub struct SpiInterface<SPI>(pub SPI);
|
||||||
|
|
||||||
impl<SPI: SpiDevice> SpiInterface<SPI> {
|
impl<SPI: SpiDevice> SpiInterface<SPI> {
|
||||||
pub async fn read_frame(
|
pub async fn read_frame(&mut self, block: RegisterBlock, address: u16, data: &mut [u8]) -> Result<(), SPI::Error> {
|
||||||
&mut self,
|
|
||||||
block: RegisterBlock,
|
|
||||||
address: u16,
|
|
||||||
data: &mut [u8],
|
|
||||||
) -> Result<(), SPI::Error> {
|
|
||||||
let address_phase = address.to_be_bytes();
|
let address_phase = address.to_be_bytes();
|
||||||
let control_phase = [(block as u8) << 3];
|
let control_phase = [(block as u8) << 3];
|
||||||
let operations = &mut [
|
let operations = &mut [
|
||||||
@ -22,12 +18,7 @@ impl<SPI: SpiDevice> SpiInterface<SPI> {
|
|||||||
self.0.transaction(operations).await
|
self.0.transaction(operations).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn write_frame(
|
pub async fn write_frame(&mut self, block: RegisterBlock, address: u16, data: &[u8]) -> Result<(), SPI::Error> {
|
||||||
&mut self,
|
|
||||||
block: RegisterBlock,
|
|
||||||
address: u16,
|
|
||||||
data: &[u8],
|
|
||||||
) -> Result<(), SPI::Error> {
|
|
||||||
let address_phase = address.to_be_bytes();
|
let address_phase = address.to_be_bytes();
|
||||||
let control_phase = [(block as u8) << 3 | 0b0000_0100];
|
let control_phase = [(block as u8) << 3 | 0b0000_0100];
|
||||||
let data_phase = data;
|
let data_phase = data;
|
||||||
|
@ -12,7 +12,8 @@ embassy-executor = { version = "0.2.0", path = "../../embassy-executor", feature
|
|||||||
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] }
|
embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] }
|
||||||
embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] }
|
embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] }
|
||||||
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
|
embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
|
||||||
embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet"] }
|
embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "udp", "dhcpv4", "medium-ethernet"] }
|
||||||
|
embassy-net-w5500 = { version = "0.1.0", path = "../../embassy-net-w5500", features = ["defmt"] }
|
||||||
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
|
embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
|
||||||
embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" }
|
embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" }
|
||||||
embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["time", "defmt"] }
|
embassy-lora = { version = "0.1.0", path = "../../embassy-lora", features = ["time", "defmt"] }
|
||||||
@ -48,6 +49,7 @@ static_cell = "1.0.0"
|
|||||||
log = "0.4"
|
log = "0.4"
|
||||||
pio-proc = "0.2"
|
pio-proc = "0.2"
|
||||||
pio = "0.2.1"
|
pio = "0.2.1"
|
||||||
|
rand = { version = "0.8.5", default-features = false }
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
debug = true
|
debug = true
|
||||||
|
@ -15,6 +15,7 @@ use embassy_rp::clocks::RoscRng;
|
|||||||
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
||||||
use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
|
use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
|
||||||
use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
|
use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
|
||||||
|
use embassy_time::Duration;
|
||||||
use embedded_hal_async::spi::ExclusiveDevice;
|
use embedded_hal_async::spi::ExclusiveDevice;
|
||||||
use embedded_io::asynch::Write;
|
use embedded_io::asynch::Write;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -62,14 +63,8 @@ async fn main(spawner: Spawner) {
|
|||||||
|
|
||||||
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
let state = singleton!(State::<8, 8>::new());
|
let state = singleton!(State::<8, 8>::new());
|
||||||
let (device, runner) = embassy_net_w5500::new(
|
let (device, runner) =
|
||||||
mac_addr,
|
embassy_net_w5500::new(mac_addr, state, ExclusiveDevice::new(spi, cs), w5500_int, w5500_reset).await;
|
||||||
state,
|
|
||||||
ExclusiveDevice::new(spi, cs),
|
|
||||||
w5500_int,
|
|
||||||
w5500_reset,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
unwrap!(spawner.spawn(ethernet_task(runner)));
|
unwrap!(spawner.spawn(ethernet_task(runner)));
|
||||||
|
|
||||||
// Generate random seed
|
// Generate random seed
|
||||||
@ -103,18 +98,14 @@ async fn listen_task(stack: &'static Stack<Device<'static>>, id: u8, port: u16)
|
|||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
loop {
|
loop {
|
||||||
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||||
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
||||||
|
|
||||||
info!("SOCKET {}: Listening on TCP:{}...", id, port);
|
info!("SOCKET {}: Listening on TCP:{}...", id, port);
|
||||||
if let Err(e) = socket.accept(port).await {
|
if let Err(e) = socket.accept(port).await {
|
||||||
warn!("accept error: {:?}", e);
|
warn!("accept error: {:?}", e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
info!(
|
info!("SOCKET {}: Received connection from {:?}", id, socket.remote_endpoint());
|
||||||
"SOCKET {}: Received connection from {:?}",
|
|
||||||
id,
|
|
||||||
socket.remote_endpoint()
|
|
||||||
);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let n = match socket.read(&mut buf).await {
|
let n = match socket.read(&mut buf).await {
|
||||||
@ -128,11 +119,7 @@ async fn listen_task(stack: &'static Stack<Device<'static>>, id: u8, port: u16)
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
info!(
|
info!("SOCKET {}: rxd {}", id, core::str::from_utf8(&buf[..n]).unwrap());
|
||||||
"SOCKET {}: rxd {}",
|
|
||||||
id,
|
|
||||||
core::str::from_utf8(&buf[..n]).unwrap()
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Err(e) = socket.write_all(&buf[..n]).await {
|
if let Err(e) = socket.write_all(&buf[..n]).await {
|
||||||
warn!("write error: {:?}", e);
|
warn!("write error: {:?}", e);
|
||||||
|
@ -7,6 +7,7 @@
|
|||||||
#![feature(type_alias_impl_trait)]
|
#![feature(type_alias_impl_trait)]
|
||||||
|
|
||||||
use core::str::FromStr;
|
use core::str::FromStr;
|
||||||
|
|
||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_futures::yield_now;
|
use embassy_futures::yield_now;
|
||||||
@ -65,14 +66,8 @@ async fn main(spawner: Spawner) {
|
|||||||
|
|
||||||
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
let state = singleton!(State::<8, 8>::new());
|
let state = singleton!(State::<8, 8>::new());
|
||||||
let (device, runner) = embassy_net_w5500::new(
|
let (device, runner) =
|
||||||
mac_addr,
|
embassy_net_w5500::new(mac_addr, state, ExclusiveDevice::new(spi, cs), w5500_int, w5500_reset).await;
|
||||||
state,
|
|
||||||
ExclusiveDevice::new(spi, cs),
|
|
||||||
w5500_int,
|
|
||||||
w5500_reset,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
unwrap!(spawner.spawn(ethernet_task(runner)));
|
unwrap!(spawner.spawn(ethernet_task(runner)));
|
||||||
|
|
||||||
// Generate random seed
|
// Generate random seed
|
||||||
@ -98,7 +93,7 @@ async fn main(spawner: Spawner) {
|
|||||||
let mut tx_buffer = [0; 4096];
|
let mut tx_buffer = [0; 4096];
|
||||||
loop {
|
loop {
|
||||||
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||||
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
||||||
|
|
||||||
led.set_low();
|
led.set_low();
|
||||||
info!("Connecting...");
|
info!("Connecting...");
|
||||||
|
@ -16,6 +16,7 @@ use embassy_rp::clocks::RoscRng;
|
|||||||
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
||||||
use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
|
use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
|
||||||
use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
|
use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
|
||||||
|
use embassy_time::Duration;
|
||||||
use embedded_hal_async::spi::ExclusiveDevice;
|
use embedded_hal_async::spi::ExclusiveDevice;
|
||||||
use embedded_io::asynch::Write;
|
use embedded_io::asynch::Write;
|
||||||
use rand::RngCore;
|
use rand::RngCore;
|
||||||
@ -64,14 +65,8 @@ async fn main(spawner: Spawner) {
|
|||||||
|
|
||||||
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
let state = singleton!(State::<8, 8>::new());
|
let state = singleton!(State::<8, 8>::new());
|
||||||
let (device, runner) = embassy_net_w5500::new(
|
let (device, runner) =
|
||||||
mac_addr,
|
embassy_net_w5500::new(mac_addr, state, ExclusiveDevice::new(spi, cs), w5500_int, w5500_reset).await;
|
||||||
state,
|
|
||||||
ExclusiveDevice::new(spi, cs),
|
|
||||||
w5500_int,
|
|
||||||
w5500_reset,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
unwrap!(spawner.spawn(ethernet_task(runner)));
|
unwrap!(spawner.spawn(ethernet_task(runner)));
|
||||||
|
|
||||||
// Generate random seed
|
// Generate random seed
|
||||||
@ -98,7 +93,7 @@ async fn main(spawner: Spawner) {
|
|||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
loop {
|
loop {
|
||||||
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||||
socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
||||||
|
|
||||||
led.set_low();
|
led.set_low();
|
||||||
info!("Listening on TCP:1234...");
|
info!("Listening on TCP:1234...");
|
||||||
|
@ -9,8 +9,8 @@
|
|||||||
use defmt::*;
|
use defmt::*;
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_futures::yield_now;
|
use embassy_futures::yield_now;
|
||||||
use embassy_net::udp::UdpSocket;
|
use embassy_net::udp::{PacketMetadata, UdpSocket};
|
||||||
use embassy_net::{PacketMetadata, Stack, StackResources};
|
use embassy_net::{Stack, StackResources};
|
||||||
use embassy_net_w5500::*;
|
use embassy_net_w5500::*;
|
||||||
use embassy_rp::clocks::RoscRng;
|
use embassy_rp::clocks::RoscRng;
|
||||||
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
||||||
@ -62,14 +62,8 @@ async fn main(spawner: Spawner) {
|
|||||||
|
|
||||||
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
||||||
let state = singleton!(State::<8, 8>::new());
|
let state = singleton!(State::<8, 8>::new());
|
||||||
let (device, runner) = embassy_net_w5500::new(
|
let (device, runner) =
|
||||||
mac_addr,
|
embassy_net_w5500::new(mac_addr, state, ExclusiveDevice::new(spi, cs), w5500_int, w5500_reset).await;
|
||||||
state,
|
|
||||||
ExclusiveDevice::new(spi, cs),
|
|
||||||
w5500_int,
|
|
||||||
w5500_reset,
|
|
||||||
)
|
|
||||||
.await;
|
|
||||||
unwrap!(spawner.spawn(ethernet_task(runner)));
|
unwrap!(spawner.spawn(ethernet_task(runner)));
|
||||||
|
|
||||||
// Generate random seed
|
// Generate random seed
|
||||||
@ -98,13 +92,7 @@ async fn main(spawner: Spawner) {
|
|||||||
let mut tx_meta = [PacketMetadata::EMPTY; 16];
|
let mut tx_meta = [PacketMetadata::EMPTY; 16];
|
||||||
let mut buf = [0; 4096];
|
let mut buf = [0; 4096];
|
||||||
loop {
|
loop {
|
||||||
let mut socket = UdpSocket::new(
|
let mut socket = UdpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
|
||||||
stack,
|
|
||||||
&mut rx_meta,
|
|
||||||
&mut rx_buffer,
|
|
||||||
&mut tx_meta,
|
|
||||||
&mut tx_buffer,
|
|
||||||
);
|
|
||||||
socket.bind(1234).unwrap();
|
socket.bind(1234).unwrap();
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
Loading…
Reference in New Issue
Block a user