2023-05-31 00:54:20 +02:00
|
|
|
//! This example shows how you can allow multiple simultaneous TCP connections, by having multiple sockets listening on the same port.
|
|
|
|
//!
|
|
|
|
//! Example written for the [`WIZnet W5500-EVB-Pico`](https://www.wiznet.io/product-item/w5500-evb-pico/) board.
|
|
|
|
|
2023-05-09 01:51:08 +02:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(type_alias_impl_trait)]
|
|
|
|
|
|
|
|
use defmt::*;
|
|
|
|
use embassy_executor::Spawner;
|
|
|
|
use embassy_futures::yield_now;
|
|
|
|
use embassy_net::{Stack, StackResources};
|
2023-08-15 23:02:53 +02:00
|
|
|
use embassy_net_wiznet::chip::W5500;
|
|
|
|
use embassy_net_wiznet::*;
|
2023-05-09 01:51:08 +02:00
|
|
|
use embassy_rp::clocks::RoscRng;
|
|
|
|
use embassy_rp::gpio::{Input, Level, Output, Pull};
|
|
|
|
use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
|
|
|
|
use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
|
2023-07-04 19:53:06 +02:00
|
|
|
use embassy_time::{Delay, Duration};
|
2023-08-16 00:40:56 +02:00
|
|
|
use embedded_hal_bus::spi::ExclusiveDevice;
|
2023-08-07 13:43:09 +02:00
|
|
|
use embedded_io_async::Write;
|
2023-05-09 01:51:08 +02:00
|
|
|
use rand::RngCore;
|
2023-06-01 01:32:11 +02:00
|
|
|
use static_cell::make_static;
|
2023-05-09 01:51:08 +02:00
|
|
|
use {defmt_rtt as _, panic_probe as _};
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
async fn ethernet_task(
|
|
|
|
runner: Runner<
|
|
|
|
'static,
|
2023-08-15 17:50:56 +02:00
|
|
|
W5500,
|
2023-07-04 19:53:06 +02:00
|
|
|
ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>, Delay>,
|
2023-05-09 01:51:08 +02:00
|
|
|
Input<'static, PIN_21>,
|
|
|
|
Output<'static, PIN_20>,
|
|
|
|
>,
|
|
|
|
) -> ! {
|
|
|
|
runner.run().await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task]
|
|
|
|
async fn net_task(stack: &'static Stack<Device<'static>>) -> ! {
|
|
|
|
stack.run().await
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::main]
|
|
|
|
async fn main(spawner: Spawner) {
|
|
|
|
let p = embassy_rp::init(Default::default());
|
|
|
|
let mut rng = RoscRng;
|
|
|
|
|
|
|
|
let mut spi_cfg = SpiConfig::default();
|
|
|
|
spi_cfg.frequency = 50_000_000;
|
|
|
|
let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18);
|
|
|
|
let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg);
|
|
|
|
let cs = Output::new(p.PIN_17, Level::High);
|
|
|
|
let w5500_int = Input::new(p.PIN_21, Pull::Up);
|
|
|
|
let w5500_reset = Output::new(p.PIN_20, Level::High);
|
|
|
|
|
|
|
|
let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
|
2023-06-01 01:32:11 +02:00
|
|
|
let state = make_static!(State::<8, 8>::new());
|
2023-08-15 23:02:53 +02:00
|
|
|
let (device, runner) = embassy_net_wiznet::new(
|
2023-07-04 19:53:06 +02:00
|
|
|
mac_addr,
|
|
|
|
state,
|
|
|
|
ExclusiveDevice::new(spi, cs, Delay),
|
|
|
|
w5500_int,
|
|
|
|
w5500_reset,
|
|
|
|
)
|
|
|
|
.await;
|
2023-05-09 01:51:08 +02:00
|
|
|
unwrap!(spawner.spawn(ethernet_task(runner)));
|
|
|
|
|
|
|
|
// Generate random seed
|
|
|
|
let seed = rng.next_u64();
|
|
|
|
|
|
|
|
// Init network stack
|
2023-06-01 01:32:11 +02:00
|
|
|
let stack = &*make_static!(Stack::new(
|
2023-05-09 01:51:08 +02:00
|
|
|
device,
|
2023-06-07 12:04:15 +02:00
|
|
|
embassy_net::Config::dhcpv4(Default::default()),
|
2023-06-01 01:32:11 +02:00
|
|
|
make_static!(StackResources::<3>::new()),
|
2023-05-09 01:51:08 +02:00
|
|
|
seed
|
|
|
|
));
|
|
|
|
|
|
|
|
// Launch network task
|
|
|
|
unwrap!(spawner.spawn(net_task(&stack)));
|
|
|
|
|
|
|
|
info!("Waiting for DHCP...");
|
|
|
|
let cfg = wait_for_config(stack).await;
|
|
|
|
let local_addr = cfg.address.address();
|
|
|
|
info!("IP address: {:?}", local_addr);
|
|
|
|
|
|
|
|
// Create two sockets listening to the same port, to handle simultaneous connections
|
|
|
|
unwrap!(spawner.spawn(listen_task(&stack, 0, 1234)));
|
|
|
|
unwrap!(spawner.spawn(listen_task(&stack, 1, 1234)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[embassy_executor::task(pool_size = 2)]
|
|
|
|
async fn listen_task(stack: &'static Stack<Device<'static>>, id: u8, port: u16) {
|
|
|
|
let mut rx_buffer = [0; 4096];
|
|
|
|
let mut tx_buffer = [0; 4096];
|
|
|
|
let mut buf = [0; 4096];
|
|
|
|
loop {
|
|
|
|
let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
2023-05-31 01:01:30 +02:00
|
|
|
socket.set_timeout(Some(Duration::from_secs(10)));
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
info!("SOCKET {}: Listening on TCP:{}...", id, port);
|
|
|
|
if let Err(e) = socket.accept(port).await {
|
|
|
|
warn!("accept error: {:?}", e);
|
|
|
|
continue;
|
|
|
|
}
|
2023-05-31 01:01:30 +02:00
|
|
|
info!("SOCKET {}: Received connection from {:?}", id, socket.remote_endpoint());
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let n = match socket.read(&mut buf).await {
|
|
|
|
Ok(0) => {
|
|
|
|
warn!("read EOF");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(n) => n,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("SOCKET {}: {:?}", id, e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2023-05-31 01:01:30 +02:00
|
|
|
info!("SOCKET {}: rxd {}", id, core::str::from_utf8(&buf[..n]).unwrap());
|
2023-05-09 01:51:08 +02:00
|
|
|
|
|
|
|
if let Err(e) = socket.write_all(&buf[..n]).await {
|
|
|
|
warn!("write error: {:?}", e);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 14:57:17 +02:00
|
|
|
async fn wait_for_config(stack: &'static Stack<Device<'static>>) -> embassy_net::StaticConfigV4 {
|
2023-05-09 01:51:08 +02:00
|
|
|
loop {
|
2023-06-05 16:00:53 +02:00
|
|
|
if let Some(config) = stack.config_v4() {
|
2023-05-09 01:51:08 +02:00
|
|
|
return config.clone();
|
|
|
|
}
|
|
|
|
yield_now().await;
|
|
|
|
}
|
|
|
|
}
|