aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorkalkyl <[email protected]>2023-05-09 01:51:08 +0200
committerkalkyl <[email protected]>2023-05-09 01:51:08 +0200
commit72b0379125b87bcd274bdb81127dd5f0ab29d661 (patch)
treeb620d67bc9ea930051668d63190dd6de62485b0a /examples
:rainbow:
Diffstat (limited to 'examples')
-rw-r--r--examples/.cargo/config.toml8
-rw-r--r--examples/Cargo.toml70
-rw-r--r--examples/README.md33
-rw-r--r--examples/build.rs36
-rw-r--r--examples/memory.x5
-rw-r--r--examples/src/bin/multisocket.rs148
-rw-r--r--examples/src/bin/tcp-client.rs128
-rw-r--r--examples/src/bin/tcp-server.rs136
-rw-r--r--examples/src/bin/udp.rs123
9 files changed, 687 insertions, 0 deletions
diff --git a/examples/.cargo/config.toml b/examples/.cargo/config.toml
new file mode 100644
index 000000000..e6b6b4a41
--- /dev/null
+++ b/examples/.cargo/config.toml
@@ -0,0 +1,8 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2runner = "probe-rs-cli run --chip RP2040"
3
4[build]
5target = "thumbv6m-none-eabi"
6
7[env]
8DEFMT_LOG = "info"
diff --git a/examples/Cargo.toml b/examples/Cargo.toml
new file mode 100644
index 000000000..013a2755f
--- /dev/null
+++ b/examples/Cargo.toml
@@ -0,0 +1,70 @@
1[package]
2name = "embassy-net-w5500-examples"
3version = "0.1.0"
4edition = "2021"
5
6[dependencies]
7embassy-executor = { version = "0.1.0", features = ["defmt", "integrated-timers", "executor-thread", "arch-cortex-m"] }
8embassy-time = { version = "0.1.0", features = ["defmt", "defmt-timestamp-uptime"] }
9embassy-rp = { version = "0.1.0", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver"] }
10embassy-net = { version = "0.1.0", features = ["defmt", "tcp", "udp", "dhcpv4", "medium-ethernet", "unstable-traits", "nightly"] }
11embassy-sync = { version = "0.1.0" }
12embassy-futures = { version = "0.1.0" }
13embassy-net-driver = { version = "0.1.0" }
14embassy-net-driver-channel = { version = "0.1.0" }
15atomic-polyfill = "0.1.5"
16static_cell = "1.0"
17
18defmt = "=0.3.2"
19defmt-rtt = "0.3"
20panic-probe = { version = "0.3", features = ["print-defmt"] }
21cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
22cortex-m-rt = "0.7.0"
23
24embedded-io = { version = "0.4.0", features = ["async", "defmt"] }
25heapless = "0.7.15"
26embedded-hal = { version = "1.0.0-alpha.10" }
27embedded-hal-async = { version = "=0.2.0-alpha.1" }
28rand = { version = "0.8.5", default-features = false }
29
30embassy-net-w5500 = { path = "../" }
31
32[patch.crates-io]
33embassy-executor = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
34embassy-time = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
35embassy-futures = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
36embassy-sync = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
37embassy-rp = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
38embassy-net = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
39embassy-net-driver = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
40embassy-net-driver-channel = { git = "https://github.com/embassy-rs/embassy", rev = "03d6363d5af5dcaf21b52734994a466ca593d2b6" }
41
42[profile.dev]
43debug = 2
44debug-assertions = true
45opt-level = 1
46overflow-checks = true
47
48[profile.release]
49codegen-units = 1
50debug = 1
51debug-assertions = false
52incremental = false
53lto = 'fat'
54opt-level = 'z'
55overflow-checks = false
56
57# do not optimize proc-macro crates = faster builds from scratch
58[profile.dev.build-override]
59codegen-units = 8
60debug = false
61debug-assertions = false
62opt-level = 0
63overflow-checks = false
64
65[profile.release.build-override]
66codegen-units = 8
67debug = false
68debug-assertions = false
69opt-level = 0
70overflow-checks = false
diff --git a/examples/README.md b/examples/README.md
new file mode 100644
index 000000000..d818c4a89
--- /dev/null
+++ b/examples/README.md
@@ -0,0 +1,33 @@
1# Examples for the rp2040 `WIZnet W5500-EVB-Pico` board
2
3Examples are written for the [`WIZnet W5500-EVB-Pico`](https://www.wiznet.io/product-item/w5500-evb-pico/) board.
4
5## Prerequisites
6```bash
7cargo install probe-rs-cli
8```
9
10## TCP server example
11```bash
12cargo run --bin tcp-server --release
13```
14This example implements a TCP echo server on port 1234 and using DHCP.
15Send it some data, you should see it echoed back and printed in the console.
16
17## Multi-socket example
18```bash
19cargo run --bin multisocket --release
20```
21This example shows how you can allow multiple simultaneous TCP connections, by having multiple sockets listening on the same port.
22
23## TCP client example
24```bash
25cargo run --bin tcp-client --release
26```
27This example implements a TCP client that attempts to connect to a host on port 1234 and send it some data once per second.
28
29## UDP server example
30```bash
31cargo run --bin udp --release
32```
33This example implements a UDP server listening on port 1234 and echoing back the data.
diff --git a/examples/build.rs b/examples/build.rs
new file mode 100644
index 000000000..3f915f931
--- /dev/null
+++ b/examples/build.rs
@@ -0,0 +1,36 @@
1//! This build script copies the `memory.x` file from the crate root into
2//! a directory where the linker can always find it at build time.
3//! For many projects this is optional, as the linker always searches the
4//! project root directory -- wherever `Cargo.toml` is. However, if you
5//! are using a workspace or have a more complicated build setup, this
6//! build script becomes required. Additionally, by requesting that
7//! Cargo re-run the build script whenever `memory.x` is changed,
8//! updating `memory.x` ensures a rebuild of the application with the
9//! new memory settings.
10
11use std::env;
12use std::fs::File;
13use std::io::Write;
14use std::path::PathBuf;
15
16fn main() {
17 // Put `memory.x` in our output directory and ensure it's
18 // on the linker search path.
19 let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
20 File::create(out.join("memory.x"))
21 .unwrap()
22 .write_all(include_bytes!("memory.x"))
23 .unwrap();
24 println!("cargo:rustc-link-search={}", out.display());
25
26 // By default, Cargo will re-run a build script whenever
27 // any file in the project changes. By specifying `memory.x`
28 // here, we ensure the build script is only re-run when
29 // `memory.x` is changed.
30 println!("cargo:rerun-if-changed=memory.x");
31
32 println!("cargo:rustc-link-arg-bins=--nmagic");
33 println!("cargo:rustc-link-arg-bins=-Tlink.x");
34 println!("cargo:rustc-link-arg-bins=-Tlink-rp.x");
35 println!("cargo:rustc-link-arg-bins=-Tdefmt.x");
36}
diff --git a/examples/memory.x b/examples/memory.x
new file mode 100644
index 000000000..eb8c1731d
--- /dev/null
+++ b/examples/memory.x
@@ -0,0 +1,5 @@
1MEMORY {
2 BOOT2 : ORIGIN = 0x10000000, LENGTH = 0x100
3 FLASH : ORIGIN = 0x10000100, LENGTH = 1024K - 0x100
4 RAM : ORIGIN = 0x20000000, LENGTH = 256K
5} \ No newline at end of file
diff --git a/examples/src/bin/multisocket.rs b/examples/src/bin/multisocket.rs
new file mode 100644
index 000000000..49bcbdbb0
--- /dev/null
+++ b/examples/src/bin/multisocket.rs
@@ -0,0 +1,148 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_futures::yield_now;
8use embassy_net::{Stack, StackResources};
9use embassy_net_w5500::*;
10use embassy_rp::clocks::RoscRng;
11use embassy_rp::gpio::{Input, Level, Output, Pull};
12use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
13use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
14use embedded_hal_async::spi::ExclusiveDevice;
15use embedded_io::asynch::Write;
16use rand::RngCore;
17use static_cell::StaticCell;
18use {defmt_rtt as _, panic_probe as _};
19
20macro_rules! singleton {
21 ($val:expr) => {{
22 type T = impl Sized;
23 static STATIC_CELL: StaticCell<T> = StaticCell::new();
24 let (x,) = STATIC_CELL.init(($val,));
25 x
26 }};
27}
28
29#[embassy_executor::task]
30async fn ethernet_task(
31 runner: Runner<
32 'static,
33 ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>>,
34 Input<'static, PIN_21>,
35 Output<'static, PIN_20>,
36 >,
37) -> ! {
38 runner.run().await
39}
40
41#[embassy_executor::task]
42async fn net_task(stack: &'static Stack<Device<'static>>) -> ! {
43 stack.run().await
44}
45
46#[embassy_executor::main]
47async fn main(spawner: Spawner) {
48 let p = embassy_rp::init(Default::default());
49 let mut rng = RoscRng;
50
51 let mut spi_cfg = SpiConfig::default();
52 spi_cfg.frequency = 50_000_000;
53 let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18);
54 let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg);
55 let cs = Output::new(p.PIN_17, Level::High);
56 let w5500_int = Input::new(p.PIN_21, Pull::Up);
57 let w5500_reset = Output::new(p.PIN_20, Level::High);
58
59 let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
60 let state = singleton!(State::<8, 8>::new());
61 let (device, runner) = embassy_net_w5500::new(
62 mac_addr,
63 state,
64 ExclusiveDevice::new(spi, cs),
65 w5500_int,
66 w5500_reset,
67 )
68 .await;
69 unwrap!(spawner.spawn(ethernet_task(runner)));
70
71 // Generate random seed
72 let seed = rng.next_u64();
73
74 // Init network stack
75 let stack = &*singleton!(Stack::new(
76 device,
77 embassy_net::Config::Dhcp(Default::default()),
78 singleton!(StackResources::<3>::new()),
79 seed
80 ));
81
82 // Launch network task
83 unwrap!(spawner.spawn(net_task(&stack)));
84
85 info!("Waiting for DHCP...");
86 let cfg = wait_for_config(stack).await;
87 let local_addr = cfg.address.address();
88 info!("IP address: {:?}", local_addr);
89
90 // Create two sockets listening to the same port, to handle simultaneous connections
91 unwrap!(spawner.spawn(listen_task(&stack, 0, 1234)));
92 unwrap!(spawner.spawn(listen_task(&stack, 1, 1234)));
93}
94
95#[embassy_executor::task(pool_size = 2)]
96async fn listen_task(stack: &'static Stack<Device<'static>>, id: u8, port: u16) {
97 let mut rx_buffer = [0; 4096];
98 let mut tx_buffer = [0; 4096];
99 let mut buf = [0; 4096];
100 loop {
101 let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
102 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
103
104 info!("SOCKET {}: Listening on TCP:{}...", id, port);
105 if let Err(e) = socket.accept(port).await {
106 warn!("accept error: {:?}", e);
107 continue;
108 }
109 info!(
110 "SOCKET {}: Received connection from {:?}",
111 id,
112 socket.remote_endpoint()
113 );
114
115 loop {
116 let n = match socket.read(&mut buf).await {
117 Ok(0) => {
118 warn!("read EOF");
119 break;
120 }
121 Ok(n) => n,
122 Err(e) => {
123 warn!("SOCKET {}: {:?}", id, e);
124 break;
125 }
126 };
127 info!(
128 "SOCKET {}: rxd {}",
129 id,
130 core::str::from_utf8(&buf[..n]).unwrap()
131 );
132
133 if let Err(e) = socket.write_all(&buf[..n]).await {
134 warn!("write error: {:?}", e);
135 break;
136 }
137 }
138 }
139}
140
141async fn wait_for_config(stack: &'static Stack<Device<'static>>) -> embassy_net::StaticConfig {
142 loop {
143 if let Some(config) = stack.config() {
144 return config.clone();
145 }
146 yield_now().await;
147 }
148}
diff --git a/examples/src/bin/tcp-client.rs b/examples/src/bin/tcp-client.rs
new file mode 100644
index 000000000..32dfb6a68
--- /dev/null
+++ b/examples/src/bin/tcp-client.rs
@@ -0,0 +1,128 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use core::str::FromStr;
6use defmt::*;
7use embassy_executor::Spawner;
8use embassy_futures::yield_now;
9use embassy_net::{Stack, StackResources};
10use embassy_net_w5500::*;
11use embassy_rp::clocks::RoscRng;
12use embassy_rp::gpio::{Input, Level, Output, Pull};
13use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
14use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
15use embassy_time::{Duration, Timer};
16use embedded_hal_async::spi::ExclusiveDevice;
17use embedded_io::asynch::Write;
18use rand::RngCore;
19use static_cell::StaticCell;
20use {defmt_rtt as _, panic_probe as _};
21
22macro_rules! singleton {
23 ($val:expr) => {{
24 type T = impl Sized;
25 static STATIC_CELL: StaticCell<T> = StaticCell::new();
26 let (x,) = STATIC_CELL.init(($val,));
27 x
28 }};
29}
30
31#[embassy_executor::task]
32async fn ethernet_task(
33 runner: Runner<
34 'static,
35 ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>>,
36 Input<'static, PIN_21>,
37 Output<'static, PIN_20>,
38 >,
39) -> ! {
40 runner.run().await
41}
42
43#[embassy_executor::task]
44async fn net_task(stack: &'static Stack<Device<'static>>) -> ! {
45 stack.run().await
46}
47
48#[embassy_executor::main]
49async fn main(spawner: Spawner) {
50 let p = embassy_rp::init(Default::default());
51 let mut rng = RoscRng;
52 let mut led = Output::new(p.PIN_25, Level::Low);
53
54 let mut spi_cfg = SpiConfig::default();
55 spi_cfg.frequency = 50_000_000;
56 let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18);
57 let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg);
58 let cs = Output::new(p.PIN_17, Level::High);
59 let w5500_int = Input::new(p.PIN_21, Pull::Up);
60 let w5500_reset = Output::new(p.PIN_20, Level::High);
61
62 let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
63 let state = singleton!(State::<8, 8>::new());
64 let (device, runner) = embassy_net_w5500::new(
65 mac_addr,
66 state,
67 ExclusiveDevice::new(spi, cs),
68 w5500_int,
69 w5500_reset,
70 )
71 .await;
72 unwrap!(spawner.spawn(ethernet_task(runner)));
73
74 // Generate random seed
75 let seed = rng.next_u64();
76
77 // Init network stack
78 let stack = &*singleton!(Stack::new(
79 device,
80 embassy_net::Config::Dhcp(Default::default()),
81 singleton!(StackResources::<2>::new()),
82 seed
83 ));
84
85 // Launch network task
86 unwrap!(spawner.spawn(net_task(&stack)));
87
88 info!("Waiting for DHCP...");
89 let cfg = wait_for_config(stack).await;
90 let local_addr = cfg.address.address();
91 info!("IP address: {:?}", local_addr);
92
93 let mut rx_buffer = [0; 4096];
94 let mut tx_buffer = [0; 4096];
95 loop {
96 let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
97 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
98
99 led.set_low();
100 info!("Connecting...");
101 let host_addr = embassy_net::Ipv4Address::from_str("192.168.1.110").unwrap();
102 if let Err(e) = socket.connect((host_addr, 1234)).await {
103 warn!("connect error: {:?}", e);
104 continue;
105 }
106 info!("Connected to {:?}", socket.remote_endpoint());
107 led.set_high();
108
109 let msg = b"Hello world!\n";
110 loop {
111 if let Err(e) = socket.write_all(msg).await {
112 warn!("write error: {:?}", e);
113 break;
114 }
115 info!("txd: {}", core::str::from_utf8(msg).unwrap());
116 Timer::after(Duration::from_secs(1)).await;
117 }
118 }
119}
120
121async fn wait_for_config(stack: &'static Stack<Device<'static>>) -> embassy_net::StaticConfig {
122 loop {
123 if let Some(config) = stack.config() {
124 return config.clone();
125 }
126 yield_now().await;
127 }
128}
diff --git a/examples/src/bin/tcp-server.rs b/examples/src/bin/tcp-server.rs
new file mode 100644
index 000000000..04b220146
--- /dev/null
+++ b/examples/src/bin/tcp-server.rs
@@ -0,0 +1,136 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_futures::yield_now;
8use embassy_net::{Stack, StackResources};
9use embassy_net_w5500::*;
10use embassy_rp::clocks::RoscRng;
11use embassy_rp::gpio::{Input, Level, Output, Pull};
12use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
13use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
14use embedded_hal_async::spi::ExclusiveDevice;
15use embedded_io::asynch::Write;
16use rand::RngCore;
17use static_cell::StaticCell;
18use {defmt_rtt as _, panic_probe as _};
19
20macro_rules! singleton {
21 ($val:expr) => {{
22 type T = impl Sized;
23 static STATIC_CELL: StaticCell<T> = StaticCell::new();
24 let (x,) = STATIC_CELL.init(($val,));
25 x
26 }};
27}
28
29#[embassy_executor::task]
30async fn ethernet_task(
31 runner: Runner<
32 'static,
33 ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>>,
34 Input<'static, PIN_21>,
35 Output<'static, PIN_20>,
36 >,
37) -> ! {
38 runner.run().await
39}
40
41#[embassy_executor::task]
42async fn net_task(stack: &'static Stack<Device<'static>>) -> ! {
43 stack.run().await
44}
45
46#[embassy_executor::main]
47async fn main(spawner: Spawner) {
48 let p = embassy_rp::init(Default::default());
49 let mut rng = RoscRng;
50 let mut led = Output::new(p.PIN_25, Level::Low);
51
52 let mut spi_cfg = SpiConfig::default();
53 spi_cfg.frequency = 50_000_000;
54 let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18);
55 let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg);
56 let cs = Output::new(p.PIN_17, Level::High);
57 let w5500_int = Input::new(p.PIN_21, Pull::Up);
58 let w5500_reset = Output::new(p.PIN_20, Level::High);
59
60 let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
61 let state = singleton!(State::<8, 8>::new());
62 let (device, runner) = embassy_net_w5500::new(
63 mac_addr,
64 state,
65 ExclusiveDevice::new(spi, cs),
66 w5500_int,
67 w5500_reset,
68 )
69 .await;
70 unwrap!(spawner.spawn(ethernet_task(runner)));
71
72 // Generate random seed
73 let seed = rng.next_u64();
74
75 // Init network stack
76 let stack = &*singleton!(Stack::new(
77 device,
78 embassy_net::Config::Dhcp(Default::default()),
79 singleton!(StackResources::<2>::new()),
80 seed
81 ));
82
83 // Launch network task
84 unwrap!(spawner.spawn(net_task(&stack)));
85
86 info!("Waiting for DHCP...");
87 let cfg = wait_for_config(stack).await;
88 let local_addr = cfg.address.address();
89 info!("IP address: {:?}", local_addr);
90
91 let mut rx_buffer = [0; 4096];
92 let mut tx_buffer = [0; 4096];
93 let mut buf = [0; 4096];
94 loop {
95 let mut socket = embassy_net::tcp::TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
96 socket.set_timeout(Some(embassy_net::SmolDuration::from_secs(10)));
97
98 led.set_low();
99 info!("Listening on TCP:1234...");
100 if let Err(e) = socket.accept(1234).await {
101 warn!("accept error: {:?}", e);
102 continue;
103 }
104 info!("Received connection from {:?}", socket.remote_endpoint());
105 led.set_high();
106
107 loop {
108 let n = match socket.read(&mut buf).await {
109 Ok(0) => {
110 warn!("read EOF");
111 break;
112 }
113 Ok(n) => n,
114 Err(e) => {
115 warn!("{:?}", e);
116 break;
117 }
118 };
119 info!("rxd {}", core::str::from_utf8(&buf[..n]).unwrap());
120
121 if let Err(e) = socket.write_all(&buf[..n]).await {
122 warn!("write error: {:?}", e);
123 break;
124 }
125 }
126 }
127}
128
129async fn wait_for_config(stack: &'static Stack<Device<'static>>) -> embassy_net::StaticConfig {
130 loop {
131 if let Some(config) = stack.config() {
132 return config.clone();
133 }
134 yield_now().await;
135 }
136}
diff --git a/examples/src/bin/udp.rs b/examples/src/bin/udp.rs
new file mode 100644
index 000000000..4dc5e1f2f
--- /dev/null
+++ b/examples/src/bin/udp.rs
@@ -0,0 +1,123 @@
1#![no_std]
2#![no_main]
3#![feature(type_alias_impl_trait)]
4
5use defmt::*;
6use embassy_executor::Spawner;
7use embassy_futures::yield_now;
8use embassy_net::udp::UdpSocket;
9use embassy_net::{PacketMetadata, Stack, StackResources};
10use embassy_net_w5500::*;
11use embassy_rp::clocks::RoscRng;
12use embassy_rp::gpio::{Input, Level, Output, Pull};
13use embassy_rp::peripherals::{PIN_17, PIN_20, PIN_21, SPI0};
14use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
15use embedded_hal_async::spi::ExclusiveDevice;
16use rand::RngCore;
17use static_cell::StaticCell;
18use {defmt_rtt as _, panic_probe as _};
19
20macro_rules! singleton {
21 ($val:expr) => {{
22 type T = impl Sized;
23 static STATIC_CELL: StaticCell<T> = StaticCell::new();
24 let (x,) = STATIC_CELL.init(($val,));
25 x
26 }};
27}
28
29#[embassy_executor::task]
30async fn ethernet_task(
31 runner: Runner<
32 'static,
33 ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static, PIN_17>>,
34 Input<'static, PIN_21>,
35 Output<'static, PIN_20>,
36 >,
37) -> ! {
38 runner.run().await
39}
40
41#[embassy_executor::task]
42async fn net_task(stack: &'static Stack<Device<'static>>) -> ! {
43 stack.run().await
44}
45
46#[embassy_executor::main]
47async fn main(spawner: Spawner) {
48 let p = embassy_rp::init(Default::default());
49 let mut rng = RoscRng;
50
51 let mut spi_cfg = SpiConfig::default();
52 spi_cfg.frequency = 50_000_000;
53 let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18);
54 let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg);
55 let cs = Output::new(p.PIN_17, Level::High);
56 let w5500_int = Input::new(p.PIN_21, Pull::Up);
57 let w5500_reset = Output::new(p.PIN_20, Level::High);
58
59 let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
60 let state = singleton!(State::<8, 8>::new());
61 let (device, runner) = embassy_net_w5500::new(
62 mac_addr,
63 state,
64 ExclusiveDevice::new(spi, cs),
65 w5500_int,
66 w5500_reset,
67 )
68 .await;
69 unwrap!(spawner.spawn(ethernet_task(runner)));
70
71 // Generate random seed
72 let seed = rng.next_u64();
73
74 // Init network stack
75 let stack = &*singleton!(Stack::new(
76 device,
77 embassy_net::Config::Dhcp(Default::default()),
78 singleton!(StackResources::<2>::new()),
79 seed
80 ));
81
82 // Launch network task
83 unwrap!(spawner.spawn(net_task(&stack)));
84
85 info!("Waiting for DHCP...");
86 let cfg = wait_for_config(stack).await;
87 let local_addr = cfg.address.address();
88 info!("IP address: {:?}", local_addr);
89
90 // Then we can use it!
91 let mut rx_buffer = [0; 4096];
92 let mut tx_buffer = [0; 4096];
93 let mut rx_meta = [PacketMetadata::EMPTY; 16];
94 let mut tx_meta = [PacketMetadata::EMPTY; 16];
95 let mut buf = [0; 4096];
96 loop {
97 let mut socket = UdpSocket::new(
98 stack,
99 &mut rx_meta,
100 &mut rx_buffer,
101 &mut tx_meta,
102 &mut tx_buffer,
103 );
104 socket.bind(1234).unwrap();
105
106 loop {
107 let (n, ep) = socket.recv_from(&mut buf).await.unwrap();
108 if let Ok(s) = core::str::from_utf8(&buf[..n]) {
109 info!("rxd from {}: {}", ep, s);
110 }
111 socket.send_to(&buf[..n], ep).await.unwrap();
112 }
113 }
114}
115
116async fn wait_for_config(stack: &'static Stack<Device<'static>>) -> embassy_net::StaticConfig {
117 loop {
118 if let Some(config) = stack.config() {
119 return config.clone();
120 }
121 yield_now().await;
122 }
123}