aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/rp/Cargo.toml2
-rw-r--r--examples/rp/src/bin/ethernet_w5500_icmp.rs143
-rw-r--r--examples/rp/src/bin/ethernet_w5500_icmp_ping.rs135
3 files changed, 279 insertions, 1 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index 50cfdca1e..c7823a711 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -12,7 +12,7 @@ embassy-executor = { version = "0.7.0", path = "../../embassy-executor", feature
12embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } 12embassy-time = { version = "0.4.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] }
13embassy-rp = { version = "0.3.0", path = "../../embassy-rp", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040"] } 13embassy-rp = { version = "0.3.0", path = "../../embassy-rp", features = ["defmt", "unstable-pac", "time-driver", "critical-section-impl", "rp2040"] }
14embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] } 14embassy-usb = { version = "0.4.0", path = "../../embassy-usb", features = ["defmt"] }
15embassy-net = { version = "0.6.0", path = "../../embassy-net", features = ["defmt", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns", "proto-ipv4", "proto-ipv6", "multicast"] } 15embassy-net = { version = "0.6.0", path = "../../embassy-net", features = ["defmt", "icmp", "tcp", "udp", "raw", "dhcpv4", "medium-ethernet", "dns", "proto-ipv4", "proto-ipv6", "multicast"] }
16embassy-net-wiznet = { version = "0.2.0", path = "../../embassy-net-wiznet", features = ["defmt"] } 16embassy-net-wiznet = { version = "0.2.0", path = "../../embassy-net-wiznet", features = ["defmt"] }
17embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } 17embassy-futures = { version = "0.1.0", path = "../../embassy-futures" }
18embassy-usb-logger = { version = "0.4.0", path = "../../embassy-usb-logger" } 18embassy-usb-logger = { version = "0.4.0", path = "../../embassy-usb-logger" }
diff --git a/examples/rp/src/bin/ethernet_w5500_icmp.rs b/examples/rp/src/bin/ethernet_w5500_icmp.rs
new file mode 100644
index 000000000..a07cdf88d
--- /dev/null
+++ b/examples/rp/src/bin/ethernet_w5500_icmp.rs
@@ -0,0 +1,143 @@
1//! This example implements an echo (ping) with an ICMP Socket and using defmt to report the results.
2//!
3//! Although there is a better way to execute pings using the child module ping of the icmp module,
4//! this example allows for other icmp messages like `Destination unreachable` to be sent aswell.
5//!
6//! Example written for the [`WIZnet W5500-EVB-Pico`](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico) board.
7
8#![no_std]
9#![no_main]
10
11use defmt::*;
12use embassy_executor::Spawner;
13use embassy_futures::yield_now;
14use embassy_net::icmp::{ChecksumCapabilities, IcmpEndpoint, IcmpSocket, Icmpv4Packet, Icmpv4Repr, PacketMetadata};
15use embassy_net::{Stack, StackResources};
16use embassy_net_wiznet::chip::W5500;
17use embassy_net_wiznet::*;
18use embassy_rp::clocks::RoscRng;
19use embassy_rp::gpio::{Input, Level, Output, Pull};
20use embassy_rp::peripherals::SPI0;
21use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
22use embassy_time::{Delay, Instant, Timer};
23use embedded_hal_bus::spi::ExclusiveDevice;
24use rand::RngCore;
25use static_cell::StaticCell;
26use {defmt_rtt as _, panic_probe as _};
27
28type ExclusiveSpiDevice = ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static>, Delay>;
29
30#[embassy_executor::task]
31async fn ethernet_task(runner: Runner<'static, W5500, ExclusiveSpiDevice, Input<'static>, Output<'static>>) -> ! {
32 runner.run().await
33}
34
35#[embassy_executor::task]
36async fn net_task(mut runner: embassy_net::Runner<'static, Device<'static>>) -> ! {
37 runner.run().await
38}
39
40#[embassy_executor::main]
41async fn main(spawner: Spawner) {
42 let p = embassy_rp::init(Default::default());
43 let mut rng = RoscRng;
44
45 let mut spi_cfg = SpiConfig::default();
46 spi_cfg.frequency = 50_000_000;
47 let (miso, mosi, clk) = (p.PIN_16, p.PIN_19, p.PIN_18);
48 let spi = Spi::new(p.SPI0, clk, mosi, miso, p.DMA_CH0, p.DMA_CH1, spi_cfg);
49 let cs = Output::new(p.PIN_17, Level::High);
50 let w5500_int = Input::new(p.PIN_21, Pull::Up);
51 let w5500_reset = Output::new(p.PIN_20, Level::High);
52
53 let mac_addr = [0x02, 0x00, 0x00, 0x00, 0x00, 0x00];
54 static STATE: StaticCell<State<8, 8>> = StaticCell::new();
55 let state = STATE.init(State::<8, 8>::new());
56 let (device, runner) = embassy_net_wiznet::new(
57 mac_addr,
58 state,
59 ExclusiveDevice::new(spi, cs, Delay),
60 w5500_int,
61 w5500_reset,
62 )
63 .await
64 .unwrap();
65 unwrap!(spawner.spawn(ethernet_task(runner)));
66
67 // Generate random seed
68 let seed = rng.next_u64();
69
70 // Init network stack
71 static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
72 let (stack, runner) = embassy_net::new(
73 device,
74 embassy_net::Config::dhcpv4(Default::default()),
75 RESOURCES.init(StackResources::new()),
76 seed,
77 );
78
79 // Launch network task
80 unwrap!(spawner.spawn(net_task(runner)));
81
82 info!("Waiting for DHCP...");
83 let cfg = wait_for_config(stack).await;
84 let local_addr = cfg.address.address();
85 info!("IP address: {:?}", local_addr);
86
87 // Then we can use it!
88 let mut rx_buffer = [0; 256];
89 let mut tx_buffer = [0; 256];
90 let mut rx_meta = [PacketMetadata::EMPTY];
91 let mut tx_meta = [PacketMetadata::EMPTY];
92
93 // Identifier used for the ICMP socket
94 let ident = 42;
95
96 // Create and bind the socket
97 let mut socket = IcmpSocket::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
98 socket.bind(IcmpEndpoint::Ident(ident)).unwrap();
99
100 // Create the repr for the packet
101 let icmp_repr = Icmpv4Repr::EchoRequest {
102 ident,
103 seq_no: 0,
104 data: b"Hello, icmp!",
105 };
106
107 // Send the packet and store the starting instant to mesure latency later
108 let start = socket
109 .send_to_with(
110 icmp_repr.buffer_len(),
111 cfg.gateway.unwrap(),
112 |buf| {
113 // Create and populate the packet buffer allocated by `send_to_with`
114 let mut icmp_packet = Icmpv4Packet::new_unchecked(buf);
115 icmp_repr.emit(&mut icmp_packet, &ChecksumCapabilities::default());
116 Instant::now() // Return the instant where the packet was sent
117 },
118 )
119 .await
120 .unwrap();
121
122 // Recieve and log the data of the reply
123 socket
124 .recv_with(|(buf, addr)| {
125 let packet = Icmpv4Packet::new_checked(buf).unwrap();
126 info!("Recieved {:?} from {} in {}ms", packet.data(), addr, start.elapsed().as_millis());
127 })
128 .await
129 .unwrap();
130
131 loop {
132 Timer::after_secs(10).await;
133 }
134}
135
136async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 {
137 loop {
138 if let Some(config) = stack.config_v4() {
139 return config.clone();
140 }
141 yield_now().await;
142 }
143}
diff --git a/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs b/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs
new file mode 100644
index 000000000..0d83e3831
--- /dev/null
+++ b/examples/rp/src/bin/ethernet_w5500_icmp_ping.rs
@@ -0,0 +1,135 @@
1//! This example implements a LAN ping scan with the ping utilities in the icmp module of embassy-net.
2//!
3//! Example written for the [`WIZnet W5500-EVB-Pico`](https://docs.wiznet.io/Product/iEthernet/W5500/w5500-evb-pico) board.
4
5#![no_std]
6#![no_main]
7
8use core::net::Ipv4Addr;
9use core::ops::Not;
10use core::str::FromStr;
11
12use defmt::*;
13use embassy_executor::Spawner;
14use embassy_futures::yield_now;
15use embassy_net::icmp::ping::{PingManager, PingParams};
16use embassy_net::icmp::PacketMetadata;
17use embassy_net::{Ipv4Cidr, Stack, StackResources};
18use embassy_net_wiznet::chip::W5500;
19use embassy_net_wiznet::*;
20use embassy_rp::clocks::RoscRng;
21use embassy_rp::gpio::{Input, Level, Output, Pull};
22use embassy_rp::peripherals::SPI0;
23use embassy_rp::spi::{Async, Config as SpiConfig, Spi};
24use embassy_time::{Delay, Duration};
25use embedded_hal_bus::spi::ExclusiveDevice;
26use rand::RngCore;
27use static_cell::StaticCell;
28use {defmt_rtt as _, panic_probe as _};
29
30type ExclusiveSpiDevice = ExclusiveDevice<Spi<'static, SPI0, Async>, Output<'static>, Delay>;
31
32#[embassy_executor::task]
33async fn ethernet_task(runner: Runner<'static, W5500, ExclusiveSpiDevice, Input<'static>, Output<'static>>) -> ! {
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 let gateway = cfg.gateway.unwrap();
89 let mask = cfg.address.netmask();
90 let lower_bound = (gateway.to_bits() & mask.to_bits()) + 1;
91 let upper_bound = gateway.to_bits() | mask.to_bits().not();
92 let addr_range = lower_bound..=upper_bound;
93
94 // Then we can use it!
95 let mut rx_buffer = [0; 256];
96 let mut tx_buffer = [0; 256];
97 let mut rx_meta = [PacketMetadata::EMPTY];
98 let mut tx_meta = [PacketMetadata::EMPTY];
99
100 // Create the ping manager instance
101 let mut ping_manager = PingManager::new(stack, &mut rx_meta, &mut rx_buffer, &mut tx_meta, &mut tx_buffer);
102 let addr = "192.168.8.1"; // Address to ping to
103 // Create the PingParams with the target address
104 let mut ping_params = PingParams::new(Ipv4Addr::from_str(addr).unwrap());
105 // (optional) Set custom properties of the ping
106 ping_params.set_payload(b"Hello, Ping!"); // custom payload
107 ping_params.set_count(1); // ping 1 times per ping call
108 ping_params.set_timeout(Duration::from_millis(500)); // wait .5 seconds instead of 4
109
110 info!("Online hosts in {}:", Ipv4Cidr::from_netmask(gateway, mask).unwrap());
111 let mut total_online_hosts = 0u32;
112 for addr in addr_range {
113 let ip_addr = Ipv4Addr::from_bits(addr);
114 // Set the target address in the ping params
115 ping_params.set_target(ip_addr);
116 // Execute the ping with the given parameters and wait for the reply
117 match ping_manager.ping(&ping_params).await {
118 Ok(time) => {
119 info!("{} is online\n- latency: {}ms\n", ip_addr, time.as_millis());
120 total_online_hosts += 1;
121 },
122 _ => continue,
123 }
124 }
125 info!("Ping scan complete, total online hosts: {}", total_online_hosts);
126}
127
128async fn wait_for_config(stack: Stack<'static>) -> embassy_net::StaticConfigV4 {
129 loop {
130 if let Some(config) = stack.config_v4() {
131 return config.clone();
132 }
133 yield_now().await;
134 }
135}