aboutsummaryrefslogtreecommitdiff
path: root/examples/rp235x/src/bin/ethernet_w5500_multisocket.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rp235x/src/bin/ethernet_w5500_multisocket.rs')
-rw-r--r--examples/rp235x/src/bin/ethernet_w5500_multisocket.rs139
1 files changed, 139 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/ethernet_w5500_multisocket.rs b/examples/rp235x/src/bin/ethernet_w5500_multisocket.rs
new file mode 100644
index 000000000..fd8bc5c7a
--- /dev/null
+++ b/examples/rp235x/src/bin/ethernet_w5500_multisocket.rs
@@ -0,0 +1,139 @@
1//! This example shows how you can allow multiple simultaneous TCP connections, by having multiple sockets listening on the same port.
2//!
3//! Example written for the [`WIZnet W5500-EVB-Pico2`](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico2) board.
4
5#![no_std]
6#![no_main]
7
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_futures::yield_now;
11use embassy_net::{Stack, StackResources};
12use embassy_net_wiznet::chip::W5500;
13use embassy_net_wiznet::*;
14use embassy_rp::clocks::RoscRng;
15use embassy_rp::gpio::{Input, Level, Output, Pull};
16use embassy_rp::peripherals::SPI0;
17use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
18use embassy_time::{Delay, Duration};
19use embedded_hal_bus::spi::ExclusiveDevice;
20use embedded_io_async::Write;
21use static_cell::StaticCell;
22use {defmt_rtt as _, panic_probe as _};
23
24#[embassy_executor::task]
25async fn ethernet_task(
26 runner: Runner<
27 'static,
28 W5500,
29 ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static>, Delay>,
30 Input<'static>,
31 Output<'static>,
32 >,
33) -> ! {
34 runner.run().await
35}
36
37#[embassy_executor::task]
38async fn net_task(mut runner: embassy_net::Runner<'static, Device<'static>>) -> ! {
39 runner.run().await
40}
41
42#[embassy_executor::main]
43async fn main(spawner: Spawner) {
44 let p = embassy_rp::init(Default::default());
45 let mut rng = RoscRng;
46
47 let mut spi_cfg = SpiConfig::default();
48 spi_cfg.frequency = 50_000_000;
49 let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18);
50 let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg);
51 let cs = Output::new(p.PIN_17, Level::High);
52 let w5500_int = Input::new(p.PIN_21, Pull::Up);
53 let w5500_reset = Output::new(p.PIN_20, Level::High);
54
55 let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
56 static STATE: StaticCell<State<8, 8>> = StaticCell::new();
57 let state = STATE.init(State::<8, 8>::new());
58 let (device, runner) = embassy_net_wiznet::new(
59 mac_addr,
60 state,
61 ExclusiveDevice::new(spi, cs, Delay),
62 w5500_int,
63 w5500_reset,
64 )
65 .await
66 .unwrap();
67 unwrap!(spawner.spawn(ethernet_task(runner)));
68
69 // Generate random seed
70 let seed = rng.next_u64();
71
72 // Init network stack
73 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
74 let (stack, runner) = embassy_net::new(
75 device,
76 embassy_net::Config::dhcpv4(Default::default()),
77 RESOURCES.init(StackResources::new()),
78 seed,
79 );
80
81 // Launch network task
82 unwrap!(spawner.spawn(net_task(runner)));
83
84 info!("Waiting for DHCP...");
85 let cfg = wait_for_config(stack).await;
86 let local_addr = cfg.address.address();
87 info!("IP address: {:?}", local_addr);
88
89 // Create two sockets listening to the same port, to handle simultaneous connections
90 unwrap!(spawner.spawn(listen_task(stack, 0, 1234)));
91 unwrap!(spawner.spawn(listen_task(stack, 1, 1234)));
92}
93
94#[embassy_executor::task(pool_size = 2)]
95async fn listen_task(stack: Stack<'static>, id: u8, port: u16) {
96 let mut rx_buffer = [0; 4096];
97 let mut tx_buffer = [0; 4096];
98 let mut buf = [0; 4096];
99 loop {
100 let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
101 socket.set_timeout(Some(Duration::from_secs(10)));
102
103 info!("SOCKET {}: Listening on TCP:{}...", id, port);
104 if let Err(e) = socket.accept(port).await {
105 warn!("accept error: {:?}", e);
106 continue;
107 }
108 info!("SOCKET {}: Received connection from {:?}", id, socket.remote_endpoint());
109
110 loop {
111 let n = match socket.read(&mut buf).await {
112 Ok(0) => {
113 warn!("read EOF");
114 break;
115 }
116 Ok(n) => n,
117 Err(e) => {
118 warn!("SOCKET {}: {:?}", id, e);
119 break;
120 }
121 };
122 info!("SOCKET {}: rxd {}", id, core::str::from_utf8(&buf[..n]).unwrap());
123
124 if let Err(e) = socket.write_all(&buf[..n]).await {
125 warn!("write error: {:?}", e);
126 break;
127 }
128 }
129 }
130}
131
132async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 {
133 loop {
134 if let Some(config) = stack.config_v4() {
135 return config.clone();
136 }
137 yield_now().await;
138 }
139}